Commit 4754b4ea by Future

加入聊天室

parent 6541e8d0
...@@ -51,6 +51,7 @@ public class ChatRoomAction { ...@@ -51,6 +51,7 @@ public class ChatRoomAction {
private ImConversationService imConversationService; private ImConversationService imConversationService;
@Autowired @Autowired
private ChannelSender channelSender; private ChannelSender channelSender;
@Autowired @Autowired
private ChatRoomCacheManager chatRoomCacheManager; private ChatRoomCacheManager chatRoomCacheManager;
......
package com.wecloud.im.controller; package com.wecloud.im.controller;
import com.wecloud.im.param.IntoChatRoomParam;
import io.geekidea.springbootplus.framework.common.api.ApiCode; import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult; import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController; import io.geekidea.springbootplus.framework.common.controller.BaseController;
...@@ -49,7 +50,6 @@ public class ImConversationController extends BaseController { ...@@ -49,7 +50,6 @@ public class ImConversationController extends BaseController {
@Autowired @Autowired
private ImConversationService imConversationService; private ImConversationService imConversationService;
/** /**
* 添加或修改会话名称 * 添加或修改会话名称
*/ */
...@@ -207,5 +207,17 @@ public class ImConversationController extends BaseController { ...@@ -207,5 +207,17 @@ public class ImConversationController extends BaseController {
return ApiResult.result(true); return ApiResult.result(true);
} }
/**
* 用户进入聊天室
*/
@PostMapping("/intoChatRoom")
@ApiOperation(value = "用户进入聊天室", notes = "用户进入聊天室")
public ApiResult<Boolean> intoChatRoom(@RequestBody IntoChatRoomParam param) {
log.info("用户进入聊天室入参 {}", JSON.toJSONString(param));
return ApiResult.ok(imConversationService.intoChatRoom(param));
}
} }
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 IntoChatRoomParam 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; package com.wecloud.im.service;
import com.wecloud.im.param.IntoChatRoomParam;
import io.geekidea.springbootplus.framework.common.api.ApiResult; import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.service.BaseService; import io.geekidea.springbootplus.framework.common.service.BaseService;
import io.geekidea.springbootplus.framework.core.pagination.Paging; import io.geekidea.springbootplus.framework.core.pagination.Paging;
...@@ -205,5 +206,12 @@ public interface ImConversationService extends BaseService<ImConversation> { ...@@ -205,5 +206,12 @@ public interface ImConversationService extends BaseService<ImConversation> {
*/ */
Long getRepetitionConversationAttributes(Long clientId1, Long clientId2, String attributes); Long getRepetitionConversationAttributes(Long clientId1, Long clientId2, String attributes);
/**
* 用户主动进入聊天室
* @param param
* @return
*/
Boolean intoChatRoom(IntoChatRoomParam param);
} }
package com.wecloud.im.service.impl; package com.wecloud.im.service.impl;
import com.wecloud.im.chatroom.cache.ChatRoomCacheManager;
import com.wecloud.im.param.IntoChatRoomParam;
import com.wecloud.im.ws.utils.RedisUtils;
import io.geekidea.springbootplus.framework.common.api.ApiCode; import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult; import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.enums.BaseEnum; import io.geekidea.springbootplus.framework.common.enums.BaseEnum;
...@@ -103,14 +106,14 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap ...@@ -103,14 +106,14 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
private ImClientService imClientService; private ImClientService imClientService;
@Autowired @Autowired
private ImApplicationService imApplicationService;
@Autowired
private ImMessageService imMessageService; private ImMessageService imMessageService;
@Autowired @Autowired
private ContextService contextService; private ContextService contextService;
@Autowired
private ChatRoomCacheManager chatRoomCacheManager;
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@Override @Override
public boolean saveImConversation(ImConversation imConversation) { public boolean saveImConversation(ImConversation imConversation) {
...@@ -1157,4 +1160,61 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap ...@@ -1157,4 +1160,61 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
return imConversationMapper.getRepetitionConversationAttributes(clientId1, clientId2, attributes); return imConversationMapper.getRepetitionConversationAttributes(clientId1, clientId2, attributes);
} }
@Override
public Boolean intoChatRoom(IntoChatRoomParam param) {
// 获取当前client
ImClient imClientSender = contextService.getImClientIfNotNullOrThrow();
// 根据appId查询application
ImApplication imApplication = contextService.getImApplicationIfNotNullOrThrow(imClientSender.getFkAppid());
// 添加到会话成员列表中
ImClient clientToConversation = imClientService.getOne(new QueryWrapper<ImClient>().lambda()
.eq(ImClient::getFkAppid, imApplication.getId())
.eq(ImClient::getClientId, param.getClientId()));
if (clientToConversation == null) {
throw new BusinessException(ApiCode.CLIENT_NOT_FOUNT);
}
// 获取房间所有成员 key是 client的主键id:platform, val是 ip
Map<String, String> chatRoomMembers = chatRoomCacheManager.findOnlineClientsByChatRoomId(param.getChatRoomId());
for (String key : chatRoomMembers.keySet()) {
if (clientToConversation.getId().toString().equals(key.split(RedisUtils.SPLIT)[0])) {
return Boolean.FALSE;
}
}
chatRoomCacheManager.intoRoom(clientToConversation.getId(), param.getChatRoomId(), param.getPlatform());
// ws邀请事件通知给群内其他人 ----------
// 生成消息id
long messageId = SnowflakeUtil.getId();
ImMessage imMessage = new ImMessage();
Map<String, Object> content = new HashMap<>();
content.put("operator", imClientSender.getClientId()); //操作的client ID
content.put("passivityOperator", clientToConversation.getClientId()); //被操作的client ID
imMessage.setContent(JsonUtils.encodeJson(content));
// 保存消息至消息表
imMessage.setId(messageId);
imMessage.setMsgType(MsgTypeEnum.INVITE_CLIENT_JOIN_CONVERSATION.getUriCode());
imMessage.setCreateTime(new Date());
imMessage.setFkAppid(imApplication.getId());
imMessage.setSender(imClientSender.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, imClientSender);
}
// 发送给刚被拉入群的成员
sendEventMsgToMember(imApplication, clientToConversation.getId(), imMessage, imClientSender);
imConversationMapper.addMemberCount(imApplication.getId(), param.getChatRoomId(), 1);
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