Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
fix: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
pannanxu committed Dec 19, 2022
1 parent 48d5c93 commit c48524b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 44 deletions.
25 changes: 5 additions & 20 deletions src/main/java/io/mvvm/halo/plugins/email/MailEndpoint.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -43,13 +31,10 @@ public RouterFunction<ServerResponse> testConnectionRouter() {
}

Mono<Boolean> 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);
}

}
17 changes: 11 additions & 6 deletions src/main/java/io/mvvm/halo/plugins/email/MailWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -39,6 +40,7 @@ public Mono<Void> pipeline(Comment comment) {
.flatMap(pipelines.auditMailSender::loader)
// 文章作者通知
.flatMap(pipelines.commentPostOwnerMailSender::loader)
.subscribeOn(Schedulers.boundedElastic())
.then();
}

Expand Down Expand Up @@ -67,6 +69,7 @@ public Mono<Void> pipeline(Reply reply) {
.flatMap(pipelines.commentPostOwnerMailSender::loader)
// 通知被回复的评论人
.flatMap(pipelines.replyTargetCommentUserMailSender::loader)
.subscribeOn(Schedulers.boundedElastic())
.then();
}

Expand All @@ -78,7 +81,10 @@ public Mono<Void> pipeline(Reply reply) {
*/
public Mono<Void> 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)
Expand All @@ -94,6 +100,7 @@ public Mono<Void> pipeline(Comment comment, Comment newComment) {
.flatMap(pipelines.commentUserAuditSuccessMailSender::loader)
// 文章作者通知
.flatMap(pipelines.commentPostOwnerMailSender::loader)
.subscribeOn(Schedulers.boundedElastic())
.then();
}
return Mono.empty();
Expand All @@ -106,7 +113,10 @@ public Mono<Void> pipeline(Comment comment, Comment newComment) {
* @param newReply 修改后的回复
*/
public Mono<Void> 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)
Expand All @@ -128,6 +138,7 @@ public Mono<Void> pipeline(Reply reply, Reply newReply) {
.flatMap(pipelines.commentPostOwnerMailSender::loader)
// 通知被回复的评论人
.flatMap(pipelines.replyTargetCommentUserMailSender::loader)
.subscribeOn(Schedulers.boundedElastic())
.then();
}
return Mono.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
package io.mvvm.halo.plugins.email.support;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.boot.autoconfigure.mail.MailProperties;

/**
* MailConfig.
*
* @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;
}
18 changes: 10 additions & 8 deletions src/main/resources/extensions/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: "邮件服务器的账号。如:[email protected]."
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

0 comments on commit c48524b

Please sign in to comment.