|
|
@@ -1,5 +1,8 @@
|
|
|
package cn.iocoder.yudao.module.im.service.conversation;
|
|
|
|
|
|
+import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
|
|
+import cn.iocoder.yudao.framework.websocket.core.sender.WebSocketMessageSender;
|
|
|
+import cn.iocoder.yudao.module.im.controller.admin.conversation.vo.ImConversationCreateReqVO;
|
|
|
import cn.iocoder.yudao.module.im.controller.admin.conversation.vo.ImConversationUpdateLastReadTimeReqVO;
|
|
|
import cn.iocoder.yudao.module.im.controller.admin.conversation.vo.ImConversationUpdatePinnedReqVO;
|
|
|
import cn.iocoder.yudao.module.im.dal.dataobject.conversation.ImConversationDO;
|
|
|
@@ -13,6 +16,8 @@ import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
+import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
|
|
+
|
|
|
/**
|
|
|
* IM 会话 Service 实现类
|
|
|
*
|
|
|
@@ -21,17 +26,24 @@ import java.util.List;
|
|
|
@Service
|
|
|
@Validated
|
|
|
public class ImConversationServiceImpl implements ImConversationService {
|
|
|
+ private final String IM_CONVERSATION_ADD = "im-conversation-add";
|
|
|
|
|
|
@Resource
|
|
|
private ImConversationMapper imConversationMapper;
|
|
|
|
|
|
+ @Resource
|
|
|
+ WebSocketMessageSender webSocketMessageSender;
|
|
|
+
|
|
|
@Override
|
|
|
public List<ImConversationDO> getConversationList(Long loginUserId) {
|
|
|
// 根据loginUserId判断targetId, 自己不能做targetId
|
|
|
- // 如何loginUserId和targetId相同,则需要调换userId和targetId
|
|
|
+ // 如果loginUserId和targetId相同,则需要调换userId和targetId
|
|
|
List<ImConversationDO> conversationList = imConversationMapper.selectList();
|
|
|
+
|
|
|
+ // 过滤和自己无关的会话
|
|
|
+ List<ImConversationDO> conversationFilteredList = conversationList.stream().filter(item -> loginUserId.equals(item.getUserId()) || loginUserId.equals(item.getTargetId())).toList();
|
|
|
// 遍历判断loginUserId和targetId相同,相同则将userId设置为targetId, targetId设置为userId
|
|
|
- conversationList.forEach(item -> {
|
|
|
+ conversationFilteredList.forEach(item -> {
|
|
|
if (item.getTargetId().equals(loginUserId)) {
|
|
|
Long targetId = item.getTargetId();
|
|
|
Long userId = item.getUserId();
|
|
|
@@ -39,7 +51,7 @@ public class ImConversationServiceImpl implements ImConversationService {
|
|
|
item.setUserId(targetId);
|
|
|
}
|
|
|
});
|
|
|
- return conversationList;
|
|
|
+ return conversationFilteredList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -84,4 +96,29 @@ public class ImConversationServiceImpl implements ImConversationService {
|
|
|
// 4. 做对应更新的 notify 推送
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public ImConversationDO createConversation(Long loginUserId, ImConversationCreateReqVO createReqVO) {
|
|
|
+ // 1. 获得会话编号
|
|
|
+ String no = ImConversationTypeEnum.generateConversationNo(loginUserId, createReqVO.getTargetId(), createReqVO.getType());
|
|
|
+ // 2. 查询会话
|
|
|
+ ImConversationDO conversation = imConversationMapper.selectByNo(no);
|
|
|
+
|
|
|
+ if (conversation == null) {
|
|
|
+ // 2.1. 不存在,则插入
|
|
|
+ conversation = insertConversation(no, loginUserId, createReqVO.getTargetId(), createReqVO.getType());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送打开会话的通知,并推送会话实体
|
|
|
+ // 给自己发送创建会话成功的通知
|
|
|
+ webSocketMessageSender.sendObject(UserTypeEnum.ADMIN.getValue(), getLoginUserId(),
|
|
|
+ IM_CONVERSATION_ADD, conversation);
|
|
|
+
|
|
|
+ // 给接受者发送创建会话的通知
|
|
|
+ webSocketMessageSender.sendObject(UserTypeEnum.ADMIN.getValue(), createReqVO.getTargetId(),
|
|
|
+ IM_CONVERSATION_ADD, conversation);
|
|
|
+
|
|
|
+ return conversation;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|