diff --git a/application/src/main/java/run/halo/app/content/comment/ReplyNotificationSubscriptionHelper.java b/application/src/main/java/run/halo/app/content/comment/ReplyNotificationSubscriptionHelper.java index 0a086330e8..38879160b7 100644 --- a/application/src/main/java/run/halo/app/content/comment/ReplyNotificationSubscriptionHelper.java +++ b/application/src/main/java/run/halo/app/content/comment/ReplyNotificationSubscriptionHelper.java @@ -1,6 +1,8 @@ package run.halo.app.content.comment; +import io.micrometer.common.util.StringUtils; import lombok.RequiredArgsConstructor; +import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; import run.halo.app.content.NotificationReasonConst; import run.halo.app.core.extension.content.Comment; @@ -56,13 +58,21 @@ public void subscribeNewReplyReasonForReply(Reply reply) { void subscribeReply(Subscription.ReasonSubject reasonSubject, Identity identity) { var subscriber = createSubscriber(identity); + if (subscriber == null) { + return; + } var interestReason = new Subscription.InterestReason(); interestReason.setReasonType(NotificationReasonConst.SOMEONE_REPLIED_TO_YOU); interestReason.setSubject(reasonSubject); notificationCenter.subscribe(subscriber, interestReason).block(); } + @Nullable private Subscription.Subscriber createSubscriber(Identity author) { + if (StringUtils.isBlank(author.name())) { + return null; + } + Subscription.Subscriber subscriber; if (author.isEmail()) { subscriber = subscriberEmailResolver.ofEmail(author.name()); diff --git a/application/src/main/java/run/halo/app/notification/EmailNotifier.java b/application/src/main/java/run/halo/app/notification/EmailNotifier.java index 50139df413..1a10e1d3c6 100644 --- a/application/src/main/java/run/halo/app/notification/EmailNotifier.java +++ b/application/src/main/java/run/halo/app/notification/EmailNotifier.java @@ -43,6 +43,11 @@ public Mono notify(NotificationContext context) { var emailSenderConfig = JsonUtils.DEFAULT_JSON_MAPPER.convertValue(senderConfig, EmailSenderConfig.class); + if (!emailSenderConfig.isEnable()) { + log.debug("Email notifier is disabled, skip sending email."); + return Mono.empty(); + } + JavaMailSenderImpl javaMailSender = getJavaMailSender(emailSenderConfig); String recipient = context.getMessage().getRecipient(); @@ -51,6 +56,11 @@ public Mono notify(NotificationContext context) { var payload = context.getMessage().getPayload(); return subscriberEmailResolver.resolve(subscriber) .flatMap(toEmail -> { + if (StringUtils.isBlank(toEmail)) { + log.debug("Cannot resolve email for subscriber: [{}], skip sending email.", + subscriber); + return Mono.empty(); + } var htmlMono = appendHtmlBodyFooter(payload.getAttributes()) .doOnNext(footer -> { if (StringUtils.isNotBlank(payload.getHtmlBody())) { @@ -138,6 +148,7 @@ Mono appendHtmlBodyFooter(ReasonAttributes attributes) { @Data static class EmailSenderConfig { + private boolean enable; private String displayName; private String username; private String password; diff --git a/application/src/main/resources/extensions/notification.yaml b/application/src/main/resources/extensions/notification.yaml index 67dcb39ed0..70fb1389cc 100644 --- a/application/src/main/resources/extensions/notification.yaml +++ b/application/src/main/resources/extensions/notification.yaml @@ -19,22 +19,31 @@ spec: - group: sender label: 发件设置 formSchema: + - $formkit: checkbox + label: "启用邮件通知器" + value: false + name: enable - $formkit: text + if: "$enable" label: "用户名" name: username validation: required - $formkit: password + if: "$enable" label: "密码" name: password validation: required - $formkit: text + if: "$enable" label: "显示名称" name: displayName - $formkit: text + if: "$enable" label: "SMTP 服务器地址" name: host validation: required - $formkit: text + if: "$enable" label: "端口号" name: port validation: required