From 22baaa0972fd4b82e530b2a2eb68342cf4672020 Mon Sep 17 00:00:00 2001 From: johnniang Date: Mon, 25 Mar 2019 14:50:15 +0800 Subject: [PATCH] Remove comment template splicing --- .../halo/service/impl/CommentServiceImpl.java | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java index 868dd1f9d..76235ecc4 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java @@ -103,39 +103,28 @@ public class CommentServiceImpl extends AbstractCrudService imple Assert.notNull(commentParam, "Comment param must not be null"); Assert.notNull(request, "Http servlet request must not be null"); - // Post id must exist - boolean postExist = postRepository.existsById(commentParam.getPostId()); - if (!postExist) { + if (!postRepository.existsById(commentParam.getPostId())) { + // Post id must exist log.error("Post: [{}] was not found", commentParam.getPostId()); throw new NotFoundException("The post was not found").setErrorData(commentParam.getPostId()); } + if (commentParam.getParentId() != null && commentParam.getParentId() > 0) { + // Validate the comment parent id + mustExistById(commentParam.getParentId()); + } + // Convert to comment Comment comment = commentParam.convertTo(); // Set some default value + comment.setContent(OwoUtil.parseOwo(formatContent(comment.getContent()))); comment.setIpAddress(ServletUtil.getClientIP(request)); // TODO Check user login status and set this field comment.setIsAdmin(false); comment.setAuthor(HtmlUtils.htmlEscape(comment.getAuthor())); comment.setGavatarMd5(SecureUtil.md5(comment.getEmail())); - if (comment.getParentId() != null && comment.getParentId() > 0) { - // Validate the comment parent id - Comment parentComment = getById(comment.getParentId()); - - // Format content and set it - String formattedContent = String.format(COMMENT_TEMPLATE, - parentComment.getId(), - parentComment.getAuthor(), - OwoUtil.parseOwo(formatContent(comment.getContent()))); - comment.setContent(formattedContent); - } else { - comment.setParentId(0L); - // Top comment - comment.setContent(OwoUtil.parseOwo(formatContent(comment.getContent()))); - } - if (StringUtils.isNotBlank(comment.getAuthorUrl())) { // Normalize the author url and set it comment.setAuthorUrl(URLUtil.normalize(comment.getAuthorUrl()));