Complate CommentEventListener.

pull/146/head
ruibaby 2019-05-07 01:47:12 +08:00
parent 0a3717e394
commit 4bd7f8f096
8 changed files with 79 additions and 71 deletions

View File

@ -4,7 +4,10 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import run.halo.app.model.dto.OptionDTO;
import run.halo.app.model.params.MailParam;
import run.halo.app.model.params.OptionParam;
import run.halo.app.model.support.BaseResponse;
import run.halo.app.service.MailService;
import run.halo.app.service.OptionService;
import javax.validation.Valid;
@ -23,8 +26,12 @@ public class OptionController {
private final OptionService optionService;
public OptionController(OptionService optionService) {
private final MailService mailService;
public OptionController(OptionService optionService,
MailService mailService) {
this.optionService = optionService;
this.mailService = mailService;
}
@GetMapping
@ -59,4 +66,10 @@ public class OptionController {
public void saveOptionsWithMapView(@RequestBody Map<String, String> optionMap) {
optionService.save(optionMap);
}
@PostMapping("test_mail")
public BaseResponse testMail(@Valid @RequestBody MailParam mailParam){
mailService.sendMail(mailParam.getTo(),mailParam.getSubject(),mailParam.getContent());
return BaseResponse.ok("发送成功");
}
}

View File

@ -1,6 +1,8 @@
package run.halo.app.event.comment;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
@ -68,7 +70,7 @@ public class CommentEventListener {
Map<String, Object> data = new HashMap<>();
if (this instanceof PostService) {
if (newEvent.getSource() instanceof PostService) {
// Get postComment id
PostComment postComment = postCommentService.getById(newEvent.getCommentId());
@ -81,7 +83,7 @@ public class CommentEventListener {
data.put("page", post.getTitle());
data.put("author", postComment.getAuthor());
data.put("content", postComment.getContent());
} else if (this instanceof SheetService) {
} else if (newEvent.getSource() instanceof SheetService) {
SheetComment sheetComment = sheetCommentService.getById(newEvent.getCommentId());
Sheet sheet = sheetService.getById(sheetComment.getPostId());
@ -93,7 +95,7 @@ public class CommentEventListener {
data.put("page", sheet.getTitle());
data.put("author", sheetComment.getAuthor());
data.put("content", sheetComment.getContent());
} else if (this instanceof JournalService) {
} else if (newEvent.getSource() instanceof JournalService) {
JournalComment journalComment = journalCommentService.getById(newEvent.getCommentId());
Journal journal = journalService.getById(journalComment.getPostId());
@ -125,18 +127,26 @@ public class CommentEventListener {
return;
}
User user = userService.getCurrentUser().orElseThrow(() -> new ServiceException("Can not find blog owner"));
String baseAuthorEmail = "";
String blogTitle = optionService.getBlogTitle();
Map<String, Object> data = new HashMap<>();
if (this instanceof PostService) {
log.debug("replyEvent.getSource():"+replyEvent.getSource().toString());
if (replyEvent.getSource() instanceof PostCommentService) {
PostComment postComment = postCommentService.getById(replyEvent.getCommentId());
PostComment baseComment = postCommentService.getById(postComment.getParentId());
if (StrUtil.isEmpty(baseComment.getEmail()) && !Validator.isEmail(baseComment.getEmail())) {
return;
}
baseAuthorEmail = baseComment.getEmail();
Post post = postService.getById(postComment.getPostId());
StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl())
@ -149,12 +159,18 @@ public class CommentEventListener {
data.put("baseContent", baseComment.getContent());
data.put("replyAuthor", postComment.getAuthor());
data.put("replyContent", postComment.getContent());
} else if (this instanceof SheetService) {
} else if (replyEvent.getSource() instanceof SheetCommentService) {
SheetComment sheetComment = sheetCommentService.getById(replyEvent.getCommentId());
SheetComment baseComment = sheetCommentService.getById(sheetComment.getParentId());
if (StrUtil.isEmpty(baseComment.getEmail()) && !Validator.isEmail(baseComment.getEmail())) {
return;
}
baseAuthorEmail = baseComment.getEmail();
Sheet sheet = sheetService.getById(sheetComment.getPostId());
StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl())
@ -167,11 +183,17 @@ public class CommentEventListener {
data.put("baseContent", baseComment.getContent());
data.put("replyAuthor", sheetComment.getAuthor());
data.put("replyContent", sheetComment.getContent());
} else if (this instanceof JournalService) {
} else if (replyEvent.getSource() instanceof JournalCommentService) {
JournalComment journalComment = journalCommentService.getById(replyEvent.getCommentId());
JournalComment baseComment = journalCommentService.getById(journalComment.getParentId());
if (StrUtil.isEmpty(baseComment.getEmail()) && !Validator.isEmail(baseComment.getEmail())) {
return;
}
baseAuthorEmail = baseComment.getEmail();
Journal journal = journalService.getById(journalComment.getPostId());
StrBuilder url = new StrBuilder(optionService.getBlogBaseUrl())
@ -184,6 +206,6 @@ public class CommentEventListener {
data.put("replyContent", journalComment.getContent());
}
mailService.sendTemplateMail(user.getEmail(), "您在【" + blogTitle + "】的评论有新回复", data, "common/mail_template/mail_reply.ftl");
mailService.sendTemplateMail(baseAuthorEmail, "您在【" + blogTitle + "】的评论有新回复", data, "common/mail_template/mail_reply.ftl");
}
}

View File

@ -1,27 +0,0 @@
package run.halo.app.model.freemarker.method;
import freemarker.template.Configuration;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author ryanwang
* @date : 2018/12/31
*/
@Component
@Deprecated
public class RecentCommentsMethod implements TemplateMethodModelEx {
public RecentCommentsMethod(Configuration configuration) {
configuration.setSharedVariable("recentCommentsMethod", this);
}
@Override
public Object exec(List arguments) throws TemplateModelException {
// TODO Complete recent comments method.
return null;
}
}

View File

@ -1,27 +0,0 @@
package run.halo.app.model.freemarker.method;
import freemarker.template.Configuration;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author ryanwang
* @date : 2018/12/31
*/
@Component
@Deprecated
public class RecentPostsMethod implements TemplateMethodModelEx {
public RecentPostsMethod(Configuration configuration) {
configuration.setSharedVariable("recentPostsMethod", this);
}
@Override
public Object exec(List arguments) throws TemplateModelException {
// TODO Complete recent post method.
return null;
}
}

View File

@ -0,0 +1,24 @@
package run.halo.app.model.params;
import lombok.Data;
import javax.validation.constraints.NotBlank;
/**
* Journal query params.
*
* @author ryanwang
* @date : 2019/05/07
*/
@Data
public class MailParam {
@NotBlank(message = "收件人不能为空")
private String to;
@NotBlank(message = "主题不能为空")
private String subject;
@NotBlank(message = "内容不能为空")
private String content;
}

View File

@ -10,9 +10,9 @@ public enum EmailProperties implements PropertyEnum {
HOST("email_host", String.class, ""),
PROTOCOL("email_protocol", String.class, ""),
PROTOCOL("email_protocol", String.class, "SSL"),
SSL_PORT("email_ssl_port", Integer.class, ""),
SSL_PORT("email_ssl_port", Integer.class, "465"),
USERNAME("email_username", String.class, ""),

View File

@ -1,6 +1,7 @@
package run.halo.app.service.impl;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import freemarker.template.Template;
import io.github.biezhi.ome.OhMyEmail;
import lombok.extern.slf4j.Slf4j;
@ -150,6 +151,8 @@ public class MailServiceImpl implements MailService {
Properties defaultProperties = OhMyEmail.defaultConfig(log.isDebugEnabled());
// Set smtp host
defaultProperties.setProperty("mail.smtp.host", optionService.getByPropertyOfNonNull(EmailProperties.HOST).toString());
defaultProperties.setProperty("mail.smtp.ssl.enable", StrUtil.isEmpty(optionService.getByPropertyOfNonNull(EmailProperties.PROTOCOL).toString()) ? "false" : "true");
defaultProperties.setProperty("mail.smtp.port", StrUtil.isEmpty(optionService.getByPropertyOfNonNull(EmailProperties.SSL_PORT).toString()) ? "25" : "465");
// Config email
OhMyEmail.config(defaultProperties,
optionService.getByPropertyOfNonNull(EmailProperties.USERNAME).toString(),

View File

@ -6,15 +6,15 @@
</h1>
<div class="emailtext" style="background:#fff;padding:20px 32px 40px;">
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">${baseAuthor}, 您好!</p>
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">您在《${page}》的留言:
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">${baseAuthor!}, 您好!</p>
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">您在《${page!}》的留言:
<br />
<p style="color: #6e6e6e;font-size:13px;line-height:24px;padding:10px 20px;background:#f8f8f8;margin:0">${baseContent}</p>
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">${replyAuthor} 给你的回复:
<p style="color: #6e6e6e;font-size:13px;line-height:24px;padding:10px 20px;background:#f8f8f8;margin:0">${baseContent!}</p>
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">${replyAuthor!} 给你的回复:
<br />
<p style="color: #6e6e6e;font-size:13px;line-height:24px;padding:10px 20px;background:#f8f8f8;margin:0">${replyContent}</p>
<p style="color: #6e6e6e;font-size:13px;line-height:24px;padding:10px 20px;background:#f8f8f8;margin:0">${replyContent!}</p>
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">你可以点击
<a href="${url}">查看完整内容</a>
<a href="${url!}">查看完整内容</a>
</p>
<p style="color: #6e6e6e;font-size:13px;line-height:24px;">欢迎再度光临
<a href="${options.blog_url!}">${options.blog_title!}</a>