// Can work only for single database approach
 public virtual async Task <List <PushRequestSubscription> > GetSubscriptionsAsync(string pushRequestName, EntityIdentifier entityIdentifier = null, int skipCount = 0, int maxResultCount = int.MaxValue)
 {
     return(await RequestStore.GetSubscriptionsAsync(pushRequestName,
                                                     entityIdentifier?.Type.FullName,
                                                     entityIdentifier?.Id.ToJsonString(),
                                                     skipCount : skipCount,
                                                     maxResultCount : maxResultCount
                                                     ));
 }
        public async Task Should_Distribute_Push_Request()
        {
            //Arrange
            var guid = _generator.Create();

            _store.GetRequestOrNullAsync(Arg.Any <Guid>()).ReturnsForAnyArgs(CreatePushRequest());
            _store.GetSubscriptionsAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>())
            .ReturnsForAnyArgs(new List <PushRequestSubscription> {
                CreatePushRequestSubscription()
            });

            //Act
            await _distributor.DistributeAsync(guid);

            //Assert
            await _store.Received().GetRequestOrNullAsync(Arg.Any <Guid>());

            await _store.Received().DeleteRequestAsync(Arg.Is <Guid>(n => n.Equals(guid)));
        }
Ejemplo n.º 3
0
        protected virtual async Task <UserIdentifier[]> GetUsers(PushRequest request)
        {
            var userIds = new List <UserIdentifier>();

            if (request.UserIds.IsNullOrEmpty())
            {
                //Get subscribed users

                List <PushRequestSubscription> subscriptions;

                if (request.TenantIds.IsNullOrWhiteSpace() ||
                    request.TenantIds.Trim() == PushRequest.AllTenantIdsString)
                {
                    //Get all subscribed users of all tenants
                    subscriptions = await RequestStore.GetSubscriptionsAsync(
                        request.Name,
                        request.EntityTypeName,
                        request.EntityId
                        );
                }
                else
                {
                    var tenantIds = PushRequest.GetTenantIds(request.TenantIds);

                    //Get all subscribed users of specified tenant(s)
                    subscriptions = await RequestStore.GetSubscriptionsAsync(
                        tenantIds,
                        request.Name,
                        request.EntityTypeName,
                        request.EntityId
                        );
                }

                //Remove invalid subscriptions
                var invalidSubscriptions = new Dictionary <Guid, PushRequestSubscription>();

                var subscriptionGroups = subscriptions.GroupBy(s => s.TenantId);
                foreach (var subscriptionGroup in subscriptionGroups)
                {
                    using (CurrentUnitOfWork.SetTenantId(subscriptionGroup.Key))
                    {
                        foreach (var subscription in subscriptionGroup)
                        {
                            if (!await DefinitionManager.IsAvailableAsync(request.Name, new UserIdentifier(subscription.TenantId, subscription.UserId)) ||
                                // TODO: exclude system push request from checking user setting
                                !SettingManager.GetSettingValueForUser <bool>(AbpPushSettingNames.Receive, subscription.TenantId, subscription.UserId))
                            {
                                invalidSubscriptions[subscription.Id] = subscription;
                            }
                        }
                    }
                }

                subscriptions.RemoveAll(s => invalidSubscriptions.ContainsKey(s.Id));

                //Get user ids
                userIds = subscriptions
                          .Select(s => new UserIdentifier(s.TenantId, s.UserId))
                          .ToList();
            }
            else
            {
                //Directly get from UserIds
                userIds = request
                          .UserIds
                          .Split(",")
                          .Select(uidAsStr => UserIdentifier.Parse(uidAsStr))
                          // TODO: exclude system push request from checking user setting
                          .Where(uid => SettingManager.GetSettingValueForUser <bool>(AbpPushSettingNames.Receive, uid.TenantId, uid.UserId))
                          .ToList();
            }

            if (!request.ExcludedUserIds.IsNullOrEmpty())
            {
                //Exclude specified users.
                var excludedUserIds = request
                                      .ExcludedUserIds
                                      .Split(",")
                                      .Select(uidAsStr => UserIdentifier.Parse(uidAsStr))
                                      .ToList();

                userIds.RemoveAll(uid => excludedUserIds.Any(euid => euid.Equals(uid)));
            }

            return(userIds.ToArray());
        }