// GET: Article/Details/5 public ActionResult Details(int?id, string comments = "") { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } article article = db.articles.Find(id); if (article == null) { return(HttpNotFound()); } if (!string.IsNullOrEmpty(comments)) { ArticleComment ac = new Models.ArticleComment(); ac.ArticleId = Convert.ToInt32(id); ac.Comments = comments; ac.DateTime = DateTime.Now; ac.IP = Request.UserHostAddress; ac.UserId = (int)Session["id"]; db.ArticleComments.Add(ac); db.SaveChanges(); } return(View(article)); }
public async Task <IHttpActionResult> CreateOne([NotNull] ArticleCommentCreateOneRequestDto requestDto) { var article = await _dbContext.Articles.Include(a => a.Author) .Where(a => a.Id == requestDto.ArticleId) .SingleOrDefaultAsync(); if (article == null) { return(this.BadRequest(nameof(requestDto), nameof(requestDto.ArticleId), Errors.NonExistent)); } var userId = User.Identity.GetUserId(); if (article.Archived != ArchivedState.None && userId != article.AuthorId && !User.IsInRole(KeylolRoles.Operator)) { return(Unauthorized()); } var comment = new Models.ArticleComment { ArticleId = article.Id, CommentatorId = userId, Content = ArticleController.SanitizeRichText(requestDto.Content), SidForArticle = await _dbContext.ArticleComments.Where(c => c.ArticleId == article.Id) .Select(c => c.SidForArticle) .DefaultIfEmpty(0) .MaxAsync() + 1 }; comment.UnstyledContent = PlainTextFormatter.FlattenHtml(comment.Content, false); if (requestDto.ReplyToComment != null) { var replyToComment = await _dbContext.ArticleComments .Include(c => c.Commentator) .Where(c => c.ArticleId == article.Id && c.SidForArticle == requestDto.ReplyToComment) .SingleOrDefaultAsync(); if (replyToComment != null) { comment.ReplyToComment = replyToComment; } } _dbContext.ArticleComments.Add(comment); await _dbContext.SaveChangesAsync(); await _cachedData.ArticleComments.IncreaseArticleCommentCountAsync(article.Id, 1); var messageNotifiedArticleAuthor = false; var steamNotifiedArticleAuthor = false; var unstyledContentWithNewLine = PlainTextFormatter.FlattenHtml(comment.Content, true); unstyledContentWithNewLine = unstyledContentWithNewLine.Length > 512 ? $"{unstyledContentWithNewLine.Substring(0, 512)} …" : unstyledContentWithNewLine; if (comment.ReplyToComment != null && comment.ReplyToComment.CommentatorId != comment.CommentatorId && !comment.ReplyToComment.DismissReplyMessage) { if (comment.ReplyToComment.Commentator.NotifyOnCommentReplied) { messageNotifiedArticleAuthor = comment.ReplyToComment.CommentatorId == article.AuthorId; await _cachedData.Messages.AddAsync(new Message { Type = MessageType.ArticleCommentReply, OperatorId = comment.CommentatorId, ReceiverId = comment.ReplyToComment.CommentatorId, ArticleCommentId = comment.Id }); } if (comment.ReplyToComment.Commentator.SteamNotifyOnCommentReplied) { steamNotifiedArticleAuthor = comment.ReplyToComment.CommentatorId == article.AuthorId; await _userManager.SendSteamChatMessageAsync(comment.ReplyToComment.Commentator, $"{comment.Commentator.UserName} 回复了你在《{article.Title}》下的评论:\n\n{unstyledContentWithNewLine}\n\nhttps://www.keylol.com/article/{article.Author.IdCode}/{article.SidForAuthor}#{comment.SidForArticle}"); } } if (comment.CommentatorId != article.AuthorId && !article.DismissCommentMessage) { if (!messageNotifiedArticleAuthor && article.Author.NotifyOnArticleReplied) { await _cachedData.Messages.AddAsync(new Message { Type = MessageType.ArticleComment, OperatorId = comment.CommentatorId, ReceiverId = article.AuthorId, ArticleCommentId = comment.Id }); } if (!steamNotifiedArticleAuthor && article.Author.SteamNotifyOnArticleReplied) { await _userManager.SendSteamChatMessageAsync(article.Author, $"{comment.Commentator.UserName} 评论了你的文章《{article.Title}》:\n\n{unstyledContentWithNewLine}\n\nhttps://www.keylol.com/article/{article.Author.IdCode}/{article.SidForAuthor}#{comment.SidForArticle}"); } } _mqChannel.SendMessage(string.Empty, MqClientProvider.ImageGarageRequestQueue, new ImageGarageRequestDto { ContentType = ImageGarageRequestContentType.ArticleComment, ContentId = comment.Id }); return(Ok(comment.SidForArticle)); }