From 6f9aac5aadf40ae0628688a6c62bd4138cff2637 Mon Sep 17 00:00:00 2001 From: johnniang Date: Sun, 31 Mar 2019 00:01:30 +0800 Subject: [PATCH] Fix comment creation bug when admin is logging in --- .../halo/service/impl/CommentServiceImpl.java | 21 +++++++++++-------- .../admin/api/CommentController.java | 8 +++---- 2 files changed, 16 insertions(+), 13 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 063d234af..d3d92ebc2 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java @@ -111,11 +111,22 @@ public class CommentServiceImpl extends AbstractCrudService imple comment.setContent(OwoUtil.parseOwo(formatContent(comment.getContent()))); comment.setIpAddress(ServletUtil.getClientIP(request)); comment.setUserAgent(ServletUtil.getHeaderIgnoreCase(request, HttpHeaders.USER_AGENT)); - // TODO Check user login status and set this field + + + // Check user login status and set this field Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { // If the user is login comment.setIsAdmin(true); + comment.setStatus(CommentStatus.PUBLISHED); + } else { + // Handle comment status + Boolean needAudit = optionService.getByPropertyOrDefault(BlogProperties.NEW_COMMENT_NEED_CHECK, Boolean.class, true); + if (needAudit) { + comment.setStatus(CommentStatus.AUDITING); + } else { + comment.setStatus(CommentStatus.PUBLISHED); + } } comment.setAuthor(HtmlUtils.htmlEscape(comment.getAuthor())); @@ -126,14 +137,6 @@ public class CommentServiceImpl extends AbstractCrudService imple comment.setAuthorUrl(URLUtil.normalize(comment.getAuthorUrl())); } - // Handle comment status - Boolean needAudit = optionService.getByPropertyOrDefault(BlogProperties.NEW_COMMENT_NEED_CHECK, Boolean.class, true); - if (needAudit) { - comment.setStatus(CommentStatus.AUDITING); - } else { - comment.setStatus(CommentStatus.PUBLISHED); - } - Comment createdComment = create(comment); // TODO Handle email sending diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/api/CommentController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/api/CommentController.java index b9c19fb11..3b96b8298 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/api/CommentController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/api/CommentController.java @@ -14,13 +14,13 @@ import cc.ryanc.halo.service.OptionService; import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.utils.ValidationUtils; import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; -import javax.validation.Valid; import java.util.List; import static org.springframework.data.domain.Sort.Direction.DESC; @@ -62,15 +62,15 @@ public class CommentController { } @PostMapping - public CommentOutputDTO createBy(@Valid @RequestBody CommentParam commentParam, HttpServletRequest request) { + public CommentOutputDTO createBy(@RequestBody CommentParam commentParam, HttpServletRequest request) { // Get authentication Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { User user = authentication.getDetail().getUser(); // If the admin is login - commentParam.setAuthor(user.getNickname()); + commentParam.setAuthor(StringUtils.isEmpty(user.getNickname()) ? user.getUsername() : user.getNickname()); commentParam.setEmail(user.getEmail()); - commentParam.setAuthor(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL)); + commentParam.setAuthorUrl(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL)); } // Validate the comment param manually