Ejemplo n.º 1
0
        public async Task <IEnumerable <NotificationDto> > GetNotifications(string userId, int?limit)
        {
            if (limit != null && limit < 0)
            {
                throw new ArgumentException("Limit must be a non-negative number");
            }

            var notifications = await _context.Notifications
                                .Where(n => n.ReceiverId.Equals(userId) && n.TopicId != null)
                                .OrderByDescending(n => n.CreatedAt)
                                .Include(n => n.Sender)
                                .Include(n => n.Topic)
                                .Include(n => n.Comment)
                                .ToListAsync();

            var output = new List <NotificationDto>();

            foreach (var notification in notifications.GroupBy(c => new { c.Type, c.TopicId, c.CommentId }).Select(n => n.ToList()))
            {
                var first = notification.First();
                if (first.Comment == null && first.CommentId != null || first.Topic.IsDeleted)
                {
                    continue;
                }

                var count = notification.GroupBy(n => n.SenderId).Select(n => n.FirstOrDefault()).Count();
                output.Add(NotificationDto.Create(first, count - 1));
            }

            return(limit == null?output.Where(n => !n.IsRead) : output.Skip((int)limit).Take(5));
        }
Ejemplo n.º 2
0
        private async Task Notify(Notification notification, ApplicationUser sender, Topic topic, Comment comment = null)
        {
            try
            {
                await Do(async() => await _context.Notifications.AddAsync(notification));

                _notificationsHub.Notify(notification.ReceiverId, NotificationDto.Create(notification, sender, topic, comment));
            }
            catch
            {
                // ignored
            }
        }