fix(消息管理):修复消息管理发送消息失败问题

--bug=1034831 --user=郭雨琦 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001034831
--bug=1034840 --user=郭雨琦 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001034840
--bug=1034844 --user=郭雨琦 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001034844
--bug=1034852 --user=郭雨琦 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001034852
--bug=1034859 --user=郭雨琦 https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001034859
This commit is contained in:
guoyuqi 2024-01-31 09:49:27 +08:00 committed by 刘瑞斌
parent 9eb5f72099
commit 992c1d8f41
9 changed files with 102 additions and 44 deletions

View File

@ -41,14 +41,14 @@ public class ProjectRobotController {
@Operation(summary = "项目管理-消息管理-新增机器人")
@RequiresPermissions(PermissionConstants.PROJECT_MESSAGE_READ_ADD)
@Log(type = OperationLogType.ADD, expression = "#msClass.addRobotLog(#projectRobotDTO)", msClass = MessageTaskLogService.class)
public void add(@Validated({Created.class}) @RequestBody ProjectRobotDTO projectRobotDTO) {
public ProjectRobot add(@Validated({Created.class}) @RequestBody ProjectRobotDTO projectRobotDTO) {
ProjectRobot projectRobot = new ProjectRobot();
BeanUtils.copyBean(projectRobot, projectRobotDTO);
projectRobot.setCreateUser(SessionUtils.getUserId());
projectRobot.setCreateTime(System.currentTimeMillis());
projectRobot.setUpdateUser(SessionUtils.getUserId());
projectRobot.setUpdateTime(System.currentTimeMillis());
projectRobotService.add(projectRobot);
return projectRobotService.add(projectRobot);
}
@PostMapping("update")

View File

@ -47,12 +47,12 @@
<select id="getProjectUserSelectList" resultType="io.metersphere.system.dto.sdk.OptionDTO">
select u.id,
u.name
from user_role_relation urr
join `user` u on urr.user_id = u.id
from `user` u
left join user_role_relation urr on urr.user_id = u.id
where urr.role_id = 'project_member'
and urr.source_id = #{projectId}
<if test="keyword != null and keyword != ''">
and name LIKE CONCAT('%', #{keyword}, '%')
and u.name LIKE CONCAT('%', #{keyword}, '%')
</if>
and u.deleted = false
order by u.update_time desc

View File

@ -99,13 +99,15 @@ public class NoticeMessageTaskService {
private void deleteUserData(MessageTaskRequest messageTaskRequest, String projectId) {
MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andReceiverNotIn(messageTaskRequest.getReceiverIds())
.andProjectIdEqualTo(projectId).andProjectRobotIdEqualTo(messageTaskRequest.getRobotId()).andTaskTypeEqualTo(messageTaskRequest.getTaskType()).andEventEqualTo(messageTaskRequest.getEvent());
.andProjectIdEqualTo(projectId).andTaskTypeEqualTo(messageTaskRequest.getTaskType()).andEventEqualTo(messageTaskRequest.getEvent());
List<MessageTask> delData = messageTaskMapper.selectByExample(messageTaskExample);
List<String> delIds = delData.stream().map(MessageTask::getId).toList();
if (CollectionUtils.isNotEmpty(delIds)) {
MessageTaskBlobExample messageTaskBlobExample = new MessageTaskBlobExample();
messageTaskBlobExample.createCriteria().andIdIn(delIds);
messageTaskBlobMapper.deleteByExample(messageTaskBlobExample);
messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andIdIn(delIds);
messageTaskMapper.deleteByExample(messageTaskExample);
}
}

View File

@ -13,6 +13,7 @@ import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserExample;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.uid.IDGenerator;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
@ -20,10 +21,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -43,32 +41,48 @@ public class ProjectRobotService {
@Resource
private UserMapper userMapper;
public void add(ProjectRobot projectRobot) {
public ProjectRobot add(ProjectRobot projectRobot) {
projectRobot.setId(IDGenerator.nextStr());
projectRobot.setEnable(projectRobot.getEnable());
checkDingTalk(projectRobot);
reSetPlatform(projectRobot);
robotMapper.insert(projectRobot);
return projectRobot;
}
private static void checkDingTalk(ProjectRobot projectRobot) {
if (StringUtils.equals(projectRobot.getPlatform(), ProjectRobotPlatform.DING_TALK.toString())) {
private void reSetPlatform(ProjectRobot projectRobot) {
switch (projectRobot.getPlatform()) {
case "DING_TALK" -> checkDingTalkAndSet(projectRobot);
case "LARK" -> projectRobot.setPlatform(NoticeConstants.Type.LARK_ROBOT);
case "WE_COM" -> projectRobot.setPlatform(NoticeConstants.Type.WECOM_ROBOT);
case "IN_SITE" -> projectRobot.setPlatform(NoticeConstants.Type.IN_SITE);
case "MAIL" -> projectRobot.setPlatform(NoticeConstants.Type.MAIL);
case "CUSTOM" -> projectRobot.setPlatform(NoticeConstants.Type.CUSTOM_WEBHOOK_ROBOT);
default -> projectRobot.setPlatform(NoticeConstants.Type.IN_SITE);
}
}
private static void checkDingTalkAndSet(ProjectRobot projectRobot) {
if (StringUtils.equals(projectRobot.getPlatform(), ProjectRobotPlatform.DING_TALK.toString()) || StringUtils.equals(projectRobot.getPlatform(), NoticeConstants.Type.DING_CUSTOM_ROBOT) || StringUtils.equals(projectRobot.getPlatform(), NoticeConstants.Type.DING_ENTERPRISE_ROBOT)) {
if (StringUtils.isBlank(projectRobot.getType())) {
throw new MSException(Translator.get("ding_type_is_null"));
}
if (StringUtils.equals(projectRobot.getType(), ProjectRobotType.ENTERPRISE.toString())) {
projectRobot.setPlatform(NoticeConstants.Type.DING_CUSTOM_ROBOT);
if (StringUtils.equals(projectRobot.getType(), ProjectRobotType.ENTERPRISE.toString()) || StringUtils.equals(projectRobot.getPlatform(), NoticeConstants.Type.DING_ENTERPRISE_ROBOT)) {
if (StringUtils.isBlank(projectRobot.getAppKey())) {
throw new MSException(Translator.get("ding_app_key_is_null"));
}
if (StringUtils.isBlank(projectRobot.getAppSecret())) {
throw new MSException(Translator.get("ding_app_secret_is_null"));
}
projectRobot.setPlatform(NoticeConstants.Type.DING_ENTERPRISE_ROBOT);
}
}
}
public void update(ProjectRobot projectRobot) {
checkRobotExist(projectRobot.getId());
checkDingTalk(projectRobot);
checkDingTalkAndSet(projectRobot);
robotMapper.updateByPrimaryKeySelective(projectRobot);
}
@ -106,41 +120,38 @@ public class ProjectRobotService {
}
public List<ProjectRobot> getList(String projectId) {
ProjectRobotExample projectExample = new ProjectRobotExample();
ProjectRobotExample.Criteria criteria = projectExample.createCriteria();
criteria.andProjectIdEqualTo(projectId);
projectExample.setOrderByClause("create_time desc");
List<ProjectRobot> projectRobots = robotMapper.selectByExample(projectExample);
Map<String, String> userMap = getUserMap(projectRobots);
int inSiteIndex = 0;
int mailIndex = 0;
for (int i = 0; i < projectRobots.size(); i++) {
ProjectRobot projectRobot = projectRobots.get(i);
if (StringUtils.equalsIgnoreCase(projectRobot.getPlatform(), ProjectRobotPlatform.IN_SITE.toString())) {
inSiteIndex = i;
}
if (StringUtils.equalsIgnoreCase(projectRobot.getPlatform(), ProjectRobotPlatform.MAIL.toString())) {
mailIndex = i;
}
for (ProjectRobot projectRobot : projectRobots) {
if ((StringUtils.equalsIgnoreCase(projectRobot.getPlatform(), ProjectRobotPlatform.IN_SITE.toString()) || StringUtils.equalsIgnoreCase(projectRobot.getPlatform(), ProjectRobotPlatform.MAIL.toString())) && StringUtils.isNotBlank(projectRobot.getDescription())) {
projectRobot.setDescription(Translator.get(projectRobot.getDescription()));
projectRobot.setName(Translator.get(projectRobot.getName()));
}
if (userMap.get(projectRobot.getCreateUser())!=null) {
if (userMap.get(projectRobot.getCreateUser()) != null) {
projectRobot.setCreateUser(userMap.get(projectRobot.getCreateUser()));
}
if (userMap.get(projectRobot.getUpdateUser())!=null) {
if (userMap.get(projectRobot.getUpdateUser()) != null) {
projectRobot.setUpdateUser(userMap.get(projectRobot.getUpdateUser()));
}
}
if (projectRobots.size()>0 && inSiteIndex != 0) {
Collections.swap(projectRobots, inSiteIndex, 0);
Map<String, List<ProjectRobot>> collect = projectRobots.stream().collect(Collectors.groupingBy(ProjectRobot::getPlatform));
List<String>defaultPlatForm = new ArrayList<>();
defaultPlatForm.add(NoticeConstants.Type.MAIL);
defaultPlatForm.add(NoticeConstants.Type.IN_SITE);
List<ProjectRobot> list = projectRobots.stream().filter(t -> !defaultPlatForm.contains(t.getPlatform())).toList();
List<ProjectRobot> newProjectRobots = new ArrayList<>(list.stream().sorted(Comparator.comparing(ProjectRobot::getCreateTime).reversed()).toList());
List<ProjectRobot> mailRobot = collect.get(NoticeConstants.Type.MAIL);
if (CollectionUtils.isNotEmpty(mailRobot)) {
newProjectRobots.add(0,mailRobot.get(0));
}
if (projectRobots.size()>1 && mailIndex != 1) {
Collections.swap(projectRobots, mailIndex, 1);
List<ProjectRobot> inSiteRobot = collect.get(NoticeConstants.Type.IN_SITE);
if (CollectionUtils.isNotEmpty(inSiteRobot)) {
newProjectRobots.add(0,inSiteRobot.get(0));
}
return projectRobots;
return newProjectRobots;
}
private Map<String, String> getUserMap(List<ProjectRobot> projectRobots) {

View File

@ -70,6 +70,7 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
List<String> userIds = new ArrayList<>();
userIds.add("project-message-user-1");
userIds.add("project-message-user-2");
userIds.add("project-message-user-3");
userIds.add("project-message-user-del");
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setRobotId("test_message_robot2");
@ -114,6 +115,34 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(contentAsString, ResultHolder.class);
Assertions.assertEquals(100200, resultHolder.getCode());
messageTaskRequest = new MessageTaskRequest();
messageTaskRequest.setProjectId("project-message-test");
messageTaskRequest.setTaskType(NoticeConstants.TaskType.API_DEFINITION_TASK);
messageTaskRequest.setEvent(NoticeConstants.Event.CREATE);
userIds = new ArrayList<>();
userIds.add("project-message-user-1");
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setRobotId("test_message_robot2");
messageTaskRequest.setEnable(true);
mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/save")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(messageTaskRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
resultHolder = JSON.parseObject(contentAsString, ResultHolder.class);
Assertions.assertEquals(100200, resultHolder.getCode());
MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectIdEqualTo("project-message-test").andTaskTypeEqualTo(NoticeConstants.TaskType.API_DEFINITION_TASK).andEventEqualTo(NoticeConstants.Event.CREATE);
List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
Assertions.assertEquals(1, messageTasks.size());
messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectIdEqualTo("project-message-test").andTaskTypeEqualTo(NoticeConstants.TaskType.API_DEFINITION_TASK).andEventEqualTo(NoticeConstants.Event.CREATE).andReceiverEqualTo("project-message-user-3");
messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
}
@Test

View File

@ -116,7 +116,8 @@ public class ProjectRobotControllerTests extends BaseTest {
projectRobotDTO.setPlatform(ProjectRobotPlatform.LARK.toString());
projectRobotDTO.setProjectId("test_project");
projectRobotDTO.setWebhook("https://open.feishu.cn/open-apis/bot/v2/hook/a6024229-9d9d-41c2-8662-7bc3da1092cb");
getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
ProjectRobot postResult = getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
Assertions.assertTrue(StringUtils.equalsIgnoreCase(postResult.getPlatform(),NoticeConstants.Type.LARK_ROBOT));
checkName("test_project", "飞书机器人");
ProjectRobot robot = getRobot("test_project", "飞书机器人");
checkContentLog(robot.getName(),OperationLogType.ADD);
@ -159,7 +160,9 @@ public class ProjectRobotControllerTests extends BaseTest {
projectRobotDTO.setEnable(true);
projectRobotDTO.setType(ProjectRobotType.CUSTOM.toString());
projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=fd963136a4d7eebaaa68de261223089148e62d7519fbaf426626fe3157725b8a");
getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
ProjectRobot postResult = getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
Assertions.assertTrue(StringUtils.equalsIgnoreCase(postResult.getPlatform(),NoticeConstants.Type.DING_CUSTOM_ROBOT));
}
@Test
@ -178,7 +181,8 @@ public class ProjectRobotControllerTests extends BaseTest {
projectRobotDTO.setAppKey("dingxwd71o7kj4qoixo7");
projectRobotDTO.setAppSecret("szmOD9bjGgKtfYk09-Xx2rPdX-xkW4R8Iic0eig_k1D3k95nG4TLKRSpUKUD_f0G");
projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=e971f376669334cd44c585d419f0fdfa1600f97f906109b377999d8a0986b11e");
getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
ProjectRobot postResult = getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
Assertions.assertTrue(StringUtils.equalsIgnoreCase(postResult.getPlatform(),NoticeConstants.Type.DING_ENTERPRISE_ROBOT));
}
@Test

View File

@ -31,6 +31,7 @@ public class AfterReturningNoticeSendService {
Map paramMap = new HashMap<>();
paramMap.put("url", baseSystemConfigDTO.getUrl());
paramMap.put(NoticeConstants.RelatedUser.OPERATOR, sessionUser.getName());
paramMap.put("Language",sessionUser.getLanguage());
paramMap.putAll(resource);
paramMap.putIfAbsent("projectId", currentProjectId);
//TODO: 加来源处理

View File

@ -2,20 +2,22 @@ package io.metersphere.system.service;
import io.metersphere.project.domain.Project;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.notice.MessageDetail;
import io.metersphere.system.notice.sender.AbstractNoticeSender;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.system.notice.MessageDetail;
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 jakarta.annotation.Resource;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.i18n.SimpleLocaleContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import io.metersphere.system.notice.NoticeModel;
import java.util.List;
import java.util.Locale;
@Component
public class NoticeSendService {
@ -57,6 +59,12 @@ public class NoticeSendService {
*/
@Async
public void send(String taskType, NoticeModel noticeModel) {
String language = (String) noticeModel.getParamMap().get("Language");
if (StringUtils.isBlank(language)) {
language = "zh-CN";
}
SimpleLocaleContext localeContext = new SimpleLocaleContext(Locale.of(language));
LocaleContextHolder.setLocaleContext(localeContext);
try {
String projectId = (String) noticeModel.getParamMap().get("projectId");
List<MessageDetail> messageDetails = messageDetailService.searchMessageByTypeAndProjectId(taskType, projectId);
@ -71,6 +79,9 @@ public class NoticeSendService {
} catch (Exception e) {
LogUtils.error(e.getMessage(), e);
}finally {
//清理语言环境
LocaleContextHolder.resetLocaleContext();
}
}

View File

@ -115,7 +115,7 @@ public class AfterReturningNoticeSendServiceTests extends BaseTest {
userDTO.setId(sessionId);
userDTO.setName("admin");
SessionUser user = SessionUser.fromUser(userDTO, sessionId);
user.setLanguage("zh-CN");
for (String event : eventList) {
afterReturningNoticeSendService.sendNotice(functionalCaseTask, event,resources, user, "100001100001");
}