From b2af6dcc0aa4a79ffbde9e6b3d1c7555b7138979 Mon Sep 17 00:00:00 2001 From: johnniang Date: Tue, 23 Apr 2019 21:34:04 +0800 Subject: [PATCH] Complete structure of mailing on comment --- .../app/event/comment/CommentBaseEvent.java | 37 ++++++++ .../event/comment/CommentEventListener.java | 85 +++++++++++++++++++ .../app/event/comment/CommentNewEvent.java | 22 +++++ .../app/event/comment/CommentPassEvent.java | 22 +++++ .../app/event/comment/CommentReplyEvent.java | 22 +++++ .../app/service/impl/CommentServiceImpl.java | 30 ++++++- 6 files changed, 215 insertions(+), 3 deletions(-) create mode 100644 src/main/java/run/halo/app/event/comment/CommentBaseEvent.java create mode 100644 src/main/java/run/halo/app/event/comment/CommentEventListener.java create mode 100644 src/main/java/run/halo/app/event/comment/CommentNewEvent.java create mode 100644 src/main/java/run/halo/app/event/comment/CommentPassEvent.java create mode 100644 src/main/java/run/halo/app/event/comment/CommentReplyEvent.java diff --git a/src/main/java/run/halo/app/event/comment/CommentBaseEvent.java b/src/main/java/run/halo/app/event/comment/CommentBaseEvent.java new file mode 100644 index 000000000..bc9ba69dd --- /dev/null +++ b/src/main/java/run/halo/app/event/comment/CommentBaseEvent.java @@ -0,0 +1,37 @@ +package run.halo.app.event.comment; + +import org.springframework.context.ApplicationEvent; +import org.springframework.lang.NonNull; +import org.springframework.util.Assert; + +/** + * Comment new event. + * + * @author johnniang + * @date 19-4-23 + */ +public abstract class CommentBaseEvent extends ApplicationEvent { + + /** + * Comment id. + */ + private final Long commentId; + + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + * @param commentId comment id + */ + public CommentBaseEvent(Object source, @NonNull Long commentId) { + super(source); + + Assert.notNull(commentId, "Comment id must not be null"); + this.commentId = commentId; + } + + @NonNull + public Long getCommentId() { + return commentId; + } +} diff --git a/src/main/java/run/halo/app/event/comment/CommentEventListener.java b/src/main/java/run/halo/app/event/comment/CommentEventListener.java new file mode 100644 index 000000000..7e22287f7 --- /dev/null +++ b/src/main/java/run/halo/app/event/comment/CommentEventListener.java @@ -0,0 +1,85 @@ +package run.halo.app.event.comment; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.event.EventListener; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; +import run.halo.app.model.entity.Comment; +import run.halo.app.model.properties.CommentProperties; +import run.halo.app.service.CommentService; +import run.halo.app.service.MailService; +import run.halo.app.service.OptionService; + +/** + * Comment event listener. + * + * @author johnniang + * @date 19-4-23 + */ +@Slf4j +@Component +public class CommentEventListener { + + private final MailService mailService; + + private final OptionService optionService; + + private final CommentService commentService; + + public CommentEventListener(MailService mailService, + OptionService optionService, + CommentService commentService) { + this.mailService = mailService; + this.optionService = optionService; + this.commentService = commentService; + } + + @Async + @EventListener + public void handleCommentNewEvent(CommentNewEvent newEvent) { + Boolean newCommentNotice = optionService.getByPropertyOrDefault(CommentProperties.NEW_NOTICE, Boolean.class, false); + + if (!newCommentNotice) { + // Skip mailing + return; + } + + // Get comment id + Comment comment = commentService.getById(newEvent.getCommentId()); + + // TODO Complete mail sending + } + + @Async + @EventListener + public void handleCommentPassEvent(CommentPassEvent passEvent) { + Boolean passCommentNotice = optionService.getByPropertyOrDefault(CommentProperties.PASS_NOTICE, Boolean.class, false); + + if (!passCommentNotice) { + // Skip mailing + return; + } + + // Get comment id + Comment comment = commentService.getById(passEvent.getCommentId()); + + // TODO Complete mail sending + } + + @Async + @EventListener + public void handleCommentReplyEvent(CommentReplyEvent replyEvent) { + Boolean replyCommentNotice = optionService.getByPropertyOrDefault(CommentProperties.REPLY_NOTICE, Boolean.class, false); + + if (!replyCommentNotice) { + // Skip mailing + return; + } + + // Get comment id + Comment comment = commentService.getById(replyEvent.getCommentId()); + + // TODO Complete mail sending + } + +} diff --git a/src/main/java/run/halo/app/event/comment/CommentNewEvent.java b/src/main/java/run/halo/app/event/comment/CommentNewEvent.java new file mode 100644 index 000000000..2a2c3e814 --- /dev/null +++ b/src/main/java/run/halo/app/event/comment/CommentNewEvent.java @@ -0,0 +1,22 @@ +package run.halo.app.event.comment; + +import org.springframework.lang.NonNull; + +/** + * Comment new event. + * + * @author johnniang + * @date 19-4-23 + */ +public class CommentNewEvent extends CommentBaseEvent { + + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + * @param commentId comment id + */ + public CommentNewEvent(Object source, @NonNull Long commentId) { + super(source, commentId); + } +} diff --git a/src/main/java/run/halo/app/event/comment/CommentPassEvent.java b/src/main/java/run/halo/app/event/comment/CommentPassEvent.java new file mode 100644 index 000000000..08153eccc --- /dev/null +++ b/src/main/java/run/halo/app/event/comment/CommentPassEvent.java @@ -0,0 +1,22 @@ +package run.halo.app.event.comment; + +import org.springframework.lang.NonNull; + +/** + * Comment pass event. + * + * @author johnniang + * @date 19-4-23 + */ +public class CommentPassEvent extends CommentBaseEvent { + + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + * @param commentId comment id + */ + public CommentPassEvent(Object source, @NonNull Long commentId) { + super(source, commentId); + } +} diff --git a/src/main/java/run/halo/app/event/comment/CommentReplyEvent.java b/src/main/java/run/halo/app/event/comment/CommentReplyEvent.java new file mode 100644 index 000000000..c37078570 --- /dev/null +++ b/src/main/java/run/halo/app/event/comment/CommentReplyEvent.java @@ -0,0 +1,22 @@ +package run.halo.app.event.comment; + +import org.springframework.lang.NonNull; + +/** + * Comment reply event. + * + * @author johnniang + * @date 19-4-23 + */ +public class CommentReplyEvent extends CommentBaseEvent { + + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + * @param commentId comment id + */ + public CommentReplyEvent(Object source, @NonNull Long commentId) { + super(source, commentId); + } +} diff --git a/src/main/java/run/halo/app/service/impl/CommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/CommentServiceImpl.java index b910e0f60..2562db024 100644 --- a/src/main/java/run/halo/app/service/impl/CommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/CommentServiceImpl.java @@ -5,6 +5,7 @@ import cn.hutool.crypto.SecureUtil; import cn.hutool.extra.servlet.ServletUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.domain.*; import org.springframework.data.domain.Sort.Order; import org.springframework.data.jpa.domain.Specification; @@ -15,6 +16,9 @@ import org.springframework.stereotype.Service; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import org.springframework.web.util.HtmlUtils; +import run.halo.app.event.comment.CommentNewEvent; +import run.halo.app.event.comment.CommentPassEvent; +import run.halo.app.event.comment.CommentReplyEvent; import run.halo.app.model.dto.post.PostMinimalOutputDTO; import run.halo.app.model.entity.Comment; import run.halo.app.model.entity.Post; @@ -57,13 +61,17 @@ public class CommentServiceImpl extends AbstractCrudService imple private final OptionService optionService; + private final ApplicationEventPublisher eventPublisher; + public CommentServiceImpl(CommentRepository commentRepository, PostRepository postRepository, - OptionService optionService) { + OptionService optionService, + ApplicationEventPublisher eventPublisher) { super(commentRepository); this.commentRepository = commentRepository; this.postRepository = postRepository; this.optionService = optionService; + this.eventPublisher = eventPublisher; } @Override @@ -91,7 +99,7 @@ public class CommentServiceImpl extends AbstractCrudService imple public Page pageBy(CommentQuery commentQuery, Pageable pageable) { Assert.notNull(commentQuery, "Comment query must not be null"); Assert.notNull(pageable, "Page info must not be null"); - Page commentPage = commentRepository.findAll(buildSpecByQuery(commentQuery), pageable); + Page commentPage = commentRepository.findAll(buildSpecByQuery(commentQuery), pageable); return convertBy(commentPage); } @@ -179,6 +187,15 @@ public class CommentServiceImpl extends AbstractCrudService imple // TODO Handle email sending + if (createdComment.getParentId() == null || createdComment.getParentId() == 0) { + // New comment + eventPublisher.publishEvent(new CommentNewEvent(this, createdComment.getId())); + } else { + // Reply comment + eventPublisher.publishEvent(new CommentReplyEvent(this, createdComment.getId())); + } + + return createdComment; } @@ -287,7 +304,14 @@ public class CommentServiceImpl extends AbstractCrudService imple comment.setStatus(status); // Update comment - return update(comment); + Comment updatedComment = update(comment); + + if (CommentStatus.PUBLISHED.equals(status)) { + // Pass a comment + eventPublisher.publishEvent(new CommentPassEvent(this, commentId)); + } + + return updatedComment; } /**