refactor: 发通知时查询关注人列表

This commit is contained in:
CaptainB 2022-10-11 15:32:24 +08:00 committed by 刘瑞斌
parent 87877bbdfa
commit e73b56eda9
2 changed files with 81 additions and 57 deletions

View File

@ -1,11 +1,10 @@
package io.metersphere.commons.utils; package io.metersphere.commons.utils;
import io.metersphere.base.domain.User;
import io.metersphere.base.domain.UserKey; import io.metersphere.base.domain.UserKey;
import io.metersphere.commons.constants.ApiKeyConstants; import io.metersphere.commons.constants.ApiKeyConstants;
import io.metersphere.commons.constants.SessionConstants; import io.metersphere.commons.constants.SessionConstants;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.user.SessionUser;
import io.metersphere.dto.UserDTO;
import io.metersphere.service.UserKeyService; import io.metersphere.service.UserKeyService;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -14,14 +13,13 @@ import org.springframework.http.MediaType;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
/** /**
* 服务之间调用需要添加HttpHeader,获取的时候注意当前线程的位置 * 服务之间调用需要添加HttpHeader,获取的时候注意当前线程的位置
*/ */
public class HttpHeaderUtils { public class HttpHeaderUtils {
private static final ThreadLocal<UserDTO> sessionUserThreadLocal = new ThreadLocal<>(); private static final ThreadLocal<User> sessionUserThreadLocal = new ThreadLocal<>();
public static HttpHeaders getHttpHeaders() { public static HttpHeaders getHttpHeaders() {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
@ -55,7 +53,7 @@ public class HttpHeaderUtils {
} }
UserDTO user = sessionUserThreadLocal.get(); User user = sessionUserThreadLocal.get();
if (user != null) { if (user != null) {
UserKey userKey = getUserKey(user); UserKey userKey = getUserKey(user);
accessKey = userKey.getAccessKey(); accessKey = userKey.getAccessKey();
@ -68,7 +66,7 @@ public class HttpHeaderUtils {
return headers; return headers;
} }
private static UserKey getUserKey(UserDTO user) { private static UserKey getUserKey(User user) {
UserKeyService userKeyService = CommonBeanFactory.getBean(UserKeyService.class); UserKeyService userKeyService = CommonBeanFactory.getBean(UserKeyService.class);
List<UserKey> userKeys = userKeyService.getUserKeysInfo(user.getId()); List<UserKey> userKeys = userKeyService.getUserKeysInfo(user.getId());
UserKey userKey; UserKey userKey;
@ -84,7 +82,7 @@ public class HttpHeaderUtils {
return userKey; return userKey;
} }
public static void runAsUser(UserDTO user) { public static void runAsUser(User user) {
if (user != null) { if (user != null) {
if (StringUtils.isBlank(user.getId())) { if (StringUtils.isBlank(user.getId())) {
throw new IllegalArgumentException("User ID can't be null or empty."); throw new IllegalArgumentException("User ID can't be null or empty.");

View File

@ -1,13 +1,19 @@
package io.metersphere.notice.sender; package io.metersphere.notice.sender;
import io.metersphere.base.domain.TestCaseReview;
import io.metersphere.base.domain.User;
import io.metersphere.base.mapper.UserMapper;
import io.metersphere.base.mapper.ext.BaseUserMapper; import io.metersphere.base.mapper.ext.BaseUserMapper;
import io.metersphere.commons.constants.MicroServiceName;
import io.metersphere.commons.constants.NoticeConstants; import io.metersphere.commons.constants.NoticeConstants;
import io.metersphere.commons.constants.NotificationConstants; import io.metersphere.commons.constants.NotificationConstants;
import io.metersphere.commons.utils.HttpHeaderUtils;
import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.notice.domain.MessageDetail; import io.metersphere.notice.domain.MessageDetail;
import io.metersphere.notice.domain.Receiver; import io.metersphere.notice.domain.Receiver;
import io.metersphere.notice.domain.UserDetail; import io.metersphere.notice.domain.UserDetail;
import io.metersphere.service.MicroService;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateFormatUtils;
@ -23,6 +29,10 @@ import java.util.stream.Collectors;
public abstract class AbstractNoticeSender implements NoticeSender { public abstract class AbstractNoticeSender implements NoticeSender {
@Resource @Resource
private BaseUserMapper baseUserMapper; private BaseUserMapper baseUserMapper;
@Resource
private MicroService microService;
@Resource
private UserMapper userMapper;
protected String getContext(MessageDetail messageDetail, NoticeModel noticeModel) { protected String getContext(MessageDetail messageDetail, NoticeModel noticeModel) {
// 如果有自定义字段 // 如果有自定义字段
@ -151,7 +161,12 @@ public abstract class AbstractNoticeSender implements NoticeSender {
} }
break; break;
case NoticeConstants.RelatedUser.FOLLOW_PEOPLE: case NoticeConstants.RelatedUser.FOLLOW_PEOPLE:
toUsers.addAll(handleFollows(messageDetail, noticeModel)); try {
List<Receiver> follows = handleFollows(messageDetail, noticeModel);
toUsers.addAll(follows);
} catch (Exception e) {
LogUtil.error("查询关注人失败: ", e);
}
break; break;
case NoticeConstants.RelatedUser.PROCESSOR: case NoticeConstants.RelatedUser.PROCESSOR:
Object value = paramMap.get("processor"); // 处理人 Object value = paramMap.get("processor"); // 处理人
@ -172,55 +187,66 @@ public abstract class AbstractNoticeSender implements NoticeSender {
private List<Receiver> handleFollows(MessageDetail messageDetail, NoticeModel noticeModel) { private List<Receiver> handleFollows(MessageDetail messageDetail, NoticeModel noticeModel) {
List<Receiver> receivers = new ArrayList<>(); List<Receiver> receivers = new ArrayList<>();
// todo List<String> follows;
// String id = (String) noticeModel.getParamMap().get("id"); List<User> followUsers;
// String taskType = messageDetail.getTaskType(); String id = (String) noticeModel.getParamMap().get("id");
// switch (taskType) { String taskType = messageDetail.getTaskType();
// case NoticeConstants.TaskType.TEST_PLAN_TASK: String operator = noticeModel.getOperator();
// receivers = testPlanService.getPlanFollow(id) HttpHeaderUtils.runAsUser(userMapper.selectByPrimaryKey(operator));
// .stream() switch (taskType) {
// .map(user -> new Receiver(user.getId(), NotificationConstants.Type.SYSTEM_NOTICE.name())) case NoticeConstants.TaskType.TEST_PLAN_TASK:
// .collect(Collectors.toList()); followUsers = microService.getForDataArray(MicroServiceName.TEST_TRACK, "/test/plan/follow/" + id, User.class);
// break; receivers = followUsers
// case NoticeConstants.TaskType.REVIEW_TASK: .stream()
// TestCaseReview request = new TestCaseReview(); .map(user -> new Receiver(user.getId(), NotificationConstants.Type.SYSTEM_NOTICE.name()))
// request.setId(id); .collect(Collectors.toList());
// receivers = testCaseReviewService.getFollowByReviewId(request).stream() break;
// .map(user -> new Receiver(user.getId(), NotificationConstants.Type.SYSTEM_NOTICE.name())) case NoticeConstants.TaskType.REVIEW_TASK:
// .collect(Collectors.toList()); TestCaseReview request = new TestCaseReview();
// break; request.setId(id);
// case NoticeConstants.TaskType.API_AUTOMATION_TASK: followUsers = microService.postForDataArray(MicroServiceName.TEST_TRACK, "/test/case/review/follow", request, User.class);
// receivers = apiAutomationService.getFollows(id) receivers = followUsers
// .stream() .stream()
// .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) .map(user -> new Receiver(user.getId(), NotificationConstants.Type.SYSTEM_NOTICE.name()))
// .collect(Collectors.toList()); .collect(Collectors.toList());
// break; break;
// case NoticeConstants.TaskType.API_DEFINITION_TASK: case NoticeConstants.TaskType.API_AUTOMATION_TASK:
// receivers = apiDefinitionService.getFollows(id) follows = microService.getForDataArray(MicroServiceName.API_TEST, "/api/automation/follow/" + id, String.class);
// .stream() receivers = follows
// .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) .stream()
// .collect(Collectors.toList()); .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name()))
// List<Receiver> caseFollows = apiTestCaseService.getFollows(id) .collect(Collectors.toList());
// .stream() break;
// .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) case NoticeConstants.TaskType.API_DEFINITION_TASK:
// .collect(Collectors.toList()); follows = microService.getForDataArray(MicroServiceName.API_TEST, "/api/definition/follow/" + id, String.class);
// receivers.addAll(caseFollows); receivers = follows
// break; .stream()
// case NoticeConstants.TaskType.PERFORMANCE_TEST_TASK: .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name()))
// receivers = performanceTestService.getFollows(id) .collect(Collectors.toList());
// .stream() follows = microService.getForDataArray(MicroServiceName.API_TEST, "/api/testcase/follow/" + id, String.class);
// .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) List<Receiver> caseFollows = follows
// .collect(Collectors.toList()); .stream()
// break; .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name()))
// case NoticeConstants.TaskType.TRACK_TEST_CASE_TASK: .collect(Collectors.toList());
// receivers = testCaseService.getFollows(id) receivers.addAll(caseFollows);
// .stream() break;
// .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) case NoticeConstants.TaskType.PERFORMANCE_TEST_TASK:
// .collect(Collectors.toList()); follows = microService.getForDataArray(MicroServiceName.PERFORMANCE_TEST, "/performance/test/follow/" + id, String.class);
// break; receivers = follows
// default: .stream()
// break; .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name()))
// } .collect(Collectors.toList());
break;
case NoticeConstants.TaskType.TRACK_TEST_CASE_TASK:
follows = microService.getForDataArray(MicroServiceName.TEST_TRACK, "/test/case/follow/" + id, String.class);
receivers = follows
.stream()
.map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name()))
.collect(Collectors.toList());
break;
default:
break;
}
LogUtil.info("FOLLOW_PEOPLE: {}", receivers); LogUtil.info("FOLLOW_PEOPLE: {}", receivers);
return receivers; return receivers;
} }