Skip to content

Commit cfffa91

Browse files
committed
fix: refactoring notification handling and message detail building
1 parent 57a4afc commit cfffa91

4 files changed

Lines changed: 29 additions & 26 deletions

File tree

backend/crm/src/main/java/cn/cordys/crm/system/notice/message/MessageDetailService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ private List<MessageDetailDTO> getMessageDetailDTOs(String module, boolean useTe
5151
if (messageTaskLists == null) {
5252
return new ArrayList<>();
5353
}
54-
getMessageDetailDTOs(useTemplate, template, messageDetails, messageTaskLists);
54+
buildMessageDetails(useTemplate, template, messageDetails, messageTaskLists);
5555
return messageDetails.stream()
5656
.sorted(Comparator.comparing(MessageDetailDTO::getCreateTime, Comparator.nullsLast(Long::compareTo)).reversed())
5757
.distinct()
5858
.collect(Collectors.toList());
5959
}
6060

61-
private void getMessageDetailDTOs(boolean useTemplate, String customTemplate, List<MessageDetailDTO> messageDetails, List<MessageTask> messageTaskLists) {
61+
private void buildMessageDetails(boolean useTemplate, String customTemplate, List<MessageDetailDTO> messageDetails, List<MessageTask> messageTaskLists) {
6262
//消息通知任务以消息类型事件接收类型唯一进行分组
6363
Map<String, List<MessageTask>> messageTaskGroup = messageTaskLists.stream().collect(Collectors.groupingBy(t -> (t.getTaskType() + t.getEvent())));
6464
messageTaskGroup.forEach((messageTaskId, messageTaskList) -> {

backend/crm/src/main/java/cn/cordys/crm/system/notice/sender/AbstractNoticeSender.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
import org.apache.commons.lang3.StringUtils;
1414
import org.apache.commons.lang3.Strings;
1515

16-
import java.util.ArrayList;
17-
import java.util.List;
18-
import java.util.Map;
16+
import java.util.*;
17+
import java.util.stream.Collectors;
1918

2019
public abstract class AbstractNoticeSender implements NoticeSender {
2120

@@ -29,10 +28,7 @@ protected String getContext(MessageDetailDTO messageDetailDTO, NoticeModel notic
2928
if (StringUtils.isNotBlank(messageDetailDTO.getTemplate())) {
3029
return MessageTemplateUtils.getContent(messageDetailDTO.getTemplate(), noticeModel.getParamMap());
3130
}
32-
String context = StringUtils.EMPTY;
33-
if (StringUtils.isBlank(context)) {
34-
context = noticeModel.getContext();
35-
}
31+
String context = noticeModel.getContext();
3632
return MessageTemplateUtils.getContent(context, noticeModel.getParamMap());
3733
}
3834

@@ -54,13 +50,13 @@ protected String getSubjectText(MessageDetailDTO messageDetailDTO, NoticeModel n
5450
private List<Receiver> getRealUserIds(NoticeModel noticeModel, String orgId) {
5551
List<Receiver> toUsers = noticeModel.getReceivers();
5652
if (CollectionUtils.isEmpty(toUsers)) {
57-
toUsers = new ArrayList<>();
53+
return new ArrayList<>();
5854
}
5955

6056
// 去重复
61-
List<String> userIds = toUsers.stream().map(Receiver::getUserId).toList();
57+
List<String> userIds = toUsers.stream().map(Receiver::getUserId).distinct().toList();
6258
List<User> users = getUsers(userIds, orgId);
63-
List<String> realUserIds = new ArrayList<>(users.stream().map(User::getId).distinct().toList());
59+
Set<String> realUserIds = users.stream().map(User::getId).collect(Collectors.toSet());
6460
//如果是admin用户,为特殊用户,将admin加入
6561
if (userIds.contains("admin")) {
6662
realUserIds.add("admin");
@@ -84,7 +80,7 @@ protected List<String> getResourceUserIds(List<String> userIds, String organizat
8480
}
8581
}
8682

87-
protected List<Receiver> getReceivers(List<Receiver> receivers, Boolean excludeSelf, String operator) {
83+
protected List<Receiver> getReceivers(List<Receiver> receivers, boolean excludeSelf, String operator) {
8884
// 排除自己
8985
List<Receiver> realReceivers = new ArrayList<>();
9086
if (excludeSelf) {

backend/crm/src/main/java/cn/cordys/crm/system/notice/sender/insite/InSiteNoticeSender.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public void sendAnnouncement(MessageDetailDTO messageDetailDTO, NoticeModel noti
7070
}
7171
notification.setType(receiver.getType());
7272
notification.setStatus(NotificationConstants.Status.UNREAD.name());
73-
notification.setCreateTime(System.currentTimeMillis());
7473
notification.setReceiver(receiver.getUserId());
7574
notification.setContent(context.getBytes());
7675
notification.setCreateUser(noticeModel.getOperator());
@@ -80,17 +79,18 @@ public void sendAnnouncement(MessageDetailDTO messageDetailDTO, NoticeModel noti
8079
notificationBaseMapper.insert(notification);
8180
String messageText = JSON.toJSONString(notification);
8281
//储存信息
83-
stringRedisTemplate.opsForZSet().add(USER_PREFIX + receiver.getUserId(), id, System.currentTimeMillis());
82+
String userRedisKey = USER_PREFIX + receiver.getUserId();
83+
stringRedisTemplate.opsForZSet().add(userRedisKey, id, System.currentTimeMillis());
8484
stringRedisTemplate.opsForValue().set(MSG_PREFIX + id, messageText);
85-
// 限制 Redis 只存 5 条消息
85+
// 限制 Redis 只存 5 条消息:移除排名 5 之后的所有条目
8686
Set<String> oldNotificationIds = stringRedisTemplate.opsForZSet()
87-
.reverseRange(USER_PREFIX + receiver.getUserId(), 4, -1);
87+
.reverseRange(userRedisKey, 4, -1);
8888
if (CollectionUtils.isNotEmpty(oldNotificationIds)) {
89-
for (String oldNotificationId : oldNotificationIds) {
90-
stringRedisTemplate.delete(MSG_PREFIX + oldNotificationId);
91-
}
89+
stringRedisTemplate.delete(oldNotificationIds.stream()
90+
.map(nid -> MSG_PREFIX + nid)
91+
.toList());
92+
stringRedisTemplate.opsForZSet().removeRange(userRedisKey, 0, -6);
9293
}
93-
stringRedisTemplate.opsForZSet().removeRange(USER_PREFIX + receiver.getUserId(), 0, -6);
9494
//更新用户的已读全部消息状态 0 为未读,1为已读
9595
stringRedisTemplate.opsForValue().set(USER_READ_PREFIX + receiver.getUserId(), "False");
9696

backend/crm/src/main/java/cn/cordys/crm/system/notice/sse/SseService.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.time.Duration;
2323
import java.util.*;
24+
import java.util.Objects;
2425
import java.util.concurrent.ConcurrentHashMap;
2526

2627
@Service
@@ -155,12 +156,18 @@ private List<NotificationDTO> buildDTOList(Set<String> values, String prefix) {
155156
.map(val -> stringRedisTemplate.opsForValue().get(prefix + val))
156157
.filter(StringUtils::isNotBlank)
157158
.map(json -> {
158-
Notification notification = JSON.parseObject(json, Notification.class);
159-
NotificationDTO dto = new NotificationDTO();
160-
BeanUtils.copyBean(dto, notification);
161-
dto.setContentText(new String(notification.getContent()));
162-
return dto;
159+
try {
160+
Notification notification = JSON.parseObject(json, Notification.class);
161+
NotificationDTO dto = new NotificationDTO();
162+
BeanUtils.copyBean(dto, notification);
163+
dto.setContentText(new String(notification.getContent()));
164+
return dto;
165+
} catch (Exception e) {
166+
log.warn("Failed to parse notification from Redis: {}", e.getMessage());
167+
return null;
168+
}
163169
})
170+
.filter(Objects::nonNull)
164171
.sorted(Comparator.comparing(NotificationDTO::getCreateTime).reversed())
165172
.toList();
166173
}

0 commit comments

Comments
 (0)