diff --git a/src/main/java/io/mvvm/halo/plugins/email/MailEndpoint.java b/src/main/java/io/mvvm/halo/plugins/email/MailEndpoint.java index 2352629..7cc2391 100644 --- a/src/main/java/io/mvvm/halo/plugins/email/MailEndpoint.java +++ b/src/main/java/io/mvvm/halo/plugins/email/MailEndpoint.java @@ -1,13 +1,11 @@ package io.mvvm.halo.plugins.email; -import io.mvvm.halo.plugins.email.support.MailServerConfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.ServerResponse; import reactor.core.publisher.Mono; -import run.halo.app.extension.ConfigMap; -import run.halo.app.infra.utils.JsonUtils; +import reactor.core.scheduler.Schedulers; import static org.springframework.web.reactive.function.server.RequestPredicates.GET; import static org.springframework.web.reactive.function.server.RouterFunctions.route; @@ -24,16 +22,6 @@ public class MailEndpoint { public MailEndpoint(MailService mailService) { this.mailService = mailService; - - // 启动时加载一次配置 - new Thread(() -> { - while (true) { - if (null != MailBeanContext.client) { - break; - } - } - testConnection().subscribe(); - }, "Mail-test-connection").start(); } @Bean @@ -43,13 +31,10 @@ public RouterFunction testConnectionRouter() { } Mono testConnection() { - return MailBeanContext.client.get(ConfigMap.class, MailServerConfig.NAME) - .map(ConfigMap::getData) - .map(config -> { - String basic = config.get(MailServerConfig.GROUP); - return JsonUtils.jsonToObject(basic, MailServerConfig.class); - }) - .flatMap(config -> Mono.just(mailService.connection(config))); + return MailBeanContext.environmentFetcher + .fetchMailServer() + .publishOn(Schedulers.boundedElastic()) + .map(mailService::connection); } } diff --git a/src/main/java/io/mvvm/halo/plugins/email/MailWatcher.java b/src/main/java/io/mvvm/halo/plugins/email/MailWatcher.java index 5583c17..3cb2f49 100644 --- a/src/main/java/io/mvvm/halo/plugins/email/MailWatcher.java +++ b/src/main/java/io/mvvm/halo/plugins/email/MailWatcher.java @@ -33,12 +33,17 @@ public void onAdd(Extension extension) { @Override public void onUpdate(Extension oldExtension, Extension newExtension) { Watcher.super.onUpdate(oldExtension, newExtension); - if (oldExtension instanceof Comment comment && newExtension instanceof Comment newComment) { - commentSender.pipeline(comment, newComment).subscribe(); - } - if (oldExtension instanceof Reply reply && newExtension instanceof Reply newReply) { - commentSender.pipeline(reply, newReply).subscribe(); - } + + // TODO 目前无法在此处感知操作是否是审核通过 +// if ("Comment".equals(oldExtension.getKind())) { +// Comment oldComment = JsonUtils.jsonToObject(JsonUtils.objectToJson(oldExtension), Comment.class); +// Comment newComment = JsonUtils.jsonToObject(JsonUtils.objectToJson(newExtension), Comment.class); +// commentSender.pipeline(oldComment, newComment).subscribe(); +// } else if ("Reply".equals(oldExtension.getKind())) { +// Reply oldReply = JsonUtils.jsonToObject(JsonUtils.objectToJson(oldExtension), Reply.class); +// Reply newReply = JsonUtils.jsonToObject(JsonUtils.objectToJson(newExtension), Reply.class); +// commentSender.pipeline(oldReply, newReply).subscribe(); +// } } @Override diff --git a/src/main/java/io/mvvm/halo/plugins/email/comment/CommentSender.java b/src/main/java/io/mvvm/halo/plugins/email/comment/CommentSender.java index 44be774..391b93b 100644 --- a/src/main/java/io/mvvm/halo/plugins/email/comment/CommentSender.java +++ b/src/main/java/io/mvvm/halo/plugins/email/comment/CommentSender.java @@ -3,6 +3,7 @@ import io.mvvm.halo.plugins.email.MailPublisher; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Mono; +import reactor.core.scheduler.Schedulers; import run.halo.app.core.extension.content.Comment; import run.halo.app.core.extension.content.Reply; @@ -39,6 +40,7 @@ public Mono pipeline(Comment comment) { .flatMap(pipelines.auditMailSender::loader) // 文章作者通知 .flatMap(pipelines.commentPostOwnerMailSender::loader) + .subscribeOn(Schedulers.boundedElastic()) .then(); } @@ -67,6 +69,7 @@ public Mono pipeline(Reply reply) { .flatMap(pipelines.commentPostOwnerMailSender::loader) // 通知被回复的评论人 .flatMap(pipelines.replyTargetCommentUserMailSender::loader) + .subscribeOn(Schedulers.boundedElastic()) .then(); } @@ -78,7 +81,10 @@ public Mono pipeline(Reply reply) { */ public Mono pipeline(Comment comment, Comment newComment) { // 修改前状态是未审核,修改后状态是已审核才是审核通过 - if (!comment.getSpec().getApproved() && !newComment.getSpec().getApproved()) { + if (!comment.getSpec().getApproved() + && newComment.getSpec().getApproved() + && null == comment.getSpec().getApprovedTime() + && null != newComment.getSpec().getApprovedTime()) { return Mono.just(ReplyCommentContext.builder().comment(newComment).build()) // 加载评论设置 .flatMap(pipelines.commentSetting::loader) @@ -94,6 +100,7 @@ public Mono pipeline(Comment comment, Comment newComment) { .flatMap(pipelines.commentUserAuditSuccessMailSender::loader) // 文章作者通知 .flatMap(pipelines.commentPostOwnerMailSender::loader) + .subscribeOn(Schedulers.boundedElastic()) .then(); } return Mono.empty(); @@ -106,7 +113,10 @@ public Mono pipeline(Comment comment, Comment newComment) { * @param newReply 修改后的回复 */ public Mono pipeline(Reply reply, Reply newReply) { - if (!reply.getSpec().getApproved() && !newReply.getSpec().getApproved()) { + if (!reply.getSpec().getApproved() + && newReply.getSpec().getApproved() + && null == reply.getSpec().getApprovedTime() + && null != newReply.getSpec().getApprovedTime()) { return Mono.just(ReplyCommentContext.builder().replyComment(newReply).build()) // 加载邮件服务配置 .flatMap(pipelines.mailServerConfig::loader) @@ -128,6 +138,7 @@ public Mono pipeline(Reply reply, Reply newReply) { .flatMap(pipelines.commentPostOwnerMailSender::loader) // 通知被回复的评论人 .flatMap(pipelines.replyTargetCommentUserMailSender::loader) + .subscribeOn(Schedulers.boundedElastic()) .then(); } return Mono.empty(); diff --git a/src/main/java/io/mvvm/halo/plugins/email/support/MailServerConfig.java b/src/main/java/io/mvvm/halo/plugins/email/support/MailServerConfig.java index 1680e37..be9e2f8 100644 --- a/src/main/java/io/mvvm/halo/plugins/email/support/MailServerConfig.java +++ b/src/main/java/io/mvvm/halo/plugins/email/support/MailServerConfig.java @@ -1,8 +1,6 @@ package io.mvvm.halo.plugins.email.support; import lombok.Data; -import lombok.EqualsAndHashCode; -import org.springframework.boot.autoconfigure.mail.MailProperties; /** * MailConfig. @@ -10,17 +8,41 @@ * @author: pan **/ @Data -@EqualsAndHashCode(callSuper = true) -public class MailServerConfig extends MailProperties { +public class MailServerConfig { public static final String NAME = "mail-settings"; public static final String GROUP = "basic"; - - private boolean enable; + + /** + * SMTP server host. For instance, 'smtp.example.com'. + */ + private String host; + + /** + * SMTP server port. + */ + private Integer port; + + /** + * Login user of the SMTP server. + */ + private String username; + + /** + * Login password of the SMTP server. + */ + private String password; + + /** + * Protocol used by the SMTP server. + */ + private String protocol = "smtp"; + + private boolean enable = true; private String adminMail; - + private String fromName; - private boolean enableTls; + private boolean enableTls = false; } diff --git a/src/main/resources/extensions/settings.yaml b/src/main/resources/extensions/settings.yaml index 8bb99cd..091fd8e 100644 --- a/src/main/resources/extensions/settings.yaml +++ b/src/main/resources/extensions/settings.yaml @@ -11,42 +11,44 @@ spec: help: "测试连接打开地址: http://ip:port/apis/io.mvvm.halo.plugins.email/testConnection" label: 启用邮件通知 name: enable - value: false + value: true - $formkit: text label: 管理员邮箱 name: adminMail validation: required + help: "有评论需要审核时会通知此邮箱" - $formkit: text label: FromName name: fromName validation: required + help: "发送邮箱时展示的名称" - $formkit: text - help: smtp.qq.com. + help: "邮件服务器地址。如QQ邮箱:smtp.qq.com." label: Host name: host validation: required - $formkit: text - help: 465. + help: "邮件服务器端口。如TSL:465." label: Port name: port validation: required - $formkit: text - help: username. + help: "邮件服务器的账号。如:123@qq.com." label: Username name: username validation: required - $formkit: text - help: password. + help: " 邮件服务器的密码。非邮件的登陆密码." label: Password name: password validation: required - $formkit: text - help: smtps, smtp. + help: "邮件服务器的协议。如:smtps, smtp." label: Protocol name: protocol validation: required - $formkit: checkbox - help: enableTls. - label: Tls + help: "是否开启TLS." + label: TLS name: enableTls value: false \ No newline at end of file