Commit ac05257ec000f04359483a1acad6f82a1c8d2aa8

Authored by YunaiV
1 parent 6da5846b

♻️ refactor(im): 修改消息相关字段类型为 Long,优化数据处理

yudao-module-im/yudao-module-im-biz/src/main/java/cn/iocoder/yudao/module/im/controller/admin/message/ImGroupMessageController.java
@@ -51,8 +51,8 @@ public class ImGroupMessageController { @@ -51,8 +51,8 @@ public class ImGroupMessageController {
51 List<ImGroupMessageDO> messages = imGroupMessageService.pullMessages(getLoginUserId(), minId, size); 51 List<ImGroupMessageDO> messages = imGroupMessageService.pullMessages(getLoginUserId(), minId, size);
52 ImMessagePullRespVO<ImGroupMessageRespVO> resp = new ImMessagePullRespVO<>(); 52 ImMessagePullRespVO<ImGroupMessageRespVO> resp = new ImMessagePullRespVO<>();
53 resp.setList(messages.stream().map(this::toRespVO).collect(Collectors.toList())); 53 resp.setList(messages.stream().map(this::toRespVO).collect(Collectors.toList()));
54 - resp.setNextMinId(messages.isEmpty() ? minId.toString()  
55 - : messages.get(messages.size() - 1).getId().toString()); 54 + resp.setNextMinId(messages.isEmpty() ? minId
  55 + : messages.get(messages.size() - 1).getId());
56 return success(resp); 56 return success(resp);
57 } 57 }
58 58
yudao-module-im/yudao-module-im-biz/src/main/java/cn/iocoder/yudao/module/im/controller/admin/message/ImPrivateMessageController.java
1 package cn.iocoder.yudao.module.im.controller.admin.message; 1 package cn.iocoder.yudao.module.im.controller.admin.message;
2 2
3 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 3 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
  4 +import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
