Esempio n. 1
0
        protected virtual async Task <UserIdentifier[]> GetUsers(NotificationInfo notificationInfo)
        {
            List <UserIdentifier> userIds;

            if (!notificationInfo.UserIds.IsNullOrEmpty())
            {
                //Directly get from UserIds
                userIds = notificationInfo
                          .UserIds
                          .Split(",")
                          .Select(uidAsStr => UserIdentifier.Parse(uidAsStr))
                          .Where(uid => this.SettingManager.GetSettingValueForUser <bool>(NotificationSettingNames.ReceiveNotifications, uid.TenantId, uid.UserId))
                          .ToList();
            }
            else
            {
                //Get subscribed users

                var tenantIds = GetTenantIds(notificationInfo);

                List <NotificationSubscriptionInfo> subscriptions;

                if (tenantIds.IsNullOrEmpty() ||
                    (tenantIds.Length == 1 && tenantIds[0] == NotificationInfo.AllTenantIds.To <int>()))
                {
                    //Get all subscribed users of all tenants
                    subscriptions = await this._notificationStore.GetSubscriptionsAsync(
                        notificationInfo.NotificationName,
                        notificationInfo.EntityTypeName,
                        notificationInfo.EntityId
                        );
                }
                else
                {
                    //Get all subscribed users of specified tenant(s)
                    subscriptions = await this._notificationStore.GetSubscriptionsAsync(
                        tenantIds,
                        notificationInfo.NotificationName,
                        notificationInfo.EntityTypeName,
                        notificationInfo.EntityId
                        );
                }

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

                //TODO: Group subscriptions per tenant for potential performance improvement
                foreach (var subscription in subscriptions)
                {
                    using (this.CurrentUnitOfWork.SetTenantId(subscription.TenantId))
                    {
                        if (!await this._notificationDefinitionManager.IsAvailableAsync(notificationInfo.NotificationName, new UserIdentifier(subscription.TenantId, subscription.UserId)) ||
                            !this.SettingManager.GetSettingValueForUser <bool>(NotificationSettingNames.ReceiveNotifications, 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();
            }

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

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

            return(userIds.ToArray());
        }
Esempio n. 2
0
        protected virtual async Task <List <UserNotification> > SaveUserNotifications(UserIdentifier[] users, NotificationInfo notificationInfo)
        {
            var userNotifications = new List <UserNotification>();

            var tenantGroups = users.GroupBy(user => user.TenantId);

            foreach (var tenantGroup in tenantGroups)
            {
                using (this._unitOfWorkManager.Current.SetTenantId(tenantGroup.Key))
                {
                    var tenantNotificationInfo = new TenantNotificationInfo(tenantGroup.Key, notificationInfo);
                    await this._notificationStore.InsertTenantNotificationAsync(tenantNotificationInfo);

                    await this._unitOfWorkManager.Current.SaveChangesAsync(); //To get tenantNotification.Id.

                    var tenantNotification = tenantNotificationInfo.ToTenantNotification();

                    foreach (var user in tenantGroup)
                    {
                        var userNotification = new UserNotificationInfo
                        {
                            TenantId             = tenantGroup.Key,
                            UserId               = user.UserId,
                            TenantNotificationId = tenantNotificationInfo.Id
                        };

                        await this._notificationStore.InsertUserNotificationAsync(userNotification);

                        userNotifications.Add(userNotification.ToUserNotification(tenantNotification));
                    }

                    await this.CurrentUnitOfWork.SaveChangesAsync(); //To get Ids of the notifications
                }
            }

            return(userNotifications);
        }