Skip to content

Commit

Permalink
fix: compatible email are empty when comment notification triggered (#…
Browse files Browse the repository at this point in the history
…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
修复当评论或回复者的邮箱为空时通知报错的问题

```
  • Loading branch information
guqing authored Oct 8, 2023
1 parent d443c3e commit b2d7221
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public Mono<Void> 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();
Expand All @@ -51,6 +56,11 @@ public Mono<Void> 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())) {
Expand Down Expand Up @@ -138,6 +148,7 @@ Mono<String> appendHtmlBodyFooter(ReasonAttributes attributes) {

@Data
static class EmailSenderConfig {
private boolean enable;
private String displayName;
private String username;
private String password;
Expand Down
9 changes: 9 additions & 0 deletions application/src/main/resources/extensions/notification.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b2d7221

Please sign in to comment.