可选是否显示未审核的评论

pull/33/merge
ruibaby 2018-07-24 18:24:00 +08:00
parent 6da6c21478
commit da6ffd600d
9 changed files with 100 additions and 34 deletions

View File

@ -71,6 +71,11 @@ public enum BlogProperties {
*/ */
COMMENT_REPLY_NOTICE("comment_reply_notice"), COMMENT_REPLY_NOTICE("comment_reply_notice"),
/**
*
*/
NEW_COMMENT_NEED_CHECK("new_comment_need_check"),
/** /**
* *
*/ */

View File

@ -64,6 +64,15 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
*/ */
List<Comment> findCommentsByPostAndCommentStatus(Post post, Integer status); List<Comment> findCommentsByPostAndCommentStatus(Post post, Integer status);
/**
*
*
* @param post post
* @param status status
* @return List
*/
List<Comment> findCommentsByPostAndCommentStatusNot(Post post, Integer status);
/** /**
* *
* *

View File

@ -98,6 +98,15 @@ public interface CommentService {
*/ */
List<Comment> findCommentsByPostAndCommentStatus(Post post, Integer status); List<Comment> findCommentsByPostAndCommentStatus(Post post, Integer status);
/**
*
*
* @param post post
* @param status status
* @return List
*/
List<Comment> findCommentsByPostAndCommentStatusNot(Post post, Integer status);
/** /**
* *
* *

View File

@ -151,6 +151,18 @@ public class CommentServiceImpl implements CommentService {
return commentRepository.findCommentsByPostAndCommentStatus(post, status); return commentRepository.findCommentsByPostAndCommentStatus(post, status);
} }
/**
*
*
* @param post post
* @param status status
* @return List
*/
@Override
public List<Comment> findCommentsByPostAndCommentStatusNot(Post post, Integer status) {
return commentRepository.findCommentsByPostAndCommentStatusNot(post, status);
}
/** /**
* *
* *

View File

@ -2,14 +2,14 @@ package cc.ryanc.halo.web.controller.front;
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;
import cc.ryanc.halo.model.enums.CommentStatus; import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.enums.PostStatus; import cc.ryanc.halo.model.enums.*;
import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.service.CommentService; import cc.ryanc.halo.service.CommentService;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.utils.CommentUtil; import cc.ryanc.halo.utils.CommentUtil;
import cc.ryanc.halo.web.controller.core.BaseController; import cc.ryanc.halo.web.controller.core.BaseController;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
@ -118,7 +118,12 @@ public class FrontArchiveController extends BaseController {
if (null != afterPosts && afterPosts.size() > 0) { if (null != afterPosts && afterPosts.size() > 0) {
model.addAttribute("afterPost", afterPosts.get(afterPosts.size() - 1)); model.addAttribute("afterPost", afterPosts.get(afterPosts.size() - 1));
} }
List<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post, CommentStatus.PUBLISHED.getCode()); List<Comment> comments = null;
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getProp()), TrueFalse.TRUE.getDesc()) || HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getProp()) == null) {
comments = commentService.findCommentsByPostAndCommentStatus(post, CommentStatus.PUBLISHED.getCode());
} else {
comments = commentService.findCommentsByPostAndCommentStatusNot(post, CommentStatus.RECYCLE.getCode());
}
model.addAttribute("post", post); model.addAttribute("post", post);
model.addAttribute("comments", CommentUtil.getComments(comments)); model.addAttribute("comments", CommentUtil.getComments(comments));
model.addAttribute("commentsCount", comments.size()); model.addAttribute("commentsCount", comments.size());

View File

@ -78,10 +78,10 @@ public class FrontCommentController {
@GetMapping(value = "/loadComment") @GetMapping(value = "/loadComment")
@ResponseBody @ResponseBody
public List<Comment> loadComment(@RequestParam(value = "page") Integer page, public List<Comment> loadComment(@RequestParam(value = "page") Integer page,
@RequestParam(value = "post") Post post){ @RequestParam(value = "post") Post post) {
Sort sort = new Sort(Sort.Direction.DESC,"commentDate"); Sort sort = new Sort(Sort.Direction.DESC, "commentDate");
Pageable pageable = PageRequest.of(page-1,10,sort); Pageable pageable = PageRequest.of(page - 1, 10, sort);
List<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post,pageable,CommentStatus.PUBLISHED.getCode()).getContent(); List<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post, pageable, CommentStatus.PUBLISHED.getCode()).getContent();
return comments; return comments;
} }
@ -96,15 +96,15 @@ public class FrontCommentController {
@PostMapping(value = "/newComment") @PostMapping(value = "/newComment")
@ResponseBody @ResponseBody
public JsonResult newComment(@ModelAttribute("comment") Comment comment, public JsonResult newComment(@ModelAttribute("comment") Comment comment,
@ModelAttribute("post") Post post, @ModelAttribute("post") Post post,
HttpServletRequest request) { HttpServletRequest request) {
if (StringUtils.equals(StringUtils.trim(comment.getCommentAuthor()), "")) { if (StringUtils.equals(StringUtils.trim(comment.getCommentAuthor()), "")) {
return new JsonResult(ResultCode.FAIL.getCode(), "请正确输入昵称!"); return new JsonResult(ResultCode.FAIL.getCode(), "请正确输入昵称!");
} }
if (StringUtils.equals(StringUtils.trim(comment.getCommentContent()), "")) { if (StringUtils.equals(StringUtils.trim(comment.getCommentContent()), "")) {
return new JsonResult(ResultCode.FAIL.getCode(), "请正确输入评论内容!"); return new JsonResult(ResultCode.FAIL.getCode(), "请正确输入评论内容!");
} }
try{ try {
Comment lastComment = null; Comment lastComment = null;
post = postService.findByPostId(post.getPostId()).get(); post = postService.findByPostId(post.getPostId()).get();
comment.setCommentAuthorEmail(HtmlUtil.encode(comment.getCommentAuthorEmail()).toLowerCase()); comment.setCommentAuthorEmail(HtmlUtil.encode(comment.getCommentAuthorEmail()).toLowerCase());
@ -113,34 +113,38 @@ public class FrontCommentController {
comment.setCommentAuthorIp(ServletUtil.getClientIP(request)); comment.setCommentAuthorIp(ServletUtil.getClientIP(request));
comment.setIsAdmin(0); comment.setIsAdmin(0);
comment.setCommentAuthor(HtmlUtil.encode(comment.getCommentAuthor())); comment.setCommentAuthor(HtmlUtil.encode(comment.getCommentAuthor()));
if(comment.getCommentParent()>0){ if (comment.getCommentParent() > 0) {
lastComment = commentService.findCommentById(comment.getCommentParent()).get(); lastComment = commentService.findCommentById(comment.getCommentParent()).get();
String lastContent = "<a href='#comment-id-" + lastComment.getCommentId() + "'>@" + lastComment.getCommentAuthor() + "</a>"; String lastContent = "<a href='#comment-id-" + lastComment.getCommentId() + "'>@" + lastComment.getCommentAuthor() + "</a>";
comment.setCommentContent(lastContent + StringUtils.substringAfter(OwoUtil.markToImg(HtmlUtil.encode(comment.getCommentContent())), ":")); comment.setCommentContent(lastContent + StringUtils.substringAfter(OwoUtil.markToImg(HtmlUtil.encode(comment.getCommentContent())), ":"));
}else{ } else {
//将评论内容的字符专为安全字符 //将评论内容的字符专为安全字符
comment.setCommentContent(OwoUtil.markToImg(HtmlUtil.encode(comment.getCommentContent()))); comment.setCommentContent(OwoUtil.markToImg(HtmlUtil.encode(comment.getCommentContent())));
} }
if(StringUtils.isNotEmpty(comment.getCommentAuthorUrl())){ if (StringUtils.isNotEmpty(comment.getCommentAuthorUrl())) {
comment.setCommentAuthorUrl(URLUtil.formatUrl(comment.getCommentAuthorUrl())); comment.setCommentAuthorUrl(URLUtil.formatUrl(comment.getCommentAuthorUrl()));
} }
commentService.saveByComment(comment); commentService.saveByComment(comment);
if(comment.getCommentParent()>0){ if (comment.getCommentParent() > 0) {
new EmailToParent(comment,lastComment,post).start(); new EmailToParent(comment, lastComment, post).start();
new EmailToAdmin(comment, post).start();
} else {
new EmailToAdmin(comment, post).start(); new EmailToAdmin(comment, post).start();
}else{
new EmailToAdmin(comment,post).start();
} }
return new JsonResult(ResultCode.SUCCESS.getCode(),"你的评论已经提交,待博主审核之后可显示。"); if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getProp()), TrueFalse.TRUE.getDesc()) || HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getProp()) == null) {
}catch (Exception e){ return new JsonResult(ResultCode.SUCCESS.getCode(), "你的评论已经提交,待博主审核之后可显示。");
return new JsonResult(ResultCode.FAIL.getCode(),"评论失败!"); } else {
return new JsonResult(ResultCode.SUCCESS.getCode(), "你的评论已经提交,刷新后即可显示。");
}
} catch (Exception e) {
return new JsonResult(ResultCode.FAIL.getCode(), "评论失败!");
} }
} }
/** /**
* *
*/ */
class EmailToAdmin extends Thread{ class EmailToAdmin extends Thread {
private Comment comment; private Comment comment;
private Post post; private Post post;
@ -148,8 +152,9 @@ public class FrontCommentController {
this.comment = comment; this.comment = comment;
this.post = post; this.post = post;
} }
@Override @Override
public void run(){ public void run() {
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) { if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) {
try { try {
//发送邮件到博主 //发送邮件到博主
@ -174,7 +179,7 @@ public class FrontCommentController {
/** /**
* *
*/ */
class EmailToParent extends Thread{ class EmailToParent extends Thread {
private Comment comment; private Comment comment;
private Comment lastComment; private Comment lastComment;
private Post post; private Post post;
@ -189,22 +194,22 @@ public class FrontCommentController {
public void run() { public void run() {
//发送通知给对方 //发送通知给对方
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) { if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) {
if(Validator.isEmail(lastComment.getCommentAuthorEmail())){ if (Validator.isEmail(lastComment.getCommentAuthorEmail())) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("blogTitle",HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp())); map.put("blogTitle", HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp()));
map.put("commentAuthor",lastComment.getCommentAuthor()); map.put("commentAuthor", lastComment.getCommentAuthor());
map.put("pageName",lastComment.getPost().getPostTitle()); map.put("pageName", lastComment.getPost().getPostTitle());
if (StringUtils.equals(post.getPostType(), PostType.POST_TYPE_POST.getDesc())) { if (StringUtils.equals(post.getPostType(), PostType.POST_TYPE_POST.getDesc())) {
map.put("pageUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "/archives/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId()); map.put("pageUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "/archives/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
} else { } else {
map.put("pageUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId()); map.put("pageUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId());
} }
map.put("commentContent",lastComment.getCommentContent()); map.put("commentContent", lastComment.getCommentContent());
map.put("replyAuthor",comment.getCommentAuthor()); map.put("replyAuthor", comment.getCommentAuthor());
map.put("replyContent",comment.getCommentContent()); map.put("replyContent", comment.getCommentContent());
map.put("blogUrl",HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp())); map.put("blogUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()));
mailService.sendTemplateMail( mailService.sendTemplateMail(
lastComment.getCommentAuthorEmail(),"您在"+HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp())+"的评论有了新回复",map,"common/mail/mail_reply.ftl"); lastComment.getCommentAuthorEmail(), "您在" + HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp()) + "的评论有了新回复", map, "common/mail/mail_reply.ftl");
} }
} }
} }

View File

@ -3,13 +3,17 @@ package cc.ryanc.halo.web.controller.front;
import cc.ryanc.halo.model.domain.Comment; import cc.ryanc.halo.model.domain.Comment;
import cc.ryanc.halo.model.domain.Gallery; import cc.ryanc.halo.model.domain.Gallery;
import cc.ryanc.halo.model.domain.Post; import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.CommentStatus; import cc.ryanc.halo.model.enums.CommentStatus;
import cc.ryanc.halo.model.enums.PostType; import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.model.enums.TrueFalse;
import cc.ryanc.halo.service.CommentService; import cc.ryanc.halo.service.CommentService;
import cc.ryanc.halo.service.GalleryService; import cc.ryanc.halo.service.GalleryService;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.utils.CommentUtil; import cc.ryanc.halo.utils.CommentUtil;
import cc.ryanc.halo.web.controller.core.BaseController; import cc.ryanc.halo.web.controller.core.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
@ -69,7 +73,12 @@ public class FrontPageController extends BaseController {
if (null == post) { if (null == post) {
return this.renderNotFound(); return this.renderNotFound();
} }
List<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post, CommentStatus.PUBLISHED.getCode()); List<Comment> comments = null;
if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getProp()), TrueFalse.TRUE.getDesc()) || HaloConst.OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getProp()) == null) {
comments = commentService.findCommentsByPostAndCommentStatus(post, CommentStatus.PUBLISHED.getCode());
} else {
comments = commentService.findCommentsByPostAndCommentStatusNot(post, CommentStatus.RECYCLE.getCode());
}
model.addAttribute("post", post); model.addAttribute("post", post);
model.addAttribute("comments", CommentUtil.getComments(comments)); model.addAttribute("comments", CommentUtil.getComments(comments));
model.addAttribute("commentsCount", comments.size()); model.addAttribute("commentsCount", comments.size());

