From 17d924f9e9e65566a5951e8c812c14e50d142623 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Fri, 20 Aug 2021 15:47:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=8F=91=E9=80=81=E4=BA=BA?= =?UTF-8?q?=E5=8E=BB=E9=87=8D=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/notice/domain/Receiver.java | 2 ++ .../notice/sender/AbstractNoticeSender.java | 18 ++++++++++-------- .../notice/sender/impl/DingNoticeSender.java | 5 ++++- .../notice/sender/impl/MailNoticeSender.java | 5 ++++- .../notice/sender/impl/WeComNoticeSender.java | 5 ++++- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/backend/src/main/java/io/metersphere/notice/domain/Receiver.java b/backend/src/main/java/io/metersphere/notice/domain/Receiver.java index 3a35704509..ea8d225588 100644 --- a/backend/src/main/java/io/metersphere/notice/domain/Receiver.java +++ b/backend/src/main/java/io/metersphere/notice/domain/Receiver.java @@ -2,9 +2,11 @@ package io.metersphere.notice.domain; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.EqualsAndHashCode; @Data @AllArgsConstructor +@EqualsAndHashCode public class Receiver { private String userId; private String type; diff --git a/backend/src/main/java/io/metersphere/notice/sender/AbstractNoticeSender.java b/backend/src/main/java/io/metersphere/notice/sender/AbstractNoticeSender.java index 9436ee6f7b..9f7f5980c0 100644 --- a/backend/src/main/java/io/metersphere/notice/sender/AbstractNoticeSender.java +++ b/backend/src/main/java/io/metersphere/notice/sender/AbstractNoticeSender.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -127,16 +128,15 @@ public abstract class AbstractNoticeSender implements NoticeSender { break; case NoticeConstants.RelatedUser.CREATOR: Object creator = paramMap.get("creator"); + Object createUser = paramMap.get("createUser"); + Object userId1 = paramMap.get("userId"); + if (creator != null) { toUsers.add(new Receiver(creator.toString(), NotificationConstants.Type.SYSTEM_NOTICE.name())); - } - Object createUser = paramMap.get("createUser"); - if (createUser != null) { - toUsers.add(new Receiver(createUser.toString(), NotificationConstants.Type.SYSTEM_NOTICE.name())); - } - createUser = paramMap.get("userId"); - if (createUser != null) { + } else if (createUser != null) { toUsers.add(new Receiver(createUser.toString(), NotificationConstants.Type.SYSTEM_NOTICE.name())); + } else if (userId1 != null) { + toUsers.add(new Receiver(userId1.toString(), NotificationConstants.Type.SYSTEM_NOTICE.name())); } break; case NoticeConstants.RelatedUser.MAINTAINER: @@ -161,6 +161,8 @@ public abstract class AbstractNoticeSender implements NoticeSender { } // 排除自己 toUsers.removeIf(u -> StringUtils.equals(u.getUserId(), noticeModel.getOperator())); - return toUsers; + // 去重复 + HashSet receivers = new HashSet<>(toUsers); + return new ArrayList<>(receivers); } } diff --git a/backend/src/main/java/io/metersphere/notice/sender/impl/DingNoticeSender.java b/backend/src/main/java/io/metersphere/notice/sender/impl/DingNoticeSender.java index 04393fdeff..04d401f5ea 100644 --- a/backend/src/main/java/io/metersphere/notice/sender/impl/DingNoticeSender.java +++ b/backend/src/main/java/io/metersphere/notice/sender/impl/DingNoticeSender.java @@ -30,7 +30,10 @@ public class DingNoticeSender extends AbstractNoticeSender { text.setContent(context); request.setText(text); OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); - List phoneList = super.getUserPhones(noticeModel, receivers.stream().map(Receiver::getUserId).collect(Collectors.toList())); + List phoneList = super.getUserPhones(noticeModel, receivers.stream() + .map(Receiver::getUserId) + .distinct() + .collect(Collectors.toList())); LogUtil.info("收件人地址: " + phoneList); at.setAtMobiles(phoneList); request.setAt(at); diff --git a/backend/src/main/java/io/metersphere/notice/sender/impl/MailNoticeSender.java b/backend/src/main/java/io/metersphere/notice/sender/impl/MailNoticeSender.java index 2eb6384a80..f92896c31f 100644 --- a/backend/src/main/java/io/metersphere/notice/sender/impl/MailNoticeSender.java +++ b/backend/src/main/java/io/metersphere/notice/sender/impl/MailNoticeSender.java @@ -37,7 +37,10 @@ public class MailNoticeSender extends AbstractNoticeSender { LogUtil.info("发件人地址" + javaMailSender.getUsername()); LogUtil.info("helper" + helper); helper.setSubject("MeterSphere " + noticeModel.getSubject()); - List userIds = noticeModel.getReceivers().stream().map(Receiver::getUserId).collect(Collectors.toList()); + List userIds = noticeModel.getReceivers().stream() + .map(Receiver::getUserId) + .distinct() + .collect(Collectors.toList()); List emails = super.getUserEmails(noticeModel, userIds); String[] users = emails.toArray(new String[0]); LogUtil.info("收件人地址: " + emails); diff --git a/backend/src/main/java/io/metersphere/notice/sender/impl/WeComNoticeSender.java b/backend/src/main/java/io/metersphere/notice/sender/impl/WeComNoticeSender.java index e04806c644..bb7dd07f84 100644 --- a/backend/src/main/java/io/metersphere/notice/sender/impl/WeComNoticeSender.java +++ b/backend/src/main/java/io/metersphere/notice/sender/impl/WeComNoticeSender.java @@ -24,7 +24,10 @@ public class WeComNoticeSender extends AbstractNoticeSender { return; } TextMessage message = new TextMessage(context); - List phoneLists = super.getUserPhones(noticeModel, receivers.stream().map(Receiver::getUserId).collect(Collectors.toList())); + List phoneLists = super.getUserPhones(noticeModel, receivers.stream() + .map(Receiver::getUserId) + .distinct() + .collect(Collectors.toList())); message.setMentionedMobileList(phoneLists); try { WxChatbotClient.send(messageDetail.getWebhook(), message);