Commit 9ebf7209019fb85ce171a4557f1ccae5c5cf8236
1 parent
7d5ee9f1
✨ feat(im): 初始化群申请 v0.0:第一把 review
Showing
34 changed files
with
2004 additions
and
38 deletions
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/group/ImGroupController.java
| @@ -10,7 +10,9 @@ import cn.iocoder.yudao.module.im.controller.admin.message.vo.group.ImGroupMessa | @@ -10,7 +10,9 @@ import cn.iocoder.yudao.module.im.controller.admin.message.vo.group.ImGroupMessa | ||
| 10 | import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupDO; | 10 | import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupDO; |
| 11 | import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupMemberDO; | 11 | import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupMemberDO; |
| 12 | import cn.iocoder.yudao.module.im.dal.dataobject.message.ImGroupMessageDO; | 12 | import cn.iocoder.yudao.module.im.dal.dataobject.message.ImGroupMessageDO; |
| 13 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupMemberRoleEnum; | ||
| 13 | import cn.iocoder.yudao.module.im.service.group.ImGroupMemberService; | 14 | import cn.iocoder.yudao.module.im.service.group.ImGroupMemberService; |
| 15 | +import cn.iocoder.yudao.module.im.service.group.ImGroupRequestService; | ||
| 14 | import cn.iocoder.yudao.module.im.service.group.ImGroupService; | 16 | import cn.iocoder.yudao.module.im.service.group.ImGroupService; |
| 15 | import cn.iocoder.yudao.module.im.service.message.ImGroupMessageService; | 17 | import cn.iocoder.yudao.module.im.service.message.ImGroupMessageService; |
| 16 | import io.swagger.v3.oas.annotations.Operation; | 18 | import io.swagger.v3.oas.annotations.Operation; |
| @@ -40,6 +42,8 @@ public class ImGroupController { | @@ -40,6 +42,8 @@ public class ImGroupController { | ||
| 40 | private ImGroupMemberService groupMemberService; | 42 | private ImGroupMemberService groupMemberService; |
| 41 | @Resource | 43 | @Resource |
| 42 | private ImGroupMessageService groupMessageService; | 44 | private ImGroupMessageService groupMessageService; |
| 45 | + @Resource | ||
| 46 | + private ImGroupRequestService groupRequestService; | ||
| 43 | 47 | ||
| 44 | // ==================== 群的写操作 ==================== | 48 | // ==================== 群的写操作 ==================== |
| 45 | 49 | ||
| @@ -177,23 +181,34 @@ public class ImGroupController { | @@ -177,23 +181,34 @@ public class ImGroupController { | ||
| 177 | } | 181 | } |
| 178 | 182 | ||
| 179 | /** | 183 | /** |
| 180 | - * 群列表批量转 VO + 关联回填 pinnedMessages | 184 | + * 群列表批量转 VO + 关联回填 pinnedMessages 与 pendingRequestCount |
| 181 | * <p> | 185 | * <p> |
| 182 | - * 仅当登录用户是某群的有效成员时才回填该群的 pinnedMessages,避免非成员 / 已退群用户越权拿到置顶消息内容 | 186 | + * pinnedMessages 仅当前用户是该群有效成员时回填;pendingRequestCount 仅当前用户是该群 owner / admin 时回填 |
| 183 | */ | 187 | */ |
| 184 | private List<ImGroupRespVO> buildGroupRespVOList(List<ImGroupDO> groups, Long loginUserId) { | 188 | private List<ImGroupRespVO> buildGroupRespVOList(List<ImGroupDO> groups, Long loginUserId) { |
| 185 | if (CollUtil.isEmpty(groups)) { | 189 | if (CollUtil.isEmpty(groups)) { |
| 186 | return Collections.emptyList(); | 190 | return Collections.emptyList(); |
| 187 | } | 191 | } |
| 188 | - // 仅当前用户是有效成员的群才允许回填置顶消息 | ||
| 189 | - Set<Long> activeGroupIds = convertSet( | ||
| 190 | - groupMemberService.getActiveGroupMemberListByUserId(loginUserId), ImGroupMemberDO::getGroupId); | 192 | + // 1.1 当前用户的活跃群成员记录;用于 pinnedMessages 回填的「成员」可见性 + pendingRequestCount 回填的「owner / admin」可见性 |
| 193 | + List<ImGroupMemberDO> myMembers = groupMemberService.getActiveGroupMemberListByUserId(loginUserId); | ||
| 194 | + Set<Long> activeGroupIds = convertSet(myMembers, ImGroupMemberDO::getGroupId); | ||
| 195 | + Set<Long> ownerOrAdminGroupIds = convertSet(myMembers, | ||
| 196 | + ImGroupMemberDO::getGroupId, | ||
| 197 | + member -> ImGroupMemberRoleEnum.isOwnerOrAdmin(member.getRole())); | ||
| 198 | + // 1.2 批量查置顶消息 | ||
| 191 | Set<Long> allMessageIds = convertSetByFlatMap(groups, group -> activeGroupIds.contains(group.getId()) | 199 | Set<Long> allMessageIds = convertSetByFlatMap(groups, group -> activeGroupIds.contains(group.getId()) |
| 192 | ? CollUtil.emptyIfNull(group.getPinnedMessageIds()).stream() : Stream.empty()); | 200 | ? CollUtil.emptyIfNull(group.getPinnedMessageIds()).stream() : Stream.empty()); |
| 193 | Map<Long, ImGroupMessageDO> messageMap = groupMessageService.getGroupMessageMap(allMessageIds); | 201 | Map<Long, ImGroupMessageDO> messageMap = groupMessageService.getGroupMessageMap(allMessageIds); |
| 194 | - // 转换输出 | 202 | + // 1.3 仅 owner / admin 群批量统计未处理申请数 |
| 203 | + // TODO @AI:这块讨论中。。。 | ||
| 204 | + Map<Long, Long> pendingCountMap = groupRequestService.getPendingCountMap( | ||
| 205 | + groups.stream().map(ImGroupDO::getId).filter(ownerOrAdminGroupIds::contains).toList()); | ||
| 206 | + // 2. 转换输出 | ||
| 195 | return convertList(groups, group -> { | 207 | return convertList(groups, group -> { |
| 196 | ImGroupRespVO vo = BeanUtils.toBean(group, ImGroupRespVO.class); | 208 | ImGroupRespVO vo = BeanUtils.toBean(group, ImGroupRespVO.class); |
| 209 | + if (ownerOrAdminGroupIds.contains(group.getId())) { | ||
| 210 | + vo.setPendingRequestCount(pendingCountMap.getOrDefault(group.getId(), 0L)); | ||
| 211 | + } | ||
| 197 | if (!activeGroupIds.contains(group.getId()) || CollUtil.isEmpty(group.getPinnedMessageIds())) { | 212 | if (!activeGroupIds.contains(group.getId()) || CollUtil.isEmpty(group.getPinnedMessageIds())) { |
| 198 | return vo; | 213 | return vo; |
| 199 | } | 214 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/group/ImGroupRequestController.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.controller.admin.group; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 4 | +import cn.hutool.core.util.ObjUtil; | ||
| 5 | +import cn.iocoder.yudao.framework.common.pojo.CommonResult; | ||
| 6 | +import cn.iocoder.yudao.framework.common.util.collection.MapUtils; | ||
| 7 | +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | ||
| 8 | +import cn.iocoder.yudao.module.im.controller.admin.group.vo.request.ImGroupRequestApplyReqVO; | ||
| 9 | +import cn.iocoder.yudao.module.im.controller.admin.group.vo.request.ImGroupRequestRespVO; | ||
| 10 | +import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupDO; | ||
| 11 | +import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupRequestDO; | ||
| 12 | +import cn.iocoder.yudao.module.im.service.group.ImGroupRequestService; | ||
| 13 | +import cn.iocoder.yudao.module.im.service.group.ImGroupService; | ||
| 14 | +import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | ||
| 15 | +import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | ||
| 16 | +import io.swagger.v3.oas.annotations.Operation; | ||
| 17 | +import io.swagger.v3.oas.annotations.Parameter; | ||
| 18 | +import io.swagger.v3.oas.annotations.tags.Tag; | ||
| 19 | +import jakarta.annotation.Resource; | ||
| 20 | +import jakarta.validation.Valid; | ||
| 21 | +import jakarta.validation.constraints.Max; | ||
| 22 | +import jakarta.validation.constraints.Min; | ||
| 23 | +import jakarta.validation.constraints.NotNull; | ||
| 24 | +import jakarta.validation.constraints.Size; | ||
| 25 | +import org.springframework.validation.annotation.Validated; | ||
| 26 | +import org.springframework.web.bind.annotation.*; | ||
| 27 | + | ||
| 28 | +import java.util.*; | ||
| 29 | +import java.util.stream.Stream; | ||
| 30 | + | ||
| 31 | +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | ||
| 32 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; | ||
| 33 | +import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId; | ||
| 34 | + | ||
| 35 | +/** | ||
| 36 | + * IM 加群申请 Controller | ||
| 37 | + * | ||
| 38 | + * @author 芋道源码 | ||
| 39 | + */ | ||
| 40 | +@Tag(name = "管理后台 - IM 加群申请") | ||
| 41 | +@RestController | ||
| 42 | +@RequestMapping("/im/group-request") | ||
| 43 | +@Validated | ||
| 44 | +public class ImGroupRequestController { | ||
| 45 | + | ||
| 46 | + @Resource | ||
| 47 | + private ImGroupRequestService groupRequestService; | ||
| 48 | + @Resource | ||
| 49 | + private ImGroupService groupService; | ||
| 50 | + | ||
| 51 | + @Resource | ||
| 52 | + private AdminUserApi adminUserApi; | ||
| 53 | + | ||
| 54 | + @PostMapping("/apply") | ||
| 55 | + @Operation(summary = "申请加群") | ||
| 56 | + public CommonResult<Long> applyJoinGroup(@Valid @RequestBody ImGroupRequestApplyReqVO reqVO) { | ||
| 57 | + ImGroupRequestDO request = groupRequestService.applyJoinGroup(getLoginUserId(), reqVO); | ||
| 58 | + return success(request != null ? request.getId() : null); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + @PutMapping("/agree") | ||
| 62 | + @Operation(summary = "同意加群申请(群主或管理员)") | ||
| 63 | + @Parameter(name = "id", description = "申请编号", required = true, example = "1024") | ||
| 64 | + public CommonResult<Boolean> agreeGroupRequest( | ||
| 65 | + @RequestParam("id") @NotNull(message = "申请编号不能为空") Long id) { | ||
| 66 | + groupRequestService.agreeGroupRequest(getLoginUserId(), id); | ||
| 67 | + return success(true); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + @PutMapping("/refuse") | ||
| 71 | + @Operation(summary = "拒绝加群申请(群主或管理员)") | ||
| 72 | + public CommonResult<Boolean> refuseGroupRequest( | ||
| 73 | + @RequestParam("id") @NotNull(message = "申请编号不能为空") Long id, | ||
| 74 | + @RequestParam(value = "handleContent", required = false) | ||
| 75 | + @Size(max = 255, message = "处理理由最多 255 个字符") String handleContent) { | ||
| 76 | + groupRequestService.refuseGroupRequest(getLoginUserId(), id, handleContent); | ||
| 77 | + return success(true); | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + @GetMapping("/list") | ||
| 81 | + @Operation(summary = "查询「我相关」的加群申请列表(含我主动申请、我被邀请待审);游标分页") | ||
| 82 | + public CommonResult<List<ImGroupRequestRespVO>> getMyGroupRequestList( | ||
| 83 | + @Parameter(description = "当前列表最旧记录的 id;首页不传") | ||
| 84 | + @RequestParam(value = "lastRequestId", required = false) Long lastRequestId, | ||
| 85 | + @Parameter(description = "单次拉取条数", required = true) | ||
| 86 | + @RequestParam("limit") @Min(1) @Max(200) Integer limit) { | ||
| 87 | + List<ImGroupRequestDO> list = groupRequestService.getMyGroupRequestList(getLoginUserId(), lastRequestId, limit); | ||
| 88 | + return success(buildList(list)); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + // TODO @AI:pending-list; | ||
| 92 | + @GetMapping("/list-pending") | ||
| 93 | + @Operation(summary = "查询指定群的未处理加群申请列表(仅群主或管理员可调);游标分页") | ||
| 94 | + public CommonResult<List<ImGroupRequestRespVO>> getPendingGroupRequestList( | ||
| 95 | + @RequestParam("groupId") @NotNull(message = "群编号不能为空") Long groupId, | ||
| 96 | + @Parameter(description = "当前列表最旧记录的 id;首页不传") | ||
| 97 | + @RequestParam(value = "lastRequestId", required = false) Long lastRequestId, | ||
| 98 | + @Parameter(description = "单次拉取条数", required = true) | ||
| 99 | + @RequestParam("limit") @Min(1) @Max(200) Integer limit) { | ||
| 100 | + List<ImGroupRequestDO> list = groupRequestService.getPendingGroupRequestList( | ||
| 101 | + getLoginUserId(), groupId, lastRequestId, limit); | ||
| 102 | + return success(buildList(list)); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + // TODO @AI:群管理里,也有个地方,可以查询这个群所有的 list;这样上面的 list、list-pending 有点重复。看看怎么组合。可以和我讨论下; | ||
| 106 | + | ||
| 107 | + // TODO @AI:方法上,不用 my;因为这个方法,都是查询我的; | ||
| 108 | + @GetMapping("/get") | ||
| 109 | + @Operation(summary = "按 id 单查「我相关」的申请记录(带越权过滤;WebSocket 通知到达后用)") | ||
| 110 | + @Parameter(name = "id", description = "申请记录编号", required = true) | ||
| 111 | + public CommonResult<ImGroupRequestRespVO> getMyGroupRequest(@RequestParam("id") Long id) { | ||
| 112 | + ImGroupRequestDO request = groupRequestService.getGroupRequest(id); | ||
| 113 | + // 越权过滤:申请人 / 被邀请人 / 邀请人 / 群主 / 管理员之外,当不存在返回 null | ||
| 114 | + if (request == null) { | ||
| 115 | + return success(null); | ||
| 116 | + } | ||
| 117 | + Long currentUserId = getLoginUserId(); | ||
| 118 | + boolean canSee = ObjUtil.equal(request.getUserId(), currentUserId) | ||
| 119 | + || ObjUtil.equal(request.getInviterUserId(), currentUserId); | ||
| 120 | + if (!canSee) { | ||
| 121 | + // 群主 / 管理员可见;通过 list-pending 校验路径 | ||
| 122 | + // TODO @AI:不要 try catch; | ||
| 123 | + try { | ||
| 124 | + // TODO @AI:这个方法的实现,不要里面抛出异常;应该调用查询是否为管理员;而不应该这么搞哈。这样,是不是可以上面 || 后面在加一个条件? | ||
| 125 | + groupRequestService.getPendingGroupRequestList(currentUserId, request.getGroupId(), null, 1); | ||
| 126 | + canSee = true; | ||
| 127 | + } catch (Exception ignored) { | ||
| 128 | + // 非 owner / admin | ||
| 129 | + } | ||
| 130 | + } | ||
| 131 | + if (!canSee) { | ||
| 132 | + return success(null); | ||
| 133 | + } | ||
| 134 | + return success(CollUtil.getFirst(buildList(Collections.singletonList(request)))); | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + /** 申请记录列表批量转 VO + 关联回填用户 / 群信息 */ | ||
| 138 | + private List<ImGroupRequestRespVO> buildList(List<ImGroupRequestDO> list) { | ||
| 139 | + if (CollUtil.isEmpty(list)) { | ||
| 140 | + return Collections.emptyList(); | ||
| 141 | + } | ||
| 142 | + // 1. 聚合 user / inviter 用户信息 | ||
| 143 | + // TODO @AI:.filter(Objects::nonNull) 内部已经过滤了; | ||
| 144 | + Set<Long> userIds = convertSetByFlatMap(list, | ||
| 145 | + request -> Stream.of(request.getUserId(), request.getInviterUserId()) | ||
| 146 | + .filter(Objects::nonNull)); | ||
| 147 | + Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds); | ||
| 148 | + // 2. 聚合群信息(封禁 / 解散群也要回填,便于前端展示历史) | ||
| 149 | + Set<Long> groupIds = convertSet(list, ImGroupRequestDO::getGroupId); | ||
| 150 | + Map<Long, ImGroupDO> groupMap = groupService.getGroupMap(groupIds); | ||
| 151 | + return convertList(list, request -> { | ||
| 152 | + ImGroupRequestRespVO vo = BeanUtils.toBean(request, ImGroupRequestRespVO.class); | ||
| 153 | + MapUtils.findAndThen(userMap, request.getUserId(), user -> | ||
| 154 | + vo.setUserNickname(user.getNickname()).setUserAvatar(user.getAvatar())); | ||
| 155 | + MapUtils.findAndThen(userMap, request.getInviterUserId(), user -> | ||
| 156 | + vo.setInviterNickname(user.getNickname()).setInviterAvatar(user.getAvatar())); | ||
| 157 | + MapUtils.findAndThen(groupMap, request.getGroupId(), group -> | ||
| 158 | + vo.setGroupName(group.getName()).setGroupAvatar(group.getAvatar())); | ||
| 159 | + return vo; | ||
| 160 | + }); | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/group/vo/ImGroupCreateReqVO.java
| 1 | package cn.iocoder.yudao.module.im.controller.admin.group.vo; | 1 | package cn.iocoder.yudao.module.im.controller.admin.group.vo; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.common.validation.InEnum; | ||
| 4 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupJoinTypeEnum; | ||
| 3 | import io.swagger.v3.oas.annotations.media.Schema; | 5 | import io.swagger.v3.oas.annotations.media.Schema; |
| 4 | import jakarta.validation.constraints.NotBlank; | 6 | import jakarta.validation.constraints.NotBlank; |
| 5 | import lombok.Data; | 7 | import lombok.Data; |
| @@ -17,4 +19,8 @@ public class ImGroupCreateReqVO { | @@ -17,4 +19,8 @@ public class ImGroupCreateReqVO { | ||
| 17 | @Schema(description = "初始成员用户编号列表(建群同时邀请的好友,不含创建者自己)", example = "[1024, 2048]") | 19 | @Schema(description = "初始成员用户编号列表(建群同时邀请的好友,不含创建者自己)", example = "[1024, 2048]") |
| 18 | private List<Long> memberUserIds; | 20 | private List<Long> memberUserIds; |
| 19 | 21 | ||
| 22 | + @Schema(description = "加群方式;不传默认 0 自由进群", example = "0") | ||
| 23 | + @InEnum(ImGroupJoinTypeEnum.class) | ||
| 24 | + private Integer joinType; // 参见 ImGroupJoinTypeEnum 枚举类 | ||
| 25 | + | ||
| 20 | } | 26 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/group/vo/ImGroupRespVO.java
| @@ -32,6 +32,13 @@ public class ImGroupRespVO { | @@ -32,6 +32,13 @@ public class ImGroupRespVO { | ||
| 32 | @Schema(description = "是否全群禁言") | 32 | @Schema(description = "是否全群禁言") |
| 33 | private Boolean mutedAll; | 33 | private Boolean mutedAll; |
| 34 | 34 | ||
| 35 | + @Schema(description = "加群方式", example = "0") | ||
| 36 | + private Integer joinType; // 参见 ImGroupJoinTypeEnum 枚举类 | ||
| 37 | + | ||
| 38 | + // TODO @AI:是不是不回填;前端自己查询 + 推送维护?类似微信,会展示有人加群,需要审批之类的。(晚点我可以给截图); | ||
| 39 | + @Schema(description = "未处理加群申请数;仅当登录用户是该群群主 / 管理员时回填,否则为 null", example = "3") | ||
| 40 | + private Long pendingRequestCount; | ||
| 41 | + | ||
| 35 | @Schema(description = "封禁时间") | 42 | @Schema(description = "封禁时间") |
| 36 | private LocalDateTime bannedTime; | 43 | private LocalDateTime bannedTime; |
| 37 | 44 |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/group/vo/ImGroupUpdateReqVO.java
| 1 | package cn.iocoder.yudao.module.im.controller.admin.group.vo; | 1 | package cn.iocoder.yudao.module.im.controller.admin.group.vo; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.common.validation.InEnum; | ||
| 4 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupJoinTypeEnum; | ||
| 3 | import io.swagger.v3.oas.annotations.media.Schema; | 5 | import io.swagger.v3.oas.annotations.media.Schema; |
| 4 | import jakarta.validation.constraints.NotNull; | 6 | import jakarta.validation.constraints.NotNull; |
| 5 | import lombok.Data; | 7 | import lombok.Data; |
| @@ -21,4 +23,8 @@ public class ImGroupUpdateReqVO { | @@ -21,4 +23,8 @@ public class ImGroupUpdateReqVO { | ||
| 21 | @Schema(description = "群公告") | 23 | @Schema(description = "群公告") |
| 22 | private String notice; | 24 | private String notice; |
| 23 | 25 | ||
| 26 | + @Schema(description = "加群方式", example = "1") | ||
| 27 | + @InEnum(ImGroupJoinTypeEnum.class) | ||
| 28 | + private Integer joinType; // 参见 ImGroupJoinTypeEnum 枚举类 | ||
| 29 | + | ||
| 24 | } | 30 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/group/vo/request/ImGroupRequestApplyReqVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.controller.admin.group.vo.request; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.validation.InEnum; | ||
| 4 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupAddSourceEnum; | ||
| 5 | +import io.swagger.v3.oas.annotations.media.Schema; | ||
| 6 | +import jakarta.validation.constraints.NotNull; | ||
| 7 | +import jakarta.validation.constraints.Size; | ||
| 8 | +import lombok.Data; | ||
| 9 | + | ||
| 10 | +@Schema(description = "管理后台 - IM 加群申请发起 Request VO") | ||
| 11 | +@Data | ||
| 12 | +public class ImGroupRequestApplyReqVO { | ||
| 13 | + | ||
| 14 | + @Schema(description = "群编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") | ||
| 15 | + @NotNull(message = "群编号不能为空") | ||
| 16 | + private Long groupId; | ||
| 17 | + | ||
| 18 | + @Schema(description = "申请理由", example = "我是芋艿(一种食材)") | ||
| 19 | + @Size(max = 255, message = "申请理由最多 255 个字符") | ||
| 20 | + private String applyContent; | ||
| 21 | + | ||
| 22 | + @Schema(description = "加入来源", example = "1") | ||
| 23 | + @InEnum(ImGroupAddSourceEnum.class) | ||
| 24 | + private Integer addSource; | ||
| 25 | + | ||
| 26 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/group/vo/request/ImGroupRequestRespVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.controller.admin.group.vo.request; | ||
| 2 | + | ||
| 3 | +import io.swagger.v3.oas.annotations.media.Schema; | ||
| 4 | +import lombok.Data; | ||
| 5 | + | ||
| 6 | +import java.time.LocalDateTime; | ||
| 7 | + | ||
| 8 | +@Schema(description = "管理后台 - IM 加群申请 Response VO") | ||
| 9 | +@Data | ||
| 10 | +public class ImGroupRequestRespVO { | ||
| 11 | + | ||
| 12 | + @Schema(description = "申请编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") | ||
| 13 | + private Long id; | ||
| 14 | + | ||
| 15 | + @Schema(description = "群编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") | ||
| 16 | + private Long groupId; | ||
| 17 | + | ||
| 18 | + @Schema(description = "申请人 / 被邀请人用户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "100") | ||
| 19 | + private Long userId; | ||
| 20 | + | ||
| 21 | + @Schema(description = "邀请人用户编号;NULL 表示用户主动申请", example = "200") | ||
| 22 | + private Long inviterUserId; | ||
| 23 | + | ||
| 24 | + @Schema(description = "处理结果", requiredMode = Schema.RequiredMode.REQUIRED, example = "0") | ||
| 25 | + private Integer handleResult; // 参见 ImGroupRequestHandleResultEnum 枚举 | ||
| 26 | + | ||
| 27 | + @Schema(description = "申请理由", example = "我想加入这个群") | ||
| 28 | + private String applyContent; | ||
| 29 | + | ||
| 30 | + @Schema(description = "处理理由(拒绝时可选填)", example = "暂不通过") | ||
| 31 | + private String handleContent; | ||
| 32 | + | ||
| 33 | + @Schema(description = "处理人用户编号", example = "31460") | ||
| 34 | + private Long handleUserId; | ||
| 35 | + | ||
| 36 | + @Schema(description = "加入来源", example = "1") | ||
| 37 | + private Integer addSource; // 参见 ImGroupAddSourceEnum 枚举 | ||
| 38 | + | ||
| 39 | + @Schema(description = "处理时间") | ||
| 40 | + private LocalDateTime handleTime; | ||
| 41 | + | ||
| 42 | + @Schema(description = "申请创建时间") | ||
| 43 | + private LocalDateTime createTime; | ||
| 44 | + | ||
| 45 | + // ========== 下面是聚合字段,方便前端显示 ========== | ||
| 46 | + | ||
| 47 | + @Schema(description = "申请人 / 被邀请人昵称(实时聚合自 AdminUser)", example = "芋道") | ||
| 48 | + private String userNickname; | ||
| 49 | + | ||
| 50 | + @Schema(description = "申请人 / 被邀请人头像(实时聚合自 AdminUser)") | ||
| 51 | + private String userAvatar; | ||
| 52 | + | ||
| 53 | + @Schema(description = "邀请人昵称(实时聚合自 AdminUser)", example = "老张") | ||
| 54 | + private String inviterNickname; | ||
| 55 | + | ||
| 56 | + @Schema(description = "邀请人头像(实时聚合自 AdminUser)") | ||
| 57 | + private String inviterAvatar; | ||
| 58 | + | ||
| 59 | + @Schema(description = "群名称(实时聚合自 ImGroup)", example = "芋道技术交流群") | ||
| 60 | + private String groupName; | ||
| 61 | + | ||
| 62 | + @Schema(description = "群头像(实时聚合自 ImGroup)") | ||
| 63 | + private String groupAvatar; | ||
| 64 | + | ||
| 65 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/manager/group/ImGroupRequestManagerController.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.controller.admin.manager.group; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 4 | +import cn.iocoder.yudao.framework.common.pojo.CommonResult; | ||
| 5 | +import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 6 | +import cn.iocoder.yudao.framework.common.util.collection.MapUtils; | ||
| 7 | +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | ||
| 8 | +import cn.iocoder.yudao.module.im.controller.admin.manager.group.vo.ImGroupRequestManagerPageReqVO; | ||
| 9 | +import cn.iocoder.yudao.module.im.controller.admin.manager.group.vo.ImGroupRequestManagerRespVO; | ||
| 10 | +import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupDO; | ||
| 11 | +import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupRequestDO; | ||
| 12 | +import cn.iocoder.yudao.module.im.service.group.ImGroupRequestService; | ||
| 13 | +import cn.iocoder.yudao.module.im.service.group.ImGroupService; | ||
| 14 | +import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | ||
| 15 | +import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | ||
| 16 | +import io.swagger.v3.oas.annotations.Operation; | ||
| 17 | +import io.swagger.v3.oas.annotations.tags.Tag; | ||
| 18 | +import jakarta.annotation.Resource; | ||
| 19 | +import jakarta.validation.Valid; | ||
| 20 | +import org.springframework.security.access.prepost.PreAuthorize; | ||
| 21 | +import org.springframework.validation.annotation.Validated; | ||
| 22 | +import org.springframework.web.bind.annotation.GetMapping; | ||
| 23 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
| 24 | +import org.springframework.web.bind.annotation.RestController; | ||
| 25 | + | ||
| 26 | +import java.util.Map; | ||
| 27 | +import java.util.Objects; | ||
| 28 | +import java.util.Set; | ||
| 29 | +import java.util.stream.Stream; | ||
| 30 | + | ||
| 31 | +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | ||
| 32 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | ||
| 33 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSetByFlatMap; | ||
| 34 | + | ||
| 35 | +@Tag(name = "管理后台 - IM 加群申请管理") | ||
| 36 | +@RestController | ||
| 37 | +@RequestMapping("/im/manager/group-request") | ||
| 38 | +@Validated | ||
| 39 | +public class ImGroupRequestManagerController { | ||
| 40 | + | ||
| 41 | + @Resource | ||
| 42 | + private ImGroupRequestService groupRequestService; | ||
| 43 | + @Resource | ||
| 44 | + private ImGroupService groupService; | ||
| 45 | + @Resource | ||
| 46 | + private AdminUserApi adminUserApi; | ||
| 47 | + | ||
| 48 | + @GetMapping("/page") | ||
| 49 | + @Operation(summary = "获得加群申请分页") | ||
| 50 | + @PreAuthorize("@ss.hasPermission('im:manager:group-request:query')") | ||
| 51 | + public CommonResult<PageResult<ImGroupRequestManagerRespVO>> getGroupRequestPage( | ||
| 52 | + @Valid ImGroupRequestManagerPageReqVO pageReqVO) { | ||
| 53 | + // 1. 分页查询 | ||
| 54 | + PageResult<ImGroupRequestDO> pageResult = groupRequestService.getGroupRequestPage(pageReqVO); | ||
| 55 | + if (CollUtil.isEmpty(pageResult.getList())) { | ||
| 56 | + return success(PageResult.empty(pageResult.getTotal())); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + // 2.1 批量聚合 user / inviter / handler 用户昵称 | ||
| 60 | + Set<Long> userIds = convertSetByFlatMap(pageResult.getList(), | ||
| 61 | + request -> Stream.of(request.getUserId(), request.getInviterUserId(), request.getHandleUserId()) | ||
| 62 | + .filter(Objects::nonNull)); | ||
| 63 | + Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds); | ||
| 64 | + // 2.2 批量聚合群信息(取群名) | ||
| 65 | + Set<Long> groupIds = convertSet(pageResult.getList(), ImGroupRequestDO::getGroupId); | ||
| 66 | + Map<Long, ImGroupDO> groupMap = groupService.getGroupMap(groupIds); | ||
| 67 | + return success(BeanUtils.toBean(pageResult, ImGroupRequestManagerRespVO.class, vo -> { | ||
| 68 | + MapUtils.findAndThen(userMap, vo.getUserId(), user -> vo.setUserNickname(user.getNickname())); | ||
| 69 | + MapUtils.findAndThen(userMap, vo.getInviterUserId(), user -> vo.setInviterNickname(user.getNickname())); | ||
| 70 | + MapUtils.findAndThen(userMap, vo.getHandleUserId(), user -> vo.setHandleNickname(user.getNickname())); | ||
| 71 | + MapUtils.findAndThen(groupMap, vo.getGroupId(), group -> vo.setGroupName(group.getName())); | ||
| 72 | + })); | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/manager/group/vo/ImGroupRequestManagerPageReqVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.controller.admin.manager.group.vo; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.pojo.PageParam; | ||
| 4 | +import io.swagger.v3.oas.annotations.media.Schema; | ||
| 5 | +import lombok.Data; | ||
| 6 | +import lombok.EqualsAndHashCode; | ||
| 7 | +import org.springframework.format.annotation.DateTimeFormat; | ||
| 8 | + | ||
| 9 | +import java.time.LocalDateTime; | ||
| 10 | + | ||
| 11 | +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | ||
| 12 | + | ||
| 13 | +@Schema(description = "管理后台 - IM 加群申请分页 Request VO") | ||
| 14 | +@Data | ||
| 15 | +@EqualsAndHashCode(callSuper = true) | ||
| 16 | +public class ImGroupRequestManagerPageReqVO extends PageParam { | ||
| 17 | + | ||
| 18 | + @Schema(description = "群编号", example = "1024") | ||
| 19 | + private Long groupId; | ||
| 20 | + | ||
| 21 | + @Schema(description = "申请人 / 被邀请人用户编号", example = "2048") | ||
| 22 | + private Long userId; | ||
| 23 | + | ||
| 24 | + @Schema(description = "邀请人用户编号", example = "31460") | ||
| 25 | + private Long inviterUserId; | ||
| 26 | + | ||
| 27 | + @Schema(description = "处理结果", example = "0") | ||
| 28 | + private Integer handleResult; // 参见 ImGroupRequestHandleResultEnum 枚举类 | ||
| 29 | + | ||
| 30 | + @Schema(description = "加入来源", example = "1") | ||
| 31 | + private Integer addSource; // 参见 ImGroupAddSourceEnum 枚举类 | ||
| 32 | + | ||
| 33 | + @Schema(description = "创建时间", example = "[2026-04-01 00:00:00, 2026-04-30 23:59:59]") | ||
| 34 | + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) | ||
| 35 | + private LocalDateTime[] createTime; | ||
| 36 | + | ||
| 37 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/manager/group/vo/ImGroupRequestManagerRespVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.controller.admin.manager.group.vo; | ||
| 2 | + | ||
| 3 | +import io.swagger.v3.oas.annotations.media.Schema; | ||
| 4 | +import lombok.Data; | ||
| 5 | + | ||
| 6 | +import java.time.LocalDateTime; | ||
| 7 | + | ||
| 8 | +@Schema(description = "管理后台 - IM 加群申请 Response VO") | ||
| 9 | +@Data | ||
| 10 | +public class ImGroupRequestManagerRespVO { | ||
| 11 | + | ||
| 12 | + @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") | ||
| 13 | + private Long id; | ||
| 14 | + | ||
| 15 | + @Schema(description = "群编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") | ||
| 16 | + private Long groupId; | ||
| 17 | + | ||
| 18 | + @Schema(description = "群名称", example = "芋道技术交流群") | ||
| 19 | + private String groupName; | ||
| 20 | + | ||
| 21 | + @Schema(description = "申请人 / 被邀请人用户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "100") | ||
| 22 | + private Long userId; | ||
| 23 | + | ||
| 24 | + @Schema(description = "申请人 / 被邀请人昵称", example = "张三") | ||
| 25 | + private String userNickname; | ||
| 26 | + | ||
| 27 | + @Schema(description = "邀请人用户编号;NULL 表示用户主动申请", example = "200") | ||
| 28 | + private Long inviterUserId; | ||
| 29 | + | ||
| 30 | + @Schema(description = "邀请人昵称", example = "老张") | ||
| 31 | + private String inviterNickname; | ||
| 32 | + | ||
| 33 | + @Schema(description = "申请理由", example = "我想加入这个群") | ||
| 34 | + private String applyContent; | ||
| 35 | + | ||
| 36 | + @Schema(description = "加入来源", example = "1") | ||
| 37 | + private Integer addSource; // 参见 ImGroupAddSourceEnum 枚举类 | ||
| 38 | + | ||
| 39 | + @Schema(description = "处理结果", requiredMode = Schema.RequiredMode.REQUIRED, example = "0") | ||
| 40 | + private Integer handleResult; // 参见 ImGroupRequestHandleResultEnum 枚举类 | ||
| 41 | + | ||
| 42 | + @Schema(description = "处理人用户编号", example = "31460") | ||
| 43 | + private Long handleUserId; | ||
| 44 | + | ||
| 45 | + @Schema(description = "处理人昵称", example = "管理员") | ||
| 46 | + private String handleNickname; | ||
| 47 | + | ||
| 48 | + @Schema(description = "处理理由", example = "暂不通过") | ||
| 49 | + private String handleContent; | ||
| 50 | + | ||
| 51 | + @Schema(description = "处理时间") | ||
| 52 | + private LocalDateTime handleTime; | ||
| 53 | + | ||
| 54 | + @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) | ||
| 55 | + private LocalDateTime createTime; | ||
| 56 | + | ||
| 57 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/dal/dataobject/group/ImGroupDO.java
| @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.im.dal.dataobject.group; | @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.im.dal.dataobject.group; | ||
| 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 4 | import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; | 4 | import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; |
| 5 | import cn.iocoder.yudao.framework.mybatis.core.type.LongListTypeHandler; | 5 | import cn.iocoder.yudao.framework.mybatis.core.type.LongListTypeHandler; |
| 6 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupJoinTypeEnum; | ||
| 6 | import com.baomidou.mybatisplus.annotation.KeySequence; | 7 | import com.baomidou.mybatisplus.annotation.KeySequence; |
| 7 | import com.baomidou.mybatisplus.annotation.TableField; | 8 | import com.baomidou.mybatisplus.annotation.TableField; |
| 8 | import com.baomidou.mybatisplus.annotation.TableId; | 9 | import com.baomidou.mybatisplus.annotation.TableId; |
| @@ -51,6 +52,12 @@ public class ImGroupDO extends BaseDO { | @@ -51,6 +52,12 @@ public class ImGroupDO extends BaseDO { | ||
| 51 | */ | 52 | */ |
| 52 | private String notice; | 53 | private String notice; |
| 53 | /** | 54 | /** |
| 55 | + * 加群方式 | ||
| 56 | + * <p> | ||
| 57 | + * 枚举 {@link ImGroupJoinTypeEnum} | ||
| 58 | + */ | ||
| 59 | + private Integer joinType; | ||
| 60 | + /** | ||
| 54 | * 是否封禁 | 61 | * 是否封禁 |
| 55 | */ | 62 | */ |
| 56 | private Boolean banned; | 63 | private Boolean banned; |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/dal/dataobject/group/ImGroupMemberDO.java
| @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.im.dal.dataobject.group; | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.im.dal.dataobject.group; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; | 3 | import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; |
| 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupAddSourceEnum; | ||
| 5 | import cn.iocoder.yudao.module.im.enums.group.ImGroupMemberRoleEnum; | 6 | import cn.iocoder.yudao.module.im.enums.group.ImGroupMemberRoleEnum; |
| 6 | import com.baomidou.mybatisplus.annotation.KeySequence; | 7 | import com.baomidou.mybatisplus.annotation.KeySequence; |
| 7 | import com.baomidou.mybatisplus.annotation.TableId; | 8 | import com.baomidou.mybatisplus.annotation.TableId; |
| @@ -71,6 +72,18 @@ public class ImGroupMemberDO extends BaseDO { | @@ -71,6 +72,18 @@ public class ImGroupMemberDO extends BaseDO { | ||
| 71 | */ | 72 | */ |
| 72 | private LocalDateTime joinTime; | 73 | private LocalDateTime joinTime; |
| 73 | /** | 74 | /** |
| 75 | + * 加入来源 | ||
| 76 | + * <p> | ||
| 77 | + * 枚举 {@link ImGroupAddSourceEnum} | ||
| 78 | + */ | ||
| 79 | + private Integer addSource; | ||
| 80 | + /** | ||
| 81 | + * 邀请人用户编号 | ||
| 82 | + * <p> | ||
| 83 | + * 关联 AdminUserDO 的 id 字段;用户主动申请进群时为 NULL | ||
| 84 | + */ | ||
| 85 | + private Long inviterUserId; | ||
| 86 | + /** | ||
| 74 | * 退群时间 | 87 | * 退群时间 |
| 75 | */ | 88 | */ |
| 76 | private LocalDateTime quitTime; | 89 | private LocalDateTime quitTime; |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/dal/dataobject/group/ImGroupRequestDO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.dal.dataobject.group; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; | ||
| 4 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupAddSourceEnum; | ||
| 5 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupRequestHandleResultEnum; | ||
| 6 | +import com.baomidou.mybatisplus.annotation.KeySequence; | ||
| 7 | +import com.baomidou.mybatisplus.annotation.TableId; | ||
| 8 | +import com.baomidou.mybatisplus.annotation.TableName; | ||
| 9 | +import lombok.*; | ||
| 10 | + | ||
| 11 | +import java.time.LocalDateTime; | ||
| 12 | + | ||
| 13 | +/** | ||
| 14 | + * IM 加群申请记录 DO | ||
| 15 | + * <p> | ||
| 16 | + * 配合「申请 - 审批」流程: | ||
| 17 | + * <ul> | ||
| 18 | + * <li>用户主动申请:调 apply 接口落库(inviterUserId=null,handleResult=UNHANDLED),群主 / 管理员审批</li> | ||
| 19 | + * <li>普通成员邀请:群 joinType=APPLY_AND_NORMAL_INVITE 时落库(inviterUserId=操作人),群主 / 管理员审批</li> | ||
| 20 | + * <li>处理:群主 / 管理员调 agree / refuse 推进状态机;同意时把 addSource / inviterUserId 同步写入 {@link ImGroupMemberDO}</li> | ||
| 21 | + * </ul> | ||
| 22 | + * | ||
| 23 | + * @author 芋道源码 | ||
| 24 | + */ | ||
| 25 | +@TableName("im_group_request") | ||
| 26 | +@KeySequence("im_group_request_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增;MySQL 等可不写 | ||
| 27 | +@Data | ||
| 28 | +@EqualsAndHashCode(callSuper = true) | ||
| 29 | +@ToString(callSuper = true) | ||
| 30 | +@Builder | ||
| 31 | +@NoArgsConstructor | ||
| 32 | +@AllArgsConstructor | ||
| 33 | +public class ImGroupRequestDO extends BaseDO { | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * 编号 | ||
| 37 | + */ | ||
| 38 | + @TableId | ||
| 39 | + private Long id; | ||
| 40 | + /** | ||
| 41 | + * 群编号 | ||
| 42 | + * <p> | ||
| 43 | + * 关联 {@link ImGroupDO#getId()} | ||
| 44 | + */ | ||
| 45 | + private Long groupId; | ||
| 46 | + /** | ||
| 47 | + * 申请人 / 被邀请人用户编号 | ||
| 48 | + * <p> | ||
| 49 | + * 关联 AdminUserDO 的 id 字段 | ||
| 50 | + */ | ||
| 51 | + private Long userId; | ||
| 52 | + /** | ||
| 53 | + * 邀请人用户编号 | ||
| 54 | + * <p> | ||
| 55 | + * NULL 表示用户主动申请进群;非 NULL 表示由群成员邀请、待群主 / 管理员审批 | ||
| 56 | + */ | ||
| 57 | + private Long inviterUserId; | ||
| 58 | + | ||
| 59 | + // ========== 申请发起阶段 ========== | ||
| 60 | + /** | ||
| 61 | + * 申请理由 | ||
| 62 | + */ | ||
| 63 | + private String applyContent; | ||
| 64 | + /** | ||
| 65 | + * 加入来源 | ||
| 66 | + * <p> | ||
| 67 | + * 枚举 {@link ImGroupAddSourceEnum};同意时同步写入 {@link ImGroupMemberDO#getAddSource()} | ||
| 68 | + */ | ||
| 69 | + private Integer addSource; | ||
| 70 | + | ||
| 71 | + // ========== 申请处理阶段 ========== | ||
| 72 | + /** | ||
| 73 | + * 处理结果 | ||
| 74 | + * <p> | ||
| 75 | + * 枚举 {@link ImGroupRequestHandleResultEnum} | ||
| 76 | + */ | ||
| 77 | + private Integer handleResult; | ||
| 78 | + /** | ||
| 79 | + * 处理人用户编号(群主或管理员) | ||
| 80 | + */ | ||
| 81 | + private Long handleUserId; | ||
| 82 | + /** | ||
| 83 | + * 处理理由(拒绝时可选填) | ||
| 84 | + */ | ||
| 85 | + private String handleContent; | ||
| 86 | + /** | ||
| 87 | + * 处理时间 | ||
| 88 | + */ | ||
| 89 | + private LocalDateTime handleTime; | ||
| 90 | + | ||
| 91 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/dal/mysql/group/ImGroupRequestMapper.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.dal.mysql.group; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 4 | +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | ||
| 5 | +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | ||
| 6 | +import cn.iocoder.yudao.module.im.controller.admin.manager.group.vo.ImGroupRequestManagerPageReqVO; | ||
| 7 | +import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupRequestDO; | ||
| 8 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupRequestHandleResultEnum; | ||
| 9 | +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | ||
| 10 | +import org.apache.ibatis.annotations.Mapper; | ||
| 11 | + | ||
| 12 | +import java.util.List; | ||
| 13 | + | ||
| 14 | +/** | ||
| 15 | + * IM 加群申请记录 Mapper | ||
| 16 | + * | ||
| 17 | + * @author 芋道源码 | ||
| 18 | + */ | ||
| 19 | +@Mapper | ||
| 20 | +public interface ImGroupRequestMapper extends BaseMapperX<ImGroupRequestDO> { | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * 取最新一条未处理申请;inviterUserId 为 null 表示主动申请 | ||
| 24 | + * <p> | ||
| 25 | + * 主动申请、不同邀请人发起的邀请申请彼此互不覆盖,复用键含 inviterUserId | ||
| 26 | + */ | ||
| 27 | + // TODO @AI:一般合理的,是不是可以覆盖???? | ||
| 28 | + default ImGroupRequestDO selectLatestPendingByGroupIdAndUserIdAndInviter( | ||
| 29 | + Long groupId, Long userId, Long inviterUserId) { | ||
| 30 | + LambdaQueryWrapperX<ImGroupRequestDO> wrapper = new LambdaQueryWrapperX<>(); | ||
| 31 | + wrapper.eq(ImGroupRequestDO::getGroupId, groupId) | ||
| 32 | + .eq(ImGroupRequestDO::getUserId, userId) | ||
| 33 | + .eq(ImGroupRequestDO::getHandleResult, ImGroupRequestHandleResultEnum.UNHANDLED.getResult()); | ||
| 34 | + if (inviterUserId == null) { | ||
| 35 | + wrapper.isNull(ImGroupRequestDO::getInviterUserId); | ||
| 36 | + } else { | ||
| 37 | + wrapper.eq(ImGroupRequestDO::getInviterUserId, inviterUserId); | ||
| 38 | + } | ||
| 39 | + return selectOne(wrapper.orderByDesc(ImGroupRequestDO::getId).last("LIMIT 1")); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * 拉取「我相关」的加群申请列表(含我主动申请、我被邀请待审);游标分页 | ||
| 44 | + */ | ||
| 45 | + // TODO @AI:selectListByMy;保持风格一致; | ||
| 46 | + default List<ImGroupRequestDO> selectMyList(Long userId, Long lastRequestId, int limit) { | ||
| 47 | + return selectList(new LambdaQueryWrapperX<ImGroupRequestDO>() | ||
| 48 | + .eq(ImGroupRequestDO::getUserId, userId) | ||
| 49 | + .ltIfPresent(ImGroupRequestDO::getId, lastRequestId) | ||
| 50 | + .orderByDesc(ImGroupRequestDO::getId) | ||
| 51 | + .last("LIMIT " + limit)); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + // TODO @AI:类似下面的注释,减少 mapper 的业务型; | ||
| 55 | + default List<ImGroupRequestDO> selectPendingListByGroupId(Long groupId, Long lastRequestId, int limit) { | ||
| 56 | + return selectList(new LambdaQueryWrapperX<ImGroupRequestDO>() | ||
| 57 | + .eq(ImGroupRequestDO::getGroupId, groupId) | ||
| 58 | + .eq(ImGroupRequestDO::getHandleResult, ImGroupRequestHandleResultEnum.UNHANDLED.getResult()) | ||
| 59 | + .ltIfPresent(ImGroupRequestDO::getId, lastRequestId) | ||
| 60 | + .orderByDesc(ImGroupRequestDO::getId) | ||
| 61 | + .last("LIMIT " + limit)); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + // TODO @AI:应该 bygroupidandhandleResult,减少不必要的条件 | ||
| 65 | + default Long selectPendingCountByGroupId(Long groupId) { | ||
| 66 | + return selectCount(new LambdaQueryWrapperX<ImGroupRequestDO>() | ||
| 67 | + .eq(ImGroupRequestDO::getGroupId, groupId) | ||
| 68 | + .eq(ImGroupRequestDO::getHandleResult, ImGroupRequestHandleResultEnum.UNHANDLED.getResult())); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + default int updateByIdAndHandleResult(Long id, Integer handleResult, ImGroupRequestDO updateObj) { | ||
| 72 | + return update(updateObj, new LambdaUpdateWrapper<ImGroupRequestDO>() | ||
| 73 | + .eq(ImGroupRequestDO::getId, id).eq(ImGroupRequestDO::getHandleResult, handleResult)); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + default PageResult<ImGroupRequestDO> selectPage(ImGroupRequestManagerPageReqVO reqVO) { | ||
| 77 | + return selectPage(reqVO, new LambdaQueryWrapperX<ImGroupRequestDO>() | ||
| 78 | + .eqIfPresent(ImGroupRequestDO::getGroupId, reqVO.getGroupId()) | ||
| 79 | + .eqIfPresent(ImGroupRequestDO::getUserId, reqVO.getUserId()) | ||
| 80 | + .eqIfPresent(ImGroupRequestDO::getInviterUserId, reqVO.getInviterUserId()) | ||
| 81 | + .eqIfPresent(ImGroupRequestDO::getHandleResult, reqVO.getHandleResult()) | ||
| 82 | + .eqIfPresent(ImGroupRequestDO::getAddSource, reqVO.getAddSource()) | ||
| 83 | + .betweenIfPresent(ImGroupRequestDO::getCreateTime, reqVO.getCreateTime()) | ||
| 84 | + .orderByDesc(ImGroupRequestDO::getId)); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/enums/ErrorCodeConstants.java
| @@ -53,6 +53,12 @@ public interface ErrorCodeConstants { | @@ -53,6 +53,12 @@ public interface ErrorCodeConstants { | ||
| 53 | ErrorCode FRIEND_NOT_BLOCKED = new ErrorCode(1_040_600_003, "对方未在黑名单中"); | 53 | ErrorCode FRIEND_NOT_BLOCKED = new ErrorCode(1_040_600_003, "对方未在黑名单中"); |
| 54 | ErrorCode FRIEND_BLOCKED_BY_PEER = new ErrorCode(1_040_600_004, "您已被对方拉入黑名单,无法发送消息"); | 54 | ErrorCode FRIEND_BLOCKED_BY_PEER = new ErrorCode(1_040_600_004, "您已被对方拉入黑名单,无法发送消息"); |
| 55 | 55 | ||
| 56 | + // ========== 加群申请 (1-040-510-000) ========== | ||
| 57 | + ErrorCode GROUP_REQUEST_NOT_EXISTS = new ErrorCode(1_040_510_001, "加群申请不存在"); | ||
| 58 | + ErrorCode GROUP_REQUEST_HANDLED = new ErrorCode(1_040_510_002, "加群申请已处理"); | ||
| 59 | + ErrorCode GROUP_REQUEST_NOT_TO_ME = new ErrorCode(1_040_510_003, "仅群主或管理员可处理加群申请"); | ||
| 60 | + ErrorCode GROUP_REQUEST_ALREADY_MEMBER = new ErrorCode(1_040_510_004, "您已在该群中,无需重复申请"); | ||
| 61 | + | ||
| 56 | // ========== 好友申请 (1-040-610-000) ========== | 62 | // ========== 好友申请 (1-040-610-000) ========== |
| 57 | ErrorCode FRIEND_REQUEST_NOT_EXISTS = new ErrorCode(1_040_610_001, "好友申请不存在"); | 63 | ErrorCode FRIEND_REQUEST_NOT_EXISTS = new ErrorCode(1_040_610_001, "好友申请不存在"); |
| 58 | ErrorCode FRIEND_REQUEST_HANDLED = new ErrorCode(1_040_610_002, "好友申请已处理"); | 64 | ErrorCode FRIEND_REQUEST_HANDLED = new ErrorCode(1_040_610_002, "好友申请已处理"); |
| @@ -64,4 +70,12 @@ public interface ErrorCodeConstants { | @@ -64,4 +70,12 @@ public interface ErrorCodeConstants { | ||
| 64 | ErrorCode SENSITIVE_WORD_NOT_EXISTS = new ErrorCode(1_040_700_000, "敏感词不存在"); | 70 | ErrorCode SENSITIVE_WORD_NOT_EXISTS = new ErrorCode(1_040_700_000, "敏感词不存在"); |
| 65 | ErrorCode SENSITIVE_WORD_DUPLICATED = new ErrorCode(1_040_700_001, "敏感词 '{}' 已存在"); | 71 | ErrorCode SENSITIVE_WORD_DUPLICATED = new ErrorCode(1_040_700_001, "敏感词 '{}' 已存在"); |
| 66 | 72 | ||
| 73 | + // ========== 表情包 (1-040-800-000) ========== | ||
| 74 | + ErrorCode FACE_PACK_NOT_EXISTS = new ErrorCode(1_040_800_000, "表情包不存在"); | ||
| 75 | + ErrorCode FACE_PACK_HAS_ITEMS = new ErrorCode(1_040_800_001, "表情包下还有表情,无法删除"); | ||
| 76 | + ErrorCode FACE_PACK_ITEM_NOT_EXISTS = new ErrorCode(1_040_800_002, "表情不存在"); | ||
| 77 | + ErrorCode FACE_USER_ITEM_NOT_EXISTS = new ErrorCode(1_040_800_010, "个人表情不存在"); | ||
| 78 | + ErrorCode FACE_USER_ITEM_NOT_OWN = new ErrorCode(1_040_800_011, "不能操作他人的表情"); | ||
| 79 | + ErrorCode FACE_USER_ITEM_DUPLICATED = new ErrorCode(1_040_800_013, "该表情已添加到个人表情"); | ||
| 80 | + | ||
| 67 | } | 81 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/enums/group/ImGroupAddSourceEnum.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.enums.group; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.core.ArrayValuable; | ||
| 4 | +import lombok.Getter; | ||
| 5 | +import lombok.RequiredArgsConstructor; | ||
| 6 | + | ||
| 7 | +import java.util.Arrays; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * IM 加入群聊来源枚举 | ||
| 11 | + * <p> | ||
| 12 | + * 由发起方在申请 / 邀请时传入;同意后同步写入 ImGroupMemberDO 的 addSource 字段 | ||
| 13 | + * | ||
| 14 | + * @author 芋道源码 | ||
| 15 | + */ | ||
| 16 | +@RequiredArgsConstructor | ||
| 17 | +@Getter | ||
| 18 | +public enum ImGroupAddSourceEnum implements ArrayValuable<Integer> { | ||
| 19 | + | ||
| 20 | + // TODO @AI:没实现的方式,记得在后面加个 TODO @芋艿:未实现,原因。。。。 | ||
| 21 | + SEARCH(1, "搜索"), | ||
| 22 | + INVITE(2, "邀请"), | ||
| 23 | + QR_CODE(3, "扫码"), | ||
| 24 | + SHARE_LINK(4, "分享链接"); | ||
| 25 | + | ||
| 26 | + public static final Integer[] ARRAYS = Arrays.stream(values()).map(ImGroupAddSourceEnum::getSource).toArray(Integer[]::new); | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * 来源 | ||
| 30 | + */ | ||
| 31 | + private final Integer source; | ||
| 32 | + /** | ||
| 33 | + * 名字 | ||
| 34 | + */ | ||
| 35 | + private final String name; | ||
| 36 | + | ||
| 37 | + @Override | ||
| 38 | + public Integer[] array() { | ||
| 39 | + return ARRAYS; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/enums/group/ImGroupJoinTypeEnum.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.enums.group; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.core.ArrayValuable; | ||
| 4 | +import lombok.Getter; | ||
| 5 | +import lombok.RequiredArgsConstructor; | ||
| 6 | + | ||
| 7 | +import java.util.Arrays; | ||
| 8 | +import java.util.Objects; | ||
| 9 | + | ||
| 10 | +// TODO @AI:这里是不是对齐微信;申请只有需要审批,和不需要审批。(不然感觉有点复杂、难理解?!) | ||
| 11 | +/** | ||
| 12 | + * IM 群加群方式枚举 | ||
| 13 | + * <p> | ||
| 14 | + * 控制「外部用户主动申请」和「群成员邀请」两条入群路径是否需要群主 / 管理员审批 | ||
| 15 | + * <ul> | ||
| 16 | + * <li>{@link #FREE}:用户搜到群可直接进群;任何成员邀请也直进</li> | ||
| 17 | + * <li>{@link #APPLY}:用户申请进群需群主 / 管理员审批;任何成员邀请仍直进</li> | ||
| 18 | + * <li>{@link #APPLY_AND_NORMAL_INVITE}:用户申请需审批;普通成员邀请也需审批;群主 / 管理员邀请直进</li> | ||
| 19 | + * </ul> | ||
| 20 | + * | ||
| 21 | + * @author 芋道源码 | ||
| 22 | + */ | ||
| 23 | +@RequiredArgsConstructor | ||
| 24 | +@Getter | ||
| 25 | +public enum ImGroupJoinTypeEnum implements ArrayValuable<Integer> { | ||
| 26 | + | ||
| 27 | + FREE(0, "自由进群"), | ||
| 28 | + APPLY(1, "申请需审批"), | ||
| 29 | + APPLY_AND_NORMAL_INVITE(2, "申请、及普通成员邀请均需审批"); | ||
| 30 | + | ||
| 31 | + public static final Integer[] ARRAYS = Arrays.stream(values()).map(ImGroupJoinTypeEnum::getType).toArray(Integer[]::new); | ||
| 32 | + | ||
| 33 | + /** | ||
| 34 | + * 类型 | ||
| 35 | + */ | ||
| 36 | + private final Integer type; | ||
| 37 | + /** | ||
| 38 | + * 名字 | ||
| 39 | + */ | ||
| 40 | + private final String name; | ||
| 41 | + | ||
| 42 | + @Override | ||
| 43 | + public Integer[] array() { | ||
| 44 | + return ARRAYS; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * 是否「自由进群」;自由进群路径无需创建申请记录 | ||
| 49 | + */ | ||
| 50 | + public static boolean isFree(Integer type) { | ||
| 51 | + return Objects.equals(FREE.type, type); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * 普通成员邀请是否需要审批 | ||
| 56 | + */ | ||
| 57 | + public static boolean isInviteNeedApproval(Integer type) { | ||
| 58 | + return Objects.equals(APPLY_AND_NORMAL_INVITE.type, type); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/enums/group/ImGroupRequestHandleResultEnum.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.enums.group; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.core.ArrayValuable; | ||
| 4 | +import lombok.Getter; | ||
| 5 | +import lombok.RequiredArgsConstructor; | ||
| 6 | + | ||
| 7 | +import java.util.Arrays; | ||
| 8 | +import java.util.Objects; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * IM 加群申请处理结果枚举 | ||
| 12 | + * <p> | ||
| 13 | + * 取值与 {@link cn.iocoder.yudao.module.im.enums.friend.ImFriendRequestHandleResultEnum} 平行,便于复用心智模型 | ||
| 14 | + * | ||
| 15 | + * @author 芋道源码 | ||
| 16 | + */ | ||
| 17 | +@RequiredArgsConstructor | ||
| 18 | +@Getter | ||
| 19 | +public enum ImGroupRequestHandleResultEnum implements ArrayValuable<Integer> { | ||
| 20 | + | ||
| 21 | + UNHANDLED(0, "未处理"), | ||
| 22 | + AGREED(1, "同意"), | ||
| 23 | + REFUSED(2, "拒绝"); | ||
| 24 | + | ||
| 25 | + public static final Integer[] ARRAYS = Arrays.stream(values()).map(ImGroupRequestHandleResultEnum::getResult).toArray(Integer[]::new); | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * 结果 | ||
| 29 | + */ | ||
| 30 | + private final Integer result; | ||
| 31 | + /** | ||
| 32 | + * 名字 | ||
| 33 | + */ | ||
| 34 | + private final String name; | ||
| 35 | + | ||
| 36 | + @Override | ||
| 37 | + public Integer[] array() { | ||
| 38 | + return ARRAYS; | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + /** | ||
| 42 | + * 判断申请是否还未处理 | ||
| 43 | + */ | ||
| 44 | + public static boolean isUnhandled(Integer result) { | ||
| 45 | + return Objects.equals(UNHANDLED.result, result); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/group/ImGroupMemberService.java
| @@ -111,6 +111,21 @@ public interface ImGroupMemberService { | @@ -111,6 +111,21 @@ public interface ImGroupMemberService { | ||
| 111 | ImGroupMemberDO addGroupMember(Long groupId, Long userId, Integer role); | 111 | ImGroupMemberDO addGroupMember(Long groupId, Long userId, Integer role); |
| 112 | 112 | ||
| 113 | /** | 113 | /** |
| 114 | + * 添加群成员(入群),并指定角色 / 加入来源 / 邀请人 | ||
| 115 | + * <p> | ||
| 116 | + * 重置旧成员行也会强制重置 role / addSource / inviterUserId,确保留痕反映「本次入群」事件 | ||
| 117 | + * | ||
| 118 | + * @param groupId 群编号 | ||
| 119 | + * @param userId 用户编号 | ||
| 120 | + * @param role 成员角色 | ||
| 121 | + * @param addSource 加入来源,见 {@link cn.iocoder.yudao.module.im.enums.group.ImGroupAddSourceEnum} | ||
| 122 | + * @param inviterUserId 邀请人用户编号;NULL 表示主动申请 | ||
| 123 | + * @return 群成员记录 | ||
| 124 | + */ | ||
| 125 | + @SuppressWarnings("UnusedReturnValue") | ||
| 126 | + ImGroupMemberDO addGroupMember(Long groupId, Long userId, Integer role, Integer addSource, Long inviterUserId); | ||
| 127 | + | ||
| 128 | + /** | ||
| 114 | * 批量添加群成员(入群) | 129 | * 批量添加群成员(入群) |
| 115 | * | 130 | * |
| 116 | * @param groupId 群编号 | 131 | * @param groupId 群编号 |
| @@ -119,6 +134,16 @@ public interface ImGroupMemberService { | @@ -119,6 +134,16 @@ public interface ImGroupMemberService { | ||
| 119 | void addGroupMembers(Long groupId, Collection<Long> userIds); | 134 | void addGroupMembers(Long groupId, Collection<Long> userIds); |
| 120 | 135 | ||
| 121 | /** | 136 | /** |
| 137 | + * 批量添加群成员(入群),统一携带加入来源 / 邀请人 | ||
| 138 | + * | ||
| 139 | + * @param groupId 群编号 | ||
| 140 | + * @param userIds 用户编号集合 | ||
| 141 | + * @param addSource 加入来源 | ||
| 142 | + * @param inviterUserId 邀请人用户编号;NULL 表示主动申请 | ||
| 143 | + */ | ||
| 144 | + void addGroupMembers(Long groupId, Collection<Long> userIds, Integer addSource, Long inviterUserId); | ||
| 145 | + | ||
| 146 | + /** | ||
| 122 | * 校验用户是否为群的有效成员 | 147 | * 校验用户是否为群的有效成员 |
| 123 | * | 148 | * |
| 124 | * @param groupId 群编号 | 149 | * @param groupId 群编号 |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/group/ImGroupMemberServiceImpl.java
| @@ -103,18 +103,24 @@ public class ImGroupMemberServiceImpl implements ImGroupMemberService { | @@ -103,18 +103,24 @@ public class ImGroupMemberServiceImpl implements ImGroupMemberService { | ||
| 103 | 103 | ||
| 104 | @Override | 104 | @Override |
| 105 | public ImGroupMemberDO addGroupMember(Long groupId, Long userId) { | 105 | public ImGroupMemberDO addGroupMember(Long groupId, Long userId) { |
| 106 | - return addGroupMember(groupId, userId, ImGroupMemberRoleEnum.NORMAL.getRole()); | 106 | + return addGroupMember(groupId, userId, ImGroupMemberRoleEnum.NORMAL.getRole(), null, null); |
| 107 | + } | ||
| 108 | + | ||
| 109 | + @Override | ||
| 110 | + public ImGroupMemberDO addGroupMember(Long groupId, Long userId, Integer role) { | ||
| 111 | + return addGroupMember(groupId, userId, role, null, null); | ||
| 107 | } | 112 | } |
| 108 | 113 | ||
| 109 | /** | 114 | /** |
| 110 | * 并发安全:依靠 im_group_member 表的唯一索引 uk_im_group_member_group_user(group_id, user_id) 保证幂等, | 115 | * 并发安全:依靠 im_group_member 表的唯一索引 uk_im_group_member_group_user(group_id, user_id) 保证幂等, |
| 111 | * 当并发 insert 触发 {@link DuplicateKeyException} 时降级为 select + update。 | 116 | * 当并发 insert 触发 {@link DuplicateKeyException} 时降级为 select + update。 |
| 112 | * <p> | 117 | * <p> |
| 113 | - * 重置旧成员行时强制重置 role,避免离群期间残留管理员身份在重新入群后被复用。 | 118 | + * 重置旧成员行时强制重置 role / addSource / inviterUserId,确保留痕反映「本次入群」事件 |
| 114 | */ | 119 | */ |
| 115 | @Override | 120 | @Override |
| 116 | @CacheEvict(cacheNames = GROUP_MEMBER_IDS, key = "#groupId") | 121 | @CacheEvict(cacheNames = GROUP_MEMBER_IDS, key = "#groupId") |
| 117 | - public ImGroupMemberDO addGroupMember(Long groupId, Long userId, Integer role) { | 122 | + public ImGroupMemberDO addGroupMember(Long groupId, Long userId, Integer role, |
| 123 | + Integer addSource, Long inviterUserId) { | ||
| 118 | LocalDateTime now = LocalDateTime.now(); | 124 | LocalDateTime now = LocalDateTime.now(); |
| 119 | // 情况一:已存在记录 → 重置或跳过 | 125 | // 情况一:已存在记录 → 重置或跳过 |
| 120 | ImGroupMemberDO exists = groupMemberMapper.selectByGroupIdAndUserId(groupId, userId); | 126 | ImGroupMemberDO exists = groupMemberMapper.selectByGroupIdAndUserId(groupId, userId); |
| @@ -122,8 +128,9 @@ public class ImGroupMemberServiceImpl implements ImGroupMemberService { | @@ -122,8 +128,9 @@ public class ImGroupMemberServiceImpl implements ImGroupMemberService { | ||
| 122 | if (CommonStatusEnum.isDisable(exists.getStatus())) { | 128 | if (CommonStatusEnum.isDisable(exists.getStatus())) { |
| 123 | groupMemberMapper.updateById(new ImGroupMemberDO().setId(exists.getId()) | 129 | groupMemberMapper.updateById(new ImGroupMemberDO().setId(exists.getId()) |
| 124 | .setStatus(CommonStatusEnum.ENABLE.getStatus()).setJoinTime(now) | 130 | .setStatus(CommonStatusEnum.ENABLE.getStatus()).setJoinTime(now) |
| 125 | - .setRole(role)); | ||
| 126 | - exists.setStatus(CommonStatusEnum.ENABLE.getStatus()).setJoinTime(now).setRole(role); | 131 | + .setRole(role).setAddSource(addSource).setInviterUserId(inviterUserId)); |
| 132 | + exists.setStatus(CommonStatusEnum.ENABLE.getStatus()).setJoinTime(now).setRole(role) | ||
| 133 | + .setAddSource(addSource).setInviterUserId(inviterUserId); | ||
| 127 | } | 134 | } |
| 128 | return exists; | 135 | return exists; |
| 129 | } | 136 | } |
| @@ -131,7 +138,7 @@ public class ImGroupMemberServiceImpl implements ImGroupMemberService { | @@ -131,7 +138,7 @@ public class ImGroupMemberServiceImpl implements ImGroupMemberService { | ||
| 131 | ImGroupMemberDO member = new ImGroupMemberDO() | 138 | ImGroupMemberDO member = new ImGroupMemberDO() |
| 132 | .setGroupId(groupId).setUserId(userId) | 139 | .setGroupId(groupId).setUserId(userId) |
| 133 | .setStatus(CommonStatusEnum.ENABLE.getStatus()).setJoinTime(now) | 140 | .setStatus(CommonStatusEnum.ENABLE.getStatus()).setJoinTime(now) |
| 134 | - .setRole(role); | 141 | + .setRole(role).setAddSource(addSource).setInviterUserId(inviterUserId); |
| 135 | try { | 142 | try { |
| 136 | groupMemberMapper.insert(member); | 143 | groupMemberMapper.insert(member); |
| 137 | return member; | 144 | return member; |
| @@ -143,8 +150,13 @@ public class ImGroupMemberServiceImpl implements ImGroupMemberService { | @@ -143,8 +150,13 @@ public class ImGroupMemberServiceImpl implements ImGroupMemberService { | ||
| 143 | } | 150 | } |
| 144 | 151 | ||
| 145 | @Override | 152 | @Override |
| 146 | - @CacheEvict(cacheNames = GROUP_MEMBER_IDS, key = "#groupId") | ||
| 147 | public void addGroupMembers(Long groupId, Collection<Long> userIds) { | 153 | public void addGroupMembers(Long groupId, Collection<Long> userIds) { |
| 154 | + addGroupMembers(groupId, userIds, null, null); | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + @Override | ||
| 158 | + @CacheEvict(cacheNames = GROUP_MEMBER_IDS, key = "#groupId") | ||
| 159 | + public void addGroupMembers(Long groupId, Collection<Long> userIds, Integer addSource, Long inviterUserId) { | ||
| 148 | LocalDateTime now = LocalDateTime.now(); | 160 | LocalDateTime now = LocalDateTime.now(); |
| 149 | Integer role = ImGroupMemberRoleEnum.NORMAL.getRole(); | 161 | Integer role = ImGroupMemberRoleEnum.NORMAL.getRole(); |
| 150 | // 1.1 查询已有记录(含已退群的 DISABLE 记录) | 162 | // 1.1 查询已有记录(含已退群的 DISABLE 记录) |
| @@ -157,10 +169,12 @@ public class ImGroupMemberServiceImpl implements ImGroupMemberService { | @@ -157,10 +169,12 @@ public class ImGroupMemberServiceImpl implements ImGroupMemberService { | ||
| 157 | ImGroupMemberDO exist = existMap.get(userId); | 169 | ImGroupMemberDO exist = existMap.get(userId); |
| 158 | if (exist == null) { | 170 | if (exist == null) { |
| 159 | inserts.add(new ImGroupMemberDO().setGroupId(groupId).setUserId(userId) | 171 | inserts.add(new ImGroupMemberDO().setGroupId(groupId).setUserId(userId) |
| 160 | - .setStatus(CommonStatusEnum.ENABLE.getStatus()).setRole(role).setJoinTime(now)); | 172 | + .setStatus(CommonStatusEnum.ENABLE.getStatus()).setRole(role).setJoinTime(now) |
| 173 | + .setAddSource(addSource).setInviterUserId(inviterUserId)); | ||
| 161 | } else if (CommonStatusEnum.DISABLE.getStatus().equals(exist.getStatus())) { | 174 | } else if (CommonStatusEnum.DISABLE.getStatus().equals(exist.getStatus())) { |
| 162 | updates.add(new ImGroupMemberDO().setId(exist.getId()) | 175 | updates.add(new ImGroupMemberDO().setId(exist.getId()) |
| 163 | - .setStatus(CommonStatusEnum.ENABLE.getStatus()).setRole(role).setJoinTime(now)); | 176 | + .setStatus(CommonStatusEnum.ENABLE.getStatus()).setRole(role).setJoinTime(now) |
| 177 | + .setAddSource(addSource).setInviterUserId(inviterUserId)); | ||
| 164 | } | 178 | } |
| 165 | } | 179 | } |
| 166 | 180 | ||
| @@ -176,7 +190,7 @@ public class ImGroupMemberServiceImpl implements ImGroupMemberService { | @@ -176,7 +190,7 @@ public class ImGroupMemberServiceImpl implements ImGroupMemberService { | ||
| 176 | } catch (DuplicateKeyException e) { | 190 | } catch (DuplicateKeyException e) { |
| 177 | log.warn("[addGroupMembers][groupId({}) userIds({}) 批量插入冲突,降级为逐个处理]", groupId, userIds); | 191 | log.warn("[addGroupMembers][groupId({}) userIds({}) 批量插入冲突,降级为逐个处理]", groupId, userIds); |
| 178 | for (ImGroupMemberDO insert : inserts) { | 192 | for (ImGroupMemberDO insert : inserts) { |
| 179 | - addGroupMember(groupId, insert.getUserId()); | 193 | + addGroupMember(groupId, insert.getUserId(), role, addSource, inviterUserId); |
| 180 | } | 194 | } |
| 181 | } | 195 | } |
| 182 | } | 196 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/group/ImGroupRequestService.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.service.group; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 4 | +import cn.iocoder.yudao.module.im.controller.admin.group.vo.request.ImGroupRequestApplyReqVO; | ||
| 5 | +import cn.iocoder.yudao.module.im.controller.admin.manager.group.vo.ImGroupRequestManagerPageReqVO; | ||
| 6 | +import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupRequestDO; | ||
| 7 | +import jakarta.validation.Valid; | ||
| 8 | + | ||
| 9 | +import java.util.Collection; | ||
| 10 | +import java.util.List; | ||
| 11 | +import java.util.Map; | ||
| 12 | + | ||
| 13 | +/** | ||
| 14 | + * IM 加群申请 Service 接口 | ||
| 15 | + * | ||
| 16 | + * @author 芋道源码 | ||
| 17 | + */ | ||
| 18 | +public interface ImGroupRequestService { | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * 用户主动申请加群 | ||
| 22 | + * <p> | ||
| 23 | + * 群 joinType=FREE 时直接入群 + 1510 全员广播 + 1505 推送给申请人; | ||
| 24 | + * 否则落 im_group_request 待审批 + 1503 定向推送给群主 / 全部管理员 | ||
| 25 | + * | ||
| 26 | + * @param userId 申请人用户编号 | ||
| 27 | + * @param reqVO 申请请求 | ||
| 28 | + * @return 申请记录;自由进群直进时返回 null | ||
| 29 | + */ | ||
| 30 | + ImGroupRequestDO applyJoinGroup(Long userId, @Valid ImGroupRequestApplyReqVO reqVO); | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * 同意加群申请(群主或管理员) | ||
| 34 | + * <p> | ||
| 35 | + * 处理前校验入群人数上限;通过后写 group_member(带 addSource / inviterUserId) + 推送通知 | ||
| 36 | + * | ||
| 37 | + * @param userId 操作人用户编号 | ||
| 38 | + * @param requestId 申请记录编号 | ||
| 39 | + */ | ||
| 40 | + void agreeGroupRequest(Long userId, Long requestId); | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * 拒绝加群申请(群主或管理员) | ||
| 44 | + * | ||
| 45 | + * @param userId 操作人用户编号 | ||
| 46 | + * @param requestId 申请记录编号 | ||
| 47 | + * @param handleContent 拒绝理由 | ||
| 48 | + */ | ||
| 49 | + void refuseGroupRequest(Long userId, Long requestId, String handleContent); | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * 邀请创建审批申请(用于 inviteGroupMember 在 joinType=APPLY_AND_NORMAL_INVITE 且邀请人是普通成员时调用) | ||
| 53 | + * <p> | ||
| 54 | + * 每个被邀请人创建一条 inviter_user_id=操作人 的待审批记录;通知群主 / 全部管理员 | ||
| 55 | + * | ||
| 56 | + * @param groupId 群编号 | ||
| 57 | + * @param inviterUserId 邀请人用户编号 | ||
| 58 | + * @param invitedUserIds 被邀请人用户编号集合 | ||
| 59 | + */ | ||
| 60 | + void createInviteRequests(Long groupId, Long inviterUserId, Collection<Long> invitedUserIds); | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * 拉取「我相关」加群申请列表(含我主动申请、我被邀请待审);游标分页 | ||
| 64 | + * | ||
| 65 | + * @param userId 用户编号 | ||
| 66 | + * @param lastRequestId 当前列表最旧记录的 id;首页传 null | ||
| 67 | + * @param limit 单次拉取条数 | ||
| 68 | + * @return 申请记录列表,按 id 倒序 | ||
| 69 | + */ | ||
| 70 | + List<ImGroupRequestDO> getMyGroupRequestList(Long userId, Long lastRequestId, Integer limit); | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * 拉取指定群下未处理申请;仅群主 / 管理员可调 | ||
| 74 | + * | ||
| 75 | + * @param userId 操作人用户编号 | ||
| 76 | + * @param groupId 群编号 | ||
| 77 | + * @param lastRequestId 游标 id | ||
| 78 | + * @param limit 单次拉取条数 | ||
| 79 | + * @return 未处理申请列表 | ||
| 80 | + */ | ||
| 81 | + List<ImGroupRequestDO> getPendingGroupRequestList(Long userId, Long groupId, Long lastRequestId, Integer limit); | ||
| 82 | + | ||
| 83 | + /** | ||
| 84 | + * 按 id 单查申请记录;通用读接口,越权过滤交由调用方 | ||
| 85 | + * | ||
| 86 | + * @param id 申请记录编号 | ||
| 87 | + * @return 申请记录 | ||
| 88 | + */ | ||
| 89 | + ImGroupRequestDO getGroupRequest(Long id); | ||
| 90 | + | ||
| 91 | + /** | ||
| 92 | + * 批量统计指定群的未处理申请数量;用于 ImGroupRespVO.pendingRequestCount 回填 | ||
| 93 | + * | ||
| 94 | + * @param groupIds 群编号集合 | ||
| 95 | + * @return 群编号 → 未处理申请数 Map | ||
| 96 | + */ | ||
| 97 | + Map<Long, Long> getPendingCountMap(Collection<Long> groupIds); | ||
| 98 | + | ||
| 99 | + // ==================== 管理后台 ==================== | ||
| 100 | + | ||
| 101 | + /** | ||
| 102 | + * 【管理后台】分页查询加群申请记录 | ||
| 103 | + */ | ||
| 104 | + PageResult<ImGroupRequestDO> getGroupRequestPage(ImGroupRequestManagerPageReqVO reqVO); | ||
| 105 | + | ||
| 106 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/group/ImGroupRequestServiceImpl.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.service.group; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.bean.BeanUtil; | ||
| 4 | +import cn.hutool.core.bean.copier.CopyOptions; | ||
| 5 | +import cn.hutool.core.collection.CollUtil; | ||
| 6 | +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||
| 7 | +import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 8 | +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | ||
| 9 | +import cn.iocoder.yudao.module.im.controller.admin.group.vo.request.ImGroupRequestApplyReqVO; | ||
| 10 | +import cn.iocoder.yudao.module.im.controller.admin.manager.group.vo.ImGroupRequestManagerPageReqVO; | ||
| 11 | +import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupDO; | ||
| 12 | +import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupMemberDO; | ||
| 13 | +import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupRequestDO; | ||
| 14 | +import cn.iocoder.yudao.module.im.dal.mysql.group.ImGroupRequestMapper; | ||
| 15 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupJoinTypeEnum; | ||
| 16 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupMemberRoleEnum; | ||
| 17 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupRequestHandleResultEnum; | ||
| 18 | +import cn.iocoder.yudao.module.im.enums.message.ImMessageTypeEnum; | ||
| 19 | +import cn.iocoder.yudao.module.im.service.message.ImGroupMessageService; | ||
| 20 | +import cn.iocoder.yudao.module.im.service.message.dto.ImGroupMessageSendDTO; | ||
| 21 | +import cn.iocoder.yudao.module.im.service.websocket.ImWebSocketService; | ||
| 22 | +import cn.iocoder.yudao.module.im.service.websocket.dto.ImPrivateMessageDTO; | ||
| 23 | +import cn.iocoder.yudao.module.im.service.websocket.dto.notification.group.BaseGroupNotification; | ||
| 24 | +import cn.iocoder.yudao.module.im.service.websocket.dto.notification.group.GroupRequestApprovedNotification; | ||
| 25 | +import cn.iocoder.yudao.module.im.service.websocket.dto.notification.group.GroupRequestNotification; | ||
| 26 | +import cn.iocoder.yudao.module.im.service.websocket.dto.notification.group.GroupRequestRejectedNotification; | ||
| 27 | +import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | ||
| 28 | +import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | ||
| 29 | +import jakarta.annotation.Resource; | ||
| 30 | +import lombok.extern.slf4j.Slf4j; | ||
| 31 | +import org.springframework.context.annotation.Lazy; | ||
| 32 | +import org.springframework.stereotype.Service; | ||
| 33 | +import org.springframework.transaction.annotation.Transactional; | ||
| 34 | +import org.springframework.validation.annotation.Validated; | ||
| 35 | + | ||
| 36 | +import java.time.LocalDateTime; | ||
| 37 | +import java.util.*; | ||
| 38 | + | ||
| 39 | +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||
| 40 | +import static cn.iocoder.yudao.module.im.enums.ErrorCodeConstants.*; | ||
| 41 | + | ||
| 42 | +// TODO @AI:payload 能不能改成 notification 更像通知一点。 | ||
| 43 | +/** | ||
| 44 | + * IM 加群申请 Service 实现类 | ||
| 45 | + * | ||
| 46 | + * @author 芋道源码 | ||
| 47 | + */ | ||
| 48 | +@Slf4j | ||
| 49 | +@Service | ||
| 50 | +@Validated | ||
| 51 | +public class ImGroupRequestServiceImpl implements ImGroupRequestService { | ||
| 52 | + | ||
| 53 | + @Resource | ||
| 54 | + private ImGroupRequestMapper groupRequestMapper; | ||
| 55 | + | ||
| 56 | + @Resource | ||
| 57 | + @Lazy // 避免循环依赖 | ||
| 58 | + private ImGroupService groupService; | ||
| 59 | + @Resource | ||
| 60 | + @Lazy // 避免循环依赖 | ||
| 61 | + private ImGroupMemberService groupMemberService; | ||
| 62 | + @Resource | ||
| 63 | + @Lazy // 避免循环依赖 | ||
| 64 | + private ImGroupMessageService groupMessageService; | ||
| 65 | + | ||
| 66 | + @Resource | ||
| 67 | + private ImWebSocketService websocketService; | ||
| 68 | + | ||
| 69 | + @Resource | ||
| 70 | + private AdminUserApi adminUserApi; | ||
| 71 | + | ||
| 72 | + @Override | ||
| 73 | + @Transactional(rollbackFor = Exception.class) | ||
| 74 | + public ImGroupRequestDO applyJoinGroup(Long userId, ImGroupRequestApplyReqVO reqVO) { | ||
| 75 | + Long groupId = reqVO.getGroupId(); | ||
| 76 | + // 1.1 校验群存在 + 未封禁 / 未解散 | ||
| 77 | + ImGroupDO group = groupService.validateGroupExists(groupId); | ||
| 78 | + // 1.2 校验未在群中 | ||
| 79 | + ImGroupMemberDO member = groupMemberService.getGroupMember(groupId, userId); | ||
| 80 | + if (member != null && !CommonStatusEnum.DISABLE.getStatus().equals(member.getStatus())) { | ||
| 81 | + throw exception(GROUP_REQUEST_ALREADY_MEMBER); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + // 2. FREE 模式:直接入群 + 推 1510 全员广播;不落申请记录 | ||
| 85 | + if (ImGroupJoinTypeEnum.isFree(group.getJoinType())) { | ||
| 86 | + // TODO @AI:这里还是写下注释;每一行的; | ||
| 87 | + groupService.validateMemberCountLimit(groupId, 1); | ||
| 88 | + groupMemberService.addGroupMember(groupId, userId, | ||
| 89 | + ImGroupMemberRoleEnum.NORMAL.getRole(), reqVO.getAddSource(), null); | ||
| 90 | + groupMessageService.sendGroupMessage(userId, | ||
| 91 | + ImGroupMessageSendDTO.ofGroupMemberEnter(groupId, userId, reqVO.getAddSource())); | ||
| 92 | + return null; | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + // 3. 非 FREE 模式:复用未处理记录或新建 | ||
| 96 | + ImGroupRequestDO request = groupRequestMapper.selectLatestPendingByGroupIdAndUserIdAndInviter( | ||
| 97 | + groupId, userId, null); | ||
| 98 | + if (request != null) { | ||
| 99 | + ImGroupRequestDO updateObj = new ImGroupRequestDO() | ||
| 100 | + .setApplyContent(reqVO.getApplyContent()).setAddSource(reqVO.getAddSource()); | ||
| 101 | + int affected = groupRequestMapper.updateByIdAndHandleResult(request.getId(), | ||
| 102 | + ImGroupRequestHandleResultEnum.UNHANDLED.getResult(), updateObj); | ||
| 103 | + if (affected == 0) { | ||
| 104 | + // 并发场景另一管理端刚处理过,提示重试 | ||
| 105 | + throw exception(GROUP_REQUEST_HANDLED); | ||
| 106 | + } | ||
| 107 | + BeanUtil.copyProperties(reqVO, request, CopyOptions.create().setIgnoreNullValue(true)); | ||
| 108 | + } else { | ||
| 109 | + request = BeanUtils.toBean(reqVO, ImGroupRequestDO.class) | ||
| 110 | + .setUserId(userId).setInviterUserId(null) | ||
| 111 | + .setHandleResult(ImGroupRequestHandleResultEnum.UNHANDLED.getResult()); | ||
| 112 | + groupRequestMapper.insert(request); | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + // 4. 1503 私聊定向推群主 + 全部管理员(多端同步);payload 携带申请方昵称 / 头像 | ||
| 116 | + AdminUserRespDTO applyUser = adminUserApi.getUser(userId); | ||
| 117 | + GroupRequestNotification payload = buildRequestNotification(group, request, applyUser); | ||
| 118 | + for (Long receiverUserId : listOwnerAndAdminUserIds(group)) { | ||
| 119 | + // TODO @AI:能不能发群的,只发给群管理员;就是 groupMessageService;只是不 persist 呀。 | ||
| 120 | + websocketService.sendPrivateMessageAsync(receiverUserId, ImPrivateMessageDTO.ofGroupNotification( | ||
| 121 | + ImMessageTypeEnum.GROUP_REQUEST_RECEIVED.getType(), userId, receiverUserId, payload)); | ||
| 122 | + } | ||
| 123 | + return request; | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + @Override | ||
| 127 | + @Transactional(rollbackFor = Exception.class) | ||
| 128 | + public void agreeGroupRequest(Long userId, Long requestId) { | ||
| 129 | + // 1. 校验申请存在 + 未处理 + 操作人是 owner/admin | ||
| 130 | + ImGroupRequestDO request = validateRequestForHandle(userId, requestId); | ||
| 131 | + // 2. 入群前校验人数上限;群已满抛错让操作人选择拒绝 | ||
| 132 | + groupService.validateMemberCountLimit(request.getGroupId(), 1); | ||
| 133 | + | ||
| 134 | + // 3. 乐观锁推进状态 | ||
| 135 | + LocalDateTime now = LocalDateTime.now(); | ||
| 136 | + ImGroupRequestDO updateObj = new ImGroupRequestDO() | ||
| 137 | + .setHandleResult(ImGroupRequestHandleResultEnum.AGREED.getResult()) | ||
| 138 | + .setHandleUserId(userId).setHandleTime(now); | ||
| 139 | + int affected = groupRequestMapper.updateByIdAndHandleResult(request.getId(), | ||
| 140 | + ImGroupRequestHandleResultEnum.UNHANDLED.getResult(), updateObj); | ||
| 141 | + if (affected == 0) { | ||
| 142 | + throw exception(GROUP_REQUEST_HANDLED); | ||
| 143 | + } | ||
| 144 | + request.setHandleResult(ImGroupRequestHandleResultEnum.AGREED.getResult()) | ||
| 145 | + .setHandleUserId(userId).setHandleTime(now); | ||
| 146 | + | ||
| 147 | + // 4. 写群成员;带 addSource / inviterUserId | ||
| 148 | + groupMemberService.addGroupMember(request.getGroupId(), request.getUserId(), | ||
| 149 | + ImGroupMemberRoleEnum.NORMAL.getRole(), request.getAddSource(), request.getInviterUserId()); | ||
| 150 | + | ||
| 151 | + // 5.1 1505 私聊推送给申请人 + 群主 + 全部管理员(每端单推) | ||
| 152 | + GroupRequestApprovedNotification payload = new GroupRequestApprovedNotification(); | ||
| 153 | + // TODO @AI:能不能改成链式调用; | ||
| 154 | + payload.setRequestId(request.getId()); | ||
| 155 | + payload.setGroupId(request.getGroupId()); | ||
| 156 | + payload.setUserId(request.getUserId()); | ||
| 157 | + payload.setOperatorUserId(userId); | ||
| 158 | + broadcastToOwnerAdminsAndApplicant(request.getGroupId(), request.getUserId(), payload, | ||
| 159 | + ImMessageTypeEnum.GROUP_REQUEST_APPROVED.getType(), userId); | ||
| 160 | + // 5.2 群事件:主动申请 → 1510 自由进群;被邀请 → 1509 成员加入 | ||
| 161 | + if (request.getInviterUserId() == null) { | ||
| 162 | + groupMessageService.sendGroupMessage(userId, | ||
| 163 | + ImGroupMessageSendDTO.ofGroupMemberEnter(request.getGroupId(), | ||
| 164 | + request.getUserId(), request.getAddSource())); | ||
| 165 | + } else { | ||
| 166 | + groupMessageService.sendGroupMessage(userId, | ||
| 167 | + ImGroupMessageSendDTO.ofGroupMemberInvite(request.getGroupId(), | ||
| 168 | + request.getInviterUserId(), Collections.singleton(request.getUserId()))); | ||
| 169 | + } | ||
| 170 | + } | ||
| 171 | + | ||
| 172 | + @Override | ||
| 173 | + @Transactional(rollbackFor = Exception.class) | ||
| 174 | + public void refuseGroupRequest(Long userId, Long requestId, String handleContent) { | ||
| 175 | + // 1. 校验 | ||
| 176 | + ImGroupRequestDO request = validateRequestForHandle(userId, requestId); | ||
| 177 | + | ||
| 178 | + // 2. 乐观锁推进 | ||
| 179 | + ImGroupRequestDO updateObj = new ImGroupRequestDO() | ||
| 180 | + .setHandleResult(ImGroupRequestHandleResultEnum.REFUSED.getResult()) | ||
| 181 | + .setHandleContent(handleContent) | ||
| 182 | + .setHandleUserId(userId).setHandleTime(LocalDateTime.now()); | ||
| 183 | + int affected = groupRequestMapper.updateByIdAndHandleResult(request.getId(), | ||
| 184 | + ImGroupRequestHandleResultEnum.UNHANDLED.getResult(), updateObj); | ||
| 185 | + if (affected == 0) { | ||
| 186 | + throw exception(GROUP_REQUEST_HANDLED); | ||
| 187 | + } | ||
| 188 | + | ||
| 189 | + // 3. 1506 私聊推送给申请人 + 群主 + 全部管理员 | ||
| 190 | + GroupRequestRejectedNotification payload = new GroupRequestRejectedNotification(); | ||
| 191 | + payload.setRequestId(request.getId()); | ||
| 192 | + payload.setGroupId(request.getGroupId()); | ||
| 193 | + payload.setUserId(request.getUserId()); | ||
| 194 | + payload.setHandleContent(handleContent); | ||
| 195 | + payload.setOperatorUserId(userId); | ||
| 196 | + broadcastToOwnerAdminsAndApplicant(request.getGroupId(), request.getUserId(), payload, | ||
| 197 | + ImMessageTypeEnum.GROUP_REQUEST_REJECTED.getType(), userId); | ||
| 198 | + } | ||
| 199 | + | ||
| 200 | + // TODO @AI:createInviteRequestList;习惯性命名; | ||
| 201 | + @Override | ||
| 202 | + @Transactional(rollbackFor = Exception.class) | ||
| 203 | + public void createInviteRequests(Long groupId, Long inviterUserId, Collection<Long> invitedUserIds) { | ||
| 204 | + if (CollUtil.isEmpty(invitedUserIds)) { | ||
| 205 | + return; | ||
| 206 | + } | ||
| 207 | + ImGroupDO group = groupService.validateGroupExists(groupId); | ||
| 208 | + | ||
| 209 | + // TODO @AI:= 两侧,最好有空格;看看有没别的地方,也要改的; | ||
| 210 | + // 1. 复用或新建 inviter_user_id=inviterUserId 的待审批记录 | ||
| 211 | + List<ImGroupRequestDO> requests = new ArrayList<>(invitedUserIds.size()); | ||
| 212 | + for (Long invitedUserId : invitedUserIds) { | ||
| 213 | + ImGroupRequestDO existing = groupRequestMapper.selectLatestPendingByGroupIdAndUserIdAndInviter( | ||
| 214 | + groupId, invitedUserId, inviterUserId); | ||
| 215 | + if (existing != null) { | ||
| 216 | + requests.add(existing); | ||
| 217 | + continue; | ||
| 218 | + } | ||
| 219 | + ImGroupRequestDO insert = new ImGroupRequestDO() | ||
| 220 | + .setGroupId(groupId).setUserId(invitedUserId).setInviterUserId(inviterUserId) | ||
| 221 | + .setHandleResult(ImGroupRequestHandleResultEnum.UNHANDLED.getResult()); | ||
| 222 | + groupRequestMapper.insert(insert); | ||
| 223 | + requests.add(insert); | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + // 2. 推 1503 给群主 + 全部管理员;多端同步;每条申请单独推一帧 | ||
| 227 | + Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(invitedUserIds); | ||
| 228 | + List<Long> ownerAndAdmins = listOwnerAndAdminUserIds(group); | ||
| 229 | + for (ImGroupRequestDO request : requests) { | ||
| 230 | + AdminUserRespDTO applyUser = userMap.get(request.getUserId()); | ||
| 231 | + // TODO @AI:GroupRequestNotification 是不是要调整下命名?因为它是 RECEIVED 的,不然显得太公用了。 | ||
| 232 | + GroupRequestNotification payload = buildRequestNotification(group, request, applyUser); | ||
| 233 | + for (Long receiverUserId : ownerAndAdmins) { | ||
| 234 | + websocketService.sendPrivateMessageAsync(receiverUserId, ImPrivateMessageDTO.ofGroupNotification( | ||
| 235 | + ImMessageTypeEnum.GROUP_REQUEST_RECEIVED.getType(), inviterUserId, receiverUserId, payload)); | ||
| 236 | + } | ||
| 237 | + } | ||
| 238 | + } | ||
| 239 | + | ||
| 240 | + @Override | ||
| 241 | + public List<ImGroupRequestDO> getMyGroupRequestList(Long userId, Long lastRequestId, Integer limit) { | ||
| 242 | + return groupRequestMapper.selectMyList(userId, lastRequestId, limit); | ||
| 243 | + } | ||
| 244 | + | ||
| 245 | + @Override | ||
| 246 | + public List<ImGroupRequestDO> getPendingGroupRequestList(Long userId, Long groupId, | ||
| 247 | + Long lastRequestId, Integer limit) { | ||
| 248 | + // 仅群主 / 管理员可拉 | ||
| 249 | + ImGroupMemberDO member = groupMemberService.validateMemberInGroup(groupId, userId); | ||
| 250 | + if (!ImGroupMemberRoleEnum.isOwnerOrAdmin(member.getRole())) { | ||
| 251 | + throw exception(GROUP_REQUEST_NOT_TO_ME); | ||
| 252 | + } | ||
| 253 | + return groupRequestMapper.selectPendingListByGroupId(groupId, lastRequestId, limit); | ||
| 254 | + } | ||
| 255 | + | ||
| 256 | + @Override | ||
| 257 | + public ImGroupRequestDO getGroupRequest(Long id) { | ||
| 258 | + return groupRequestMapper.selectById(id); | ||
| 259 | + } | ||
| 260 | + | ||
| 261 | + // TODO @AI:【后面点改】应该批量查询;不然这样性能太差了。如果群太多; | ||
| 262 | + @Override | ||
| 263 | + public Map<Long, Long> getPendingCountMap(Collection<Long> groupIds) { | ||
| 264 | + if (CollUtil.isEmpty(groupIds)) { | ||
| 265 | + return Collections.emptyMap(); | ||
| 266 | + } | ||
| 267 | + Map<Long, Long> result = new HashMap<>(groupIds.size()); | ||
| 268 | + for (Long groupId : groupIds) { | ||
| 269 | + result.put(groupId, groupRequestMapper.selectPendingCountByGroupId(groupId)); | ||
| 270 | + } | ||
| 271 | + return result; | ||
| 272 | + } | ||
| 273 | + | ||
| 274 | + @Override | ||
| 275 | + public PageResult<ImGroupRequestDO> getGroupRequestPage(ImGroupRequestManagerPageReqVO reqVO) { | ||
| 276 | + return groupRequestMapper.selectPage(reqVO); | ||
| 277 | + } | ||
| 278 | + | ||
| 279 | + /** | ||
| 280 | + * 校验申请可被「当前用户」处理:申请存在 + 未处理 + 操作人是群主 / 管理员 | ||
| 281 | + */ | ||
| 282 | + private ImGroupRequestDO validateRequestForHandle(Long userId, Long requestId) { | ||
| 283 | + ImGroupRequestDO request = groupRequestMapper.selectById(requestId); | ||
| 284 | + if (request == null) { | ||
| 285 | + throw exception(GROUP_REQUEST_NOT_EXISTS); | ||
| 286 | + } | ||
| 287 | + if (!ImGroupRequestHandleResultEnum.isUnhandled(request.getHandleResult())) { | ||
| 288 | + throw exception(GROUP_REQUEST_HANDLED); | ||
| 289 | + } | ||
| 290 | + ImGroupMemberDO operator = groupMemberService.validateMemberInGroup(request.getGroupId(), userId); | ||
| 291 | + if (!ImGroupMemberRoleEnum.isOwnerOrAdmin(operator.getRole())) { | ||
| 292 | + throw exception(GROUP_REQUEST_NOT_TO_ME); | ||
| 293 | + } | ||
| 294 | + return request; | ||
| 295 | + } | ||
| 296 | + | ||
| 297 | + /** | ||
| 298 | + * 构建 1503 通知 payload;聚合申请方昵称 / 头像供前端直接渲染 | ||
| 299 | + */ | ||
| 300 | + private GroupRequestNotification buildRequestNotification(ImGroupDO group, ImGroupRequestDO request, | ||
| 301 | + AdminUserRespDTO applyUser) { | ||
| 302 | + // TODO @AI:看看能不能链式调用; | ||
| 303 | + GroupRequestNotification payload = new GroupRequestNotification(); | ||
| 304 | + payload.setRequestId(request.getId()); | ||
| 305 | + payload.setGroupId(group.getId()); | ||
| 306 | + payload.setUserId(request.getUserId()); | ||
| 307 | + payload.setInviterUserId(request.getInviterUserId()); | ||
| 308 | + payload.setApplyContent(request.getApplyContent()); | ||
| 309 | + payload.setAddSource(request.getAddSource()); | ||
| 310 | + payload.setOperatorUserId(request.getInviterUserId() != null ? request.getInviterUserId() : request.getUserId()); | ||
| 311 | + if (applyUser != null) { | ||
| 312 | + payload.setUserNickname(applyUser.getNickname()); | ||
| 313 | + payload.setUserAvatar(applyUser.getAvatar()); | ||
| 314 | + } | ||
| 315 | + return payload; | ||
| 316 | + } | ||
| 317 | + | ||
| 318 | + /** | ||
| 319 | + * 1505 / 1506 受众:申请人 + 群主 + 全部管理员;每端单独推一帧,前端按 receiver 是否申请人区分文案 | ||
| 320 | + */ | ||
| 321 | + private void broadcastToOwnerAdminsAndApplicant(Long groupId, Long applicantUserId, BaseGroupNotification payload, | ||
| 322 | + Integer messageType, Long operatorUserId) { | ||
| 323 | + ImGroupDO group = groupService.getGroup(groupId); | ||
| 324 | + if (group == null) { | ||
| 325 | + return; | ||
| 326 | + } | ||
| 327 | + Set<Long> receivers = new LinkedHashSet<>(listOwnerAndAdminUserIds(group)); | ||
| 328 | + receivers.add(applicantUserId); | ||
| 329 | + // TODO @AI:看看能不能用群的通道; | ||
| 330 | + for (Long receiverUserId : receivers) { | ||
| 331 | + websocketService.sendPrivateMessageAsync(receiverUserId, ImPrivateMessageDTO.ofGroupNotification( | ||
| 332 | + messageType, operatorUserId, receiverUserId, payload)); | ||
| 333 | + } | ||
| 334 | + } | ||
| 335 | + | ||
| 336 | + // TODO @AI:这个方法,搞到到 ImGroupMemberService 里去;getGroupMemberListByOwnerAndAdmin;返回 list《do》; | ||
| 337 | + /** | ||
| 338 | + * 列出群主 + 全部管理员的用户编号;activeMembers 通常 ≤ 500 + admin 上限 3,过滤成本可接受 | ||
| 339 | + */ | ||
| 340 | + private List<Long> listOwnerAndAdminUserIds(ImGroupDO group) { | ||
| 341 | + List<ImGroupMemberDO> activeMembers = groupMemberService.getActiveGroupMemberListByGroupId(group.getId()); | ||
| 342 | + Set<Long> set = new LinkedHashSet<>(); | ||
| 343 | + set.add(group.getOwnerUserId()); | ||
| 344 | + for (ImGroupMemberDO member : activeMembers) { | ||
| 345 | + if (ImGroupMemberRoleEnum.isOwnerOrAdmin(member.getRole())) { | ||
| 346 | + set.add(member.getUserId()); | ||
| 347 | + } | ||
| 348 | + } | ||
| 349 | + return new ArrayList<>(set); | ||
| 350 | + } | ||
| 351 | + | ||
| 352 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/group/ImGroupService.java
| @@ -86,6 +86,17 @@ public interface ImGroupService { | @@ -86,6 +86,17 @@ public interface ImGroupService { | ||
| 86 | ImGroupDO validateGroupExists(Long groupId); | 86 | ImGroupDO validateGroupExists(Long groupId); |
| 87 | 87 | ||
| 88 | /** | 88 | /** |
| 89 | + * 校验入群人数上限 | ||
| 90 | + * <p> | ||
| 91 | + * 调用方场景:自由进群 / 审批通过等不经 inviteGroupMember 的入群路径,需在写群成员前主动校验 | ||
| 92 | + * | ||
| 93 | + * @param groupId 群编号 | ||
| 94 | + * @param addCount 即将新增的人数 | ||
| 95 | + * @throws cn.iocoder.yudao.framework.common.exception.ServiceException 群已满抛 GROUP_MEMBER_EXCEED | ||
| 96 | + */ | ||
| 97 | + void validateMemberCountLimit(Long groupId, int addCount); | ||
| 98 | + | ||
| 99 | + /** | ||
| 89 | * 获取指定用户的群列表 | 100 | * 获取指定用户的群列表 |
| 90 | * <p> | 101 | * <p> |
| 91 | * 返回用户当前仍有效的群,以及最近 {@link cn.iocoder.yudao.module.im.enums.ImCommonConstants#MESSAGE_GROUP_PULL_MAX_DAYS} | 102 | * 返回用户当前仍有效的群,以及最近 {@link cn.iocoder.yudao.module.im.enums.ImCommonConstants#MESSAGE_GROUP_PULL_MAX_DAYS} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/group/ImGroupServiceImpl.java
| @@ -27,6 +27,8 @@ import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupDO; | @@ -27,6 +27,8 @@ import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupDO; | ||
| 27 | import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupMemberDO; | 27 | import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupMemberDO; |
| 28 | import cn.iocoder.yudao.module.im.dal.dataobject.message.ImGroupMessageDO; | 28 | import cn.iocoder.yudao.module.im.dal.dataobject.message.ImGroupMessageDO; |
| 29 | import cn.iocoder.yudao.module.im.dal.mysql.group.ImGroupMapper; | 29 | import cn.iocoder.yudao.module.im.dal.mysql.group.ImGroupMapper; |
| 30 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupAddSourceEnum; | ||
| 31 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupJoinTypeEnum; | ||
| 30 | import cn.iocoder.yudao.module.im.enums.group.ImGroupMemberRoleEnum; | 32 | import cn.iocoder.yudao.module.im.enums.group.ImGroupMemberRoleEnum; |
| 31 | import cn.iocoder.yudao.module.im.enums.message.ImMessageStatusEnum; | 33 | import cn.iocoder.yudao.module.im.enums.message.ImMessageStatusEnum; |
| 32 | import cn.iocoder.yudao.module.im.enums.message.ImMessageTypeEnum; | 34 | import cn.iocoder.yudao.module.im.enums.message.ImMessageTypeEnum; |
| @@ -72,6 +74,9 @@ public class ImGroupServiceImpl implements ImGroupService { | @@ -72,6 +74,9 @@ public class ImGroupServiceImpl implements ImGroupService { | ||
| 72 | @Resource | 74 | @Resource |
| 73 | @Lazy // 避免循环依赖 | 75 | @Lazy // 避免循环依赖 |
| 74 | private ImGroupMessageService groupMessageService; | 76 | private ImGroupMessageService groupMessageService; |
| 77 | + @Resource | ||
| 78 | + @Lazy // 避免循环依赖 | ||
| 79 | + private ImGroupRequestService groupRequestService; | ||
| 75 | 80 | ||
| 76 | @Resource | 81 | @Resource |
| 77 | private ImFriendService friendService; | 82 | private ImFriendService friendService; |
| @@ -108,9 +113,10 @@ public class ImGroupServiceImpl implements ImGroupService { | @@ -108,9 +113,10 @@ public class ImGroupServiceImpl implements ImGroupService { | ||
| 108 | groupMapper.insert(group); | 113 | groupMapper.insert(group); |
| 109 | // 2.2 创建者作为 OWNER 入群 | 114 | // 2.2 创建者作为 OWNER 入群 |
| 110 | groupMemberService.addGroupMember(group.getId(), userId, ImGroupMemberRoleEnum.OWNER.getRole()); | 115 | groupMemberService.addGroupMember(group.getId(), userId, ImGroupMemberRoleEnum.OWNER.getRole()); |
| 111 | - // 2.3 批量添加初始成员 | 116 | + // 2.3 批量添加初始成员;标记 addSource=INVITE / inviter=创建者 |
| 112 | if (CollUtil.isNotEmpty(initialMemberUserIds)) { | 117 | if (CollUtil.isNotEmpty(initialMemberUserIds)) { |
| 113 | - groupMemberService.addGroupMembers(group.getId(), initialMemberUserIds); | 118 | + groupMemberService.addGroupMembers(group.getId(), initialMemberUserIds, |
| 119 | + ImGroupAddSourceEnum.INVITE.getSource(), userId); | ||
| 114 | } | 120 | } |
| 115 | 121 | ||
| 116 | // 3. 推送 GROUP_CREATE 通知给全员(含创建者多端同步 + 初始成员) | 122 | // 3. 推送 GROUP_CREATE 通知给全员(含创建者多端同步 + 初始成员) |
| @@ -187,8 +193,8 @@ public class ImGroupServiceImpl implements ImGroupService { | @@ -187,8 +193,8 @@ public class ImGroupServiceImpl implements ImGroupService { | ||
| 187 | public void inviteGroupMember(Long userId, ImGroupMemberInviteReqVO inviteReqVO) { | 193 | public void inviteGroupMember(Long userId, ImGroupMemberInviteReqVO inviteReqVO) { |
| 188 | Long groupId = inviteReqVO.getGroupId(); | 194 | Long groupId = inviteReqVO.getGroupId(); |
| 189 | // 1.1 校验群存在 + 当前用户是群成员 | 195 | // 1.1 校验群存在 + 当前用户是群成员 |
| 190 | - validateGroupExists(groupId); | ||
| 191 | - groupMemberService.validateMemberInGroup(groupId, userId); | 196 | + ImGroupDO group = validateGroupExists(groupId); |
| 197 | + ImGroupMemberDO operator = groupMemberService.validateMemberInGroup(groupId, userId); | ||
| 192 | // 1.2 排除已在群中的用户 | 198 | // 1.2 排除已在群中的用户 |
| 193 | List<ImGroupMemberDO> activeMembers = groupMemberService.getActiveGroupMemberListByGroupId(groupId); | 199 | List<ImGroupMemberDO> activeMembers = groupMemberService.getActiveGroupMemberListByGroupId(groupId); |
| 194 | activeMembers.forEach(member -> inviteReqVO.getMemberUserIds().remove(member.getUserId())); | 200 | activeMembers.forEach(member -> inviteReqVO.getMemberUserIds().remove(member.getUserId())); |
| @@ -208,10 +214,21 @@ public class ImGroupServiceImpl implements ImGroupService { | @@ -208,10 +214,21 @@ public class ImGroupServiceImpl implements ImGroupService { | ||
| 208 | throw exception(GROUP_MEMBER_EXCEED, MAX_GROUP_MEMBER); | 214 | throw exception(GROUP_MEMBER_EXCEED, MAX_GROUP_MEMBER); |
| 209 | } | 215 | } |
| 210 | 216 | ||
| 211 | - // 2. 批量添加群成员 | ||
| 212 | - groupMemberService.addGroupMembers(groupId, memberUserIds); | 217 | + // TODO @AI:是不是注释的风格,是情况一、情况二,这样搞? |
| 218 | + // 2. 按群 joinType + 操作人角色分流:APPLY_AND_NORMAL_INVITE 模式下,普通成员邀请走审批,落 group_request;其余直进 | ||
| 219 | + // TODO @AI:是不是可以放到 isInviteNeedApproval 判断,是否要审批,这样更简洁一点? | ||
| 220 | + boolean needApproval = ImGroupJoinTypeEnum.isInviteNeedApproval(group.getJoinType()) | ||
| 221 | + && !ImGroupMemberRoleEnum.isOwnerOrAdmin(operator.getRole()); | ||
| 222 | + if (needApproval) { | ||
| 223 | + groupRequestService.createInviteRequests(groupId, userId, memberUserIds); | ||
| 224 | + return; | ||
| 225 | + } | ||
| 226 | + | ||
| 227 | + // 3. 直进:批量添加群成员,写 addSource=INVITE / inviterUserId=操作人,留痕 | ||
| 228 | + groupMemberService.addGroupMembers(groupId, memberUserIds, | ||
| 229 | + ImGroupAddSourceEnum.INVITE.getSource(), userId); | ||
| 213 | 230 | ||
| 214 | - // 3. 发 GROUP_MEMBER_INVITE 通知给全员;本地拼 receivers(已查的 active + 新邀请)避免缓存刚 evict 后强制走 DB | 231 | + // 4. 发 GROUP_MEMBER_INVITE 通知给全员;本地拼 receivers(已查的 active + 新邀请)避免缓存刚 evict 后强制走 DB |
| 215 | Set<Long> allReceivers = new HashSet<>(memberUserIds); | 232 | Set<Long> allReceivers = new HashSet<>(memberUserIds); |
| 216 | activeMembers.forEach(member -> allReceivers.add(member.getUserId())); | 233 | activeMembers.forEach(member -> allReceivers.add(member.getUserId())); |
| 217 | groupMessageService.sendGroupMessage(userId, allReceivers, | 234 | groupMessageService.sendGroupMessage(userId, allReceivers, |
| @@ -446,6 +463,15 @@ public class ImGroupServiceImpl implements ImGroupService { | @@ -446,6 +463,15 @@ public class ImGroupServiceImpl implements ImGroupService { | ||
| 446 | return group; | 463 | return group; |
| 447 | } | 464 | } |
| 448 | 465 | ||
| 466 | + // todo @AI:这个是不是可以放到 groupmemberservice 里面? | ||
| 467 | + @Override | ||
| 468 | + public void validateMemberCountLimit(Long groupId, int addCount) { | ||
| 469 | + int activeCount = groupMemberService.getActiveGroupMemberUserIdsByGroupId(groupId).size(); | ||
| 470 | + if (activeCount + addCount > MAX_GROUP_MEMBER) { | ||
| 471 | + throw exception(GROUP_MEMBER_EXCEED, MAX_GROUP_MEMBER); | ||
| 472 | + } | ||
| 473 | + } | ||
| 474 | + | ||
| 449 | /** | 475 | /** |
| 450 | * 校验当前用户是群主,否则抛 GROUP_NOT_OWNER | 476 | * 校验当前用户是群主,否则抛 GROUP_NOT_OWNER |
| 451 | */ | 477 | */ |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/message/dto/ImGroupMessageSendDTO.java
| @@ -102,6 +102,15 @@ public class ImGroupMessageSendDTO { | @@ -102,6 +102,15 @@ public class ImGroupMessageSendDTO { | ||
| 102 | .setType(ImMessageTypeEnum.GROUP_MEMBER_INVITE.getType()).setContent(notification); | 102 | .setType(ImMessageTypeEnum.GROUP_MEMBER_INVITE.getType()).setContent(notification); |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | + public static ImGroupMessageSendDTO ofGroupMemberEnter(Long groupId, Long entrantUserId, Integer addSource) { | ||
| 106 | + GroupMemberEnterNotification notification = new GroupMemberEnterNotification(); | ||
| 107 | + notification.setOperatorUserId(entrantUserId); | ||
| 108 | + notification.setEntrantUserId(entrantUserId); | ||
| 109 | + notification.setAddSource(addSource); | ||
| 110 | + return new ImGroupMessageSendDTO().setGroupId(groupId) | ||
| 111 | + .setType(ImMessageTypeEnum.GROUP_MEMBER_ENTER.getType()).setContent(notification); | ||
| 112 | + } | ||
| 113 | + | ||
| 105 | public static ImGroupMessageSendDTO ofGroupDissolve(Long groupId, Long operatorUserId) { | 114 | public static ImGroupMessageSendDTO ofGroupDissolve(Long groupId, Long operatorUserId) { |
| 106 | GroupDissolveNotification notification = new GroupDissolveNotification(); | 115 | GroupDissolveNotification notification = new GroupDissolveNotification(); |
| 107 | notification.setOperatorUserId(operatorUserId); | 116 | notification.setOperatorUserId(operatorUserId); |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/websocket/dto/ImPrivateMessageDTO.java
| @@ -5,6 +5,7 @@ import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | @@ -5,6 +5,7 @@ import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | ||
| 5 | import cn.iocoder.yudao.module.im.dal.dataobject.message.ImPrivateMessageDO; | 5 | import cn.iocoder.yudao.module.im.dal.dataobject.message.ImPrivateMessageDO; |
| 6 | import cn.iocoder.yudao.module.im.enums.message.ImMessageTypeEnum; | 6 | import cn.iocoder.yudao.module.im.enums.message.ImMessageTypeEnum; |
| 7 | import cn.iocoder.yudao.module.im.service.websocket.dto.notification.friend.BaseFriendNotification; | 7 | import cn.iocoder.yudao.module.im.service.websocket.dto.notification.friend.BaseFriendNotification; |
| 8 | +import cn.iocoder.yudao.module.im.service.websocket.dto.notification.group.BaseGroupNotification; | ||
| 8 | import lombok.Data; | 9 | import lombok.Data; |
| 9 | import lombok.experimental.Accessors; | 10 | import lombok.experimental.Accessors; |
| 10 | 11 | ||
| @@ -120,4 +121,26 @@ public class ImPrivateMessageDTO { | @@ -120,4 +121,26 @@ public class ImPrivateMessageDTO { | ||
| 120 | .setContent(JsonUtils.toJsonString(payload)).setSendTime(LocalDateTime.now()); | 121 | .setContent(JsonUtils.toJsonString(payload)).setSendTime(LocalDateTime.now()); |
| 121 | } | 122 | } |
| 122 | 123 | ||
| 124 | + // ==================== 群定向私聊通知 ==================== | ||
| 125 | + | ||
| 126 | + // TODO @AI:能不能走群聊的通道????只是接收人不同。 | ||
| 127 | + /** | ||
| 128 | + * 构建群通知推送 DTO(走私聊通道定向推送,不入群消息流) | ||
| 129 | + * <p> | ||
| 130 | + * 用于 GROUP_REQUEST_RECEIVED / GROUP_REQUEST_APPROVED / GROUP_REQUEST_REJECTED 段位; | ||
| 131 | + * 与 {@link cn.iocoder.yudao.module.im.service.message.dto.ImGroupMessageSendDTO} 走 sendGroupMessage 群广播路径不同 | ||
| 132 | + * | ||
| 133 | + * @param type 消息类型;取自 {@link ImMessageTypeEnum} 中的 GROUP_REQUEST_* 段 | ||
| 134 | + * @param operatorUserId 操作人用户编号;同时作为帧的 senderId | ||
| 135 | + * @param receiverUserId 推送目标用户编号 | ||
| 136 | + * @param payload 群事件 payload(继承 {@link BaseGroupNotification}) | ||
| 137 | + * @return 私聊 DTO | ||
| 138 | + */ | ||
| 139 | + public static ImPrivateMessageDTO ofGroupNotification(Integer type, Long operatorUserId, | ||
| 140 | + Long receiverUserId, BaseGroupNotification payload) { | ||
| 141 | + return new ImPrivateMessageDTO().setType(type) | ||
| 142 | + .setSenderId(operatorUserId).setReceiverId(receiverUserId) | ||
| 143 | + .setContent(JsonUtils.toJsonString(payload)).setSendTime(LocalDateTime.now()); | ||
| 144 | + } | ||
| 145 | + | ||
| 123 | } | 146 | } |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/websocket/dto/notification/group/GroupMemberEnterNotification.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.service.websocket.dto.notification.group; | ||
| 2 | + | ||
| 3 | +import lombok.Data; | ||
| 4 | +import lombok.EqualsAndHashCode; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * 自由进群事件通知 | ||
| 8 | + * <p> | ||
| 9 | + * 用户经搜索 / 二维码 / 分享链接自由进群(FREE 模式或审批通过后),全员广播; | ||
| 10 | + * 进群者前端按 entrantUserId === self 自判 fetchGroupInfo + fetchGroupMembers bootstrap,其余成员局部插入新成员 | ||
| 11 | + */ | ||
| 12 | +@EqualsAndHashCode(callSuper = true) | ||
| 13 | +@Data | ||
| 14 | +public class GroupMemberEnterNotification extends BaseGroupNotification { | ||
| 15 | + | ||
| 16 | + /** | ||
| 17 | + * 进群者用户编号 | ||
| 18 | + */ | ||
| 19 | + private Long entrantUserId; | ||
| 20 | + /** | ||
| 21 | + * 加入来源 | ||
| 22 | + */ | ||
| 23 | + private Integer addSource; | ||
| 24 | + | ||
| 25 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/websocket/dto/notification/group/GroupRequestApprovedNotification.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.service.websocket.dto.notification.group; | ||
| 2 | + | ||
| 3 | +import lombok.Data; | ||
| 4 | +import lombok.EqualsAndHashCode; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * 入群申请被同意通知 | ||
| 8 | + * <p> | ||
| 9 | + * 走私聊通道定向推送给申请人 + 群主 + 全部管理员; | ||
| 10 | + * 申请人侧据此弹 toast、列表项更新;admin 侧据此把 pendingRequestCount 减 1 并从未处理列表移除 | ||
| 11 | + */ | ||
| 12 | +@EqualsAndHashCode(callSuper = true) | ||
| 13 | +@Data | ||
| 14 | +public class GroupRequestApprovedNotification extends BaseGroupNotification { | ||
| 15 | + | ||
| 16 | + /** | ||
| 17 | + * 已处理的申请记录编号 | ||
| 18 | + */ | ||
| 19 | + private Long requestId; | ||
| 20 | + /** | ||
| 21 | + * 群编号 | ||
| 22 | + */ | ||
| 23 | + private Long groupId; | ||
| 24 | + /** | ||
| 25 | + * 申请人 / 被邀请人用户编号 | ||
| 26 | + */ | ||
| 27 | + private Long userId; | ||
| 28 | + | ||
| 29 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/websocket/dto/notification/group/GroupRequestNotification.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.service.websocket.dto.notification.group; | ||
| 2 | + | ||
| 3 | +import lombok.Data; | ||
| 4 | +import lombok.EqualsAndHashCode; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * 收到新的入群申请通知 | ||
| 8 | + * <p> | ||
| 9 | + * 走私聊通道定向推送给群主 + 全部管理员(多端同步);payload 已携带申请方昵称 / 头像,前端按 requestId 直接 push 进列表 | ||
| 10 | + */ | ||
| 11 | +@EqualsAndHashCode(callSuper = true) | ||
| 12 | +@Data | ||
| 13 | +public class GroupRequestNotification extends BaseGroupNotification { | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * 申请记录编号 | ||
| 17 | + */ | ||
| 18 | + private Long requestId; | ||
| 19 | + /** | ||
| 20 | + * 群编号 | ||
| 21 | + */ | ||
| 22 | + private Long groupId; | ||
| 23 | + /** | ||
| 24 | + * 申请人 / 被邀请人用户编号 | ||
| 25 | + */ | ||
| 26 | + private Long userId; | ||
| 27 | + /** | ||
| 28 | + * 邀请人用户编号;NULL 表示用户主动申请 | ||
| 29 | + */ | ||
| 30 | + private Long inviterUserId; | ||
| 31 | + /** | ||
| 32 | + * 申请理由 | ||
| 33 | + */ | ||
| 34 | + private String applyContent; | ||
| 35 | + /** | ||
| 36 | + * 加入来源 | ||
| 37 | + */ | ||
| 38 | + private Integer addSource; | ||
| 39 | + | ||
| 40 | + // ========== 聚合自 AdminUser,避免前端再调 system 接口 ========== | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * 申请方 / 被邀请人昵称 | ||
| 44 | + */ | ||
| 45 | + private String userNickname; | ||
| 46 | + /** | ||
| 47 | + * 申请方 / 被邀请人头像 | ||
| 48 | + */ | ||
| 49 | + private String userAvatar; | ||
| 50 | + | ||
| 51 | +} |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/service/websocket/dto/notification/group/GroupRequestRejectedNotification.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.service.websocket.dto.notification.group; | ||
| 2 | + | ||
| 3 | +import lombok.Data; | ||
| 4 | +import lombok.EqualsAndHashCode; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * 入群申请被拒绝通知 | ||
| 8 | + * <p> | ||
| 9 | + * 走私聊通道定向推送给申请人 + 群主 + 全部管理员;admin 侧据此把 pendingRequestCount 减 1 并从未处理列表移除 | ||
| 10 | + */ | ||
| 11 | +@EqualsAndHashCode(callSuper = true) | ||
| 12 | +@Data | ||
| 13 | +public class GroupRequestRejectedNotification extends BaseGroupNotification { | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * 已处理的申请记录编号 | ||
| 17 | + */ | ||
| 18 | + private Long requestId; | ||
| 19 | + /** | ||
| 20 | + * 群编号 | ||
| 21 | + */ | ||
| 22 | + private Long groupId; | ||
| 23 | + /** | ||
| 24 | + * 申请人 / 被邀请人用户编号 | ||
| 25 | + */ | ||
| 26 | + private Long userId; | ||
| 27 | + /** | ||
| 28 | + * 拒绝理由(可选) | ||
| 29 | + */ | ||
| 30 | + private String handleContent; | ||
| 31 | + | ||
| 32 | +} |
yudao-module-im/src/test/java/cn/iocoder/yudao/module/im/service/group/ImGroupRequestServiceImplTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.im.service.group; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||
| 4 | +import cn.iocoder.yudao.framework.common.exception.ServiceException; | ||
| 5 | +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | ||
| 6 | +import cn.iocoder.yudao.module.im.controller.admin.group.vo.request.ImGroupRequestApplyReqVO; | ||
| 7 | +import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupDO; | ||
| 8 | +import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupMemberDO; | ||
| 9 | +import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupRequestDO; | ||
| 10 | +import cn.iocoder.yudao.module.im.dal.mysql.group.ImGroupRequestMapper; | ||
| 11 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupAddSourceEnum; | ||
| 12 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupJoinTypeEnum; | ||
| 13 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupMemberRoleEnum; | ||
| 14 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupRequestHandleResultEnum; | ||
| 15 | +import cn.iocoder.yudao.module.im.enums.message.ImMessageTypeEnum; | ||
| 16 | +import cn.iocoder.yudao.module.im.service.message.ImGroupMessageService; | ||
| 17 | +import cn.iocoder.yudao.module.im.service.message.dto.ImGroupMessageSendDTO; | ||
| 18 | +import cn.iocoder.yudao.module.im.service.websocket.ImWebSocketService; | ||
| 19 | +import cn.iocoder.yudao.module.im.service.websocket.dto.ImPrivateMessageDTO; | ||
| 20 | +import cn.iocoder.yudao.module.system.api.user.AdminUserApi; | ||
| 21 | +import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; | ||
| 22 | +import org.junit.jupiter.api.Test; | ||
| 23 | +import org.mockito.ArgumentCaptor; | ||
| 24 | +import org.mockito.InjectMocks; | ||
| 25 | +import org.mockito.Mock; | ||
| 26 | + | ||
| 27 | +import java.util.List; | ||
| 28 | + | ||
| 29 | +import static cn.iocoder.yudao.module.im.enums.ErrorCodeConstants.*; | ||
| 30 | +import static org.junit.jupiter.api.Assertions.*; | ||
| 31 | +import static org.mockito.ArgumentMatchers.*; | ||
| 32 | +import static org.mockito.Mockito.*; | ||
| 33 | + | ||
| 34 | +/** | ||
| 35 | + * IM 加群申请 Service 单元测试 | ||
| 36 | + * | ||
| 37 | + * @author 芋道源码 | ||
| 38 | + */ | ||
| 39 | +public class ImGroupRequestServiceImplTest extends BaseMockitoUnitTest { | ||
| 40 | + | ||
| 41 | + @InjectMocks | ||
| 42 | + private ImGroupRequestServiceImpl groupRequestService; | ||
| 43 | + | ||
| 44 | + @Mock | ||
| 45 | + private ImGroupRequestMapper groupRequestMapper; | ||
| 46 | + @Mock | ||
| 47 | + private ImGroupService groupService; | ||
| 48 | + @Mock | ||
| 49 | + private ImGroupMemberService groupMemberService; | ||
| 50 | + @Mock | ||
| 51 | + private ImGroupMessageService groupMessageService; | ||
| 52 | + @Mock | ||
| 53 | + private ImWebSocketService websocketService; | ||
| 54 | + @Mock | ||
| 55 | + private AdminUserApi adminUserApi; | ||
| 56 | + | ||
| 57 | + // ==================== applyJoinGroup ==================== | ||
| 58 | + | ||
| 59 | + @Test | ||
| 60 | + public void testApplyJoinGroup_freeMode_directJoin() { | ||
| 61 | + // 准备:群是 FREE 模式 | ||
| 62 | + ImGroupDO group = ImGroupDO.builder().id(10L).ownerUserId(99L) | ||
| 63 | + .joinType(ImGroupJoinTypeEnum.FREE.getType()) | ||
| 64 | + .status(CommonStatusEnum.ENABLE.getStatus()).build(); | ||
| 65 | + when(groupService.validateGroupExists(10L)).thenReturn(group); | ||
| 66 | + | ||
| 67 | + ImGroupRequestApplyReqVO reqVO = new ImGroupRequestApplyReqVO(); | ||
| 68 | + reqVO.setGroupId(10L); | ||
| 69 | + reqVO.setAddSource(ImGroupAddSourceEnum.SEARCH.getSource()); | ||
| 70 | + | ||
| 71 | + // 调用 | ||
| 72 | + ImGroupRequestDO result = groupRequestService.applyJoinGroup(1L, reqVO); | ||
| 73 | + | ||
| 74 | + // 断言:FREE 路径直接入群,不落申请记录 | ||
| 75 | + assertNull(result); | ||
| 76 | + verify(groupService).validateMemberCountLimit(10L, 1); | ||
| 77 | + verify(groupMemberService).addGroupMember(eq(10L), eq(1L), | ||
| 78 | + eq(ImGroupMemberRoleEnum.NORMAL.getRole()), | ||
| 79 | + eq(ImGroupAddSourceEnum.SEARCH.getSource()), isNull()); | ||
| 80 | + verify(groupRequestMapper, never()).insert(any(ImGroupRequestDO.class)); | ||
| 81 | + // 推 1510 自由进群 | ||
| 82 | + ArgumentCaptor<ImGroupMessageSendDTO> dtoCaptor = ArgumentCaptor.forClass(ImGroupMessageSendDTO.class); | ||
| 83 | + verify(groupMessageService).sendGroupMessage(eq(1L), dtoCaptor.capture()); | ||
| 84 | + assertEquals(ImMessageTypeEnum.GROUP_MEMBER_ENTER.getType(), dtoCaptor.getValue().getType()); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + @Test | ||
| 88 | + public void testApplyJoinGroup_approvalMode_createsRequest() { | ||
| 89 | + // 准备:群是 APPLY 模式 | ||
| 90 | + ImGroupDO group = ImGroupDO.builder().id(10L).ownerUserId(99L) | ||
| 91 | + .joinType(ImGroupJoinTypeEnum.APPLY.getType()) | ||
| 92 | + .status(CommonStatusEnum.ENABLE.getStatus()).build(); | ||
| 93 | + when(groupService.validateGroupExists(10L)).thenReturn(group); | ||
| 94 | + // 群里有 owner + 一个 admin,作为 1503 推送目标 | ||
| 95 | + when(groupMemberService.getActiveGroupMemberListByGroupId(10L)).thenReturn(List.of( | ||
| 96 | + ImGroupMemberDO.builder().groupId(10L).userId(99L) | ||
| 97 | + .role(ImGroupMemberRoleEnum.OWNER.getRole()) | ||
| 98 | + .status(CommonStatusEnum.ENABLE.getStatus()).build(), | ||
| 99 | + ImGroupMemberDO.builder().groupId(10L).userId(98L) | ||
| 100 | + .role(ImGroupMemberRoleEnum.ADMIN.getRole()) | ||
| 101 | + .status(CommonStatusEnum.ENABLE.getStatus()).build())); | ||
| 102 | + when(adminUserApi.getUser(1L)).thenReturn(buildUser(1L, "申请人")); | ||
| 103 | + | ||
| 104 | + ImGroupRequestApplyReqVO reqVO = new ImGroupRequestApplyReqVO(); | ||
| 105 | + reqVO.setGroupId(10L); | ||
| 106 | + reqVO.setApplyContent("我想进群"); | ||
| 107 | + reqVO.setAddSource(ImGroupAddSourceEnum.SEARCH.getSource()); | ||
| 108 | + | ||
| 109 | + // 调用 | ||
| 110 | + ImGroupRequestDO result = groupRequestService.applyJoinGroup(1L, reqVO); | ||
| 111 | + | ||
| 112 | + // 断言:申请记录已落库 + 不直进群 | ||
| 113 | + assertNotNull(result); | ||
| 114 | + verify(groupMemberService, never()).addGroupMember(anyLong(), anyLong(), anyInt(), anyInt(), anyLong()); | ||
| 115 | + verify(groupRequestMapper).insert(any(ImGroupRequestDO.class)); | ||
| 116 | + // 1503 推送给 owner(99) + admin(98),去重后两条 | ||
| 117 | + verify(websocketService, times(2)).sendPrivateMessageAsync(anyLong(), any(ImPrivateMessageDTO.class)); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + @Test | ||
| 121 | + public void testApplyJoinGroup_alreadyMember_throws() { | ||
| 122 | + ImGroupDO group = ImGroupDO.builder().id(10L).ownerUserId(99L) | ||
| 123 | + .joinType(ImGroupJoinTypeEnum.APPLY.getType()) | ||
| 124 | + .status(CommonStatusEnum.ENABLE.getStatus()).build(); | ||
| 125 | + when(groupService.validateGroupExists(10L)).thenReturn(group); | ||
| 126 | + when(groupMemberService.getGroupMember(10L, 1L)).thenReturn( | ||
| 127 | + ImGroupMemberDO.builder().groupId(10L).userId(1L) | ||
| 128 | + .status(CommonStatusEnum.ENABLE.getStatus()).build()); | ||
| 129 | + | ||
| 130 | + ImGroupRequestApplyReqVO reqVO = new ImGroupRequestApplyReqVO(); | ||
| 131 | + reqVO.setGroupId(10L); | ||
| 132 | + | ||
| 133 | + ServiceException exception = assertThrows(ServiceException.class, | ||
| 134 | + () -> groupRequestService.applyJoinGroup(1L, reqVO)); | ||
| 135 | + assertEquals(GROUP_REQUEST_ALREADY_MEMBER.getCode(), exception.getCode()); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + // ==================== agreeGroupRequest ==================== | ||
| 139 | + | ||
| 140 | + @Test | ||
| 141 | + public void testAgreeGroupRequest_success_activeApply() { | ||
| 142 | + // 准备:主动申请未处理,操作人是 admin | ||
| 143 | + ImGroupRequestDO request = new ImGroupRequestDO() | ||
| 144 | + .setGroupId(10L).setUserId(2L).setInviterUserId(null) | ||
| 145 | + .setAddSource(ImGroupAddSourceEnum.SEARCH.getSource()) | ||
| 146 | + .setHandleResult(ImGroupRequestHandleResultEnum.UNHANDLED.getResult()); | ||
| 147 | + request.setId(50L); | ||
| 148 | + when(groupRequestMapper.selectById(50L)).thenReturn(request); | ||
| 149 | + when(groupMemberService.validateMemberInGroup(10L, 1L)).thenReturn( | ||
| 150 | + ImGroupMemberDO.builder().groupId(10L).userId(1L) | ||
| 151 | + .role(ImGroupMemberRoleEnum.ADMIN.getRole()) | ||
| 152 | + .status(CommonStatusEnum.ENABLE.getStatus()).build()); | ||
| 153 | + when(groupRequestMapper.updateByIdAndHandleResult(eq(50L), | ||
| 154 | + eq(ImGroupRequestHandleResultEnum.UNHANDLED.getResult()), any())).thenReturn(1); | ||
| 155 | + | ||
| 156 | + ImGroupDO group = ImGroupDO.builder().id(10L).ownerUserId(99L) | ||
| 157 | + .status(CommonStatusEnum.ENABLE.getStatus()).build(); | ||
| 158 | + when(groupService.getGroup(10L)).thenReturn(group); | ||
| 159 | + when(groupMemberService.getActiveGroupMemberListByGroupId(10L)).thenReturn(List.of( | ||
| 160 | + ImGroupMemberDO.builder().groupId(10L).userId(99L) | ||
| 161 | + .role(ImGroupMemberRoleEnum.OWNER.getRole()) | ||
| 162 | + .status(CommonStatusEnum.ENABLE.getStatus()).build())); | ||
| 163 | + | ||
| 164 | + // 调用 | ||
| 165 | + groupRequestService.agreeGroupRequest(1L, 50L); | ||
| 166 | + | ||
| 167 | + // 断言:人数校验 + 写群成员 + 推 1505 + 推 1510(主动申请) | ||
| 168 | + verify(groupService).validateMemberCountLimit(10L, 1); | ||
| 169 | + verify(groupMemberService).addGroupMember(eq(10L), eq(2L), | ||
| 170 | + eq(ImGroupMemberRoleEnum.NORMAL.getRole()), | ||
| 171 | + eq(ImGroupAddSourceEnum.SEARCH.getSource()), isNull()); | ||
| 172 | + ArgumentCaptor<ImGroupMessageSendDTO> dtoCaptor = ArgumentCaptor.forClass(ImGroupMessageSendDTO.class); | ||
| 173 | + verify(groupMessageService).sendGroupMessage(eq(1L), dtoCaptor.capture()); | ||
| 174 | + assertEquals(ImMessageTypeEnum.GROUP_MEMBER_ENTER.getType(), dtoCaptor.getValue().getType()); | ||
| 175 | + // 1505 推送给申请人 + owner,去重后两条 | ||
| 176 | + verify(websocketService, times(2)).sendPrivateMessageAsync(anyLong(), any(ImPrivateMessageDTO.class)); | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + @Test | ||
| 180 | + public void testAgreeGroupRequest_concurrent_secondCallFails() { | ||
| 181 | + // 准备:申请存在但乐观锁更新返回 0(被并发处理过) | ||
| 182 | + ImGroupRequestDO request = new ImGroupRequestDO() | ||
| 183 | + .setGroupId(10L).setUserId(2L) | ||
| 184 | + .setHandleResult(ImGroupRequestHandleResultEnum.UNHANDLED.getResult()); | ||
| 185 | + request.setId(50L); | ||
| 186 | + when(groupRequestMapper.selectById(50L)).thenReturn(request); | ||
| 187 | + when(groupMemberService.validateMemberInGroup(10L, 1L)).thenReturn( | ||
| 188 | + ImGroupMemberDO.builder().groupId(10L).userId(1L) | ||
| 189 | + .role(ImGroupMemberRoleEnum.OWNER.getRole()) | ||
| 190 | + .status(CommonStatusEnum.ENABLE.getStatus()).build()); | ||
| 191 | + when(groupRequestMapper.updateByIdAndHandleResult(eq(50L), | ||
| 192 | + eq(ImGroupRequestHandleResultEnum.UNHANDLED.getResult()), any())).thenReturn(0); | ||
| 193 | + | ||
| 194 | + ServiceException exception = assertThrows(ServiceException.class, | ||
| 195 | + () -> groupRequestService.agreeGroupRequest(1L, 50L)); | ||
| 196 | + assertEquals(GROUP_REQUEST_HANDLED.getCode(), exception.getCode()); | ||
| 197 | + // 不写群成员 | ||
| 198 | + verify(groupMemberService, never()).addGroupMember(anyLong(), anyLong(), anyInt(), any(), any()); | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + @Test | ||
| 202 | + public void testAgreeGroupRequest_notOwnerOrAdmin_throws() { | ||
| 203 | + ImGroupRequestDO request = new ImGroupRequestDO() | ||
| 204 | + .setGroupId(10L).setUserId(2L) | ||
| 205 | + .setHandleResult(ImGroupRequestHandleResultEnum.UNHANDLED.getResult()); | ||
| 206 | + request.setId(50L); | ||
| 207 | + when(groupRequestMapper.selectById(50L)).thenReturn(request); | ||
| 208 | + when(groupMemberService.validateMemberInGroup(10L, 1L)).thenReturn( | ||
| 209 | + ImGroupMemberDO.builder().groupId(10L).userId(1L) | ||
| 210 | + .role(ImGroupMemberRoleEnum.NORMAL.getRole()) | ||
| 211 | + .status(CommonStatusEnum.ENABLE.getStatus()).build()); | ||
| 212 | + | ||
| 213 | + ServiceException exception = assertThrows(ServiceException.class, | ||
| 214 | + () -> groupRequestService.agreeGroupRequest(1L, 50L)); | ||
| 215 | + assertEquals(GROUP_REQUEST_NOT_TO_ME.getCode(), exception.getCode()); | ||
| 216 | + } | ||
| 217 | + | ||
| 218 | + // ==================== refuseGroupRequest ==================== | ||
| 219 | + | ||
| 220 | + @Test | ||
| 221 | + public void testRefuseGroupRequest_success() { | ||
| 222 | + ImGroupRequestDO request = new ImGroupRequestDO() | ||
| 223 | + .setGroupId(10L).setUserId(2L) | ||
| 224 | + .setHandleResult(ImGroupRequestHandleResultEnum.UNHANDLED.getResult()); | ||
| 225 | + request.setId(50L); | ||
| 226 | + when(groupRequestMapper.selectById(50L)).thenReturn(request); | ||
| 227 | + when(groupMemberService.validateMemberInGroup(10L, 1L)).thenReturn( | ||
| 228 | + ImGroupMemberDO.builder().groupId(10L).userId(1L) | ||
| 229 | + .role(ImGroupMemberRoleEnum.OWNER.getRole()) | ||
| 230 | + .status(CommonStatusEnum.ENABLE.getStatus()).build()); | ||
| 231 | + when(groupRequestMapper.updateByIdAndHandleResult(eq(50L), | ||
| 232 | + eq(ImGroupRequestHandleResultEnum.UNHANDLED.getResult()), any())).thenReturn(1); | ||
| 233 | + | ||
| 234 | + ImGroupDO group = ImGroupDO.builder().id(10L).ownerUserId(1L) | ||
| 235 | + .status(CommonStatusEnum.ENABLE.getStatus()).build(); | ||
| 236 | + when(groupService.getGroup(10L)).thenReturn(group); | ||
| 237 | + when(groupMemberService.getActiveGroupMemberListByGroupId(10L)).thenReturn(List.of( | ||
| 238 | + ImGroupMemberDO.builder().groupId(10L).userId(1L) | ||
| 239 | + .role(ImGroupMemberRoleEnum.OWNER.getRole()) | ||
| 240 | + .status(CommonStatusEnum.ENABLE.getStatus()).build())); | ||
| 241 | + | ||
| 242 | + groupRequestService.refuseGroupRequest(1L, 50L, "暂不通过"); | ||
| 243 | + | ||
| 244 | + // 不写群成员;推 1506 给申请人 + 群主(同一人 1L 时去重为 1 + 申请人 2L = 2 条) | ||
| 245 | + verify(groupMemberService, never()).addGroupMember(anyLong(), anyLong(), anyInt(), any(), any()); | ||
| 246 | + verify(websocketService, times(2)).sendPrivateMessageAsync(anyLong(), any(ImPrivateMessageDTO.class)); | ||
| 247 | + } | ||
| 248 | + | ||
| 249 | + // ==================== createInviteRequests ==================== | ||
| 250 | + | ||
| 251 | + @Test | ||
| 252 | + public void testCreateInviteRequests_success() { | ||
| 253 | + ImGroupDO group = ImGroupDO.builder().id(10L).ownerUserId(99L) | ||
| 254 | + .joinType(ImGroupJoinTypeEnum.APPLY_AND_NORMAL_INVITE.getType()) | ||
| 255 | + .status(CommonStatusEnum.ENABLE.getStatus()).build(); | ||
| 256 | + when(groupService.validateGroupExists(10L)).thenReturn(group); | ||
| 257 | + when(groupMemberService.getActiveGroupMemberListByGroupId(10L)).thenReturn(List.of( | ||
| 258 | + ImGroupMemberDO.builder().groupId(10L).userId(99L) | ||
| 259 | + .role(ImGroupMemberRoleEnum.OWNER.getRole()) | ||
| 260 | + .status(CommonStatusEnum.ENABLE.getStatus()).build())); | ||
| 261 | + when(adminUserApi.getUserMap(anyCollection())).thenReturn(java.util.Map.of( | ||
| 262 | + 2L, buildUser(2L, "用户A"), | ||
| 263 | + 3L, buildUser(3L, "用户B"))); | ||
| 264 | + | ||
| 265 | + // 调用:邀请人 1L 邀请 2L、3L | ||
| 266 | + groupRequestService.createInviteRequests(10L, 1L, List.of(2L, 3L)); | ||
| 267 | + | ||
| 268 | + // 断言:插入 2 条申请记录 + 推 1503 给 owner(每条 1 帧)共 2 帧 | ||
| 269 | + verify(groupRequestMapper, times(2)).insert(any(ImGroupRequestDO.class)); | ||
| 270 | + verify(websocketService, times(2)).sendPrivateMessageAsync(anyLong(), any(ImPrivateMessageDTO.class)); | ||
| 271 | + } | ||
| 272 | + | ||
| 273 | + private AdminUserRespDTO buildUser(Long id, String nickname) { | ||
| 274 | + AdminUserRespDTO user = new AdminUserRespDTO(); | ||
| 275 | + user.setId(id); | ||
| 276 | + user.setNickname(nickname); | ||
| 277 | + return user; | ||
| 278 | + } | ||
| 279 | + | ||
| 280 | +} |
yudao-module-im/src/test/java/cn/iocoder/yudao/module/im/service/group/ImGroupServiceImplTest.java
| @@ -15,6 +15,8 @@ import cn.iocoder.yudao.module.im.dal.dataobject.friend.ImFriendDO; | @@ -15,6 +15,8 @@ import cn.iocoder.yudao.module.im.dal.dataobject.friend.ImFriendDO; | ||
| 15 | import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupDO; | 15 | import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupDO; |
| 16 | import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupMemberDO; | 16 | import cn.iocoder.yudao.module.im.dal.dataobject.group.ImGroupMemberDO; |
| 17 | import cn.iocoder.yudao.module.im.dal.mysql.group.ImGroupMapper; | 17 | import cn.iocoder.yudao.module.im.dal.mysql.group.ImGroupMapper; |
| 18 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupAddSourceEnum; | ||
| 19 | +import cn.iocoder.yudao.module.im.enums.group.ImGroupJoinTypeEnum; | ||
| 18 | import cn.iocoder.yudao.module.im.enums.group.ImGroupMemberRoleEnum; | 20 | import cn.iocoder.yudao.module.im.enums.group.ImGroupMemberRoleEnum; |
| 19 | import cn.iocoder.yudao.module.im.enums.message.ImMessageTypeEnum; | 21 | import cn.iocoder.yudao.module.im.enums.message.ImMessageTypeEnum; |
| 20 | import cn.iocoder.yudao.module.im.service.friend.ImFriendService; | 22 | import cn.iocoder.yudao.module.im.service.friend.ImFriendService; |
| @@ -62,6 +64,8 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | @@ -62,6 +64,8 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | ||
| 62 | @Mock | 64 | @Mock |
| 63 | private ImFriendService friendService; | 65 | private ImFriendService friendService; |
| 64 | @Mock | 66 | @Mock |
| 67 | + private ImGroupRequestService groupRequestService; | ||
| 68 | + @Mock | ||
| 65 | private AdminUserApi adminUserApi; | 69 | private AdminUserApi adminUserApi; |
| 66 | 70 | ||
| 67 | // ========== createGroup ========== | 71 | // ========== createGroup ========== |
| @@ -89,7 +93,7 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | @@ -89,7 +93,7 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | ||
| 89 | verify(groupMemberService, never()).addGroupMembers(anyLong(), anyCollection()); | 93 | verify(groupMemberService, never()).addGroupMembers(anyLong(), anyCollection()); |
| 90 | // 验证:推送 GROUP_CREATE 通知(payload memberUserIds 含创建者自己) | 94 | // 验证:推送 GROUP_CREATE 通知(payload memberUserIds 含创建者自己) |
| 91 | ArgumentCaptor<ImGroupMessageSendDTO> dtoCaptor = ArgumentCaptor.forClass(ImGroupMessageSendDTO.class); | 95 | ArgumentCaptor<ImGroupMessageSendDTO> dtoCaptor = ArgumentCaptor.forClass(ImGroupMessageSendDTO.class); |
| 92 | - verify(groupMessageService).sendGroupMessage(eq(1L), dtoCaptor.capture()); | 96 | + verify(groupMessageService).sendGroupMessage(eq(1L), anyCollection(), dtoCaptor.capture()); |
| 93 | assertEquals(ImMessageTypeEnum.GROUP_CREATE.getType(), dtoCaptor.getValue().getType()); | 97 | assertEquals(ImMessageTypeEnum.GROUP_CREATE.getType(), dtoCaptor.getValue().getType()); |
| 94 | } | 98 | } |
| 95 | 99 | ||
| @@ -117,10 +121,11 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | @@ -117,10 +121,11 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | ||
| 117 | // 断言:群创建成功 + 创建者 + 初始成员都加入 | 121 | // 断言:群创建成功 + 创建者 + 初始成员都加入 |
| 118 | assertEquals(100L, result.getId()); | 122 | assertEquals(100L, result.getId()); |
| 119 | verify(groupMemberService).addGroupMember(100L, 1L, ImGroupMemberRoleEnum.OWNER.getRole()); | 123 | verify(groupMemberService).addGroupMember(100L, 1L, ImGroupMemberRoleEnum.OWNER.getRole()); |
| 120 | - verify(groupMemberService).addGroupMembers(eq(100L), anyCollection()); | 124 | + verify(groupMemberService).addGroupMembers(eq(100L), anyCollection(), |
| 125 | + eq(ImGroupAddSourceEnum.INVITE.getSource()), eq(1L)); | ||
| 121 | // 验证:推送 GROUP_CREATE 通知,payload memberUserIds 含全员(创建者 + 邀请) | 126 | // 验证:推送 GROUP_CREATE 通知,payload memberUserIds 含全员(创建者 + 邀请) |
| 122 | ArgumentCaptor<ImGroupMessageSendDTO> dtoCaptor = ArgumentCaptor.forClass(ImGroupMessageSendDTO.class); | 127 | ArgumentCaptor<ImGroupMessageSendDTO> dtoCaptor = ArgumentCaptor.forClass(ImGroupMessageSendDTO.class); |
| 123 | - verify(groupMessageService).sendGroupMessage(eq(1L), dtoCaptor.capture()); | 128 | + verify(groupMessageService).sendGroupMessage(eq(1L), anyCollection(), dtoCaptor.capture()); |
| 124 | assertEquals(ImMessageTypeEnum.GROUP_CREATE.getType(), dtoCaptor.getValue().getType()); | 129 | assertEquals(ImMessageTypeEnum.GROUP_CREATE.getType(), dtoCaptor.getValue().getType()); |
| 125 | } | 130 | } |
| 126 | 131 | ||
| @@ -193,7 +198,7 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | @@ -193,7 +198,7 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | ||
| 193 | assertEquals("新名字", result.getName()); | 198 | assertEquals("新名字", result.getName()); |
| 194 | // 推送 GROUP_NAME_UPDATE 通知给全员 | 199 | // 推送 GROUP_NAME_UPDATE 通知给全员 |
| 195 | ArgumentCaptor<ImGroupMessageSendDTO> dtoCaptor = ArgumentCaptor.forClass(ImGroupMessageSendDTO.class); | 200 | ArgumentCaptor<ImGroupMessageSendDTO> dtoCaptor = ArgumentCaptor.forClass(ImGroupMessageSendDTO.class); |
| 196 | - verify(groupMessageService).sendGroupMessage(eq(1L), dtoCaptor.capture()); | 201 | + verify(groupMessageService).sendGroupMessage(eq(1L), anyCollection(), dtoCaptor.capture()); |
| 197 | assertEquals(ImMessageTypeEnum.GROUP_NAME_UPDATE.getType(), dtoCaptor.getValue().getType()); | 202 | assertEquals(ImMessageTypeEnum.GROUP_NAME_UPDATE.getType(), dtoCaptor.getValue().getType()); |
| 198 | } | 203 | } |
| 199 | } | 204 | } |
| @@ -210,13 +215,6 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | @@ -210,13 +215,6 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | ||
| 210 | ImGroupDO group = ImGroupDO.builder().id(10L).name("群").ownerUserId(1L) | 215 | ImGroupDO group = ImGroupDO.builder().id(10L).name("群").ownerUserId(1L) |
| 211 | .status(CommonStatusEnum.ENABLE.getStatus()).build(); | 216 | .status(CommonStatusEnum.ENABLE.getStatus()).build(); |
| 212 | when(groupMapper.selectById(10L)).thenReturn(group); | 217 | when(groupMapper.selectById(10L)).thenReturn(group); |
| 213 | - List<ImGroupMemberDO> members = List.of( | ||
| 214 | - ImGroupMemberDO.builder().groupId(10L).userId(1L) | ||
| 215 | - .status(CommonStatusEnum.ENABLE.getStatus()).build(), | ||
| 216 | - ImGroupMemberDO.builder().groupId(10L).userId(2L) | ||
| 217 | - .status(CommonStatusEnum.ENABLE.getStatus()).build() | ||
| 218 | - ); | ||
| 219 | - when(groupMemberService.getActiveGroupMemberListByGroupId(10L)).thenReturn(members); | ||
| 220 | 218 | ||
| 221 | // 调用 | 219 | // 调用 |
| 222 | groupService.dissolveGroup(10L, 1L); | 220 | groupService.dissolveGroup(10L, 1L); |
| @@ -259,14 +257,20 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | @@ -259,14 +257,20 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | ||
| 259 | springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(ImGroupServiceImpl.class))) | 257 | springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(ImGroupServiceImpl.class))) |
| 260 | .thenReturn(groupService); | 258 | .thenReturn(groupService); |
| 261 | 259 | ||
| 262 | - // 准备:群存在 + 当前用户是群成员 | 260 | + // 准备:群存在 + joinType 默认 FREE + 当前用户是群主(操作人必走「直进」分支) |
| 263 | ImGroupDO group = ImGroupDO.builder().id(10L).ownerUserId(1L) | 261 | ImGroupDO group = ImGroupDO.builder().id(10L).ownerUserId(1L) |
| 262 | + .joinType(ImGroupJoinTypeEnum.FREE.getType()) | ||
| 264 | .status(CommonStatusEnum.ENABLE.getStatus()).build(); | 263 | .status(CommonStatusEnum.ENABLE.getStatus()).build(); |
| 265 | when(groupMapper.selectById(10L)).thenReturn(group); | 264 | when(groupMapper.selectById(10L)).thenReturn(group); |
| 265 | + when(groupMemberService.validateMemberInGroup(10L, 1L)).thenReturn( | ||
| 266 | + ImGroupMemberDO.builder().groupId(10L).userId(1L) | ||
| 267 | + .role(ImGroupMemberRoleEnum.OWNER.getRole()) | ||
| 268 | + .status(CommonStatusEnum.ENABLE.getStatus()).build()); | ||
| 266 | 269 | ||
| 267 | // 当前成员只有群主 | 270 | // 当前成员只有群主 |
| 268 | List<ImGroupMemberDO> activeMembers = new ArrayList<>(); | 271 | List<ImGroupMemberDO> activeMembers = new ArrayList<>(); |
| 269 | activeMembers.add(ImGroupMemberDO.builder().groupId(10L).userId(1L) | 272 | activeMembers.add(ImGroupMemberDO.builder().groupId(10L).userId(1L) |
| 273 | + .role(ImGroupMemberRoleEnum.OWNER.getRole()) | ||
| 270 | .status(CommonStatusEnum.ENABLE.getStatus()).build()); | 274 | .status(CommonStatusEnum.ENABLE.getStatus()).build()); |
| 271 | when(groupMemberService.getActiveGroupMemberListByGroupId(10L)).thenReturn(activeMembers); | 275 | when(groupMemberService.getActiveGroupMemberListByGroupId(10L)).thenReturn(activeMembers); |
| 272 | 276 | ||
| @@ -285,17 +289,98 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | @@ -285,17 +289,98 @@ public class ImGroupServiceImplTest extends BaseMockitoUnitTest { | ||
| 285 | // 调用 | 289 | // 调用 |
| 286 | groupService.inviteGroupMember(1L, reqVO); | 290 | groupService.inviteGroupMember(1L, reqVO); |
| 287 | 291 | ||
| 288 | - // 断言:校验群成员 + 批量添加成员 + 推送 GROUP_MEMBER_INVITE 通知(被邀请人 bootstrap 由前端按 memberUserIds 自判,不再发 GROUP_CREATE) | 292 | + // 断言:校验群成员 + 批量添加成员(带 INVITE 来源 + 邀请人)+ 推送 GROUP_MEMBER_INVITE |
| 289 | verify(groupMemberService).validateMemberInGroup(10L, 1L); | 293 | verify(groupMemberService).validateMemberInGroup(10L, 1L); |
| 290 | - verify(groupMemberService).addGroupMembers(eq(10L), anyCollection()); | 294 | + verify(groupMemberService).addGroupMembers(eq(10L), anyCollection(), |
| 295 | + eq(ImGroupAddSourceEnum.INVITE.getSource()), eq(1L)); | ||
| 296 | + verify(groupRequestService, never()).createInviteRequests(anyLong(), anyLong(), anyCollection()); | ||
| 291 | verify(webSocketService, never()).sendGroupMessageAsync(anyCollection(), any(ImGroupMessageDTO.class)); | 297 | verify(webSocketService, never()).sendGroupMessageAsync(anyCollection(), any(ImGroupMessageDTO.class)); |
| 292 | ArgumentCaptor<ImGroupMessageSendDTO> dtoCaptor = ArgumentCaptor.forClass(ImGroupMessageSendDTO.class); | 298 | ArgumentCaptor<ImGroupMessageSendDTO> dtoCaptor = ArgumentCaptor.forClass(ImGroupMessageSendDTO.class); |
| 293 | - verify(groupMessageService).sendGroupMessage(eq(1L), dtoCaptor.capture()); | 299 | + verify(groupMessageService).sendGroupMessage(eq(1L), anyCollection(), dtoCaptor.capture()); |
| 294 | assertEquals(ImMessageTypeEnum.GROUP_MEMBER_INVITE.getType(), dtoCaptor.getValue().getType()); | 300 | assertEquals(ImMessageTypeEnum.GROUP_MEMBER_INVITE.getType(), dtoCaptor.getValue().getType()); |
| 295 | } | 301 | } |
| 296 | } | 302 | } |
| 297 | 303 | ||
| 298 | @Test | 304 | @Test |
| 305 | + public void testInviteGroupMember_strictMode_normalMember_routesToApproval() { | ||
| 306 | + try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||
| 307 | + springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(ImGroupServiceImpl.class))) | ||
| 308 | + .thenReturn(groupService); | ||
| 309 | + | ||
| 310 | + // 准备:joinType=APPLY_AND_NORMAL_INVITE 且操作人是普通成员;应走审批分支,落 group_request | ||
| 311 | + ImGroupDO group = ImGroupDO.builder().id(10L).ownerUserId(99L) | ||
| 312 | + .joinType(ImGroupJoinTypeEnum.APPLY_AND_NORMAL_INVITE.getType()) | ||
| 313 | + .status(CommonStatusEnum.ENABLE.getStatus()).build(); | ||
| 314 | + when(groupMapper.selectById(10L)).thenReturn(group); | ||
| 315 | + when(groupMemberService.validateMemberInGroup(10L, 1L)).thenReturn( | ||
| 316 | + ImGroupMemberDO.builder().groupId(10L).userId(1L) | ||
| 317 | + .role(ImGroupMemberRoleEnum.NORMAL.getRole()) | ||
| 318 | + .status(CommonStatusEnum.ENABLE.getStatus()).build()); | ||
| 319 | + when(groupMemberService.getActiveGroupMemberListByGroupId(10L)).thenReturn(List.of( | ||
| 320 | + ImGroupMemberDO.builder().groupId(10L).userId(99L) | ||
| 321 | + .role(ImGroupMemberRoleEnum.OWNER.getRole()) | ||
| 322 | + .status(CommonStatusEnum.ENABLE.getStatus()).build(), | ||
| 323 | + ImGroupMemberDO.builder().groupId(10L).userId(1L) | ||
| 324 | + .role(ImGroupMemberRoleEnum.NORMAL.getRole()) | ||
| 325 | + .status(CommonStatusEnum.ENABLE.getStatus()).build())); | ||
| 326 | + | ||
| 327 | + ImGroupMemberInviteReqVO reqVO = new ImGroupMemberInviteReqVO(); | ||
| 328 | + reqVO.setGroupId(10L); | ||
| 329 | + reqVO.setMemberUserIds(new ArrayList<>(List.of(2L, 3L))); | ||
| 330 | + when(friendService.getActiveFriendList(eq(1L), anyCollection())).thenReturn(List.of( | ||
| 331 | + ImFriendDO.builder().userId(1L).friendUserId(2L) | ||
| 332 | + .status(CommonStatusEnum.ENABLE.getStatus()).build(), | ||
| 333 | + ImFriendDO.builder().userId(1L).friendUserId(3L) | ||
| 334 | + .status(CommonStatusEnum.ENABLE.getStatus()).build())); | ||
| 335 | + | ||
| 336 | + // 调用 | ||
| 337 | + groupService.inviteGroupMember(1L, reqVO); | ||
| 338 | + | ||
| 339 | + // 断言:走审批分支:调 createInviteRequests;不写群成员、不推 1509 | ||
| 340 | + verify(groupRequestService).createInviteRequests(eq(10L), eq(1L), anyCollection()); | ||
| 341 | + verify(groupMemberService, never()).addGroupMembers(anyLong(), anyCollection(), any(), any()); | ||
| 342 | + verify(groupMessageService, never()).sendGroupMessage(anyLong(), any(ImGroupMessageSendDTO.class)); | ||
| 343 | + } | ||
| 344 | + } | ||
| 345 | + | ||
| 346 | + @Test | ||
| 347 | + public void testInviteGroupMember_strictMode_owner_directJoin() { | ||
| 348 | + try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||
| 349 | + springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(ImGroupServiceImpl.class))) | ||
| 350 | + .thenReturn(groupService); | ||
| 351 | + | ||
| 352 | + // 准备:joinType=APPLY_AND_NORMAL_INVITE 但操作人是群主;走「直进」分支 | ||
| 353 | + ImGroupDO group = ImGroupDO.builder().id(10L).ownerUserId(1L) | ||
| 354 | + .joinType(ImGroupJoinTypeEnum.APPLY_AND_NORMAL_INVITE.getType()) | ||
| 355 | + .status(CommonStatusEnum.ENABLE.getStatus()).build(); | ||
| 356 | + when(groupMapper.selectById(10L)).thenReturn(group); | ||
| 357 | + when(groupMemberService.validateMemberInGroup(10L, 1L)).thenReturn( | ||
| 358 | + ImGroupMemberDO.builder().groupId(10L).userId(1L) | ||
| 359 | + .role(ImGroupMemberRoleEnum.OWNER.getRole()) | ||
| 360 | + .status(CommonStatusEnum.ENABLE.getStatus()).build()); | ||
| 361 | + when(groupMemberService.getActiveGroupMemberListByGroupId(10L)).thenReturn(List.of( | ||
| 362 | + ImGroupMemberDO.builder().groupId(10L).userId(1L) | ||
| 363 | + .role(ImGroupMemberRoleEnum.OWNER.getRole()) | ||
| 364 | + .status(CommonStatusEnum.ENABLE.getStatus()).build())); | ||
| 365 | + | ||
| 366 | + ImGroupMemberInviteReqVO reqVO = new ImGroupMemberInviteReqVO(); | ||
| 367 | + reqVO.setGroupId(10L); | ||
| 368 | + reqVO.setMemberUserIds(new ArrayList<>(List.of(2L))); | ||
| 369 | + when(friendService.getActiveFriendList(eq(1L), anyCollection())).thenReturn(List.of( | ||
| 370 | + ImFriendDO.builder().userId(1L).friendUserId(2L) | ||
| 371 | + .status(CommonStatusEnum.ENABLE.getStatus()).build())); | ||
| 372 | + | ||
| 373 | + // 调用 | ||
| 374 | + groupService.inviteGroupMember(1L, reqVO); | ||
| 375 | + | ||
| 376 | + // 断言:走直进分支 | ||
| 377 | + verify(groupMemberService).addGroupMembers(eq(10L), anyCollection(), | ||
| 378 | + eq(ImGroupAddSourceEnum.INVITE.getSource()), eq(1L)); | ||
| 379 | + verify(groupRequestService, never()).createInviteRequests(anyLong(), anyLong(), anyCollection()); | ||
| 380 | + } | ||
| 381 | + } | ||
| 382 | + | ||
| 383 | + @Test | ||
| 299 | public void testInviteGroupMember_notFriend() { | 384 | public void testInviteGroupMember_notFriend() { |
| 300 | try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | 385 | try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { |
| 301 | springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(ImGroupServiceImpl.class))) | 386 | springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(ImGroupServiceImpl.class))) |
yudao-module-im/src/test/resources/sql/clean.sql
| @@ -4,3 +4,7 @@ DELETE FROM "im_group"; | @@ -4,3 +4,7 @@ DELETE FROM "im_group"; | ||
| 4 | DELETE FROM "im_group_member"; | 4 | DELETE FROM "im_group_member"; |
| 5 | DELETE FROM "im_friend"; | 5 | DELETE FROM "im_friend"; |
| 6 | DELETE FROM "im_friend_request"; | 6 | DELETE FROM "im_friend_request"; |
| 7 | +DELETE FROM "im_group_request"; | ||
| 8 | +DELETE FROM "im_face_pack"; | ||
| 9 | +DELETE FROM "im_face_pack_item"; | ||
| 10 | +DELETE FROM "im_face_user_item"; |
yudao-module-im/src/test/resources/sql/create_tables.sql
| @@ -49,6 +49,7 @@ CREATE TABLE IF NOT EXISTS "im_group" ( | @@ -49,6 +49,7 @@ CREATE TABLE IF NOT EXISTS "im_group" ( | ||
| 49 | "status" tinyint NOT NULL COMMENT '群状态', | 49 | "status" tinyint NOT NULL COMMENT '群状态', |
| 50 | "dissolved_time" timestamp DEFAULT NULL COMMENT '解散时间', | 50 | "dissolved_time" timestamp DEFAULT NULL COMMENT '解散时间', |
| 51 | "muted_all" bit DEFAULT FALSE COMMENT '是否全群禁言', | 51 | "muted_all" bit DEFAULT FALSE COMMENT '是否全群禁言', |
| 52 | + "join_type" tinyint NOT NULL DEFAULT 0 COMMENT '加群方式;0自由进群 1申请需审批 2申请和普通成员邀请均需审批', | ||
| 52 | "pinned_message_ids" varchar(128) DEFAULT NULL COMMENT '群置顶消息编号列表,逗号分隔', | 53 | "pinned_message_ids" varchar(128) DEFAULT NULL COMMENT '群置顶消息编号列表,逗号分隔', |
| 53 | "creator" varchar(64) DEFAULT '', | 54 | "creator" varchar(64) DEFAULT '', |
| 54 | "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | 55 | "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| @@ -69,6 +70,8 @@ CREATE TABLE IF NOT EXISTS "im_group_member" ( | @@ -69,6 +70,8 @@ CREATE TABLE IF NOT EXISTS "im_group_member" ( | ||
| 69 | "status" tinyint NOT NULL COMMENT '成员状态', | 70 | "status" tinyint NOT NULL COMMENT '成员状态', |
| 70 | "role" tinyint NOT NULL DEFAULT 3 COMMENT '成员角色:1=群主 2=管理员 3=普通成员', | 71 | "role" tinyint NOT NULL DEFAULT 3 COMMENT '成员角色:1=群主 2=管理员 3=普通成员', |
| 71 | "join_time" timestamp DEFAULT NULL COMMENT '入群时间', | 72 | "join_time" timestamp DEFAULT NULL COMMENT '入群时间', |
| 73 | + "add_source" tinyint DEFAULT NULL COMMENT '加入来源', | ||
| 74 | + "inviter_user_id" bigint DEFAULT NULL COMMENT '邀请人用户编号;用户主动申请进群时为 NULL', | ||
| 72 | "quit_time" timestamp DEFAULT NULL COMMENT '退群时间', | 75 | "quit_time" timestamp DEFAULT NULL COMMENT '退群时间', |
| 73 | "mute_end_time" timestamp DEFAULT NULL COMMENT '禁言到期时间', | 76 | "mute_end_time" timestamp DEFAULT NULL COMMENT '禁言到期时间', |
| 74 | "creator" varchar(64) DEFAULT '', | 77 | "creator" varchar(64) DEFAULT '', |
| @@ -120,3 +123,74 @@ CREATE TABLE IF NOT EXISTS "im_friend_request" ( | @@ -120,3 +123,74 @@ CREATE TABLE IF NOT EXISTS "im_friend_request" ( | ||
| 120 | "tenant_id" bigint NOT NULL DEFAULT 0, | 123 | "tenant_id" bigint NOT NULL DEFAULT 0, |
| 121 | PRIMARY KEY ("id") | 124 | PRIMARY KEY ("id") |
| 122 | ) COMMENT 'IM 好友申请记录表'; | 125 | ) COMMENT 'IM 好友申请记录表'; |
| 126 | + | ||
| 127 | +CREATE TABLE IF NOT EXISTS "im_group_request" ( | ||
| 128 | + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '编号', | ||
| 129 | + "group_id" bigint NOT NULL COMMENT '群编号', | ||
| 130 | + "user_id" bigint NOT NULL COMMENT '申请人 / 被邀请人用户编号', | ||
| 131 | + "inviter_user_id" bigint DEFAULT NULL COMMENT '邀请人用户编号;NULL=主动申请;非NULL=被邀请待审批', | ||
| 132 | + "apply_content" varchar(255) DEFAULT NULL COMMENT '申请理由', | ||
| 133 | + "add_source" tinyint DEFAULT NULL COMMENT '加入来源', | ||
| 134 | + "handle_result" tinyint NOT NULL DEFAULT 0 COMMENT '处理结果;0未处理;1同意;2拒绝', | ||
| 135 | + "handle_user_id" bigint DEFAULT NULL COMMENT '处理人用户编号', | ||
| 136 | + "handle_content" varchar(255) DEFAULT NULL COMMENT '处理理由', | ||
| 137 | + "handle_time" timestamp DEFAULT NULL COMMENT '处理时间', | ||
| 138 | + "creator" varchar(64) DEFAULT '', | ||
| 139 | + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| 140 | + "updater" varchar(64) DEFAULT '', | ||
| 141 | + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| 142 | + "deleted" bit NOT NULL DEFAULT FALSE, | ||
| 143 | + "tenant_id" bigint NOT NULL DEFAULT 0, | ||
| 144 | + PRIMARY KEY ("id") | ||
| 145 | +) COMMENT 'IM 加群申请记录表'; | ||
| 146 | + | ||
| 147 | +CREATE TABLE IF NOT EXISTS "im_face_pack" ( | ||
| 148 | + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '编号', | ||
| 149 | + "name" varchar(64) NOT NULL COMMENT '表情包名称', | ||
| 150 | + "icon_url" varchar(512) DEFAULT NULL COMMENT '表情包图标 URL(面板底部 tab 显示)', | ||
| 151 | + "sort" int NOT NULL DEFAULT 0 COMMENT '排序', | ||
| 152 | + "status" tinyint NOT NULL COMMENT '状态', | ||
| 153 | + "creator" varchar(64) DEFAULT '', | ||
| 154 | + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| 155 | + "updater" varchar(64) DEFAULT '', | ||
| 156 | + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| 157 | + "deleted" bit NOT NULL DEFAULT FALSE, | ||
| 158 | + "tenant_id" bigint NOT NULL DEFAULT 0, | ||
| 159 | + PRIMARY KEY ("id") | ||
| 160 | +) COMMENT 'IM 表情包表'; | ||
| 161 | + | ||
| 162 | +CREATE TABLE IF NOT EXISTS "im_face_pack_item" ( | ||
| 163 | + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '编号', | ||
| 164 | + "pack_id" bigint NOT NULL COMMENT '所属表情包编号', | ||
| 165 | + "url" varchar(512) NOT NULL COMMENT '表情图 URL', | ||
| 166 | + "name" varchar(64) DEFAULT NULL COMMENT '表情名(可选;如「狗头」「捂脸」)', | ||
| 167 | + "width" int NOT NULL DEFAULT 0 COMMENT '渲染宽度(像素)', | ||
| 168 | + "height" int NOT NULL DEFAULT 0 COMMENT '渲染高度(像素)', | ||
| 169 | + "sort" int NOT NULL DEFAULT 0 COMMENT '排序', | ||
| 170 | + "status" tinyint NOT NULL COMMENT '状态', | ||
| 171 | + "creator" varchar(64) DEFAULT '', | ||
| 172 | + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| 173 | + "updater" varchar(64) DEFAULT '', | ||
| 174 | + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| 175 | + "deleted" bit NOT NULL DEFAULT FALSE, | ||
| 176 | + "tenant_id" bigint NOT NULL DEFAULT 0, | ||
| 177 | + PRIMARY KEY ("id") | ||
| 178 | +) COMMENT 'IM 表情包项表'; | ||
| 179 | + | ||
| 180 | +CREATE TABLE IF NOT EXISTS "im_face_user_item" ( | ||
| 181 | + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY COMMENT '编号', | ||
| 182 | + "user_id" bigint NOT NULL COMMENT '所属用户编号', | ||
| 183 | + "url" varchar(512) NOT NULL COMMENT '表情图 URL', | ||
| 184 | + "name" varchar(64) DEFAULT NULL COMMENT '表情名(可选)', | ||
| 185 | + "width" int NOT NULL DEFAULT 0 COMMENT '渲染宽度(像素)', | ||
| 186 | + "height" int NOT NULL DEFAULT 0 COMMENT '渲染高度(像素)', | ||
| 187 | + "source_message_id" bigint DEFAULT NULL COMMENT '来源消息编号(从消息「添加到表情」时记录;自己上传则为 NULL)', | ||
| 188 | + "sort" int NOT NULL DEFAULT 0 COMMENT '排序', | ||
| 189 | + "creator" varchar(64) DEFAULT '', | ||
| 190 | + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| 191 | + "updater" varchar(64) DEFAULT '', | ||
| 192 | + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| 193 | + "deleted" bit NOT NULL DEFAULT FALSE, | ||
| 194 | + "tenant_id" bigint NOT NULL DEFAULT 0, | ||
| 195 | + PRIMARY KEY ("id") | ||
| 196 | +) COMMENT 'IM 用户私有表情表'; |