Commit f9784c21 by 罗长华

完成创建群 解散群 加入群 离开群 群成员列表接口逻辑

parent 91b31c16
...@@ -6,9 +6,12 @@ import io.geekidea.springbootplus.framework.core.pagination.Paging; ...@@ -6,9 +6,12 @@ import io.geekidea.springbootplus.framework.core.pagination.Paging;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.wecloud.im.entity.ImConversation; import com.wecloud.im.entity.ImConversation;
import com.wecloud.im.entity.ImConversationMembers;
import com.wecloud.im.entity.ImMessage;
import com.wecloud.im.param.ChatRoomMemberPageParam; import com.wecloud.im.param.ChatRoomMemberPageParam;
import com.wecloud.im.param.DisbandConversationParam; import com.wecloud.im.param.DisbandConversationParam;
import com.wecloud.im.param.ExitChatRoomParam; import com.wecloud.im.param.ExitChatRoomParam;
...@@ -265,5 +268,20 @@ public interface ImConversationService extends BaseService<ImConversation> { ...@@ -265,5 +268,20 @@ public interface ImConversationService extends BaseService<ImConversation> {
*/ */
List<ImConversation> getConversationBySenderAndReceivers(Long senderImClientId, Collection<Long> receiverImClientIds); List<ImConversation> getConversationBySenderAndReceivers(Long senderImClientId, Collection<Long> receiverImClientIds);
/**
* 给群成员发消息
* @Author luozh
* @Date 2022年05月10日 05:47:36
* @param conversationId
* @param membersList
* @param appId
* @param sender
* @param content
* @param message
* @Return
*/
void sendMsgToMembers(Long conversationId, List<ImConversationMembers> membersList, Long appId,
String sender, Map content, ImMessage message);
} }
...@@ -14,12 +14,12 @@ public interface ImGroupService { ...@@ -14,12 +14,12 @@ public interface ImGroupService {
* 创建群组 * 创建群组
* @Author luozh * @Author luozh
* @Date 2022年05月10日 03:22:13 * @Date 2022年05月10日 03:22:13
* @param groupOwnerUserId * @param creatorClientId
* @param groupName * @param groupName
* @param memberIds * @param memberIds
* @Return * @Return
*/ */
Long createGroup(String groupOwnerUserId, String groupName, List<String> memberIds); Long createGroup(String creatorClientId, String groupName, List<String> memberIds);
/** /**
* 解散群组 * 解散群组
...@@ -39,7 +39,7 @@ public interface ImGroupService { ...@@ -39,7 +39,7 @@ public interface ImGroupService {
* @param userIds * @param userIds
* @Return * @Return
*/ */
Boolean joinGroup(String groupId, String userIds); Boolean joinGroup(String groupId, List<String> memberClientIds);
/** /**
* 离开群组 * 离开群组
...@@ -49,7 +49,7 @@ public interface ImGroupService { ...@@ -49,7 +49,7 @@ public interface ImGroupService {
* @param userIds * @param userIds
* @Return * @Return
*/ */
Boolean leaveGroup(String groupId, String userIds); Boolean leaveGroup(String groupId, List<String> memberClientIds);
/** /**
* 获取群成员列表 * 获取群成员列表
......
...@@ -510,7 +510,7 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap ...@@ -510,7 +510,7 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
if (clientToConversation == null) { if (clientToConversation == null) {
throw new BusinessException(ApiCode.CLIENT_NOT_FOUNT); throw new BusinessException(ApiCode.CLIENT_NOT_FOUNT);
} }
// 判断用户是否已经在该会话 // 判断用户是否已经在该会话
ImConversationMembers members = imConversationMembersService.getOne(new QueryWrapper<ImConversationMembers>().lambda() ImConversationMembers members = imConversationMembersService.getOne(new QueryWrapper<ImConversationMembers>().lambda()
.eq(ImConversationMembers::getFkAppid, imApplication.getId()) .eq(ImConversationMembers::getFkAppid, imApplication.getId())
.eq(ImConversationMembers::getFkConversationId, imClientToConversation.getConversationId()) .eq(ImConversationMembers::getFkConversationId, imClientToConversation.getConversationId())
...@@ -1488,8 +1488,9 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap ...@@ -1488,8 +1488,9 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
* @Author luozh * @Author luozh
* @Date 2022年04月27日 02:11:19 * @Date 2022年04月27日 02:11:19
*/ */
private void sendMsgToMembers(Long conversationId, List<ImConversationMembers> membersList, Long appId, @Override
String sender, Map content, ImMessage message) { public void sendMsgToMembers(Long conversationId, List<ImConversationMembers> membersList, Long appId,
String sender, Map content, ImMessage message) {
for (ImConversationMembers member : membersList) { for (ImConversationMembers member : membersList) {
// 不关心事件是否发送成功 // 不关心事件是否发送成功
try { try {
......
...@@ -4,17 +4,31 @@ import io.geekidea.springbootplus.framework.common.exception.BusinessException; ...@@ -4,17 +4,31 @@ import io.geekidea.springbootplus.framework.common.exception.BusinessException;
import io.geekidea.springbootplus.framework.shiro.util.SecurityUtils; import io.geekidea.springbootplus.framework.shiro.util.SecurityUtils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wecloud.im.entity.ImApplication;
import com.wecloud.im.entity.ImClient;
import com.wecloud.im.entity.ImConversation; import com.wecloud.im.entity.ImConversation;
import com.wecloud.im.entity.ImConversationMembers; import com.wecloud.im.entity.ImConversationMembers;
import com.wecloud.im.mapper.ImConversationMapper;
import com.wecloud.im.param.add.ServerImConversationCreate;
import com.wecloud.im.sdk.enums.ChatTypeEnum;
import com.wecloud.im.sdk.enums.GroupRoleEnum;
import com.wecloud.im.sdk.enums.MutedEnum;
import com.wecloud.im.service.ImApplicationService;
import com.wecloud.im.service.ImClientService;
import com.wecloud.im.service.ImConversationMembersService; import com.wecloud.im.service.ImConversationMembersService;
import com.wecloud.im.service.ImConversationService; import com.wecloud.im.service.ImConversationService;
import com.wecloud.im.service.ImGroupService; import com.wecloud.im.service.ImGroupService;
import com.wecloud.imserver.client.model.enums.DeviceTypeEnum;
import com.wecloud.utils.SnowflakeUtil;
/** /**
* 群服务 * 群服务
...@@ -27,6 +41,16 @@ import com.wecloud.im.service.ImGroupService; ...@@ -27,6 +41,16 @@ import com.wecloud.im.service.ImGroupService;
public class ImGroupServiceImpl implements ImGroupService { public class ImGroupServiceImpl implements ImGroupService {
/** /**
* 应用服务
*/
private final ImApplicationService applicationService;
/**
* 客户端服务
*/
private final ImClientService clientService;
/**
* 会话服务 * 会话服务
*/ */
private final ImConversationService conversationService; private final ImConversationService conversationService;
...@@ -36,24 +60,155 @@ public class ImGroupServiceImpl implements ImGroupService { ...@@ -36,24 +60,155 @@ public class ImGroupServiceImpl implements ImGroupService {
*/ */
private final ImConversationMembersService conversationMembersService; private final ImConversationMembersService conversationMembersService;
/**
* 会话mapper
*/
private final ImConversationMapper imConversationMapper;
@Override @Override
public Long createGroup(String groupOwnerUserId, String groupName, List<String> memberIds) { public Long createGroup(String creatorClientId, String groupName, List<String> memberIds) {
return null; // 获取应用
Long appId = SecurityUtils.getCurrentAppId();
ImApplication application = applicationService.getCacheById(appId);
// 获取创建人信息
ImClient creator = clientService.getCacheImClient(appId, creatorClientId);
if (creator == null) {
throw new BusinessException("创建人不存在");
}
// 获取群成员信息
List<ImClient> members = clientService.listByIds(memberIds);
if (members.isEmpty()) {
throw new BusinessException("群成员列表为空");
}
ServerImConversationCreate serverImConversationCreate = new ServerImConversationCreate();
serverImConversationCreate.setName(groupName);
serverImConversationCreate.setApplication(application);
serverImConversationCreate.setCreator(creator);
serverImConversationCreate.setMembers(members);
serverImConversationCreate.setChatType(ChatTypeEnum.NORMAL_GROUP);
serverImConversationCreate.setPlatform(DeviceTypeEnum.IOS);
ImConversation conversation =
conversationService.serverCreateImConversation(serverImConversationCreate);
return conversation.getId();
} }
@Override @Override
public Boolean dismissGroup(String userId, String groupId) { public Boolean dismissGroup(String userId, String groupId) {
return null; // 获取应用
Long appId = SecurityUtils.getCurrentAppId();
ImApplication application = applicationService.getCacheById(appId);
// 查询操作人
ImClient operator = clientService.getCacheImClient(appId, userId);
if (operator == null) {
throw new BusinessException("操作人不存在");
}
// 查询会话
ImConversation conversation =
conversationService.getOne(Wrappers.<ImConversation>lambdaQuery().eq(ImConversation::getFkAppid,
appId).eq(ImConversation::getId, groupId));
if (conversation == null) {
throw new BusinessException("群组不存在");
}
// 查询该会话所有成员
List<ImConversationMembers> membersList = conversationMembersService.list(
new QueryWrapper<ImConversationMembers>().lambda()
.eq(ImConversationMembers::getFkAppid, appId)
.eq(ImConversationMembers::getFkConversationId, conversation.getId())
);
if (membersList.isEmpty()) {
throw new BusinessException("群聊成员列表为空");
}
// 删除所有成员
conversationMembersService.deleteByConversationId(conversation.getId());
return true;
} }
@Override @Override
public Boolean joinGroup(String groupId, String userIds) { public Boolean joinGroup(String groupId, List<String> memberClientIds) {
return null; Long appId = SecurityUtils.getCurrentAppId();
ImApplication imApplication = applicationService.getCacheById(appId);
// 查询会话
ImConversation conversation =
conversationService.getOne(Wrappers.<ImConversation>lambdaQuery().eq(ImConversation::getFkAppid,
appId).eq(ImConversation::getId, groupId));
if (conversation == null) {
throw new BusinessException("群组不存在");
}
// 查询客户端信息
List<ImClient> clientList = clientService.list(Wrappers.<ImClient>lambdaQuery().eq(ImClient::getFkAppid,
appId).in(ImClient::getClientId, memberClientIds));
// 查找已经在群组里的客户端
List<ImConversationMembers> existMemberList =
conversationMembersService.list(Wrappers.<ImConversationMembers>lambdaQuery()
.eq(ImConversationMembers::getFkConversationId, conversation.getId())
.in(ImConversationMembers::getClientId, memberClientIds));
if (!existMemberList.isEmpty()) {
List<String> existMemberClientIds =
existMemberList.stream().map(ImConversationMembers::getClientId).collect(Collectors.toList());
clientList = clientList.stream().filter(client -> !existMemberClientIds.contains(client.getClientId())).collect(Collectors.toList());
}
// 加入群聊
// 将他人添加到会话
List<ImConversationMembers> newMemberList = new ArrayList<>();
for (ImClient client : clientList) {
Date now = new Date();
Long imConversationMembersId2 = SnowflakeUtil.getId();
ImConversationMembers newMember = new ImConversationMembers();
newMember.setUpdateTime(now);
newMember.setId(imConversationMembersId2);
newMember.setCreateTime(now);
newMember.setFkAppid(imApplication.getId());
newMember.setFkConversationId(conversation.getId());
newMember.setFkClientId(client.getId());
newMember.setClientId(client.getClientId());
newMember.setMuted(MutedEnum.NO.getCode());
newMember.setRole(GroupRoleEnum.NORMAL.getCode());
newMemberList.add(newMember);
}
conversationMembersService.saveBatch(newMemberList);
// 将群成员数量增加
imConversationMapper.addMemberCount(imApplication.getId(), conversation.getId(), clientList.size());
return true;
} }
@Override @Override
public Boolean leaveGroup(String groupId, String userIds) { public Boolean leaveGroup(String groupId, List<String> memberClientIds) {
return null; Long appId = SecurityUtils.getCurrentAppId();
ImApplication imApplication = applicationService.getCacheById(appId);
// 查询会话
ImConversation conversation =
conversationService.getOne(Wrappers.<ImConversation>lambdaQuery().eq(ImConversation::getFkAppid,
appId).eq(ImConversation::getId, groupId));
if (conversation == null) {
throw new BusinessException("群组不存在");
}
// 查找在群组里的客户端
List<ImConversationMembers> existMemberList =
conversationMembersService.list(Wrappers.<ImConversationMembers>lambdaQuery()
.eq(ImConversationMembers::getFkConversationId, conversation.getId())
.in(ImConversationMembers::getClientId, memberClientIds));
if (!existMemberList.isEmpty()) {
List<Long> conversationMemberIds =
existMemberList.stream().map(ImConversationMembers::getId).collect(Collectors.toList());
conversationMembersService.removeByIds(conversationMemberIds);
}
// 将群成员数量减
imConversationMapper.addMemberCount(imApplication.getId(), conversation.getId(), existMemberList.size());
return true;
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment