예제 #1
0
        public static async Task SendCommentNotification(Comment comment)
        {
            whoaverseEntities _db  = new whoaverseEntities();
            Random            _rnd = new Random();

            if (comment.ParentId != null && comment.CommentContent != null)
            {
                // find the parent comment and its author
                var parentComment = _db.Comments.Find(comment.ParentId);
                if (parentComment != null)
                {
                    // check if recipient exists
                    if (User.UserExists(parentComment.Name))
                    {
                        // do not send notification if author is the same as comment author
                        if (parentComment.Name != HttpContext.Current.User.Identity.Name)
                        {
                            // send the message

                            var commentMessage = _db.Messages.Find(comment.MessageId);
                            if (commentMessage != null)
                            {
                                var commentReplyNotification = new Commentreplynotification();
                                commentReplyNotification.CommentId    = comment.Id;
                                commentReplyNotification.SubmissionId = commentMessage.Id;
                                commentReplyNotification.Recipient    = parentComment.Name;
                                if (parentComment.Message.Anonymized || parentComment.Message.Subverses.anonymized_mode)
                                {
                                    commentReplyNotification.Sender = _rnd.Next(10000, 20000).ToString(CultureInfo.InvariantCulture);
                                }
                                else
                                {
                                    commentReplyNotification.Sender = HttpContext.Current.User.Identity.Name;
                                }
                                commentReplyNotification.Body      = comment.CommentContent;
                                commentReplyNotification.Subverse  = commentMessage.Subverse;
                                commentReplyNotification.Status    = true;
                                commentReplyNotification.Timestamp = DateTime.Now;

                                // self = type 1, url = type 2
                                commentReplyNotification.Subject = parentComment.Message.Type == 1 ? parentComment.Message.Title : parentComment.Message.Linkdescription;

                                _db.Commentreplynotifications.Add(commentReplyNotification);

                                await _db.SaveChangesAsync();

                                // get count of unread notifications
                                int unreadNotifications = User.UnreadTotalNotificationsCount(commentReplyNotification.Recipient);

                                // send SignalR realtime notification to recipient
                                var hubContext = GlobalHost.ConnectionManager.GetHubContext <MessagingHub>();
                                hubContext.Clients.User(commentReplyNotification.Recipient).setNotificationsPending(unreadNotifications);
                            }
                        }
                    }
                }
            }
            else
            {
                // comment reply is sent to a root comment which has no parent id, trigger post reply notification
                var commentMessage = _db.Messages.Find(comment.MessageId);
                if (commentMessage != null)
                {
                    // check if recipient exists
                    if (User.UserExists(commentMessage.Name))
                    {
                        // do not send notification if author is the same as comment author
                        if (commentMessage.Name != HttpContext.Current.User.Identity.Name)
                        {
                            // send the message
                            var postReplyNotification = new Postreplynotification();

                            postReplyNotification.CommentId    = comment.Id;
                            postReplyNotification.SubmissionId = commentMessage.Id;
                            postReplyNotification.Recipient    = commentMessage.Name;

                            if (commentMessage.Anonymized || commentMessage.Subverses.anonymized_mode)
                            {
                                postReplyNotification.Sender = _rnd.Next(10000, 20000).ToString(CultureInfo.InvariantCulture);
                            }
                            else
                            {
                                postReplyNotification.Sender = HttpContext.Current.User.Identity.Name;
                            }

                            postReplyNotification.Body      = comment.CommentContent;
                            postReplyNotification.Subverse  = commentMessage.Subverse;
                            postReplyNotification.Status    = true;
                            postReplyNotification.Timestamp = DateTime.Now;

                            // self = type 1, url = type 2
                            postReplyNotification.Subject = commentMessage.Type == 1 ? commentMessage.Title : commentMessage.Linkdescription;

                            _db.Postreplynotifications.Add(postReplyNotification);

                            await _db.SaveChangesAsync();

                            // get count of unread notifications
                            int unreadNotifications = User.UnreadTotalNotificationsCount(postReplyNotification.Recipient);

                            // send SignalR realtime notification to recipient
                            var hubContext = GlobalHost.ConnectionManager.GetHubContext <MessagingHub>();
                            hubContext.Clients.User(postReplyNotification.Recipient).setNotificationsPending(unreadNotifications);
                        }
                    }
                }
            }
        }
