Commit e2eab4fe787c8d644d98ae0a25944e1339c5b42a

Authored by YunaiV
1 parent 15a1bca7

♻️ refactor(friend): 重构好友关系管理逻辑,简化双向绑定方法

yudao-module-im/yudao-module-im-biz/src/main/java/cn/iocoder/yudao/module/im/service/friend/ImFriendServiceImpl.java
... ... @@ -5,6 +5,7 @@ import cn.iocoder.yudao.module.im.controller.admin.friend.vo.ImFriendUpdateReqVO
5 5 import cn.iocoder.yudao.module.im.dal.dataobject.friend.ImFriendDO;
6 6 import cn.iocoder.yudao.module.im.dal.mysql.friend.ImFriendMapper;
7 7 import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
  8 +import cn.hutool.extra.spring.SpringUtil;
8 9 import jakarta.annotation.Resource;
9 10 import org.springframework.cache.annotation.CacheEvict;
10 11 import org.springframework.cache.annotation.Cacheable;
... ... @@ -77,26 +78,20 @@ public class ImFriendServiceImpl implements ImFriendService {
77 78 // 1.2 校验对方存在(禁用/不存在的账号不允许添加)
78 79 adminUserApi.validateUser(friendUserId);
79 80  
80   - // 双向绑定
81   - // DONE @AI:改成 createOrUpdateFriend 之类的接口,内部自动处理"已删除的好友再加回来"的情况;或者直接在数据库层面通过唯一索引 + 物理更新来解决。
82   - // 说明:bindFriend 内部已处理 DISABLE → ENABLE 的恢复逻辑
83   - bindFriend(userId, friendUserId);
84   - bindFriend(friendUserId, userId);
  81 + // 2. 双向绑定
  82 + getSelf().addFriend0(userId, friendUserId);
  83 + getSelf().addFriend0(friendUserId, userId);
85 84  
86   - // TODO @AI:推送异步化:1)好友的打招呼;2)通知好友发生变化的消息;
87   -
88   - // TODO @AI:实现如下:
89   - // TODO @芋艿:【对齐】这里缺少了好友添加成功的系统提示消息,
90   - // 需要通过 `ImPrivateMessageService` 插入一条 TIP_TEXT 类型的消息并走 WebSocket 推送给双方。
91   - // 涉及 WebSocket 推送器的事件类型扩展(当前 `ImWsEventType` 无 FRIEND_NEW),待统一后补上。
  85 + // TODO @芋艿:好友添加成功后:1)插入 TIP_TEXT 系统提示消息并推送双方;2)通知双方好友列表变更。
  86 + // 依赖 WebSocket 事件类型扩展(当前无 FRIEND_NEW),待统一后补上。
92 87 }
93 88  
94 89 @Override
95 90 @Transactional(rollbackFor = Exception.class)
96 91 public void deleteFriend(Long userId, Long friendUserId) {
97 92 // 双向标记为 DISABLE + 记录 deleteTime
98   - unbindFriend(userId, friendUserId);
99   - unbindFriend(friendUserId, userId);
  93 + getSelf().deleteFriend0(userId, friendUserId);
  94 + getSelf().deleteFriend0(friendUserId, userId);
100 95  
101 96 // TODO @芋艿:【对齐】这里缺少了好友删除的系统提示 + FRIEND_DEL WebSocket 推送,
102 97 // 待 WS 事件类型扩展后补上。
... ... @@ -109,29 +104,20 @@ public class ImFriendServiceImpl implements ImFriendService {
109 104 * - 已存在但 DISABLE:恢复关系(status=ENABLE,刷新 addTime,清空 deleteTime)
110 105 */
111 106 @CacheEvict(cacheNames = FRIEND, key = "#userId + '_' + #friendUserId")
112   - public void bindFriend(Long userId, Long friendUserId) {
  107 + public void addFriend0(Long userId, Long friendUserId) {
113 108 ImFriendDO exists = imFriendMapper.selectByUserIdAndFriendUserId(userId, friendUserId);
  109 + // 情况一:恢复已删除的好友关系
114 110 if (exists != null) {
115 111 if (CommonStatusEnum.isEnable(exists.getStatus())) {
116   - return; // 已是好友,跳过
  112 + return;
117 113 }
118   - // 恢复已删除的好友关系
119   - ImFriendDO updateObj = new ImFriendDO();
120   - updateObj.setId(exists.getId());
121   - updateObj.setStatus(CommonStatusEnum.ENABLE.getStatus());
122   - updateObj.setAddTime(LocalDateTime.now());
123   - updateObj.setDeleteTime(null);
124   - imFriendMapper.updateById(updateObj);
  114 + imFriendMapper.updateById(new ImFriendDO().setId(exists.getId())
  115 + .setStatus(CommonStatusEnum.ENABLE.getStatus()).setAddTime(LocalDateTime.now()));
125 116 return;
126 117 }
127   - // 新增
128   - ImFriendDO friend = ImFriendDO.builder()
129   - .userId(userId)
130   - .friendUserId(friendUserId)
131   - .muted(false)
132   - .status(CommonStatusEnum.ENABLE.getStatus())
133   - .addTime(LocalDateTime.now())
134   - .build();
  118 + // 情况二:新增好有关系
  119 + ImFriendDO friend = ImFriendDO.builder().userId(userId).friendUserId(friendUserId)
  120 + .muted(false).status(CommonStatusEnum.ENABLE.getStatus()).addTime(LocalDateTime.now()).build();
135 121 imFriendMapper.insert(friend);
136 122 }
137 123  
... ... @@ -139,16 +125,17 @@ public class ImFriendServiceImpl implements ImFriendService {
139 125 * 单向解除好友关系(status 设为 DISABLE,记录 deleteTime)
140 126 */
141 127 @CacheEvict(cacheNames = FRIEND, key = "#userId + '_' + #friendUserId")
142   - public void unbindFriend(Long userId, Long friendUserId) {
  128 + public void deleteFriend0(Long userId, Long friendUserId) {
143 129 ImFriendDO exists = imFriendMapper.selectByUserIdAndFriendUserId(userId, friendUserId);
144 130 if (exists == null || CommonStatusEnum.isDisable(exists.getStatus())) {
145 131 return;
146 132 }
147   - ImFriendDO updateObj = new ImFriendDO();
148   - updateObj.setId(exists.getId());
149   - updateObj.setStatus(CommonStatusEnum.DISABLE.getStatus());
150   - updateObj.setDeleteTime(LocalDateTime.now());
151   - imFriendMapper.updateById(updateObj);
  133 + imFriendMapper.updateById(new ImFriendDO().setId(exists.getId())
  134 + .setStatus(CommonStatusEnum.DISABLE.getStatus()).setDeleteTime(LocalDateTime.now()));
  135 + }
  136 +
  137 + private ImFriendServiceImpl getSelf() {
  138 + return SpringUtil.getBean(getClass());
152 139 }
153 140  
154 141 }
... ...