From 0a3717e3940ae07dae1d9c83adf13b0870566bc9 Mon Sep 17 00:00:00 2001 From: ruibaby Date: Tue, 7 May 2019 00:01:15 +0800 Subject: [PATCH] Complate CommentEventListener. --- .../event/comment/CommentEventListener.java | 145 ++++++++++++++---- .../run/halo/app/model/params/PostParam.java | 2 + .../run/halo/app/service/OptionService.java | 7 + .../app/service/impl/OptionServiceImpl.java | 5 + .../common/mail_template/mail_notice.ftl | 2 +- .../common/mail_template/mail_reply.ftl | 18 +-- 6 files changed, 135 insertions(+), 44 deletions(-) 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 a1e9c75cf..6f95c2de9 100644 --- a/src/main/java/run/halo/app/event/comment/CommentEventListener.java +++ b/src/main/java/run/halo/app/event/comment/CommentEventListener.java @@ -6,10 +6,7 @@ import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import run.halo.app.exception.ServiceException; -import run.halo.app.model.entity.PostComment; -import run.halo.app.model.entity.Post; -import run.halo.app.model.entity.User; -import run.halo.app.model.properties.BlogProperties; +import run.halo.app.model.entity.*; import run.halo.app.model.properties.CommentProperties; import run.halo.app.service.*; @@ -32,19 +29,27 @@ public class CommentEventListener { private final PostCommentService postCommentService; + private final SheetCommentService sheetCommentService; + + private final JournalCommentService journalCommentService; + private final PostService postService; + private final SheetService sheetService; + + private final JournalService journalService; + private final UserService userService; - public CommentEventListener(MailService mailService, - OptionService optionService, - PostCommentService postCommentService, - PostService postService, - UserService userService) { + public CommentEventListener(MailService mailService, OptionService optionService, PostCommentService postCommentService, SheetCommentService sheetCommentService, JournalCommentService journalCommentService, PostService postService, SheetService sheetService, JournalService journalService, UserService userService) { this.mailService = mailService; this.optionService = optionService; this.postCommentService = postCommentService; + this.sheetCommentService = sheetCommentService; + this.journalCommentService = journalCommentService; this.postService = postService; + this.sheetService = sheetService; + this.journalService = journalService; this.userService = userService; } @@ -60,37 +65,54 @@ public class CommentEventListener { User user = userService.getCurrentUser().orElseThrow(() -> new ServiceException("Can not find blog owner")); - // Get postComment id - PostComment postComment = postCommentService.getById(newEvent.getCommentId()); - - Post post = postService.getById(postComment.getPostId()); Map data = new HashMap<>(); - StrBuilder url = new StrBuilder(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL).toString()) - .append("/archives/") - .append(post.getUrl()); - data.put("url", url.toString()); - data.put("page", post.getTitle()); - data.put("author", postComment.getAuthor()); - data.put("content", postComment.getContent()); + if (this instanceof PostService) { + // Get postComment id + PostComment postComment = postCommentService.getById(newEvent.getCommentId()); + + Post post = postService.getById(postComment.getPostId()); + + StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl()) + .append("/archives/") + .append(post.getUrl()); + data.put("url", url.toString()); + data.put("page", post.getTitle()); + data.put("author", postComment.getAuthor()); + data.put("content", postComment.getContent()); + } else if (this instanceof SheetService) { + SheetComment sheetComment = sheetCommentService.getById(newEvent.getCommentId()); + + Sheet sheet = sheetService.getById(sheetComment.getPostId()); + + StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl()) + .append("/s/") + .append(sheet.getUrl()); + data.put("url", url.toString()); + data.put("page", sheet.getTitle()); + data.put("author", sheetComment.getAuthor()); + data.put("content", sheetComment.getContent()); + } else if (this instanceof JournalService) { + JournalComment journalComment = journalCommentService.getById(newEvent.getCommentId()); + + Journal journal = journalService.getById(journalComment.getPostId()); + + StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl()) + .append("/journals"); + data.put("url", url.toString()); + data.put("page", journal.getCreateTime()); + data.put("author", journalComment.getAuthor()); + data.put("content", journalComment.getContent()); + } + mailService.sendTemplateMail(user.getEmail(), "您的博客有新的评论", data, "common/mail_template/mail_notice.ftl"); } @Async @EventListener public void handleCommentPassEvent(CommentPassEvent passEvent) { - Boolean passCommentNotice = optionService.getByPropertyOrDefault(CommentProperties.PASS_NOTICE, Boolean.class, false); - if (!passCommentNotice) { - // Skip mailing - return; - } - - // Get postComment id - PostComment postComment = postCommentService.getById(passEvent.getCommentId()); - - // TODO Complete mail sending } @Async @@ -103,10 +125,65 @@ public class CommentEventListener { return; } - // Get postComment id - PostComment postComment = postCommentService.getById(replyEvent.getCommentId()); + User user = userService.getCurrentUser().orElseThrow(() -> new ServiceException("Can not find blog owner")); - // TODO Complete mail sending + String blogTitle = optionService.getBlogTitle(); + + Map data = new HashMap<>(); + + if (this instanceof PostService) { + + PostComment postComment = postCommentService.getById(replyEvent.getCommentId()); + + PostComment baseComment = postCommentService.getById(postComment.getParentId()); + + Post post = postService.getById(postComment.getPostId()); + + StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl()) + .append("/archives/") + .append(post.getUrl()); + + data.put("url", url); + data.put("page", post.getTitle()); + data.put("baseAuthor", baseComment.getAuthor()); + data.put("baseContent", baseComment.getContent()); + data.put("replyAuthor", postComment.getAuthor()); + data.put("replyContent", postComment.getContent()); + } else if (this instanceof SheetService) { + + SheetComment sheetComment = sheetCommentService.getById(replyEvent.getCommentId()); + + SheetComment baseComment = sheetCommentService.getById(sheetComment.getParentId()); + + Sheet sheet = sheetService.getById(sheetComment.getPostId()); + + StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl()) + .append("/s/") + .append(sheet.getUrl()); + + data.put("url", url); + data.put("page", sheet.getTitle()); + data.put("baseAuthor", baseComment.getAuthor()); + data.put("baseContent", baseComment.getContent()); + data.put("replyAuthor", sheetComment.getAuthor()); + data.put("replyContent", sheetComment.getContent()); + } else if (this instanceof JournalService) { + JournalComment journalComment = journalCommentService.getById(replyEvent.getCommentId()); + + JournalComment baseComment = journalCommentService.getById(journalComment.getParentId()); + + Journal journal = journalService.getById(journalComment.getPostId()); + + StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl()) + .append("/journals"); + data.put("url", url); + data.put("page", journal.getContent()); + data.put("baseAuthor", baseComment.getAuthor()); + data.put("baseContent", baseComment.getContent()); + data.put("replyAuthor", journalComment.getAuthor()); + data.put("replyContent", journalComment.getContent()); + } + + mailService.sendTemplateMail(user.getEmail(), "您在【" + blogTitle + "】的评论有新回复", data, "common/mail_template/mail_reply.ftl"); } - } diff --git a/src/main/java/run/halo/app/model/params/PostParam.java b/src/main/java/run/halo/app/model/params/PostParam.java index 87c7df7e1..13563ae12 100644 --- a/src/main/java/run/halo/app/model/params/PostParam.java +++ b/src/main/java/run/halo/app/model/params/PostParam.java @@ -34,6 +34,8 @@ public class PostParam implements InputConverter { @NotBlank(message = "Original content must not be blank") private String originalContent; + private String summary; + @Size(max = 255, message = "Length of thumbnail must not be more than {max}") private String thumbnail; diff --git a/src/main/java/run/halo/app/service/OptionService.java b/src/main/java/run/halo/app/service/OptionService.java index 4653de673..6a281dda6 100755 --- a/src/main/java/run/halo/app/service/OptionService.java +++ b/src/main/java/run/halo/app/service/OptionService.java @@ -288,6 +288,13 @@ public interface OptionService extends CrudService { @NonNull String getBlogBaseUrl(); + /** + * Gets blog title. + * @return blog title. + */ + @NonNull + String getBlogTitle(); + /** * Gets blog birthday. * diff --git a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java index 35fca70f0..5533c2c31 100644 --- a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java @@ -379,6 +379,11 @@ public class OptionServiceImpl extends AbstractCrudService impl return blogUrl; } + @Override + public String getBlogTitle() { + return getByProperty(BlogProperties.BLOG_TITLE).orElse("").toString(); + } + @Override public long getBirthday() { return getByProperty(PrimaryProperties.BIRTHDAY, Long.class).orElseGet(() -> { 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 cc7c9a32d..13ee1adbc 100644 --- a/src/main/resources/templates/common/mail_template/mail_notice.ftl +++ b/src/main/resources/templates/common/mail_template/mail_notice.ftl @@ -9,7 +9,7 @@

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


- ${content!}:${commentContent!} + ${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 e469c7592..bf8653987 100644 --- a/src/main/resources/templates/common/mail_template/mail_reply.ftl +++ b/src/main/resources/templates/common/mail_template/mail_reply.ftl @@ -2,28 +2,28 @@

- 您在${blogTitle}上的留言有回复啦! + 您在 ${options.blog_title!} 上的留言有回复啦!

-

${commentAuthor}, 您好!

-

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

${baseAuthor}, 您好!

+

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

${commentContent}

+

${baseContent}

${replyAuthor} 给你的回复:
-

${replyContent}

+

${replyContent}

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

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

-

(此邮件由系统自动发出, 请勿回复。)

+

(此邮件由系统自动发出, 请勿回复。如有打扰,请见谅。)

邮件发自: - ${blogTitle} + ${options.blog_title!}