예제 #2
0
        public static async Task SendCommentNotification(Comment comment, Action <string> onSuccess)
        {
            try
            {
                using (var _db = new voatEntities())
                {
                    Random _rnd = new Random();

                    if (comment.ParentId != null && comment.CommentContent != null)
                    {
                        // find the parent comment and its author
                        var parentComment = _db.Comments.Find(comment.ParentId);
                        if (parentComment != null)
                        {
                            // check if recipient exists
                            if (UserHelper.UserExists(parentComment.Name))
                            {
                                // do not send notification if author is the same as comment author
                                if (parentComment.Name != HttpContext.Current.User.Identity.Name)
                                {
                                    // send the message

                                    var submission = DataCache.Submission.Retrieve(comment.MessageId);
                                    if (submission != null)
                                    {
                                        var subverse = DataCache.Subverse.Retrieve(submission.Subverse);

                                        var commentReplyNotification = new Commentreplynotification();
                                        commentReplyNotification.CommentId    = comment.Id;
                                        commentReplyNotification.SubmissionId = submission.Id;
                                        commentReplyNotification.Recipient    = parentComment.Name;
                                        if (submission.Anonymized || subverse.anonymized_mode)
                                        {
                                            commentReplyNotification.Sender = _rnd.Next(10000, 20000).ToString(CultureInfo.InvariantCulture);
                                        }
                                        else
                                        {
                                            commentReplyNotification.Sender = HttpContext.Current.User.Identity.Name;
                                        }
                                        commentReplyNotification.Body      = comment.CommentContent;
                                        commentReplyNotification.Subverse  = subverse.name;
                                        commentReplyNotification.Status    = true;
                                        commentReplyNotification.Timestamp = DateTime.Now;

                                        // self = type 1, url = type 2
                                        commentReplyNotification.Subject = submission.Type == 1 ? submission.Title : submission.Linkdescription;

                                        _db.Commentreplynotifications.Add(commentReplyNotification);

                                        await _db.SaveChangesAsync();

                                        if (onSuccess != null)
                                        {
                                            onSuccess(commentReplyNotification.Recipient);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        // comment reply is sent to a root comment which has no parent id, trigger post reply notification
                        var submission = DataCache.Submission.Retrieve(comment.MessageId);
                        if (submission != null)
                        {
                            // check if recipient exists
                            if (UserHelper.UserExists(submission.Name))
                            {
                                // do not send notification if author is the same as comment author
                                if (submission.Name != HttpContext.Current.User.Identity.Name)
                                {
                                    // send the message
                                    var postReplyNotification = new Postreplynotification();

                                    postReplyNotification.CommentId    = comment.Id;
                                    postReplyNotification.SubmissionId = submission.Id;
                                    postReplyNotification.Recipient    = submission.Name;
                                    var subverse = DataCache.Subverse.Retrieve(submission.Subverse);
                                    if (submission.Anonymized || subverse.anonymized_mode)
                                    {
                                        postReplyNotification.Sender = _rnd.Next(10000, 20000).ToString(CultureInfo.InvariantCulture);
                                    }
                                    else
                                    {
                                        postReplyNotification.Sender = HttpContext.Current.User.Identity.Name;
                                    }

                                    postReplyNotification.Body      = comment.CommentContent;
                                    postReplyNotification.Subverse  = submission.Subverse;
                                    postReplyNotification.Status    = true;
                                    postReplyNotification.Timestamp = DateTime.Now;

                                    // self = type 1, url = type 2
                                    postReplyNotification.Subject = submission.Type == 1 ? submission.Title : submission.Linkdescription;

                                    _db.Postreplynotifications.Add(postReplyNotification);

                                    await _db.SaveChangesAsync();

                                    if (onSuccess != null)
                                    {
                                        onSuccess(postReplyNotification.Recipient);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }