fix: compatible email are empty when comment notification triggered (#4685)

#### 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
修复当评论或回复者的邮箱为空时通知报错的问题

```
pull/4686/head
guqing 2023-10-08 17:30:21 +08:00 committed by GitHub
parent d443c3ed29
commit b2d7221316
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 0 deletions

View File

@ -1,6 +1,8 @@
package run.halo.app.content.comment; package run.halo.app.content.comment;
import io.micrometer.common.util.StringUtils;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import run.halo.app.content.NotificationReasonConst; import run.halo.app.content.NotificationReasonConst;
import run.halo.app.core.extension.content.Comment; import run.halo.app.core.extension.content.Comment;
@ -56,13 +58,21 @@ public class ReplyNotificationSubscriptionHelper {
void subscribeReply(Subscription.ReasonSubject reasonSubject, void subscribeReply(Subscription.ReasonSubject reasonSubject,
Identity identity) { Identity identity) {
var subscriber = createSubscriber(identity); var subscriber = createSubscriber(identity);
if (subscriber == null) {
return;
}
var interestReason = new Subscription.InterestReason(); var interestReason = new Subscription.InterestReason();
interestReason.setReasonType(NotificationReasonConst.SOMEONE_REPLIED_TO_YOU); interestReason.setReasonType(NotificationReasonConst.SOMEONE_REPLIED_TO_YOU);
interestReason.setSubject(reasonSubject); interestReason.setSubject(reasonSubject);
notificationCenter.subscribe(subscriber, interestReason).block(); notificationCenter.subscribe(subscriber, interestReason).block();
} }
@Nullable
private Subscription.Subscriber createSubscriber(Identity author) { private Subscription.Subscriber createSubscriber(Identity author) {
if (StringUtils.isBlank(author.name())) {
return null;
}
Subscription.Subscriber subscriber; Subscription.Subscriber subscriber;
if (author.isEmail()) { if (author.isEmail()) {
subscriber = subscriberEmailResolver.ofEmail(author.name()); subscriber = subscriberEmailResolver.ofEmail(author.name());

View File

@ -43,6 +43,11 @@ public class EmailNotifier implements ReactiveNotifier {
var emailSenderConfig = var emailSenderConfig =
JsonUtils.DEFAULT_JSON_MAPPER.convertValue(senderConfig, EmailSenderConfig.class); 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); JavaMailSenderImpl javaMailSender = getJavaMailSender(emailSenderConfig);
String recipient = context.getMessage().getRecipient(); String recipient = context.getMessage().getRecipient();
@ -51,6 +56,11 @@ public class EmailNotifier implements ReactiveNotifier {
var payload = context.getMessage().getPayload(); var payload = context.getMessage().getPayload();
return subscriberEmailResolver.resolve(subscriber) return subscriberEmailResolver.resolve(subscriber)
.flatMap(toEmail -> { .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()) var htmlMono = appendHtmlBodyFooter(payload.getAttributes())
.doOnNext(footer -> { .doOnNext(footer -> {
if (StringUtils.isNotBlank(payload.getHtmlBody())) { if (StringUtils.isNotBlank(payload.getHtmlBody())) {
@ -138,6 +148,7 @@ public class EmailNotifier implements ReactiveNotifier {
@Data @Data
static class EmailSenderConfig { static class EmailSenderConfig {
private boolean enable;
private String displayName; private String displayName;
private String username; private String username;
private String password; private String password;

View File

@ -19,22 +19,31 @@ spec:
- group: sender - group: sender
label: 发件设置 label: 发件设置
formSchema: formSchema:
- $formkit: checkbox
label: "启用邮件通知器"
value: false
name: enable
- $formkit: text - $formkit: text
if: "$enable"
label: "用户名" label: "用户名"
name: username name: username
validation: required validation: required
- $formkit: password - $formkit: password
if: "$enable"
label: "密码" label: "密码"
name: password name: password
validation: required validation: required
- $formkit: text - $formkit: text
if: "$enable"
label: "显示名称" label: "显示名称"
name: displayName name: displayName
- $formkit: text - $formkit: text
if: "$enable"
label: "SMTP 服务器地址" label: "SMTP 服务器地址"
name: host name: host
validation: required validation: required
- $formkit: text - $formkit: text
if: "$enable"
label: "端口号" label: "端口号"
name: port name: port
validation: required validation: required