Esempio n. 1
0
        async Task <IEnumerable <EntityLabel> > GetEntityLabelsByEntityIdAsync(int entityId)
        {
            if (entityId == 0)
            {
                // return empty collection for new topics
                return(new List <EntityLabel>());
            }

            return(await _entityLabelStore.GetByEntityIdAsync(entityId) ?? new List <EntityLabel>());
        }
Esempio n. 2
0
        async Task <IList <int> > SendNotificationsAsync(TEntity entity, IList <int> usersToExclude)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            // Compile a list of notified users
            var notifiedUsers = new List <int>();

            // Follow type name
            var name = FollowTypes.Label.Name;

            // Get follow state for entity
            var state = entity.GetOrCreate <FollowState>();

            // Have notifications already been sent for the entity?
            var follow = state.FollowsSent.FirstOrDefault(f =>
                                                          f.Equals(name, StringComparison.InvariantCultureIgnoreCase));

            if (follow != null)
            {
                return(notifiedUsers);
            }

            // Compile all entity label ids for the entity
            var labels       = new List <int>();
            var entityLabels = await _entityLabelStore.GetByEntityIdAsync(entity.Id);

            if (entityLabels != null)
            {
                foreach (var entityLabel in entityLabels)
                {
                    if (!labels.Contains(entityLabel.LabelId))
                    {
                        labels.Add(entityLabel.LabelId);
                    }
                }
            }

            // Get all follows for entity labels
            var follows = await _followStore.QueryAsync()
                          .Select <FollowQueryParams>(q =>
            {
                q.Name.Equals(name);
                q.ThingId.IsIn(labels.ToArray());
                if (usersToExclude.Count > 0)
                {
                    q.CreatedUserId.IsNotIn(usersToExclude.ToArray());
                }
            })
                          .ToList();

            // Reduce the users for all found follows
            var users = await ReduceUsersAsync(follows?.Data, entity);

            // No users simply return
            if (users == null)
            {
                return(notifiedUsers);
            }

            // Send notifications
            foreach (var user in users)
            {
                // Email notifications
                if (user.NotificationEnabled(_userNotificationTypeDefaults, EmailNotifications.NewLabel))
                {
                    // Track notified
                    if (!notifiedUsers.Contains(user.Id))
                    {
                        notifiedUsers.Add(user.Id);
                    }

                    // Notify
                    await _notificationManager.SendAsync(new Notification(EmailNotifications.NewLabel)
                    {
                        To = user,
                    }, entity);
                }

                // Web notifications
                if (user.NotificationEnabled(_userNotificationTypeDefaults, WebNotifications.NewLabel))
                {
                    // Track notified
                    if (!notifiedUsers.Contains(user.Id))
                    {
                        notifiedUsers.Add(user.Id);
                    }

                    // Notify
                    await _notificationManager.SendAsync(new Notification(WebNotifications.NewLabel)
                    {
                        To   = user,
                        From = new User
                        {
                            Id          = entity.CreatedBy.Id,
                            UserName    = entity.CreatedBy.UserName,
                            DisplayName = entity.CreatedBy.DisplayName,
                            Alias       = entity.CreatedBy.Alias,
                            PhotoUrl    = entity.CreatedBy.PhotoUrl,
                            PhotoColor  = entity.CreatedBy.PhotoColor
                        }
                    }, entity);
                }
            }

            // Update state
            state.AddSent(name);
            entity.AddOrUpdate(state);

            // Persist state
            await _entityStore.UpdateAsync(entity);

            // Return a list of all notified users
            return(notifiedUsers);
        }