From c847bef498ee97665ef356cad1343b784026e100 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Wed, 18 Mar 2020 21:38:26 +0800 Subject: [PATCH] feat: support custom email templates. (#691) --- .../comment/CommentEventListener.java | 45 ++++++++++++------- .../common/mail_template/mail_notice.ftl | 4 +- .../common/mail_template/mail_reply.ftl | 4 +- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/main/java/run/halo/app/listener/comment/CommentEventListener.java b/src/main/java/run/halo/app/listener/comment/CommentEventListener.java index 2f7554387..1cf69fd18 100644 --- a/src/main/java/run/halo/app/listener/comment/CommentEventListener.java +++ b/src/main/java/run/halo/app/listener/comment/CommentEventListener.java @@ -48,7 +48,9 @@ public class CommentEventListener { private final UserService userService; - public CommentEventListener(MailService mailService, OptionService optionService, PostCommentService postCommentService, SheetCommentService sheetCommentService, JournalCommentService journalCommentService, PostService postService, SheetService sheetService, JournalService journalService, UserService userService) { + private final ThemeService themeService; + + public CommentEventListener(MailService mailService, OptionService optionService, PostCommentService postCommentService, SheetCommentService sheetCommentService, JournalCommentService journalCommentService, PostService postService, SheetService sheetService, JournalService journalService, UserService userService, ThemeService themeService) { this.mailService = mailService; this.optionService = optionService; this.postCommentService = postCommentService; @@ -58,6 +60,7 @@ public class CommentEventListener { this.sheetService = sheetService; this.journalService = journalService; this.userService = userService; + this.themeService = themeService; } /** @@ -90,8 +93,8 @@ public class CommentEventListener { BasePostMinimalDTO post = postService.convertToMinimal(postService.getById(postComment.getPostId())); - data.put("url", post.getFullPath()); - data.put("page", post.getTitle()); + data.put("pageFullPath", post.getFullPath()); + data.put("pageTitle", post.getTitle()); data.put("author", postComment.getAuthor()); data.put("content", postComment.getContent()); @@ -106,8 +109,8 @@ public class CommentEventListener { BasePostMinimalDTO sheet = sheetService.convertToMinimal(sheetService.getById(sheetComment.getPostId())); - data.put("url", sheet.getFullPath()); - data.put("page", sheet.getTitle()); + data.put("pageFullPath", sheet.getFullPath()); + data.put("pageTitle", sheet.getTitle()); data.put("author", sheetComment.getAuthor()); data.put("content", sheetComment.getContent()); @@ -124,15 +127,21 @@ public class CommentEventListener { StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl()) .append("/") .append(optionService.getJournalsPrefix()); - data.put("url", url.toString()); - data.put("page", journal.getCreateTime()); + data.put("pageFullPath", url.toString()); + data.put("pageTitle", journal.getCreateTime()); data.put("author", journalComment.getAuthor()); data.put("content", journalComment.getContent()); subject.append("您的博客日志有了新的评论"); } - mailService.sendTemplateMail(user.getEmail(), subject.toString(), data, "common/mail_template/mail_notice.ftl"); + String template = "common/mail_template/mail_notice.ftl"; + + if (themeService.templateExists("mail_template/mail_notice.ftl")) { + template = themeService.renderWithSuffix("mail_template/mail_notice"); + } + + mailService.sendTemplateMail(user.getEmail(), subject.toString(), data, template); } /** @@ -178,8 +187,8 @@ public class CommentEventListener { BasePostMinimalDTO post = postService.convertToMinimal(postService.getById(postComment.getPostId())); - data.put("url", post.getFullPath()); - data.put("page", post.getTitle()); + data.put("pageFullPath", post.getFullPath()); + data.put("pageTitle", post.getTitle()); data.put("baseAuthor", baseComment.getAuthor()); data.put("baseContent", baseComment.getContent()); data.put("replyAuthor", postComment.getAuthor()); @@ -208,8 +217,8 @@ public class CommentEventListener { BasePostMinimalDTO sheet = sheetService.convertToMinimal(sheetService.getById(sheetComment.getPostId())); - data.put("url", sheet.getFullPath()); - data.put("page", sheet.getTitle()); + data.put("pageFullPath", sheet.getFullPath()); + data.put("pageTitle", sheet.getTitle()); data.put("baseAuthor", baseComment.getAuthor()); data.put("baseContent", baseComment.getContent()); data.put("replyAuthor", sheetComment.getAuthor()); @@ -240,8 +249,8 @@ public class CommentEventListener { StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl()) .append("/") .append(optionService.getJournalsPrefix()); - data.put("url", url); - data.put("page", journal.getContent()); + data.put("pageFullPath", url); + data.put("pageTitle", journal.getContent()); data.put("baseAuthor", baseComment.getAuthor()); data.put("baseContent", baseComment.getContent()); data.put("replyAuthor", journalComment.getAuthor()); @@ -253,6 +262,12 @@ public class CommentEventListener { .append("有了新的评论。"); } - mailService.sendTemplateMail(baseAuthorEmail, subject.toString(), data, "common/mail_template/mail_reply.ftl"); + String template = "common/mail_template/mail_reply.ftl"; + + if (themeService.templateExists("mail_template/mail_reply.ftl")) { + template = themeService.renderWithSuffix("mail_template/mail_reply"); + } + + mailService.sendTemplateMail(baseAuthorEmail, subject.toString(), data, template); } } diff --git a/src/main/resources/templates/common/mail_template/mail_notice.ftl b/src/main/resources/templates/common/mail_template/mail_notice.ftl index f3e94cece..9cb647330 100644 --- a/src/main/resources/templates/common/mail_template/mail_notice.ftl +++ b/src/main/resources/templates/common/mail_template/mail_notice.ftl @@ -6,13 +6,13 @@

${user.nickname!}, 您好!

-

有访客在《${page!}》留言:

+

有访客在《${pageTitle!}》留言:


${author!}:${content!}


-

您可以点击查看完整内容

+

您可以点击查看完整内容

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 961c93be3..c7ba2fd9e 100644 --- a/src/main/resources/templates/common/mail_template/mail_reply.ftl +++ b/src/main/resources/templates/common/mail_template/mail_reply.ftl @@ -7,14 +7,14 @@

${baseAuthor!}, 您好!

-

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

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

${baseContent!}

${replyAuthor!} 给您的回复:

${replyContent!}

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

欢迎再度光临 ${blog_title!}