Ejemplo n.º 1
0
        async Task <IEnumerable <EntityTag> > GetEntityTagsByEntityReplyIdAsync(int entityId)
        {
            if (entityId == 0)
            {
                // return empty collection for new topics
                return(null);
            }

            return(await _entityTagStore.GetByEntityReplyIdAsync(entityId));
        }
Ejemplo n.º 2
0
        async Task <IList <int> > SendNotificationsAsync(TEntityReply reply, IList <int> usersToExclude)
        {
            if (reply == null)
            {
                throw new ArgumentNullException(nameof(reply));
            }

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

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

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

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

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

            // Compile all entity tags ids for the entity
            var tags       = new List <int>();
            var entityTags = await _entityTagStore.GetByEntityReplyIdAsync(reply.Id);

            if (entityTags != null)
            {
                foreach (var entityTag in entityTags)
                {
                    if (!tags.Contains(entityTag.TagId))
                    {
                        tags.Add(entityTag.TagId);
                    }
                }
            }

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

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

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

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

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

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

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

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

            // Persist state
            await _entityReplyStore.UpdateAsync(reply);

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