示例#1
0
        public ActionResult Article(int id = 0, int commentId = -1)
        {
            if (id < 1)
            {
                return(HttpNotFound());
            }

            var article = repo.GetItem(id);

            if (article == null)
            {
                return(HttpNotFound());
            }
            var viewArticle = new ArticleForView(article);



            if (User.Identity.IsAuthenticated)
            {
                if (commentId >= 0)
                {
                    var count = notificationRepo.ViewByContext(User.Identity.GetUserId <int>(), commentId, id);
                    if (count > 0)
                    {
                        notifiCountCache.Update(User.Identity.GetUserId <int>(), -count);
                    }
                    viewArticle.CommentId = commentId;
                }
                else
                {
                    viewArticle.CommentId = 0;
                }
                viewArticle.CurUserName = User.Identity.Name.Split('@')[0];
                if (article.UserId == User.Identity.GetUserId <int>())
                {
                    viewArticle.Editable = true;
                }
                viewArticle.CurUserId = User.Identity.GetUserId <int>();
                var userImage = userRepo.GetUserImage(viewArticle.CurUserId);
                if (userImage == "Default")
                {
                    viewArticle.CurUserImage = "profile.png";
                }
                else
                {
                    viewArticle.CurUserImage = viewArticle.CurUserId + "/" + userImage;
                }
            }
            else
            {
                viewArticle.CurUserImage = "";
                viewArticle.CurUserName  = "";
                viewArticle.CurUserId    = 0;
            }
            ViewBag.MaxCommentLength = int.Parse(ConfigurationManager.AppSettings["MaxCommentLength"]);
            return(View(viewArticle));
        }
        //[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"));
        }