Complete structure of mailing on comment

pull/146/head
johnniang 2019-04-23 21:34:04 +08:00
parent 3a15fb307c
commit b2af6dcc0a
6 changed files with 215 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -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
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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<Comment, Long> 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
@ -179,6 +187,15 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> 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<Comment, Long> 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;
}
/**