From b2d7221316738fa4bfb928e25387a99fa21ef454 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Sun, 8 Oct 2023 17:30:21 +0800 Subject: [PATCH] fix: compatible email are empty when comment notification triggered (#4685) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind bug /area core /milestone 2.10.x #### What this PR does / why we need it: 修复当评论或回复者的邮箱为空时通知报错的问题 #### Which issue(s) this PR fixes: Fixes #4684 #### Does this PR introduce a user-facing change? ```release-note 修复当评论或回复者的邮箱为空时通知报错的问题 ``` --- .../comment/ReplyNotificationSubscriptionHelper.java | 10 ++++++++++ .../java/run/halo/app/notification/EmailNotifier.java | 11 +++++++++++ .../src/main/resources/extensions/notification.yaml | 9 +++++++++ 3 files changed, 30 insertions(+) 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