public async Task <IHttpActionResult> UpdateOne(string id, [NotNull] ArticleCommentUpdateOneRequestDto requestDto) { var comment = await _dbContext.ArticleComments.FindAsync(id); if (comment == null) { return(NotFound()); } var userId = User.Identity.GetUserId(); if (comment.CommentatorId != userId && !User.IsInRole(KeylolRoles.Operator)) { return(Unauthorized()); } comment.Content = ArticleController.SanitizeRichText(requestDto.Content); comment.UnstyledContent = PlainTextFormatter.FlattenHtml(comment.Content, false); if (requestDto.ReplyToComment != null) { var replyToComment = await _dbContext.ArticleComments .Where(c => c.ArticleId == comment.ArticleId && c.SidForArticle == requestDto.ReplyToComment) .SingleOrDefaultAsync(); if (replyToComment != null) { comment.ReplyToComment = replyToComment; } } else { comment.ReplyToCommentId = null; } await _dbContext.SaveChangesAsync(); _mqChannel.SendMessage(string.Empty, MqClientProvider.ImageGarageRequestQueue, new ImageGarageRequestDto { ContentType = ImageGarageRequestContentType.ArticleComment, ContentId = comment.Id }); return(Ok()); }
public async Task <IHttpActionResult> UpdateOne(string id, [NotNull] ArticleCreateOrUpdateOneRequestDto requestDto) { var article = await _dbContext.Articles.FindAsync(id); if (article == null) { return(NotFound()); } var userId = User.Identity.GetUserId(); if (article.AuthorId != userId && !User.IsInRole(KeylolRoles.Operator)) { return(Unauthorized()); } article.Title = requestDto.Title; article.Subtitle = string.IsNullOrWhiteSpace(requestDto.Subtitle) ? string.Empty : requestDto.Subtitle; article.Content = SanitizeRichText(requestDto.Content); article.UnstyledContent = PlainTextFormatter.FlattenHtml(article.Content, true); article.CoverImage = SanitizeCoverImage(requestDto.CoverImage); var targetPoint = await _dbContext.Points.Where(p => p.Id == requestDto.TargetPointId) .Select(p => new { p.Id, p.Type }).SingleOrDefaultAsync(); if (targetPoint == null) { return(this.BadRequest(nameof(requestDto), nameof(requestDto.TargetPointId), Errors.NonExistent)); } if (targetPoint.Type == PointType.Game || targetPoint.Type == PointType.Hardware) { article.Rating = requestDto.Rating; article.Pros = JsonConvert.SerializeObject(requestDto.Pros ?? new List <string>()); article.Cons = JsonConvert.SerializeObject(requestDto.Cons ?? new List <string>()); } else { article.Rating = null; article.Pros = string.Empty; article.Cons = string.Empty; } article.ReproductionRequirement = requestDto.ReproductionRequirement == null ? string.Empty : JsonConvert.SerializeObject(requestDto.ReproductionRequirement); await _dbContext.SaveChangesAsync(); var oldAttachedPoints = Helpers.SafeDeserialize <List <string> >(article.AttachedPoints) ?? new List <string>(); if (requestDto.TargetPointId != article.TargetPointId || !requestDto.AttachedPointIds.OrderBy(s => s).SequenceEqual(oldAttachedPoints.OrderBy(s => s))) { article.TargetPointId = targetPoint.Id; requestDto.AttachedPointIds = requestDto.AttachedPointIds.Select(pointId => pointId.Trim()) .Where(pointId => pointId != targetPoint.Id.Trim()).Distinct().ToList(); article.AttachedPoints = JsonConvert.SerializeObject(requestDto.AttachedPointIds); await _dbContext.SaveChangesAsync(); _mqChannel.SendMessage(string.Empty, MqClientProvider.PushHubRequestQueue, new PushHubRequestDto { Type = ContentPushType.Article, ContentId = article.Id }); } _mqChannel.SendMessage(string.Empty, MqClientProvider.ImageGarageRequestQueue, new ImageGarageRequestDto { ContentType = ImageGarageRequestContentType.Article, ContentId = article.Id }); return(Ok()); }
public async Task <IHttpActionResult> CreateOne([NotNull] ArticleCreateOrUpdateOneRequestDto requestDto) { var userId = User.Identity.GetUserId(); if (!await _coupon.CanTriggerEventAsync(userId, CouponEvent.发布文章)) { return(Unauthorized()); } var article = new Models.Article { AuthorId = userId, Title = requestDto.Title, Content = SanitizeRichText(requestDto.Content), CoverImage = SanitizeCoverImage(requestDto.CoverImage) }; article.UnstyledContent = PlainTextFormatter.FlattenHtml(article.Content, true); if (!string.IsNullOrWhiteSpace(requestDto.Subtitle)) { article.Subtitle = requestDto.Subtitle; } var targetPoint = await _dbContext.Points.Where(p => p.Id == requestDto.TargetPointId).SingleOrDefaultAsync(); if (targetPoint == null) { return(this.BadRequest(nameof(requestDto), nameof(requestDto.TargetPointId), Errors.NonExistent)); } targetPoint.LastActivityTime = DateTime.Now; article.TargetPointId = targetPoint.Id; requestDto.AttachedPointIds = requestDto.AttachedPointIds.Select(id => id.Trim()) .Where(id => id != targetPoint.Id).Distinct().ToList(); article.AttachedPoints = JsonConvert.SerializeObject(requestDto.AttachedPointIds); if (targetPoint.Type == PointType.Game || targetPoint.Type == PointType.Hardware) { article.Rating = requestDto.Rating; article.Pros = JsonConvert.SerializeObject(requestDto.Pros ?? new List <string>()); article.Cons = JsonConvert.SerializeObject(requestDto.Cons ?? new List <string>()); } if (requestDto.ReproductionRequirement != null) { article.ReproductionRequirement = JsonConvert.SerializeObject(requestDto.ReproductionRequirement); } _dbContext.Articles.Add(article); article.SidForAuthor = await _dbContext.Articles.Where(a => a.AuthorId == article.AuthorId) .Select(a => a.SidForAuthor) .DefaultIfEmpty(0) .MaxAsync() + 1; await _dbContext.SaveChangesAsync(); await _coupon.UpdateAsync(await _userManager.FindByIdAsync(userId), CouponEvent.发布文章, new { ArticleId = article.Id }); _mqChannel.SendMessage(string.Empty, MqClientProvider.PushHubRequestQueue, new PushHubRequestDto { Type = ContentPushType.Article, ContentId = article.Id }); _mqChannel.SendMessage(string.Empty, MqClientProvider.ImageGarageRequestQueue, new ImageGarageRequestDto { ContentType = ImageGarageRequestContentType.Article, ContentId = article.Id }); SteamCnProvider.TriggerArticleUpdate(); return(Ok(article.SidForAuthor)); }
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)); }