Commit 20d354e8 by Future

退出聊天室

parent 4754b4ea
package com.wecloud.im.param;
import io.geekidea.springbootplus.framework.common.entity.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* @Author wenzhida
* @Date 2022/4/27 11:44
* @Description 用户退出聊天室入参
*/
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "用户退出聊天室入参")
public class ExitChatRoomParam extends BaseEntity {
private static final long serialVersionUID = -3602523207000275557L;
@ApiModelProperty("聊天室id")
private Long chatRoomId;
@ApiModelProperty("离开聊天室的clientId")
private String clientId;
@ApiModelProperty("客户端平台: 1 web, 2 安卓, 3 ios, 4 pc-win, 5 pc-macOs, 需与生成sign时的值一致")
private Integer platform;
}
package com.wecloud.im.service;
import com.wecloud.im.param.ExitChatRoomParam;
import com.wecloud.im.param.IntoChatRoomParam;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.service.BaseService;
......@@ -213,5 +214,12 @@ public interface ImConversationService extends BaseService<ImConversation> {
*/
Boolean intoChatRoom(IntoChatRoomParam param);
/**
* 用户离开聊天室
* @param param
* @return
*/
Boolean exitRoom(ExitChatRoomParam param);
}
package com.wecloud.im.service.impl;
import com.wecloud.im.chatroom.cache.ChatRoomCacheManager;
import com.wecloud.im.param.ExitChatRoomParam;
import com.wecloud.im.param.IntoChatRoomParam;
import com.wecloud.im.ws.utils.RedisUtils;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
......@@ -1217,4 +1218,58 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
return true;
}
@Override
public Boolean exitRoom(ExitChatRoomParam param) {
// 获取当前client
ImClient currentClient = contextService.getImClientIfNotNullOrThrow();
// 根据appId查询application
ImApplication imApplication = contextService.getImApplicationIfNotNullOrThrow(currentClient.getFkAppid());
ImConversation imConversation = imConversationService.getById(param.getChatRoomId());
if (imConversation == null) {
throw new BusinessException("查无会话消息");
}
// 获取房间所有成员 key是 client的主键id:platform, val是 ip
Map<String, String> chatRoomMembers = chatRoomCacheManager.findOnlineClientsByChatRoomId(param.getChatRoomId());
if (chatRoomMembers.isEmpty()) {
throw new BusinessException("会话中查无群人员");
}
Boolean inRoom = Boolean.FALSE;
for (String key : chatRoomMembers.keySet()) {
if (currentClient.getId().toString().equals(key.split(RedisUtils.SPLIT)[0])) {
inRoom = Boolean.TRUE;
continue;
}
}
if (!inRoom) {
// 不在房间
return Boolean.FALSE;
}
// 将群成员数量减1
imConversationMapper.addMemberCount(currentClient.getFkAppid(), param.getChatRoomId(), -1);
chatRoomCacheManager.exitRoom(currentClient.getId(), param.getChatRoomId(), param.getPlatform());
// ws 退出事件通知给房间内其他人 ----------
// 生成消息id
long messageId = SnowflakeUtil.getId();
ImMessage imMessage = new ImMessage();
// 保存消息至消息表
imMessage.setId(messageId);
imMessage.setMsgType(MsgTypeEnum.LEAVE_CONVERSATION.getUriCode());
imMessage.setCreateTime(new Date());
imMessage.setFkAppid(currentClient.getFkAppid());
imMessage.setSender(currentClient.getId());
imMessage.setWithdraw(false);
imMessage.setEvent(true);
imMessage.setSystemFlag(false);
imMessage.setSendStatus(2);
imMessage.setFkConversationId(param.getChatRoomId());
// 遍历发送给已在群内的成员
for (String key : chatRoomMembers.keySet()) {
Long fkClientId = Long.valueOf(key.split(RedisUtils.SPLIT)[0]);
sendEventMsgToMember(imApplication, fkClientId, imMessage, currentClient);
}
return true;
}
}
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