From 4bd7f8f096a350c7f4d9603a654588098f73dee1 Mon Sep 17 00:00:00 2001 From: ruibaby Date: Tue, 7 May 2019 01:47:12 +0800 Subject: [PATCH] Complate CommentEventListener. --- .../admin/api/OptionController.java | 15 +++++++- .../event/comment/CommentEventListener.java | 38 +++++++++++++++---- .../method/RecentCommentsMethod.java | 27 ------------- .../freemarker/method/RecentPostsMethod.java | 27 ------------- .../run/halo/app/model/params/MailParam.java | 24 ++++++++++++ .../app/model/properties/EmailProperties.java | 4 +- .../app/service/impl/MailServiceImpl.java | 3 ++ .../common/mail_template/mail_reply.ftl | 12 +++--- 8 files changed, 79 insertions(+), 71 deletions(-) delete mode 100644 src/main/java/run/halo/app/model/freemarker/method/RecentCommentsMethod.java delete mode 100644 src/main/java/run/halo/app/model/freemarker/method/RecentPostsMethod.java create mode 100644 src/main/java/run/halo/app/model/params/MailParam.java diff --git a/src/main/java/run/halo/app/controller/admin/api/OptionController.java b/src/main/java/run/halo/app/controller/admin/api/OptionController.java index 54a0cc458..b9b86ef7a 100644 --- a/src/main/java/run/halo/app/controller/admin/api/OptionController.java +++ b/src/main/java/run/halo/app/controller/admin/api/OptionController.java @@ -4,7 +4,10 @@ import io.swagger.annotations.ApiOperation; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import run.halo.app.model.dto.OptionDTO; +import run.halo.app.model.params.MailParam; import run.halo.app.model.params.OptionParam; +import run.halo.app.model.support.BaseResponse; +import run.halo.app.service.MailService; import run.halo.app.service.OptionService; import javax.validation.Valid; @@ -23,8 +26,12 @@ public class OptionController { private final OptionService optionService; - public OptionController(OptionService optionService) { + private final MailService mailService; + + public OptionController(OptionService optionService, + MailService mailService) { this.optionService = optionService; + this.mailService = mailService; } @GetMapping @@ -59,4 +66,10 @@ public class OptionController { public void saveOptionsWithMapView(@RequestBody Map optionMap) { optionService.save(optionMap); } + + @PostMapping("test_mail") + public BaseResponse testMail(@Valid @RequestBody MailParam mailParam){ + mailService.sendMail(mailParam.getTo(),mailParam.getSubject(),mailParam.getContent()); + return BaseResponse.ok("发送成功"); + } } diff --git a/src/main/java/run/halo/app/event/comment/CommentEventListener.java b/src/main/java/run/halo/app/event/comment/CommentEventListener.java index 6f95c2de9..a576f93f5 100644 --- a/src/main/java/run/halo/app/event/comment/CommentEventListener.java +++ b/src/main/java/run/halo/app/event/comment/CommentEventListener.java @@ -1,6 +1,8 @@ package run.halo.app.event.comment; +import cn.hutool.core.lang.Validator; import cn.hutool.core.text.StrBuilder; +import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; @@ -68,7 +70,7 @@ public class CommentEventListener { Map data = new HashMap<>(); - if (this instanceof PostService) { + if (newEvent.getSource() instanceof PostService) { // Get postComment id PostComment postComment = postCommentService.getById(newEvent.getCommentId()); @@ -81,7 +83,7 @@ public class CommentEventListener { data.put("page", post.getTitle()); data.put("author", postComment.getAuthor()); data.put("content", postComment.getContent()); - } else if (this instanceof SheetService) { + } else if (newEvent.getSource() instanceof SheetService) { SheetComment sheetComment = sheetCommentService.getById(newEvent.getCommentId()); Sheet sheet = sheetService.getById(sheetComment.getPostId()); @@ -93,7 +95,7 @@ public class CommentEventListener { data.put("page", sheet.getTitle()); data.put("author", sheetComment.getAuthor()); data.put("content", sheetComment.getContent()); - } else if (this instanceof JournalService) { + } else if (newEvent.getSource() instanceof JournalService) { JournalComment journalComment = journalCommentService.getById(newEvent.getCommentId()); Journal journal = journalService.getById(journalComment.getPostId()); @@ -125,18 +127,26 @@ public class CommentEventListener { return; } - User user = userService.getCurrentUser().orElseThrow(() -> new ServiceException("Can not find blog owner")); + String baseAuthorEmail = ""; String blogTitle = optionService.getBlogTitle(); Map data = new HashMap<>(); - if (this instanceof PostService) { + log.debug("replyEvent.getSource():"+replyEvent.getSource().toString()); + + if (replyEvent.getSource() instanceof PostCommentService) { PostComment postComment = postCommentService.getById(replyEvent.getCommentId()); PostComment baseComment = postCommentService.getById(postComment.getParentId()); + if (StrUtil.isEmpty(baseComment.getEmail()) && !Validator.isEmail(baseComment.getEmail())) { + return; + } + + baseAuthorEmail = baseComment.getEmail(); + Post post = postService.getById(postComment.getPostId()); StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl()) @@ -149,12 +159,18 @@ public class CommentEventListener { data.put("baseContent", baseComment.getContent()); data.put("replyAuthor", postComment.getAuthor()); data.put("replyContent", postComment.getContent()); - } else if (this instanceof SheetService) { + } else if (replyEvent.getSource() instanceof SheetCommentService) { SheetComment sheetComment = sheetCommentService.getById(replyEvent.getCommentId()); SheetComment baseComment = sheetCommentService.getById(sheetComment.getParentId()); + if (StrUtil.isEmpty(baseComment.getEmail()) && !Validator.isEmail(baseComment.getEmail())) { + return; + } + + baseAuthorEmail = baseComment.getEmail(); + Sheet sheet = sheetService.getById(sheetComment.getPostId()); StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl()) @@ -167,11 +183,17 @@ public class CommentEventListener { data.put("baseContent", baseComment.getContent()); data.put("replyAuthor", sheetComment.getAuthor()); data.put("replyContent", sheetComment.getContent()); - } else if (this instanceof JournalService) { + } else if (replyEvent.getSource() instanceof JournalCommentService) { JournalComment journalComment = journalCommentService.getById(replyEvent.getCommentId()); JournalComment baseComment = journalCommentService.getById(journalComment.getParentId()); + if (StrUtil.isEmpty(baseComment.getEmail()) && !Validator.isEmail(baseComment.getEmail())) { + return; + } + + baseAuthorEmail = baseComment.getEmail(); + Journal journal = journalService.getById(journalComment.getPostId()); StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl()) @@ -184,6 +206,6 @@ public class CommentEventListener { data.put("replyContent", journalComment.getContent()); } - mailService.sendTemplateMail(user.getEmail(), "您在【" + blogTitle + "】的评论有新回复", data, "common/mail_template/mail_reply.ftl"); + mailService.sendTemplateMail(baseAuthorEmail, "您在【" + blogTitle + "】的评论有新回复", data, "common/mail_template/mail_reply.ftl"); } } diff --git a/src/main/java/run/halo/app/model/freemarker/method/RecentCommentsMethod.java b/src/main/java/run/halo/app/model/freemarker/method/RecentCommentsMethod.java deleted file mode 100644 index eeb789f83..000000000 --- a/src/main/java/run/halo/app/model/freemarker/method/RecentCommentsMethod.java +++ /dev/null @@ -1,27 +0,0 @@ -package run.halo.app.model.freemarker.method; - -import freemarker.template.Configuration; -import freemarker.template.TemplateMethodModelEx; -import freemarker.template.TemplateModelException; -import org.springframework.stereotype.Component; - -import java.util.List; - -/** - * @author ryanwang - * @date : 2018/12/31 - */ -@Component -@Deprecated -public class RecentCommentsMethod implements TemplateMethodModelEx { - - public RecentCommentsMethod(Configuration configuration) { - configuration.setSharedVariable("recentCommentsMethod", this); - } - - @Override - public Object exec(List arguments) throws TemplateModelException { - // TODO Complete recent comments method. - return null; - } -} diff --git a/src/main/java/run/halo/app/model/freemarker/method/RecentPostsMethod.java b/src/main/java/run/halo/app/model/freemarker/method/RecentPostsMethod.java deleted file mode 100644 index 8e8ce466b..000000000 --- a/src/main/java/run/halo/app/model/freemarker/method/RecentPostsMethod.java +++ /dev/null @@ -1,27 +0,0 @@ -package run.halo.app.model.freemarker.method; - -import freemarker.template.Configuration; -import freemarker.template.TemplateMethodModelEx; -import freemarker.template.TemplateModelException; -import org.springframework.stereotype.Component; - -import java.util.List; - -/** - * @author ryanwang - * @date : 2018/12/31 - */ -@Component -@Deprecated -public class RecentPostsMethod implements TemplateMethodModelEx { - - public RecentPostsMethod(Configuration configuration) { - configuration.setSharedVariable("recentPostsMethod", this); - } - - @Override - public Object exec(List arguments) throws TemplateModelException { - // TODO Complete recent post method. - return null; - } -} diff --git a/src/main/java/run/halo/app/model/params/MailParam.java b/src/main/java/run/halo/app/model/params/MailParam.java new file mode 100644 index 000000000..d106a8ad1 --- /dev/null +++ b/src/main/java/run/halo/app/model/params/MailParam.java @@ -0,0 +1,24 @@ +package run.halo.app.model.params; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; + +/** + * Journal query params. + * + * @author ryanwang + * @date : 2019/05/07 + */ +@Data +public class MailParam { + + @NotBlank(message = "收件人不能为空") + private String to; + + @NotBlank(message = "主题不能为空") + private String subject; + + @NotBlank(message = "内容不能为空") + private String content; +} diff --git a/src/main/java/run/halo/app/model/properties/EmailProperties.java b/src/main/java/run/halo/app/model/properties/EmailProperties.java index 71f02cc23..5eb6366a8 100644 --- a/src/main/java/run/halo/app/model/properties/EmailProperties.java +++ b/src/main/java/run/halo/app/model/properties/EmailProperties.java @@ -10,9 +10,9 @@ public enum EmailProperties implements PropertyEnum { HOST("email_host", String.class, ""), - PROTOCOL("email_protocol", String.class, ""), + PROTOCOL("email_protocol", String.class, "SSL"), - SSL_PORT("email_ssl_port", Integer.class, ""), + SSL_PORT("email_ssl_port", Integer.class, "465"), USERNAME("email_username", String.class, ""), diff --git a/src/main/java/run/halo/app/service/impl/MailServiceImpl.java b/src/main/java/run/halo/app/service/impl/MailServiceImpl.java index d012f1b31..cd3d245c6 100644 --- a/src/main/java/run/halo/app/service/impl/MailServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/MailServiceImpl.java @@ -1,6 +1,7 @@ package run.halo.app.service.impl; import cn.hutool.core.text.StrBuilder; +import cn.hutool.core.util.StrUtil; import freemarker.template.Template; import io.github.biezhi.ome.OhMyEmail; import lombok.extern.slf4j.Slf4j; @@ -150,6 +151,8 @@ public class MailServiceImpl implements MailService { Properties defaultProperties = OhMyEmail.defaultConfig(log.isDebugEnabled()); // Set smtp host defaultProperties.setProperty("mail.smtp.host", optionService.getByPropertyOfNonNull(EmailProperties.HOST).toString()); + defaultProperties.setProperty("mail.smtp.ssl.enable", StrUtil.isEmpty(optionService.getByPropertyOfNonNull(EmailProperties.PROTOCOL).toString()) ? "false" : "true"); + defaultProperties.setProperty("mail.smtp.port", StrUtil.isEmpty(optionService.getByPropertyOfNonNull(EmailProperties.SSL_PORT).toString()) ? "25" : "465"); // Config email OhMyEmail.config(defaultProperties, optionService.getByPropertyOfNonNull(EmailProperties.USERNAME).toString(), diff --git a/src/main/resources/templates/common/mail_template/mail_reply.ftl b/src/main/resources/templates/common/mail_template/mail_reply.ftl index bf8653987..a54f94e2d 100644 --- a/src/main/resources/templates/common/mail_template/mail_reply.ftl +++ b/src/main/resources/templates/common/mail_template/mail_reply.ftl @@ -6,15 +6,15 @@
-

${baseAuthor}, 您好!

-

您在《${page}》的留言: +

${baseAuthor!}, 您好!

+

您在《${page!}》的留言:
-

${baseContent}

-

${replyAuthor} 给你的回复: +

${baseContent!}

+

${replyAuthor!} 给你的回复:
-

${replyContent}

+

${replyContent!}

你可以点击 - 查看完整内容 + 查看完整内容

欢迎再度光临 ${options.blog_title!}