mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-14 06:03:42 +08:00
feat: 新增添加管理员功能
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package com.abin.mallchat.custom.chat.constant;
|
||||
|
||||
/**
|
||||
* @Author Kkuil
|
||||
* @Date 2023/10/24 16:06
|
||||
* @Description 群常量
|
||||
*/
|
||||
public class GroupConst {
|
||||
|
||||
/**
|
||||
* 最大群管理员数量
|
||||
*/
|
||||
public static final int MAX_MANAGE_COUNT = 3;
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.abin.mallchat.custom.chat.controller;
|
||||
|
||||
|
||||
import com.abin.mallchat.common.chat.service.IGroupMemberService;
|
||||
import com.abin.mallchat.common.common.domain.vo.request.IdReqVO;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.ApiResult;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
@@ -10,6 +11,7 @@ import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.*;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberListResp;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.response.MemberResp;
|
||||
import com.abin.mallchat.custom.chat.service.GroupMemberService;
|
||||
import com.abin.mallchat.custom.chat.service.RoomAppService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
@@ -35,6 +37,8 @@ import java.util.List;
|
||||
public class RoomController {
|
||||
@Autowired
|
||||
private RoomAppService roomService;
|
||||
@Autowired
|
||||
private GroupMemberService groupMemberService;
|
||||
|
||||
@GetMapping("/public/group")
|
||||
@ApiOperation("群组详情")
|
||||
@@ -62,7 +66,6 @@ public class RoomController {
|
||||
roomService.delMember(uid, request);
|
||||
return ApiResult.success();
|
||||
}
|
||||
|
||||
@PostMapping("/group")
|
||||
@ApiOperation("新增群组")
|
||||
public ApiResult<IdRespVO> addGroup(@Valid @RequestBody GroupAddReq request) {
|
||||
@@ -78,5 +81,12 @@ public class RoomController {
|
||||
roomService.addMember(uid, request);
|
||||
return ApiResult.success();
|
||||
}
|
||||
}
|
||||
|
||||
@PutMapping("/group/admin")
|
||||
@ApiOperation("添加管理员")
|
||||
public ApiResult<Boolean> addAdmin(@Valid @RequestBody AdminAddReq request) {
|
||||
Long uid = RequestHolder.get().getUid();
|
||||
groupMemberService.addAdmin(uid, request);
|
||||
return ApiResult.success();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.abin.mallchat.custom.chat.domain.vo.request;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author Kkuil
|
||||
* @Date 2023/10/24 12:46
|
||||
* @Description 添加管理员请求信息
|
||||
*/
|
||||
@Data
|
||||
public class AdminAddReq {
|
||||
@NotNull
|
||||
@ApiModelProperty("房间号")
|
||||
private Long roomId;
|
||||
|
||||
@NotNull
|
||||
@Size(min = 1, max = 3)
|
||||
@ApiModelProperty("需要添加管理的列表")
|
||||
private List<Long> uidList;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.abin.mallchat.custom.chat.service;
|
||||
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.AdminAddReq;
|
||||
|
||||
/**
|
||||
* @Author Kkuil
|
||||
* @Date 2023/10/24 15:45
|
||||
* @Description 群成员服务接口
|
||||
*/
|
||||
public interface GroupMemberService {
|
||||
/**
|
||||
* 增加管理员
|
||||
*
|
||||
* @param uid 用户ID
|
||||
* @param request 请求信息
|
||||
*/
|
||||
void addAdmin(Long uid, AdminAddReq request);
|
||||
}
|
||||
@@ -151,28 +151,30 @@ public class ChatServiceImpl implements ChatService {
|
||||
String timeCursor = pair.getValue();
|
||||
List<ChatMemberResp> resultList = new ArrayList<>();// 最终列表
|
||||
Boolean isLast = Boolean.FALSE;
|
||||
if (activeStatusEnum == ChatActiveStatusEnum.ONLINE) {//在线列表
|
||||
if (activeStatusEnum == ChatActiveStatusEnum.ONLINE) {// 在线列表
|
||||
CursorPageBaseResp<User> cursorPage = userDao.getCursorPage(memberUidList, new CursorPageBaseReq(request.getPageSize(), timeCursor), ChatActiveStatusEnum.ONLINE);
|
||||
resultList.addAll(MemberAdapter.buildMember(request.getRoomId(), cursorPage.getList()));//添加在线列表
|
||||
if (cursorPage.getIsLast()) {//如果是最后一页,从离线列表再补点数据
|
||||
resultList.addAll(MemberAdapter.buildMember(request.getRoomId(), cursorPage.getList()));// 添加在线列表
|
||||
if (cursorPage.getIsLast()) {// 如果是最后一页,从离线列表再补点数据
|
||||
activeStatusEnum = ChatActiveStatusEnum.OFFLINE;
|
||||
Integer leftSize = request.getPageSize() - cursorPage.getList().size();
|
||||
cursorPage = userDao.getCursorPage(memberUidList, new CursorPageBaseReq(leftSize, null), ChatActiveStatusEnum.OFFLINE);
|
||||
resultList.addAll(MemberAdapter.buildMember(request.getRoomId(), cursorPage.getList()));//添加离线线列表
|
||||
resultList.addAll(MemberAdapter.buildMember(request.getRoomId(), cursorPage.getList()));// 添加离线线列表
|
||||
}
|
||||
timeCursor = cursorPage.getCursor();
|
||||
isLast = cursorPage.getIsLast();
|
||||
} else if (activeStatusEnum == ChatActiveStatusEnum.OFFLINE) {//离线列表
|
||||
} else if (activeStatusEnum == ChatActiveStatusEnum.OFFLINE) {// 离线列表
|
||||
CursorPageBaseResp<User> cursorPage = userDao.getCursorPage(memberUidList, new CursorPageBaseReq(request.getPageSize(), timeCursor), ChatActiveStatusEnum.OFFLINE);
|
||||
resultList.addAll(MemberAdapter.buildMember(request.getRoomId(), cursorPage.getList()));//添加离线线列表
|
||||
resultList.addAll(MemberAdapter.buildMember(request.getRoomId(), cursorPage.getList()));// 添加离线线列表
|
||||
timeCursor = cursorPage.getCursor();
|
||||
isLast = cursorPage.getIsLast();
|
||||
}
|
||||
// 获取群成员角色ID
|
||||
List<Long> uidList = resultList.stream().map(ChatMemberResp::getUid).collect(Collectors.toList());
|
||||
RoomGroup roomGroup = roomGroupDao.getByRoomId(request.getRoomId());
|
||||
Map<Long, Integer> uidMapRole = groupMemberDao.getMemberMapRole(roomGroup.getId(), uidList);
|
||||
resultList.forEach(member -> member.setRoleId(uidMapRole.get(member.getUid())));
|
||||
if (!roomDao.getById(request.getRoomId()).isHotRoom()) {
|
||||
// 获取群成员角色ID
|
||||
List<Long> uidList = resultList.stream().map(ChatMemberResp::getUid).collect(Collectors.toList());
|
||||
RoomGroup roomGroup = roomGroupDao.getByRoomId(request.getRoomId());
|
||||
Map<Long, Integer> uidMapRole = groupMemberDao.getMemberMapRole(roomGroup.getId(), uidList);
|
||||
resultList.forEach(member -> member.setRoleId(uidMapRole.get(member.getUid())));
|
||||
}
|
||||
// 组装结果
|
||||
return new CursorPageBaseResp<>(ChatMemberHelper.generateCursor(activeStatusEnum, timeCursor), isLast, resultList);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.abin.mallchat.custom.chat.service.impl;
|
||||
|
||||
import com.abin.mallchat.common.chat.dao.GroupMemberDao;
|
||||
import com.abin.mallchat.common.chat.dao.RoomGroupDao;
|
||||
import com.abin.mallchat.common.chat.domain.entity.RoomGroup;
|
||||
import com.abin.mallchat.common.common.exception.GroupErrorEnum;
|
||||
import com.abin.mallchat.common.common.utils.AssertUtil;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.AdminAddReq;
|
||||
import com.abin.mallchat.custom.chat.service.GroupMemberService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import static com.abin.mallchat.custom.chat.constant.GroupConst.MAX_MANAGE_COUNT;
|
||||
|
||||
/**
|
||||
* @Author Kkuil
|
||||
* @Date 2023/10/24 15:45
|
||||
* @Description 群成员服务类
|
||||
*/
|
||||
@Service
|
||||
public class GroupMemberServiceImpl implements GroupMemberService {
|
||||
|
||||
@Autowired
|
||||
private GroupMemberDao groupMemberDao;
|
||||
|
||||
@Autowired
|
||||
private RoomGroupDao roomGroupDao;
|
||||
|
||||
/**
|
||||
* 增加管理员
|
||||
*
|
||||
* @param uid 用户ID
|
||||
* @param request 请求信息
|
||||
*/
|
||||
@Override
|
||||
public void addAdmin(Long uid, AdminAddReq request) {
|
||||
// 1. 判断群聊是否存在
|
||||
RoomGroup roomGroup = roomGroupDao.getByRoomId(request.getRoomId());
|
||||
AssertUtil.isNotEmpty(roomGroup, GroupErrorEnum.GROUP_NOT_EXIST);
|
||||
|
||||
// 2. 判断该用户是否是群主
|
||||
Boolean isLord = groupMemberDao.isLord(roomGroup.getId(), uid);
|
||||
AssertUtil.isTrue(isLord, GroupErrorEnum.NOT_ALLOWED_OPERATION);
|
||||
|
||||
// 3. 判断群成员是否在群中
|
||||
Boolean isGroupShip = groupMemberDao.isGroupShip(roomGroup.getRoomId(), request.getUidList());
|
||||
AssertUtil.isTrue(isGroupShip, GroupErrorEnum.USER_NOT_IN_GROUP);
|
||||
|
||||
// 4. 判断管理员数量是否达到上限
|
||||
// 4.1 查询现有管理员数量
|
||||
List<Long> manageUidList = groupMemberDao.getManageUidList(roomGroup.getId());
|
||||
// 4.2 去重
|
||||
HashSet<Long> manageUidSet = new HashSet<>(manageUidList);
|
||||
manageUidSet.addAll(request.getUidList());
|
||||
AssertUtil.isFalse(manageUidSet.size() > MAX_MANAGE_COUNT, GroupErrorEnum.MANAGE_COUNT_EXCEED);
|
||||
|
||||
// 5. 增加管理员
|
||||
groupMemberDao.addAdmin(roomGroup.getId(), request.getUidList());
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,6 @@ public class CreateTokenTest {
|
||||
.sign(Algorithm.HMAC256("dsfsdfsdfsdfsd")); // signature
|
||||
log.info("生成的token为 {}",token);
|
||||
|
||||
|
||||
try {
|
||||
JWTVerifier verifier = JWT.require(Algorithm.HMAC256("dsfsdfsdfsdfsd")).build();
|
||||
DecodedJWT jwt = verifier.verify(token);
|
||||
|
||||
Reference in New Issue
Block a user