diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseNoticeService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseNoticeService.java index 7692c39e31..a7e7c27538 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseNoticeService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiTestCaseNoticeService.java @@ -12,9 +12,11 @@ import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.SubListUtils; import io.metersphere.system.domain.User; import io.metersphere.system.dto.sdk.ApiDefinitionCaseDTO; +import io.metersphere.system.dto.sdk.BaseSystemConfigDTO; import io.metersphere.system.mapper.UserMapper; import io.metersphere.system.notice.constants.NoticeConstants; import io.metersphere.system.service.CommonNoticeSendService; +import io.metersphere.system.service.SystemParameterService; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.BooleanUtils; @@ -34,6 +36,8 @@ public class ApiTestCaseNoticeService { private CommonNoticeSendService commonNoticeSendService; @Resource private ExtApiTestCaseMapper extApiTestCaseMapper; + @Resource + private SystemParameterService systemParameterService; public ApiDefinitionCaseDTO addCaseDto(ApiTestCaseAddRequest request) { ApiDefinitionCaseDTO caseDTO = new ApiDefinitionCaseDTO(); @@ -104,26 +108,22 @@ public class ApiTestCaseNoticeService { if (BooleanUtils.isTrue(notificationConfig.getScenarioCreator()) || BooleanUtils.isTrue(notificationConfig.getApiCaseCreator())) { for (Map resource : resources) { String caseId = (String) resource.get("id"); - String relatedUsers = null; + List extraUsers = new ArrayList<>(); if (BooleanUtils.isTrue(notificationConfig.getScenarioCreator())) { // 添加引用该用例的场景的创建人 Set userIds = Optional.ofNullable(caseRefApiScenarioCreatorMap.get(caseId)).orElse(new HashSet<>(1)); - relatedUsers = userIds.stream().collect(Collectors.joining(";")); + extraUsers.addAll(userIds); } if (BooleanUtils.isTrue(notificationConfig.getApiCaseCreator())) { // 添加用例创建人 String createUser = (String) resource.get("createUser"); - if (relatedUsers == null) { - relatedUsers = createUser; - } else { - relatedUsers += ";" + createUser; - } + extraUsers.add(createUser); } - // 添加特殊通知人 - resource.put("relatedUsers", relatedUsers); + BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); + commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, event, resource, user, projectId, + baseSystemConfigDTO, extraUsers, true); } } - commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, event, resources, user, projectId); } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonNoticeSendService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonNoticeSendService.java index 3b52d56be2..24ba78251a 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonNoticeSendService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonNoticeSendService.java @@ -58,6 +58,48 @@ public class CommonNoticeSendService { } } + /** + * 发送通知 + * @param taskType + * @param event + * @param operator + * @param currentProjectId + * @param baseSystemConfig + * @param resource + * @param extraUsers 除了消息通知配置的用户,需要额外通知的用户 + * @param excludeSelf 是否排除自己 + */ + public void sendNotice(String taskType, String event, Map resource, User operator, String currentProjectId, BaseSystemConfigDTO baseSystemConfig, + List extraUsers, boolean excludeSelf) { + Map paramMap = new HashMap<>(); + paramMap.put("url", baseSystemConfig.getUrl()); + paramMap.put(NoticeConstants.RelatedUser.OPERATOR, operator.getName()); + paramMap.put("Language", operator.getLanguage()); + paramMap.putAll(resource); + paramMap.putIfAbsent("projectId", currentProjectId); + + // 占位符 + handleDefaultValues(paramMap); + + String context = getContext(taskType, event); + + String subject = getSubject(taskType, event); + + List relatedUsers = getRelatedUsers(resource.get("relatedUsers")); + + NoticeModel noticeModel = NoticeModel.builder() + .operator(operator.getId()) + .context(context) + .subject(subject) + .paramMap(paramMap) + .event(event) + .status((String) paramMap.get("status")) + .excludeSelf(true) + .relatedUsers(relatedUsers) + .build(); + noticeSendService.sendOther(taskType, noticeModel, extraUsers, excludeSelf); + } + private List getRelatedUsers(Object relatedUsers) { String relatedUser = (String) relatedUsers; List relatedUserList = new ArrayList<>(); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/NoticeSendService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/NoticeSendService.java index c76c639e4b..acac9b73d5 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/NoticeSendService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/NoticeSendService.java @@ -8,7 +8,9 @@ import io.metersphere.system.notice.NoticeModel; import io.metersphere.system.notice.constants.NoticeConstants; import io.metersphere.system.notice.sender.AbstractNoticeSender; import io.metersphere.system.notice.sender.impl.*; +import io.metersphere.system.notice.utils.MessageTemplateUtils; import jakarta.annotation.Resource; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.SerializationUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.context.i18n.LocaleContextHolder; @@ -17,6 +19,7 @@ import org.springframework.stereotype.Component; import java.util.List; import java.util.Locale; +import java.util.Map; @Component public class NoticeSendService { @@ -168,4 +171,63 @@ public class NoticeSendService { } } + /** + * 其他类型 + */ + @Async("threadPoolTaskExecutor") + public void sendOther(String taskType, NoticeModel noticeModel, List users, boolean excludeSelf) { + //如果在线需要排除自己,也需要选定当前环境选择的语言 + if (excludeSelf) { + setLanguage(noticeModel); + } + // 定时任务调用不排除自己 + noticeModel.setExcludeSelf(excludeSelf); + try { + String projectId = (String) noticeModel.getParamMap().get("projectId"); + List messageDetails = messageDetailService.searchMessageByTypeAndProjectId(taskType, projectId) + .stream() + .filter(messageDetail -> StringUtils.equals(messageDetail.getEvent(), noticeModel.getEvent())) + .toList(); + if (CollectionUtils.isEmpty(messageDetails)) { + NoticeModel n = SerializationUtils.clone(noticeModel); + MessageDetail m = buildMessageTails(taskType, noticeModel, users, projectId); + inSiteNoticeSender.send(m, n); + } else { + // 异步发送通知 + messageDetails.stream() + .forEach(messageDetail -> { + MessageDetail m = SerializationUtils.clone(messageDetail); + if (CollectionUtils.isNotEmpty(users)) { + m.getReceiverIds().addAll(users); + } + NoticeModel n = SerializationUtils.clone(noticeModel); + try { + this.getNoticeSender(m).send(m, n); + } catch (Exception e) { + LogUtils.error(e); + } + }); + } + + } catch (Exception e) { + LogUtils.error(e.getMessage(), e); + } + } + + private static MessageDetail buildMessageTails(String taskType, NoticeModel noticeModel, List users, String projectId) { + MessageDetail m = new MessageDetail(); + Map defaultTemplateTitleMap = MessageTemplateUtils.getDefaultTemplateSubjectMap(); + String defaultSubject = defaultTemplateTitleMap.get(taskType + "_" + noticeModel.getEvent()); + Map defaultTemplateMap = MessageTemplateUtils.getDefaultTemplateMap(); + String defaultTemplate = defaultTemplateMap.get(taskType + "_" + noticeModel.getEvent()); + m.setReceiverIds(users); + m.setProjectId(projectId); + m.setEvent(noticeModel.getEvent()); + m.setTaskType(taskType); + m.setTemplate(defaultTemplate); + m.setSubject(defaultSubject); + m.setType(NoticeConstants.Type.IN_SITE); + return m; + } + }