Refactor comment creation

pull/137/head
johnniang 2019-02-25 14:17:05 +08:00
parent a84f747111
commit 2446b21650
5 changed files with 88 additions and 70 deletions

View File

@ -1,6 +1,9 @@
package cc.ryanc.halo.model.dto; package cc.ryanc.halo.model.dto;
import lombok.Data; import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
/** /**
* <pre> * <pre>
@ -24,7 +27,7 @@ public class JsonResult {
private String msg; private String msg;
/** /**
* Dev message.(only setting in dev environment) * Development message.(Development environment only)
*/ */
private String devMsg; private String devMsg;
@ -86,4 +89,27 @@ public class JsonResult {
this.devMsg = devMsg; this.devMsg = devMsg;
this.result = result; this.result = result;
} }
/**
* Create an ok result with message and data.
*
* @param data result data
* @param message result message
* @return ok result with message and data
*/
@NonNull
public static JsonResult ok(@Nullable String message, @Nullable Object data) {
return new JsonResult(HttpStatus.OK.value(), message, data);
}
/**
* Creates an ok result with message only.
*
* @param message result message
* @return ok result with message only
*/
@NonNull
public static JsonResult ok(@Nullable String message) {
return ok(message, null);
}
} }

View File

@ -1,11 +1,8 @@
package cc.ryanc.halo.web.controller.api; package cc.ryanc.halo.web.controller.api;
import cc.ryanc.halo.model.dto.Archive; import cc.ryanc.halo.model.dto.Archive;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -72,13 +69,8 @@ public class ApiArchivesController {
* @return JsonResult * @return JsonResult
*/ */
@GetMapping(value = "/year") @GetMapping(value = "/year")
public JsonResult archivesYear() { public List<Archive> archivesYear() {
final List<Archive> archives = postService.findPostGroupByYear(); return postService.findPostGroupByYear();
if (!CollectionUtils.isEmpty(archives)) {
return new JsonResult(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), archives);
} else {
return new JsonResult(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.getReasonPhrase());
}
} }
/** /**
@ -132,7 +124,6 @@ public class ApiArchivesController {
/** /**
* @return JsonResult * @return JsonResult
*
* @Author Aquan * @Author Aquan
* @Description * @Description
* @Date 2019.1.4 11:06 * @Date 2019.1.4 11:06

View File

@ -1,6 +1,5 @@
package cc.ryanc.halo.web.controller.api; package cc.ryanc.halo.web.controller.api;
import cc.ryanc.halo.exception.BadRequestException;
import cc.ryanc.halo.logging.Logger; import cc.ryanc.halo.logging.Logger;
import cc.ryanc.halo.model.domain.Comment; import cc.ryanc.halo.model.domain.Comment;
import cc.ryanc.halo.model.domain.Post; import cc.ryanc.halo.model.domain.Post;
@ -16,8 +15,9 @@ import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.SecureUtil;
import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HtmlUtil; import cn.hutool.http.HtmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -37,13 +37,19 @@ import static cc.ryanc.halo.model.dto.HaloConst.OPTIONS;
@RequestMapping(value = "/api/comments") @RequestMapping(value = "/api/comments")
public class ApiCommentController { public class ApiCommentController {
private final Logger log = Logger.getLogger(getClass()); /**
* Comment format [formatter 1: commentId, formatter 2: commentAuthor, formatter 3: commentContent]
*/
private final static String COMMENT_FORMAT = "<a href='#comment-id-%s'>@%s</a> %s";
@Autowired private final CommentService commentService;
private CommentService commentService;
@Autowired private final PostService postService;
private PostService postService;
public ApiCommentController(CommentService commentService, PostService postService) {
this.commentService = commentService;
this.postService = postService;
}
/** /**
* *
@ -51,49 +57,54 @@ public class ApiCommentController {
* @param comment comment * @param comment comment
* @param postId postId * @param postId postId
* @param request request * @param request request
*
* @return JsonResult * @return JsonResult
*/ */
@PostMapping(value = "/save") @PostMapping("save")
@ResponseBody
public JsonResult save(@Valid Comment comment, public JsonResult save(@Valid Comment comment,
@RequestParam(value = "postId") Long postId, @RequestParam(value = "postId") Long postId,
HttpServletRequest request) { HttpServletRequest request) {
try { comment.setCommentAuthorEmail(HtmlUtil.escape(comment.getCommentAuthorEmail()).toLowerCase());
Comment lastComment = null; comment.setPost(postService.fetchById(postId).orElse(new Post()));
final Post post = postService.fetchById(postId).orElse(new Post()); comment.setCommentAuthorIp(ServletUtil.getClientIP(request));
comment.setCommentAuthorEmail(HtmlUtil.escape(comment.getCommentAuthorEmail()).toLowerCase()); comment.setIsAdmin(0);
comment.setPost(post); comment.setCommentAuthor(HtmlUtil.escape(comment.getCommentAuthor()));
comment.setCommentAuthorIp(ServletUtil.getClientIP(request));
comment.setIsAdmin(0); if (StrUtil.isNotBlank(comment.getCommentAuthorEmail())) {
comment.setCommentAuthor(HtmlUtil.escape(comment.getCommentAuthor())); comment.setCommentAuthorAvatarMd5(SecureUtil.md5(comment.getCommentAuthorEmail()));
if (StrUtil.isNotBlank(comment.getCommentAuthorEmail())) { }
comment.setCommentAuthorAvatarMd5(SecureUtil.md5(comment.getCommentAuthorEmail())); if (comment.getCommentParent() > 0) {
} // Get last comment
if (comment.getCommentParent() > 0) { Comment lastComment = commentService.fetchById(comment.getCommentParent()).orElse(new Comment());
lastComment = commentService.fetchById(comment.getCommentParent()).orElse(new Comment()); // Format and set comment content
final StrBuilder buildContent = new StrBuilder("<a href='#comment-id-"); comment.setCommentContent(String.format(COMMENT_FORMAT, lastComment.getCommentId(), lastComment.getCommentAuthor(), convertToSecureString(comment.getCommentContent())));
buildContent.append(lastComment.getCommentId()); } else {
buildContent.append("'>@"); //将评论内容的字符专为安全字符
buildContent.append(lastComment.getCommentAuthor()); comment.setCommentContent(convertToSecureString(comment.getCommentContent()));
buildContent.append("</a> "); }
buildContent.append(OwoUtil.markToImg(HtmlUtil.escape(comment.getCommentContent()).replace("&lt;br/&gt;", "<br/>"))); if (StrUtil.isNotEmpty(comment.getCommentAuthorUrl())) {
comment.setCommentContent(buildContent.toString()); comment.setCommentAuthorUrl(URLUtil.normalize(comment.getCommentAuthorUrl()));
} else { }
//将评论内容的字符专为安全字符
comment.setCommentContent(OwoUtil.markToImg(HtmlUtil.escape(comment.getCommentContent()).replace("&lt;br/&gt;", "<br/>"))); // Create the comment
} commentService.create(comment);
if (StrUtil.isNotEmpty(comment.getCommentAuthorUrl())) {
comment.setCommentAuthorUrl(URLUtil.normalize(comment.getCommentAuthorUrl())); if (StrUtil.equals(OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()), TrueFalseEnum.TRUE.getDesc()) || OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()) == null) {
} return JsonResult.ok("你的评论已经提交,待博主审核之后可显示。");
commentService.create(comment); } else {
if (StrUtil.equals(OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()), TrueFalseEnum.TRUE.getDesc()) || OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()) == null) { return JsonResult.ok("你的评论已经提交,刷新后即可显示。");
return new JsonResult(HttpStatus.OK.value(), "你的评论已经提交,待博主审核之后可显示。");
} else {
return new JsonResult(HttpStatus.OK.value(), "你的评论已经提交,刷新后即可显示。");
}
} catch (Exception e) {
throw new BadRequestException("评论失败!", e);
} }
} }
/**
* Converts content to secure content.
*
* @param originalContent original content must not be null
* @return secure content
*/
@NonNull
private String convertToSecureString(@NonNull String originalContent) {
Assert.hasText(originalContent, "Original content must not be blank");
return OwoUtil.markToImg(HtmlUtil.escape(originalContent).replace("&lt;br/&gt;", "<br/>"));
}
} }

