diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/CommentController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/CommentController.java index eca368522..1abf32ef4 100755 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/CommentController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/CommentController.java @@ -111,6 +111,7 @@ public class CommentController extends BaseController{ public String moveToPublish(@PathParam("commentId") Long commentId, @PathParam("status") Integer status){ Comment comment = commentService.updateCommentStatus(commentId,0); + Post post = comment.getPost(); //判断评论者的邮箱是否符合规则 Pattern patternEmail = Pattern.compile("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}"); @@ -121,8 +122,12 @@ public class CommentController extends BaseController{ try { if (status == 1 && matcher.find()) { Map map = new HashMap<>(); - map.put("pageUrl", HaloConst.OPTIONS.get("blog_url")+"/archives/"+comment.getPost().getPostUrl()+"#comment-id-"+comment.getCommentId()); - map.put("pageName", comment.getPost().getPostTitle()); + if (StringUtils.equals(post.getPostType(), HaloConst.POST_TYPE_POST)) { + map.put("pageUrl", HaloConst.OPTIONS.get("blog_url") + "/archives/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId()); + } else { + map.put("pageUrl", HaloConst.OPTIONS.get("blog_url") + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId()); + } + map.put("pageName", post.getPostTitle()); map.put("commentContent", comment.getCommentContent()); map.put("blogUrl", HaloConst.OPTIONS.get("blog_url")); map.put("blogTitle", HaloConst.OPTIONS.get("blog_title")); @@ -213,7 +218,11 @@ public class CommentController extends BaseController{ map.put("blogTitle",HaloConst.OPTIONS.get("blog_title")); map.put("commentAuthor",lastComment.getCommentAuthor()); map.put("pageName",lastComment.getPost().getPostTitle()); - map.put("pageUrl",HaloConst.OPTIONS.get("blog_url")+"/archives/"+post.getPostUrl()+"#comment-id-"+comment.getCommentId()); + if (StringUtils.equals(post.getPostType(), HaloConst.POST_TYPE_POST)) { + map.put("pageUrl", HaloConst.OPTIONS.get("blog_url") + "/archives/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId()); + } else { + map.put("pageUrl", HaloConst.OPTIONS.get("blog_url") + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId()); + } map.put("commentContent",lastComment.getCommentContent()); map.put("replyAuthor",user.getUserDisplayName()); map.put("replyContent",commentContent); diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java index 657b75d1c..b4f7778bd 100755 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java @@ -178,7 +178,7 @@ public class PostController extends BaseController{ List categories = categoryService.strListToCateList(cateList); post.setCategories(categories); if(StringUtils.isNotEmpty(tagList)){ - List tags = tagService.strListToTagList(StringUtils.trim(tagList)); + List tags = tagService.strListToTagList(StringUtils.deleteWhitespace(tagList)); post.setTags(tags); } post.setPostUrl(urlFilter(post.getPostUrl())); diff --git a/src/main/java/cc/ryanc/halo/web/controller/front/FrontCommentController.java b/src/main/java/cc/ryanc/halo/web/controller/front/FrontCommentController.java index e6be8ebe0..dbf024834 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/front/FrontCommentController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/front/FrontCommentController.java @@ -8,6 +8,7 @@ import cc.ryanc.halo.service.MailService; import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.UserService; import cc.ryanc.halo.utils.HaloUtils; +import lombok.Value; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -20,6 +21,8 @@ import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @author : RYAN0UP @@ -61,6 +64,23 @@ public class FrontCommentController { return comments; } + /** + * 加载评论 + * + * @param page 页码 + * @param post 当前文章 + * @return List + */ + @GetMapping(value = "/loadComment") + @ResponseBody + public List loadComment(@RequestParam(value = "page") Integer page, + @RequestParam(value = "post") Post post){ + Sort sort = new Sort(Sort.Direction.DESC,"commentDate"); + Pageable pageable = PageRequest.of(page-1,10,sort); + List comments = commentService.findCommentsByPostAndCommentStatus(post,pageable,2).getContent(); + return comments; + } + /** * 提交新评论 * @@ -74,31 +94,104 @@ public class FrontCommentController { public boolean newComment(@ModelAttribute("comment") Comment comment, @ModelAttribute("post") Post post, HttpServletRequest request) { + Comment lastComment = null; post = postService.findByPostId(post.getPostId()).get(); - if (StringUtils.isBlank(comment.getCommentAuthor())) { - comment.setCommentAuthor("小猪佩琪"); - } comment.setCommentAuthorEmail(comment.getCommentAuthorEmail().toLowerCase()); comment.setPost(post); comment.setCommentDate(new Date()); comment.setCommentAuthorIp(HaloUtils.getIpAddr(request)); comment.setIsAdmin(0); - commentService.saveByComment(comment); - - if (StringUtils.equals(HaloConst.OPTIONS.get("smtp_email_enable"), "true") && StringUtils.equals(HaloConst.OPTIONS.get("new_comment_notice"), "true")) { - try { - //发送邮件到博主 - Map map = new HashMap<>(); - map.put("author", userService.findUser().getUserDisplayName()); - map.put("pageName", post.getPostTitle()); - map.put("pageUrl", HaloConst.OPTIONS.get("blog_url")+"/archives/"+post.getPostUrl()+"#comment-id-"+comment.getCommentId()); - map.put("visitor", comment.getCommentAuthor()); - map.put("commentContent", comment.getCommentContent()); - mailService.sendTemplateMail(userService.findUser().getUserEmail(), "有新的评论", map, "common/mail/mail_admin.ftl"); - } catch (Exception e) { - log.error("邮件服务器未配置:{0}", e.getMessage()); + if(comment.getCommentParent()>0){ + lastComment = commentService.findCommentById(comment.getCommentParent()).get(); + String lastContent = " //@"+lastComment.getCommentAuthor()+":"+lastComment.getCommentContent(); + comment.setCommentContent(StringUtils.substringAfter(comment.getCommentContent(),":")+lastContent); + } + if(StringUtils.isNotEmpty(comment.getCommentAuthorUrl())){ + if(!StringUtils.containsAny(comment.getCommentAuthorUrl(),"https://") || !StringUtils.containsAny(comment.getCommentAuthorUrl(),"http://")){ + comment.setCommentAuthorUrl("http://"+comment.getCommentAuthorUrl()); } } + commentService.saveByComment(comment); + if(comment.getCommentParent()>0){ + //new EmailToParent(comment,lastComment,post).start(); + }else{ + new EmailToAdmin(comment,post).start(); + } return true; } + + /** + * 发送邮件给博主 + */ + class EmailToAdmin extends Thread{ + private Comment comment; + private Post post; + public EmailToAdmin(Comment comment,Post post){ + this.comment = comment; + this.post = post; + } + @Override + public void run(){ + if (StringUtils.equals(HaloConst.OPTIONS.get("smtp_email_enable"), "true") && StringUtils.equals(HaloConst.OPTIONS.get("new_comment_notice"), "true")) { + try { + //发送邮件到博主 + Map map = new HashMap<>(); + map.put("author", userService.findUser().getUserDisplayName()); + map.put("pageName", post.getPostTitle()); + if (StringUtils.equals(post.getPostType(), HaloConst.POST_TYPE_POST)) { + map.put("pageUrl", HaloConst.OPTIONS.get("blog_url") + "/archives/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId()); + } else { + map.put("pageUrl", HaloConst.OPTIONS.get("blog_url") + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId()); + } + map.put("visitor", comment.getCommentAuthor()); + map.put("commentContent", comment.getCommentContent()); + mailService.sendTemplateMail(userService.findUser().getUserEmail(), "有新的评论", map, "common/mail/mail_admin.ftl"); + } catch (Exception e) { + log.error("邮件服务器未配置:", e.getMessage()); + } + } + } + } + + /** + * 发送邮件给被评论方 + */ + class EmailToParent extends Thread{ + private Comment comment; + private Comment lastComment; + private Post post; + public EmailToParent(Comment comment,Comment lastComment,Post post){ + this.comment = comment; + this.lastComment = lastComment; + this.post = post; + } + + Pattern patternEmail = Pattern.compile("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}"); + Matcher matcher = patternEmail.matcher(lastComment.getCommentAuthorEmail()); + + @Override + public void run() { + //发送通知给对方 + if(StringUtils.equals(HaloConst.OPTIONS.get("smtp_email_enable"),"true") && StringUtils.equals(HaloConst.OPTIONS.get("comment_reply_notice"),"true")) { + if(matcher.find()){ + Map map = new HashMap<>(); + map.put("blogTitle",HaloConst.OPTIONS.get("blog_title")); + map.put("commentAuthor",lastComment.getCommentAuthor()); + map.put("pageName",lastComment.getPost().getPostTitle()); + if (StringUtils.equals(post.getPostType(), HaloConst.POST_TYPE_POST)) { + map.put("pageUrl", HaloConst.OPTIONS.get("blog_url") + "/archives/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId()); + } else { + map.put("pageUrl", HaloConst.OPTIONS.get("blog_url") + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId()); + } + map.put("commentContent",lastComment.getCommentContent()); + map.put("replyAuthor",comment.getCommentAuthor()); + map.put("replyContent",comment.getCommentContent()); + map.put("blogUrl",HaloConst.OPTIONS.get("blog_url")); + mailService.sendTemplateMail( + lastComment.getCommentAuthorEmail(),"您在"+HaloConst.OPTIONS.get("blog_title")+"的评论有了新回复",map,"common/mail/mail_reply.ftl"); + } + } + } + } } + diff --git a/src/main/resources/templates/admin/admin_comment.ftl b/src/main/resources/templates/admin/admin_comment.ftl index e59cfadad..a9e01abe8 100755 --- a/src/main/resources/templates/admin/admin_comment.ftl +++ b/src/main/resources/templates/admin/admin_comment.ftl @@ -51,14 +51,18 @@ ${comment.commentAuthor} ${comment.commentContent} - ${comment.post.postTitle} + <#if comment.post.postType == "post"> + ${comment.post.postTitle} + <#else > + ${comment.post.postTitle} + ${comment.commentDate} <#switch comment.commentStatus> <#case 0> - + <#break > <#case 1> 通过 diff --git a/src/main/resources/templates/admin/admin_index.ftl b/src/main/resources/templates/admin/admin_index.ftl index 488fdad45..b37467e07 100755 --- a/src/main/resources/templates/admin/admin_index.ftl +++ b/src/main/resources/templates/admin/admin_index.ftl @@ -229,7 +229,7 @@ 评论者 评论页面 - 内容 + 内容 状态 时间 @@ -240,7 +240,11 @@ ${comment.commentAuthor} - ${comment.post.postTitle} + <#if comment.post.postType=="post"> + ${comment.post.postTitle} + <#else> + ${comment.post.postTitle} + <#switch comment.commentStatus> diff --git a/src/main/resources/templates/admin/admin_option.ftl b/src/main/resources/templates/admin/admin_option.ftl index 7a398c8d8..b952690f1 100755 --- a/src/main/resources/templates/admin/admin_option.ftl +++ b/src/main/resources/templates/admin/admin_option.ftl @@ -265,7 +265,12 @@ - +
+ +
+ +
+