public async Task AddNotification(
            CProfile profileTo,
            int profileFrom,
            string text,
            string link,
            string alt,
            int?commentId = null)
        {
            var notification = new Notification
            {
                NotificationDate = DateTime.Now,
                ProfileIdFrom    = profileFrom,
                Profile          = profileTo,
                NotificationText = text,
                Url       = link,
                Alt       = alt,
                CommentId = commentId
            };

            await context.Notifications.AddAsync(notification);

            await context.SaveChangesAsync();

            await Notify(profileTo, notification);
        }
Beispiel #2
0
        private async Task SetNotification(CProfile profileTo, CProfile ownerProfile, string link)
        {
            var profileFrom  = ownerProfile.Id;
            var userNameFrom = ownerProfile.User.UserName;
            var text         = $"На ваши новости подписался пользователь";
            var alt          = userNameFrom;

            await notificationService.AddNotification(profileTo, profileFrom, text, link, alt);
        }
Beispiel #3
0
        private async Task NoticeSubs(CProfile profileFrom, string link, bool isModifyed = false)
        {
            string text;

            if (isModifyed)
            {
                text = $"Пользователь {profileFrom.User.UserName} обновил";
            }
            else
            {
                text = $"Пользователь {profileFrom.User.UserName} опубликовал";
            }

            var subs = await subService.GetSubscribers(profileFrom.Id);

            var alt = "статью";

            List <Notification> notifications = new List <Notification>();

            await Task.Run(() =>
                           subs
                           .ForEach(sub =>
            {
                var profileTo = profileService.GetSimpleProfileById(sub.ProfileID);

                var notification = new Notification
                {
                    NotificationDate = DateTime.Now,
                    ProfileIdFrom    = profileFrom.Id,
                    Profile          = profileTo.Result,
                    NotificationText = text,
                    Url       = link,
                    Alt       = alt,
                    CommentId = null
                };
                notifications.Add(notification);
            }));

            await context.Notifications.AddRangeAsync(notifications);

            await context.SaveChangesAsync();
        }
        private async Task Notify(CProfile profileTo, Notification notification)
        {
            var username = (await context
                            .Profiles
                            .Include(p => p.User)
                            .FirstOrDefaultAsync(p => p == profileTo))
                           .User
                           .UserName;

            var notify = new GetNotificationsModel
            {
                Id = notification.Id,
                NotificationText = notification.NotificationText,
                Url              = notification.Url,
                Alt              = notification.Alt,
                CommentId        = notification.CommentId,
                NotificationDate = notification.NotificationDate,
                isViewed         = notification.isViewed
            };

            var result = JsonConvert.SerializeObject(notify);

            await hubContext.Clients.User(username).SendAsync("SignalNotification", result);
        }