4 import cn.iocoder.yudao.module.im.controller.admin.message.vo.*; 5 import cn.iocoder.yudao.module.im.controller.admin.message.vo.*;
5 -import cn.iocoder.yudao.module.im.controller.admin.message.vo.privates.ImPrivateMessageReadReqVO;  
6 import cn.iocoder.yudao.module.im.controller.admin.message.vo.privates.ImPrivateMessageRespVO; 6 import cn.iocoder.yudao.module.im.controller.admin.message.vo.privates.ImPrivateMessageRespVO;
7 import cn.iocoder.yudao.module.im.controller.admin.message.vo.privates.ImPrivateMessageSendReqVO; 7 import cn.iocoder.yudao.module.im.controller.admin.message.vo.privates.ImPrivateMessageSendReqVO;
8 import cn.iocoder.yudao.module.im.dal.dataobject.message.ImPrivateMessageDO; 8 import cn.iocoder.yudao.module.im.dal.dataobject.message.ImPrivateMessageDO;
@@ -16,7 +16,6 @@ import org.springframework.validation.annotation.Validated; @@ -16,7 +16,6 @@ import org.springframework.validation.annotation.Validated;
16 import org.springframework.web.bind.annotation.*; 16 import org.springframework.web.bind.annotation.*;
17 17
18 import java.util.List; 18 import java.util.List;
19 -import java.util.stream.Collectors;  
20 19
21 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; 20 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
22 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; 21 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@@ -37,10 +36,9 @@ public class ImPrivateMessageController { @@ -37,10 +36,9 @@ public class ImPrivateMessageController {
37 @Operation(summary = "发送私聊消息") 36 @Operation(summary = "发送私聊消息")
38 public CommonResult<ImPrivateMessageRespVO> sendMessage(@Valid @RequestBody ImPrivateMessageSendReqVO reqVO) { 37 public CommonResult<ImPrivateMessageRespVO> sendMessage(@Valid @RequestBody ImPrivateMessageSendReqVO reqVO) {
39 ImPrivateMessageDO message = imPrivateMessageService.sendMessage(getLoginUserId(), reqVO); 38 ImPrivateMessageDO message = imPrivateMessageService.sendMessage(getLoginUserId(), reqVO);
40 - return success(toRespVO(message)); 39 + return success(BeanUtils.toBean(message, ImPrivateMessageRespVO.class));
41 } 40 }
42 41
43 - // DONE @AI:需要在评估下,是否需要 nextMinId。结论:需要,前端用 nextMinId 做增量拉取游标  
44 @GetMapping("/pull") 42 @GetMapping("/pull")
45 @Operation(summary = "拉取私聊消息(增量)") 43 @Operation(summary = "拉取私聊消息(增量)")
46 @Parameter(name = "minId", description = "最小消息 id", required = true, example = "0") 44 @Parameter(name = "minId", description = "最小消息 id", required = true, example = "0")
@@ -50,21 +48,20 @@ public class ImPrivateMessageController { @@ -50,21 +48,20 @@ public class ImPrivateMessageController {
50 @RequestParam("size") Integer size) { 48 @RequestParam("size") Integer size) {
51 List<ImPrivateMessageDO> messages = imPrivateMessageService.pullMessages(getLoginUserId(), minId, size); 49 List<ImPrivateMessageDO> messages = imPrivateMessageService.pullMessages(getLoginUserId(), minId, size);
52 ImMessagePullRespVO<ImPrivateMessageRespVO> resp = new ImMessagePullRespVO<>(); 50 ImMessagePullRespVO<ImPrivateMessageRespVO> resp = new ImMessagePullRespVO<>();
53 - resp.setList(messages.stream().map(this::toRespVO).collect(Collectors.toList()));  
54 - resp.setNextMinId(messages.isEmpty() ? minId.toString()  
55 - : messages.get(messages.size() - 1).getId().toString()); 51 + resp.setList(BeanUtils.toBean(messages, ImPrivateMessageRespVO.class));
  52 + resp.setNextMinId(messages.isEmpty() ? minId
  53 + : messages.get(messages.size() - 1).getId());
56 return success(resp); 54 return success(resp);
57 } 55 }
58 56
59 - // DONE @AI:它是不是不用 VO。结论:需要用 ReqVO,承载 friendId 字段并做参数校验  
60 @PutMapping("/read") 57 @PutMapping("/read")
61 @Operation(summary = "标记私聊消息已读") 58 @Operation(summary = "标记私聊消息已读")
62 - public CommonResult<Boolean> readMessages(@Valid @RequestBody ImPrivateMessageReadReqVO reqVO) {  
63 - imPrivateMessageService.readMessages(getLoginUserId(), reqVO.getFriendId()); 59 + @Parameter(name = "friendId", description = "好友用户编号", required = true, example = "2")
  60 + public CommonResult<Boolean> readMessages(@RequestParam("friendId") Long friendId) {
  61 + imPrivateMessageService.readMessages(getLoginUserId(), friendId);
64 return success(true); 62 return success(true);
65 } 63 }
66 64
67 - // DONE @AI:不使用 path variable,改为 query param  
68 @DeleteMapping("/recall") 65 @DeleteMapping("/recall")
69 @Operation(summary = "撤回私聊消息") 66 @Operation(summary = "撤回私聊消息")
70 @Parameter(name = "id", description = "消息编号", required = true, example = "1") 67 @Parameter(name = "id", description = "消息编号", required = true, example = "1")
@@ -73,21 +70,4 @@ public class ImPrivateMessageController { @@ -73,21 +70,4 @@ public class ImPrivateMessageController {
73 return success(true); 70 return success(true);
74 } 71 }
75 72
76 - // DONE @AI:beanutils。结论:因 id/senderId/receiverId 需 Long→String 手动转换,不适合用 BeanUtils  
77 - /**  
78 - * DO 转换为 RespVO(Long id 统一转 String)  
79 - */  
80 - private ImPrivateMessageRespVO toRespVO(ImPrivateMessageDO message) {  
81 - ImPrivateMessageRespVO vo = new ImPrivateMessageRespVO();  
82 - vo.setId(message.getId().toString());  
83 - vo.setClientMessageId(message.getClientMessageId());  
84 - vo.setSenderId(message.getSenderId().toString());  
85 - vo.setReceiverId(message.getReceiverId().toString());  
86 - vo.setType(message.getType());  
87 - vo.setContent(message.getContent());  
88 - vo.setStatus(message.getStatus());  
89 - vo.setSendTime(message.getSendTime());  
90 - return vo;  
91 - }  
92 -  
93 } 73 }
yudao-module-im/yudao-module-im-biz/src/main/java/cn/iocoder/yudao/module/im/controller/admin/message/vo/ImMessagePullRespVO.java
@@ -16,6 +16,6 @@ public class ImMessagePullRespVO&lt;T&gt; { @@ -16,6 +16,6 @@ public class ImMessagePullRespVO&lt;T&gt; {
16 private List<T> list; 16 private List<T> list;
17 17
18 @Schema(description = "下次拉取的最小 id", example = "100") 18 @Schema(description = "下次拉取的最小 id", example = "100")
19 - private String nextMinId; 19 + private Long nextMinId;
20 20
21 } 21 }
yudao-module-im/yudao-module-im-biz/src/main/java/cn/iocoder/yudao/module/im/controller/admin/message/vo/privates/ImPrivateMessageReadReqVO.java deleted
1 -package cn.iocoder.yudao.module.im.controller.admin.message.vo.privates;  
2 -  
3 -import io.swagger.v3.oas.annotations.media.Schema;  
4 -import jakarta.validation.constraints.NotNull;  
5 -import lombok.Data;  
6 -  
7 -/**  
8 - * 私聊已读 Request VO  
9 - */  
10 -@Schema(description = "管理后台 - 私聊已读 Request VO")  
11 -@Data  
12 -public class ImPrivateMessageReadReqVO {  
13 -  
14 - @Schema(description = "好友用户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")  
15 - @NotNull(message = "好友用户编号不能为空")  
16 - private Long friendId;  
17 -  
18 -}  
yudao-module-im/yudao-module-im-biz/src/main/java/cn/iocoder/yudao/module/im/controller/admin/message/vo/privates/ImPrivateMessageRespVO.java
@@ -19,10 +19,10 @@ public class ImPrivateMessageRespVO { @@ -19,10 +19,10 @@ public class ImPrivateMessageRespVO {
19 private String clientMessageId; 19 private String clientMessageId;
20 20
21 @Schema(description = "发送人编号", example = "1") 21 @Schema(description = "发送人编号", example = "1")
22 - private String senderId; 22 + private Long senderId;
23 23
24 @Schema(description = "接收人编号", example = "2") 24 @Schema(description = "接收人编号", example = "2")
25 - private String receiverId; 25 + private Long receiverId;
26 26
27 @Schema(description = "消息类型", example = "0") 27 @Schema(description = "消息类型", example = "0")
28 private Integer type; 28 private Integer type;