Remove comment template splicing

pull/137/head
johnniang 2019-03-25 14:50:15 +08:00
parent 42fced1c02
commit 22baaa0972
1 changed files with 8 additions and 19 deletions

View File

@ -103,39 +103,28 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
Assert.notNull(commentParam, "Comment param must not be null"); Assert.notNull(commentParam, "Comment param must not be null");
Assert.notNull(request, "Http servlet request must not be null"); Assert.notNull(request, "Http servlet request must not be null");
// Post id must exist if (!postRepository.existsById(commentParam.getPostId())) {
boolean postExist = postRepository.existsById(commentParam.getPostId()); // Post id must exist
if (!postExist) {
log.error("Post: [{}] was not found", commentParam.getPostId()); log.error("Post: [{}] was not found", commentParam.getPostId());
throw new NotFoundException("The post was not found").setErrorData(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 // Convert to comment
Comment comment = commentParam.convertTo(); Comment comment = commentParam.convertTo();
// Set some default value // Set some default value
comment.setContent(OwoUtil.parseOwo(formatContent(comment.getContent())));
comment.setIpAddress(ServletUtil.getClientIP(request)); comment.setIpAddress(ServletUtil.getClientIP(request));
// TODO Check user login status and set this field // TODO Check user login status and set this field
comment.setIsAdmin(false); comment.setIsAdmin(false);
comment.setAuthor(HtmlUtils.htmlEscape(comment.getAuthor())); comment.setAuthor(HtmlUtils.htmlEscape(comment.getAuthor()));
comment.setGavatarMd5(SecureUtil.md5(comment.getEmail())); 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())) { if (StringUtils.isNotBlank(comment.getAuthorUrl())) {
// Normalize the author url and set it // Normalize the author url and set it
comment.setAuthorUrl(URLUtil.normalize(comment.getAuthorUrl())); comment.setAuthorUrl(URLUtil.normalize(comment.getAuthorUrl()));