fix: 修复退出群聊bug

This commit is contained in:
Kkuil 2023-11-13 22:11:35 +08:00
parent 2bb70c4133
commit 1bf5ad3f7d
6 changed files with 54 additions and 22 deletions

3
.gitignore vendored
View File

@ -72,3 +72,6 @@ mybatis-generator-config.xml
ChannelBizRankClient.java
ChannelBizBrandWallClient.java
/*/rebel.xml
# yml
*.yml

View File

@ -1,5 +1,6 @@
package com.abin.mallchat.common.chat.dao;
import cn.hutool.core.collection.CollectionUtil;
import com.abin.mallchat.common.chat.domain.entity.Contact;
import com.abin.mallchat.common.chat.domain.entity.Message;
import com.abin.mallchat.common.chat.mapper.ContactMapper;
@ -96,12 +97,17 @@ public class ContactDao extends ServiceImpl<ContactMapper, Contact> {
/**
* 根据房间ID删除会话
*
* @param roomId 房间ID
* @param roomId 房间ID
* @param uidList 群成员列表
* @return 是否删除成功
*/
public Boolean removeByRoomId(Long roomId) {
LambdaQueryWrapper<Contact> wrapper = new QueryWrapper<Contact>().lambda()
.eq(Contact::getRoomId, roomId);
return this.remove(wrapper);
public Boolean removeByRoomId(Long roomId, List<Long> uidList) {
if (CollectionUtil.isNotEmpty(uidList)) {
LambdaQueryWrapper<Contact> wrapper = new QueryWrapper<Contact>().lambda()
.eq(Contact::getRoomId, roomId)
.in(Contact::getUid, uidList);
return this.remove(wrapper);
}
return false;
}
}

View File

@ -1,5 +1,6 @@
package com.abin.mallchat.common.chat.dao;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.abin.mallchat.common.chat.domain.entity.Contact;
import com.abin.mallchat.common.chat.domain.entity.GroupMember;
@ -177,12 +178,17 @@ public class GroupMemberDao extends ServiceImpl<GroupMemberMapper, GroupMember>
* 根据群组ID删除群成员
*
* @param groupId 群组ID
* @param uidList 群成员列表
* @return 是否删除成功
*/
public Boolean removeByGroupId(Long groupId) {
LambdaQueryWrapper<GroupMember> wrapper = new QueryWrapper<GroupMember>()
.lambda()
.eq(GroupMember::getGroupId, groupId);
return this.remove(wrapper);
public Boolean removeByGroupId(Long groupId, List<Long> uidList) {
if (CollectionUtil.isNotEmpty(uidList)) {
LambdaQueryWrapper<GroupMember> wrapper = new QueryWrapper<GroupMember>()
.lambda()
.eq(GroupMember::getGroupId, groupId)
.in(GroupMember::getUid, uidList);
return this.remove(wrapper);
}
return false;
}
}

View File

@ -1,5 +1,6 @@
package com.abin.mallchat.common.chat.dao;
import cn.hutool.core.collection.CollectionUtil;
import com.abin.mallchat.common.chat.domain.entity.Contact;
import com.abin.mallchat.common.chat.domain.entity.Message;
import com.abin.mallchat.common.chat.domain.enums.MessageStatusEnum;
@ -15,6 +16,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Objects;
/**
@ -72,13 +74,18 @@ public class MessageDao extends ServiceImpl<MessageMapper, Message> {
/**
* 根据房间ID逻辑删除消息
*
* @param roomId 房间ID
* @param roomId 房间ID
* @param uidList 群成员列表
* @return 是否删除成功
*/
public Boolean removeByRoomId(Long roomId) {
LambdaUpdateWrapper<Message> wrapper = new UpdateWrapper<Message>().lambda()
.eq(Message::getRoomId, roomId)
.set(Message::getStatus, MessageStatusEnum.DELETE.getStatus());
return this.update(wrapper);
public Boolean removeByRoomId(Long roomId, List<Long> uidList) {
if (CollectionUtil.isNotEmpty(uidList)) {
LambdaUpdateWrapper<Message> wrapper = new UpdateWrapper<Message>().lambda()
.eq(Message::getRoomId, roomId)
.in(Message::getFromUid, uidList)
.set(Message::getStatus, MessageStatusEnum.DELETE.getStatus());
return this.update(wrapper);
}
return false;
}
}

View File

@ -29,5 +29,11 @@ public interface IGroupMemberService {
*/
void revokeAdmin(Long uid, AdminRevokeReq request);
/**
* 退出群聊
*
* @param uid 用户ID
* @param request 请求信息
*/
void exitGroup(Long uid, MemberExitReq request);
}

View File

@ -139,19 +139,23 @@ public class GroupMemberServiceImpl implements IGroupMemberService {
boolean isDelRoom = roomDao.removeById(roomId);
AssertUtil.isTrue(isDelRoom, CommonErrorEnum.SYSTEM_ERROR);
// 4.2 删除会话
Boolean isDelContact = contactDao.removeByRoomId(roomId);
Boolean isDelContact = contactDao.removeByRoomId(roomId, Collections.EMPTY_LIST);
AssertUtil.isTrue(isDelContact, CommonErrorEnum.SYSTEM_ERROR);
// 4.3 删除群成员
Boolean isDelGroupMember = groupMemberDao.removeByGroupId(roomGroup.getId());
Boolean isDelGroupMember = groupMemberDao.removeByGroupId(roomGroup.getId(), Collections.EMPTY_LIST);
AssertUtil.isTrue(isDelGroupMember, CommonErrorEnum.SYSTEM_ERROR);
// 4.4 删除消息记录 (逻辑删除)
Boolean isDelMessage = messageDao.removeByRoomId(roomId);
Boolean isDelMessage = messageDao.removeByRoomId(roomId, Collections.EMPTY_LIST);
AssertUtil.isTrue(isDelMessage, CommonErrorEnum.SYSTEM_ERROR);
// TODO 这里也可以告知群成员 群聊已被删除的消息
} else {
// 4.5 删除成员
groupMemberDao.removeById(uid);
// 发送移除事件告知群成员
// 4.5 删除会话
Boolean isDelContact = contactDao.removeByRoomId(roomId, Collections.singletonList(uid));
AssertUtil.isTrue(isDelContact, CommonErrorEnum.SYSTEM_ERROR);
// 4.6 删除群成员
Boolean isDelGroupMember = groupMemberDao.removeByGroupId(roomGroup.getId(), Collections.singletonList(uid));
AssertUtil.isTrue(isDelGroupMember, CommonErrorEnum.SYSTEM_ERROR);
// 4.7 发送移除事件告知群成员
List<Long> memberUidList = groupMemberCache.getMemberUidList(roomGroup.getRoomId());
WSBaseResp<WSMemberChange> ws = MemberAdapter.buildMemberRemoveWS(roomGroup.getRoomId(), uid);
pushService.sendPushMsg(ws, memberUidList);