View File

@ -112,9 +112,6 @@ public class ApiPostController {
} }
final Pageable pageable = PageRequest.of(page - 1, size, sort); final Pageable pageable = PageRequest.of(page - 1, size, sort);
final Page<Post> posts = postService.findPostByStatus(PostStatusEnum.PUBLISHED.getCode(), PostTypeEnum.POST_TYPE_POST.getDesc(), pageable); final Page<Post> posts = postService.findPostByStatus(PostStatusEnum.PUBLISHED.getCode(), PostTypeEnum.POST_TYPE_POST.getDesc(), pageable);
if (null == posts) {
return new JsonResult(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.getReasonPhrase());
}
return new JsonResult(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), posts); return new JsonResult(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), posts);
} }

View File

@ -2,10 +2,8 @@ package cc.ryanc.halo.web.controller.api;
import cc.ryanc.halo.exception.NotFoundException; import cc.ryanc.halo.exception.NotFoundException;
import cc.ryanc.halo.model.domain.Tag; import cc.ryanc.halo.model.domain.Tag;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.service.TagService; import cc.ryanc.halo.service.TagService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
@ -51,13 +49,8 @@ public class ApiTagController {
* @return JsonResult * @return JsonResult
*/ */
@GetMapping @GetMapping
public JsonResult tags() { public List<Tag> tags() {
final List<Tag> tags = tagService.listAll(); return tagService.listAll();
if (null != tags && tags.size() > 0) {
return new JsonResult(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), tags);
} else {
return new JsonResult(HttpStatus.NO_CONTENT.value(), HttpStatus.NO_CONTENT.getReasonPhrase());
}
} }
/** /**
@ -87,7 +80,7 @@ public class ApiTagController {
final Tag tag = tagService.findByTagUrl(tagUrl); final Tag tag = tagService.findByTagUrl(tagUrl);
if (tag == null) { if (tag == null) {
throw new NotFoundException("Tag with url: " + tagUrl + " was not found"); throw new NotFoundException("Tag with url: " + tagUrl + " was not found").setErrorData(tagUrl);
} }
return tag; return tag;