Send email to admin when have a new comment.

pull/146/head
ruibaby 2019-04-23 23:31:29 +08:00
parent bfc19c9cf3
commit 6b663cce6a
5 changed files with 59 additions and 16 deletions

View File

@ -1,14 +1,21 @@
package run.halo.app.event.comment;
import cn.hutool.core.text.StrBuilder;
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.exception.ServiceException;
import run.halo.app.model.entity.Comment;
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.properties.CommentProperties;
import run.halo.app.service.CommentService;
import run.halo.app.service.MailService;
import run.halo.app.service.OptionService;
import run.halo.app.service.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* Comment event listener.
@ -26,12 +33,20 @@ public class CommentEventListener {
private final CommentService commentService;
private final PostService postService;
private final UserService userService;
public CommentEventListener(MailService mailService,
OptionService optionService,
CommentService commentService) {
CommentService commentService,
PostService postService,
UserService userService) {
this.mailService = mailService;
this.optionService = optionService;
this.commentService = commentService;
this.postService = postService;
this.userService = userService;
}
@Async
@ -44,10 +59,23 @@ public class CommentEventListener {
return;
}
User user = userService.getCurrentUser().orElseThrow(() -> new ServiceException("Can not find blog owner"));
// Get comment id
Comment comment = commentService.getById(newEvent.getCommentId());
// TODO Complete mail sending
Post post = postService.getById(comment.getPostId());
Map<String, Object> data = new HashMap<>();
StrBuilder url = new StrBuilder(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL))
.append("/archives/")
.append(post.getUrl());
data.put("url", url.toString());
data.put("page", post.getTitle());
data.put("author", comment.getAuthor());
data.put("content",comment.getContent());
mailService.sendTemplateMail(user.getEmail(), "您的博客有新的评论", data, "common/mail_template/mail_notice.ftl");
}
@Async

View File

@ -0,0 +1,20 @@
package run.halo.app.web.controller.admin;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
/**
* Admin page.
*
* @author : RYAN0UP
* @date : 2019-04-23
*/
@Controller
public class MainController {
@GetMapping("/admin")
public String admin() {
return "redirect:/admin/index.html";
}
}

View File

@ -33,7 +33,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
*/
@Slf4j
@Controller
@RequestMapping(value = "archives")
@RequestMapping(value = "/archives")
public class ContentArchiveController {
private final PostService postService;

View File

@ -83,9 +83,4 @@ public class ContentIndexController {
model.addAttribute("rainbow", rainbow);
return themeService.render("index");
}
@GetMapping("/admin")
public String admin() {
return "redirect:/admin/index.html";
}
}

View File

@ -5,14 +5,14 @@
您的博客有新的评论!
</h1>
<div class="emailtext" style="background:#fff;padding:20px 32px 40px;">
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">${author}, 您好!</p>
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">有访客在《${pageName}》留言:</p>
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">${user.nickName!}, 您好!</p>
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">有访客在《${page!}》留言:</p>
<br />
<p style="color: #6e6e6e;font-size:13px;line-height:24px;padding:10px 20px;background:#f8f8f8;margin:0px">
${visitor}${commentContent}
<p style="color: #6e6e6e;font-size:13px;line-height:24px;padding:10px 20px;background:#f8f8f8;margin:0">
${content!}${commentContent!}
</p>
<br />
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">你可以点击<a href="${pageUrl}">查看完整内容</a></p>
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">你可以点击<a href="${url!}">查看完整内容</a></p>
</div>
</div>
</div>