Commit f670341d by 罗长华

Merge remote-tracking branch 'origin/xiaohudou_20220427' into xiaohudou_20220427

parents 10f5ad2d ae35ab17
......@@ -45,6 +45,10 @@ public class ImConversation extends BaseEntity {
@ApiModelProperty("群成员数量")
private Integer memberCount;
/**
* 会话属性
* @see com.wecloud.im.sdk.enums.ChatTypeEnum
*/
@ApiModelProperty("会话属性,1:单聊,2:普通群,3:万人群,4:聊天室")
private Integer chatType;
......
......@@ -2,7 +2,9 @@ package com.wecloud.im.param;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
......@@ -15,6 +17,8 @@ import java.io.Serializable;
@Data
@Accessors(chain = true)
@ApiModel(value = "聊天室成员分页入参")
@NoArgsConstructor
@AllArgsConstructor
public class ChatRoomMemberPageParam implements Serializable {
private static final long serialVersionUID = 3284648263835691087L;
......
package com.wecloud.im.service.impl;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.enums.BaseEnum;
import io.geekidea.springbootplus.framework.common.exception.BusinessException;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
......@@ -88,6 +62,29 @@ import com.wecloud.im.ws.sender.ChannelSender;
import com.wecloud.im.ws.utils.RedisUtils;
import com.wecloud.utils.JsonUtils;
import com.wecloud.utils.SnowflakeUtil;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.enums.BaseEnum;
import io.geekidea.springbootplus.framework.common.exception.BusinessException;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static cn.hutool.core.date.DatePattern.CHINESE_DATE_PATTERN;
import static com.wecloud.im.ws.enums.MsgTypeEnum.CONVERSATION_FORBID_ADD_FRIEND;
......@@ -687,8 +684,17 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
@Override
public void disband(DisbandConversationParam param) {
ImConversation imConversation = imConversationMapper.selectById(param.getConversationId());
if (imConversation == null) {
throw new BusinessException("查无会话信息");
}
// 获取当前client
ImClient currentClient = contextService.getImClientIfNotNullOrThrow();
if (ChatTypeEnum.CHAT_ROOM.getCode().equals(imConversation.getChatType())) {
// 聊天室事件发送逻辑
disbandChatRoom(param.getConversationId(), currentClient);
return;
}
// 判断是否为群主
ImConversationMembers conversationMember = imConversationMembersService.getOne(
new QueryWrapper<ImConversationMembers>().lambda()
......@@ -735,6 +741,40 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
sendMsgToMembers(conversationId, membersList, appId, sender, content, imMessage);
}
/**
* 解散聊天室
* @param conversationId
* @param currentClient
*/
private void disbandChatRoom(Long conversationId, ImClient currentClient) {
Map<String, String> chatRoomMembers = chatRoomCacheManager.findOnlineClientsByChatRoomId(conversationId);
if (chatRoomMembers.isEmpty()) {
throw new BusinessException("聊天室成员列为空");
}
// 保存事件消息
ImMessage imMessage = new ImMessage();
Map<String, Object> content = new HashMap<>();
content.put("operator", currentClient.getClientId());
imMessage.setContent(JsonUtils.encodeJson(content));
// 保存消息至消息表
imMessage.setId(SnowflakeUtil.getId());
imMessage.setMsgType(MsgTypeEnum.CONVERSATION_DISBAND.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(conversationId);
// 遍历发送给已在群内的成员
for (String key : chatRoomMembers.keySet()) {
Long fkClientId = Long.valueOf(key.split(RedisUtils.SPLIT)[0]);
sendEventMsgToMember(conversationId, currentClient.getFkAppid(), currentClient.getClientId(),
fkClientId, null, imMessage);
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean leaveConversation(ImClientLeaveConversation imClientToConversation) {
......@@ -1008,30 +1048,24 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
public void saveOrUpdateAttr(ImConversationAttrUpdate imConversationAttrUpdate) {
// 获取当前client
ImClient currentClient = contextService.getImClientIfNotNullOrThrow();
// 根据appId查询application
ImApplication imApplication = contextService.getImApplicationIfNotNullOrThrow(currentClient.getFkAppid());
ImConversation imConversationById = imConversationService.getById(imConversationAttrUpdate.getConversationId());
// 判断是否为群主
// if (imConversationById == null || !imConversationById.getCreator().equals(imClientSender.getId())) {
// return ApiResult.result(false);
// }
// 查询该会话所有成员
List<ImConversationMembers> membersList = imConversationMembersService.list(
new QueryWrapper<ImConversationMembers>().lambda()
.eq(ImConversationMembers::getFkAppid, imApplication.getId())
.eq(ImConversationMembers::getFkConversationId, imConversationAttrUpdate.getConversationId())
.ne(ImConversationMembers::getFkClientId, currentClient.getId())
);
imConversationById.setAttributes(imConversationAttrUpdate.getAttributes());
boolean b = imConversationService.updateById(imConversationById);
// 删除redis中该会话的缓存
deleteCacheImConversationById(imConversationAttrUpdate.getConversationId());
if (b) {
if (ChatTypeEnum.CHAT_ROOM.getCode().equals(imConversationById.getChatType())) {
// 聊天室事件发送逻辑
chatRoomAttrChanged(imConversationById, currentClient);
return;
}
// 查询该会话所有成员
List<ImConversationMembers> membersList = imConversationMembersService.list(
new QueryWrapper<ImConversationMembers>().lambda()
.eq(ImConversationMembers::getFkAppid, currentClient.getFkAppid())
.eq(ImConversationMembers::getFkConversationId, imConversationAttrUpdate.getConversationId())
.ne(ImConversationMembers::getFkClientId, currentClient.getId())
);
// ws下发拓展字段变动事件
HashMap<String, String> content = Maps.newHashMap();
content.put("attributes", imConversationById.getAttributes());
......@@ -1047,7 +1081,40 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
} else {
throw new BusinessException("修改错误");
}
}
/**
* 解散聊天室
* @param imConversation
* @param currentClient
*/
private void chatRoomAttrChanged(ImConversation imConversation, ImClient currentClient) {
Map<String, String> chatRoomMembers = chatRoomCacheManager.findOnlineClientsByChatRoomId(imConversation.getId());
if (chatRoomMembers.isEmpty()) {
throw new BusinessException("聊天室成员列为空");
}
// 保存事件消息
ImMessage imMessage = new ImMessage();
HashMap<String, String> content = Maps.newHashMap();
content.put("attributes", imConversation.getAttributes());
imMessage.setContent(JsonUtils.encodeJson(content));
imMessage.setId(SnowflakeUtil.getId());
imMessage.setMsgType(MsgTypeEnum.CONVERSATION_EXPAND_FIELD_CHANGE.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(imConversation.getId());
// 遍历发送给已在群内的成员
for (String key : chatRoomMembers.keySet()) {
Long fkClientId = Long.valueOf(key.split(RedisUtils.SPLIT)[0]);
sendEventMsgToMember(imConversation.getId(), currentClient.getFkAppid(), currentClient.getClientId(),
fkClientId, null, imMessage);
}
}
@Transactional(rollbackFor = Exception.class)
......@@ -1218,6 +1285,9 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
@Override
public Boolean intoChatRoom(IntoChatRoomParam param) {
if (param.getPlatform() == null) {
throw new BusinessException("平台入参不可为空");
}
// 获取当前client
ImClient imClientSender = contextService.getImClientIfNotNullOrThrow();
// 根据appId查询application
......@@ -1369,6 +1439,9 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
}
private ImConversationCreateVo createChatRoom(ImConversationCreate imConversationCreate, ImClient createClient) {
if (imConversationCreate.getPlatform() == null) {
throw new BusinessException("平台入参不可为空");
}
// 会话id
Long chatRoomId = SnowflakeUtil.getId();
......
......@@ -41,7 +41,7 @@ public class UserStateCacheManager extends UserStateListener {
String[] split = platformAndIp.split(RedisUtils.SPLIT);
String innerPlatform = split[0];
if(innerPlatform.equals(String.valueOf(platform))) {
redisUtils.removeForSet(getUserStateCacheKey(clientId), platformAndIp);
// redisUtils.removeForSet(getUserStateCacheKey(clientId), platformAndIp);
}
}
......@@ -53,7 +53,7 @@ public class UserStateCacheManager extends UserStateListener {
String key = getUserStateCacheKey(clientId);
String value = platform + RedisUtils.SPLIT + GetIpUtils.getlanIp();
log.info("ws用户离线删除redis key: {}, value: {}, uid: {}, clientId: {}", key, value, longChannelId, clientId);
redisUtils.removeForSet(key, value);
// redisUtils.removeForSet(key, value);
}
/**
......
......@@ -69,11 +69,11 @@ public class MangerRtcCacheServiceImpl implements MangerRtcCacheService {
String channelKey = String.format(RtcRedisKey.RTC_CHANNEL_INFO, rtcChannelId);
redisUtils.addKey(channelKey, rtcChannelInfoJson, Duration.ofDays(10));
//用户当前在线的频道ID
// 用户当前在线的频道ID
String userJoinChannelKey = String.format(RtcRedisKey.USER_JOIN_CHANNEL, clientId);
redisUtils.addKey(userJoinChannelKey, rtcChannelId.toString(), Duration.ofDays(10));
//频道中存在的用户
// 频道中存在的用户
String rtcChannelUsers = String.format(RtcRedisKey.RTC_CHANNEL_USERS, rtcChannelId);
redisUtils.addForSet(rtcChannelUsers, clientId + "", 10, TimeUnit.DAYS);
redisUtils.addForSet(rtcChannelUsers, toClientId + "", 10, TimeUnit.DAYS);
......@@ -97,8 +97,8 @@ public class MangerRtcCacheServiceImpl implements MangerRtcCacheService {
redisUtils.delKey(userJoinChannelKey);
//频道中存在的用户
String rtcChannelUserskey = String.format(RtcRedisKey.RTC_CHANNEL_USERS, rtcChannelId);
redisUtils.removeForSet(rtcChannelUserskey, clientId + "");
String rtcChannelUsersKey = String.format(RtcRedisKey.RTC_CHANNEL_USERS, rtcChannelId);
redisUtils.removeForSet(rtcChannelUsersKey, clientId + "");
}
@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