View File

@ -232,6 +232,17 @@
</select> </select>
</div> </div>
</div> </div>
<div class="form-group">
<label class="col-lg-2 col-sm-4 control-label">评论审核后才显示:</label>
<div class="col-lg-4 col-sm-8">
<label class="radio-inline">
<input type="radio" name="new_comment_need_check" value="true" ${((options.new_comment_need_check?default("true"))=='true')?string('checked','')}> 开启
</label>
<label class="radio-inline">
<input type="radio" name="new_comment_need_check" value="false" ${((options.new_comment_need_check?if_exists)=='false')?string('checked','')}> 关闭
</label>
</div>
</div>
<div class="form-group"> <div class="form-group">
<label class="col-lg-2 col-sm-4 control-label">新评论通知:</label> <label class="col-lg-2 col-sm-4 control-label">新评论通知:</label>
<div class="col-lg-4 col-sm-8"> <div class="col-lg-4 col-sm-8">

View File

@ -388,6 +388,7 @@
$(".native-message").fadeOut(1000); $(".native-message").fadeOut(1000);
$("#btn-push").removeAttr("disabled"); $("#btn-push").removeAttr("disabled");
$("#btn-push").html("提交"); $("#btn-push").html("提交");
window.location.reload();
},1500); },1500);
} }
}); });