diff --git a/src/main/java/cc/ryanc/halo/model/enums/BackupType.java b/src/main/java/cc/ryanc/halo/model/enums/BackupType.java new file mode 100644 index 000000000..daa1b66b8 --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/enums/BackupType.java @@ -0,0 +1,33 @@ +package cc.ryanc.halo.model.enums; + +/** + * @author : RYAN0UP + * @date : 2018/7/22 + */ +public enum BackupType { + + /** + * 资源文件 + */ + RESOURCES("resources"), + + /** + * 数据库 + */ + DATABASES("databases"), + + /** + * 文章 + */ + POSTS("posts"); + + private String desc; + + BackupType(String desc) { + this.desc = desc; + } + + public String getDesc() { + return desc; + } +} diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/BackupController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/BackupController.java index 8ac98e7a1..6b0c2d55b 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/BackupController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/BackupController.java @@ -5,10 +5,7 @@ import cc.ryanc.halo.model.domain.User; import cc.ryanc.halo.model.dto.BackupDto; import cc.ryanc.halo.model.dto.HaloConst; import cc.ryanc.halo.model.dto.JsonResult; -import cc.ryanc.halo.model.enums.BlogProperties; -import cc.ryanc.halo.model.enums.PostType; -import cc.ryanc.halo.model.enums.ResultCode; -import cc.ryanc.halo.model.enums.TrueFalse; +import cc.ryanc.halo.model.enums.*; import cc.ryanc.halo.service.MailService; import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.utils.HaloUtils; @@ -58,12 +55,12 @@ public class BackupController { @GetMapping public String backup(@RequestParam(value = "type", defaultValue = "resources") String type, Model model) { List backups = null; - if (StringUtils.equals(type, "resources")) { - backups = HaloUtils.getBackUps("resources"); - } else if (StringUtils.equals(type, "databases")) { - backups = HaloUtils.getBackUps("databases"); - } else if (StringUtils.equals(type, "posts")) { - backups = HaloUtils.getBackUps("posts"); + if (StringUtils.equals(type,BackupType.RESOURCES.getDesc())) { + backups = HaloUtils.getBackUps(BackupType.RESOURCES.getDesc()); + } else if (StringUtils.equals(type, BackupType.DATABASES.getDesc())) { + backups = HaloUtils.getBackUps(BackupType.DATABASES.getDesc()); + } else if (StringUtils.equals(type, BackupType.POSTS.getDesc())) { + backups = HaloUtils.getBackUps(BackupType.POSTS.getDesc()); } else { backups = new ArrayList<>(); } @@ -81,11 +78,11 @@ public class BackupController { @GetMapping(value = "doBackup") @ResponseBody public JsonResult doBackup(@RequestParam("type") String type) { - if (StringUtils.equals("resources", type)) { + if (StringUtils.equals(BackupType.RESOURCES.getDesc(), type)) { return this.backupResources(); - } else if (StringUtils.equals("databases", type)) { + } else if (StringUtils.equals(BackupType.DATABASES.getDesc(), type)) { return this.backupDatabase(); - } else if (StringUtils.equals("posts", type)) { + } else if (StringUtils.equals(BackupType.POSTS.getDesc(), type)) { return this.backupPosts(); } else { return new JsonResult(ResultCode.FAIL.getCode(), "备份失败!"); @@ -99,7 +96,7 @@ public class BackupController { */ public JsonResult backupDatabase() { try { - if (HaloUtils.getBackUps("databases").size() > 10) { + if (HaloUtils.getBackUps(BackupType.DATABASES.getDesc()).size() > 10) { FileUtil.del(System.getProperties().getProperty("user.home") + "/halo/backup/databases/"); } String srcPath = System.getProperties().getProperty("user.home") + "/halo/"; @@ -121,7 +118,7 @@ public class BackupController { */ public JsonResult backupResources() { try { - if (HaloUtils.getBackUps("resources").size() > 10) { + if (HaloUtils.getBackUps(BackupType.RESOURCES.getDesc()).size() > 10) { FileUtil.del(System.getProperties().getProperty("user.home") + "/halo/backup/resources/"); } File path = new File(ResourceUtils.getURL("classpath:").getPath()); @@ -146,7 +143,7 @@ public class BackupController { List posts = postService.findAllPosts(PostType.POST_TYPE_POST.getDesc()); posts.addAll(postService.findAllPosts(PostType.POST_TYPE_PAGE.getDesc())); try { - if (HaloUtils.getBackUps("posts").size() > 10) { + if (HaloUtils.getBackUps(BackupType.POSTS.getDesc()).size() > 10) { FileUtil.del(System.getProperties().getProperty("user.home") + "/halo/backup/posts/"); } //打包好的文件名 @@ -210,11 +207,14 @@ public class BackupController { return new JsonResult(ResultCode.SUCCESS.getCode(), "邮件发送成功!"); } + /** + * 异步发送附件到邮箱 + */ class EmailToAdmin extends Thread { private String srcPath; private User user; - public EmailToAdmin(String srcPath, User user) { + private EmailToAdmin(String srcPath, User user) { this.srcPath = srcPath; this.user = user; } 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 ce1289df1..c4e213103 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 @@ -11,7 +11,6 @@ import cc.ryanc.halo.model.enums.TrueFalse; import cc.ryanc.halo.service.CommentService; import cc.ryanc.halo.service.MailService; import cc.ryanc.halo.service.PostService; -import cc.ryanc.halo.service.UserService; import cc.ryanc.halo.utils.OwoUtil; import cc.ryanc.halo.web.controller.core.BaseController; import cn.hutool.core.date.DateUtil; @@ -54,9 +53,6 @@ public class CommentController extends BaseController { @Autowired private MailService mailService; - @Autowired - private UserService userService; - @Autowired private PostService postService; @@ -121,28 +117,7 @@ public class CommentController extends BaseController { User user = (User) session.getAttribute(HaloConst.USER_SESSION_KEY); //判断是否启用邮件服务 - if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.COMMENT_REPLY_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) { - try { - if (status == 1 && Validator.isEmail(comment.getCommentAuthorEmail())) { - Map map = new HashMap<>(); - 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()); - } else { - map.put("pageUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId()); - } - map.put("pageName", post.getPostTitle()); - map.put("commentContent", comment.getCommentContent()); - map.put("blogUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp())); - map.put("blogTitle", HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp())); - map.put("author", user.getUserDisplayName()); - mailService.sendTemplateMail( - comment.getCommentAuthorEmail(), - "您在" + HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "的评论已审核通过!", map, "common/mail/mail_passed.ftl"); - } - } catch (Exception e) { - log.error("邮件服务器未配置:{}", e.getMessage()); - } - } + new NoticeToAuthor(comment, post, user, status).start(); return "redirect:/admin/comments?status=" + status; } @@ -151,7 +126,7 @@ public class CommentController extends BaseController { * * @param commentId commentId 评论编号 * @param status status 评论状态 - * @param session session session + * @param page 当前页码 * @return string 重定向到/admin/comments */ @GetMapping("/remove") @@ -212,6 +187,34 @@ public class CommentController extends BaseController { commentService.saveByComment(comment); //邮件通知 + new EmailToAuthor(comment, lastComment, post, user, commentContent).start(); + } catch (Exception e) { + log.error("回复评论失败:{}", e.getMessage()); + } + return "redirect:/admin/comments"; + } + + /** + * 异步发送邮件回复给评论者 + */ + class EmailToAuthor extends Thread { + + private Comment comment; + private Comment lastComment; + private Post post; + private User user; + private String commentContent; + + private EmailToAuthor(Comment comment, Comment lastComment, Post post, User user, String commentContent) { + this.comment = comment; + this.lastComment = lastComment; + this.post = post; + this.user = user; + this.commentContent = commentContent; + } + + @Override + public void run() { if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.COMMENT_REPLY_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) { if (Validator.isEmail(lastComment.getCommentAuthorEmail())) { Map map = new HashMap<>(); @@ -231,9 +234,50 @@ public class CommentController extends BaseController { lastComment.getCommentAuthorEmail(), "您在" + HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "的评论有了新回复", map, "common/mail/mail_reply.ftl"); } } - } catch (Exception e) { - log.error("回复评论失败:{}", e.getMessage()); } - return "redirect:/admin/comments"; + } + + /** + * 异步通知评论者审核通过 + */ + class NoticeToAuthor extends Thread { + + private Comment comment; + private Post post; + private User user; + private Integer status; + + private NoticeToAuthor(Comment comment, Post post, User user, Integer status) { + this.comment = comment; + this.post = post; + this.user = user; + this.status = status; + } + + @Override + public void run() { + if (StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.SMTP_EMAIL_ENABLE.getProp()), TrueFalse.TRUE.getDesc()) && StringUtils.equals(HaloConst.OPTIONS.get(BlogProperties.COMMENT_REPLY_NOTICE.getProp()), TrueFalse.TRUE.getDesc())) { + try { + if (status == 1 && Validator.isEmail(comment.getCommentAuthorEmail())) { + Map map = new HashMap<>(); + 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()); + } else { + map.put("pageUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "/p/" + post.getPostUrl() + "#comment-id-" + comment.getCommentId()); + } + map.put("pageName", post.getPostTitle()); + map.put("commentContent", comment.getCommentContent()); + map.put("blogUrl", HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp())); + map.put("blogTitle", HaloConst.OPTIONS.get(BlogProperties.BLOG_TITLE.getProp())); + map.put("author", user.getUserDisplayName()); + mailService.sendTemplateMail( + comment.getCommentAuthorEmail(), + "您在" + HaloConst.OPTIONS.get(BlogProperties.BLOG_URL.getProp()) + "的评论已审核通过!", map, "common/mail/mail_passed.ftl"); + } + } catch (Exception e) { + log.error("邮件服务器未配置:{}", e.getMessage()); + } + } + } } } diff --git a/src/main/java/cc/ryanc/halo/web/controller/core/InstallController.java b/src/main/java/cc/ryanc/halo/web/controller/core/InstallController.java index 1dfea3fd3..3093d31b6 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/core/InstallController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/core/InstallController.java @@ -3,6 +3,7 @@ package cc.ryanc.halo.web.controller.core; import cc.ryanc.halo.model.domain.*; import cc.ryanc.halo.model.dto.HaloConst; import cc.ryanc.halo.model.dto.LogsRecord; +import cc.ryanc.halo.model.enums.AllowComment; import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.TrueFalse; import cc.ryanc.halo.service.*; @@ -132,6 +133,7 @@ public class InstallController { post.setPostUrl("hello-halo"); post.setUser(user); post.setCategories(categories); + post.setAllowComment(AllowComment.ALLOW.getCode()); postService.saveByPost(post); //第一个评论 diff --git a/src/main/resources/templates/admin/admin_index.ftl b/src/main/resources/templates/admin/admin_index.ftl index f3b4697e4..8fcb913df 100755 --- a/src/main/resources/templates/admin/admin_index.ftl +++ b/src/main/resources/templates/admin/admin_index.ftl @@ -345,7 +345,7 @@