//[Authorize]
        public void Send(int articleId = 0, int replyCommentId = 0, string text = "", string sendId = "")
        {
            text = text.Trim();
            if (replyCommentId < 0 || !commentsHelper.ValidateText(text))
            {
                Clients.Caller.Result(0, sendId, "error1", "");
                return;
            }
            var commentDepth   = 0;
            var userIdentityId = Context.User.Identity.GetUserId <int>();
            var name           = Context.User.Identity.Name.Split('@')[0];
            var UserImage      = usersRepository.GetUserImage(userIdentityId);

            if (UserImage == "Default")
            {
                UserImage = "profile.png";
            }
            else
            {
                UserImage = userIdentityId.ToString() + "/" + UserImage;
            }
            if (replyCommentId != 0)
            {
                var replyComment = commentsRepository.GetCommentInfo(replyCommentId);
                if (replyComment == null || replyComment.ArticleId != articleId)
                {
                    Clients.Caller.Result(0, sendId, "error2", "");
                    return;
                }
                commentDepth = replyComment.Depth + 1;
                if (userIdentityId != replyComment.UserId)
                {
                    var notificationId = notoficationsRepository.Save(new Notification()
                    {
                        CommentId = replyCommentId,
                        FromWho   = name,
                        ArticleId = articleId,
                        Message   = text,
                        UserId    = replyComment.UserId
                    });

                    Clients.Group("user-" + replyComment.UserId.ToString()).Notify(replyCommentId, name, text, articleId, notificationId);
                    notifiCountCache.Update(replyComment.UserId, +1);
                }
            }
            else
            {
                var authorId = articleRepository.GetUserId(articleId);
                if (authorId == 0)
                {
                    Clients.Caller.Result(0, sendId, "error3", "");
                    return;
                }
                if (authorId != userIdentityId)
                {
                    var notificationId = notoficationsRepository.Save(new Notification()
                    {
                        CommentId = 0,
                        FromWho   = name,
                        ArticleId = articleId,
                        Message   = text,
                        UserId    = authorId
                    });
                    Clients.Group("user-" + authorId.ToString()).Notify(0, name, text, articleId, notificationId);
                    notifiCountCache.Update(authorId, +1);
                }
            }
            Comment comment = new Comment();

            comment.UserId         = userIdentityId;
            comment.ArticleId      = articleId;
            comment.Text           = text;
            comment.UserName       = Context.User.Identity.Name.Split('@')[0];
            comment.Depth          = commentDepth;
            comment.Created        = DateTime.Now;
            comment.ReplyCommentId = replyCommentId;
            comment.Deleted        = false;
            comment.UserImage      = UserImage;
            var id = commentsRepository.Save(comment);

            //id, userId, name, message, date, reply, userimage
            Clients.OthersInGroup("article-" + articleId).addMessage(id, userIdentityId, comment.UserName, comment.Text, comment.Created.ToString("yyyy-MM-dd HH:mm:ss"), replyCommentId, UserImage);
            Clients.Caller.Result(id, sendId, "ok", comment.Created.ToString("yyyy-MM-dd HH:mm:ss"));
        }
Beispiel #2
0
        public void Send(int articleId = 0, int replyCommentId = 0, string text = "")
        {
            text = text.Trim();
            if (!commentsHelper.ValidateText(text))
            {
                return;
            }
            if (replyCommentId < 0)
            {
                return;
            }
            var commentDepth   = 0;
            var userIdentityId = Context.User.Identity.GetUserId <int>();
            var name           = Context.User.Identity.Name.Split('@')[0];

            if (replyCommentId != 0)
            {
                var replyComment = commentsRepository.GetCommentInfo(replyCommentId);
                if (replyComment == null || replyComment.ArticleId != articleId)
                {
                    return;
                }
                commentDepth = replyComment.Depth + 1;
                if (userIdentityId != replyComment.UserId)
                {
                    Clients.Group("user-" + replyComment.UserId.ToString()).Notify(replyCommentId, name, text, articleId);
                    notoficationsRepository.Save(new Notification()
                    {
                        CommentId = replyCommentId,
                        FromWho   = name,
                        ArticleId = articleId,
                        Message   = text,
                        UserId    = replyComment.UserId
                    });
                    notifiCountCache.Update(replyComment.UserId, +1);
                }
            }
            else
            {
                var authorId = articleRepository.GetUserId(articleId);
                if (authorId == 0)
                {
                    return;
                }
                if (authorId != userIdentityId)
                {
                    Clients.Group("user-" + authorId.ToString()).Notify(0, name, text, articleId);
                    notoficationsRepository.Save(new Notification()
                    {
                        CommentId = 0,
                        FromWho   = name,
                        ArticleId = articleId,
                        Message   = text,
                        UserId    = authorId
                    });
                    notifiCountCache.Update(authorId, +1);
                }
            }
            Comment comment = new Comment();

            comment.UserId         = userIdentityId;
            comment.ArticleId      = articleId;
            comment.Text           = text;
            comment.UserName       = Context.User.Identity.Name.Split('@')[0];
            comment.Depth          = commentDepth;
            comment.Created        = DateTime.Now;
            comment.ReplyCommentId = replyCommentId;
            comment.Deleted        = false;
            var id = commentsRepository.Save(comment);

            Clients.Group("article-" + articleId.ToString()).addMessage(id, userIdentityId, comment.UserName, comment.Text, comment.Created.ToString("yyyy-MM-dd HH:mm:ss"), replyCommentId);
        }