Commit ba1a85e38aae4e7d9f08d662f4ce2a2f940a2436
1 parent
f0be7ba1
✨ feat(im): 优化好友申请逻辑,增加自我添加校验与异常处理
更新好友申请功能,使用 computed 包裹当前用户 ID,避免在 keep-alive 实例中持有旧 ID。增加自我添加好友的校验逻辑,防止用户添加自己为好友。同时,增强自动通过好友申请的异常处理,确保在事务提交后能正确处理失败情况。
Showing
1 changed file
with
10 additions
and
2 deletions
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/friend/ImFriendRequestServiceImpl.java
| 1 | package cn.iocoder.yudao.module.im.service.friend; | 1 | package cn.iocoder.yudao.module.im.service.friend; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.bean.BeanUtil; | ||
| 4 | +import cn.hutool.core.bean.copier.CopyOptions; | ||
| 3 | import cn.hutool.core.util.ObjUtil; | 5 | import cn.hutool.core.util.ObjUtil; |
| 4 | import cn.hutool.extra.spring.SpringUtil; | 6 | import cn.hutool.extra.spring.SpringUtil; |
| 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 7 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| @@ -87,7 +89,7 @@ public class ImFriendRequestServiceImpl implements ImFriendRequestService { | @@ -87,7 +89,7 @@ public class ImFriendRequestServiceImpl implements ImFriendRequestService { | ||
| 87 | ImFriendRequestDO request = friendRequestMapper.selectLatestByFromUserIdAndToUserId(fromUserId, toUserId); | 89 | ImFriendRequestDO request = friendRequestMapper.selectLatestByFromUserIdAndToUserId(fromUserId, toUserId); |
| 88 | if (request != null && ImFriendRequestHandleResultEnum.isUnhandled(request.getHandleResult())) { | 90 | if (request != null && ImFriendRequestHandleResultEnum.isUnhandled(request.getHandleResult())) { |
| 89 | // 复用未处理记录:刷新申请理由 / 备注 / 来源;createTime 维持首次申请时间不变(与列表 id DESC 排序一致) | 91 | // 复用未处理记录:刷新申请理由 / 备注 / 来源;createTime 维持首次申请时间不变(与列表 id DESC 排序一致) |
| 90 | - BeanUtils.copyProperties(reqVO, request); | 92 | + BeanUtil.copyProperties(reqVO, request, CopyOptions.create().setIgnoreNullValue(true)); |
| 91 | friendRequestMapper.updateById(request); | 93 | friendRequestMapper.updateById(request); |
| 92 | } else { | 94 | } else { |
| 93 | request = BeanUtils.toBean(reqVO, ImFriendRequestDO.class) | 95 | request = BeanUtils.toBean(reqVO, ImFriendRequestDO.class) |
| @@ -104,13 +106,19 @@ public class ImFriendRequestServiceImpl implements ImFriendRequestService { | @@ -104,13 +106,19 @@ public class ImFriendRequestServiceImpl implements ImFriendRequestService { | ||
| 104 | ImMessageTypeEnum.FRIEND_APPLICATION.getType(), fromUserId, toUserId, payload)); | 106 | ImMessageTypeEnum.FRIEND_APPLICATION.getType(), fromUserId, toUserId, payload)); |
| 105 | 107 | ||
| 106 | // 4. 全局自动通过开关:注册 afterCommit 回调,事务提交后再走同意流程 | 108 | // 4. 全局自动通过开关:注册 afterCommit 回调,事务提交后再走同意流程 |
| 109 | + // 回调内 try/catch 兜底 —— afterCommit 异常会被 Spring 静默吞掉,否则同意失败时申请方永远等不到 APPROVED | ||
| 107 | if (imProperties.getFriend().isAutoAccept()) { | 110 | if (imProperties.getFriend().isAutoAccept()) { |
| 108 | Long requestId = request.getId(); | 111 | Long requestId = request.getId(); |
| 109 | TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { | 112 | TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() { |
| 110 | 113 | ||
| 111 | @Override | 114 | @Override |
| 112 | public void afterCommit() { | 115 | public void afterCommit() { |
| 113 | - getSelf().agreeFriendRequest(toUserId, requestId); | 116 | + try { |
| 117 | + getSelf().agreeFriendRequest(toUserId, requestId); | ||
| 118 | + } catch (Exception e) { | ||
| 119 | + log.error("[applyFriend][autoAccept fromUserId={} toUserId={} requestId={} 自动通过失败]", | ||
| 120 | + fromUserId, toUserId, requestId, e); | ||
| 121 | + } | ||
| 114 | } | 122 | } |
| 115 | 123 | ||
| 116 | }); | 124 | }); |