Commit 6ce86067 by 李晓钟

创建会话的逻辑修改。

parent ea3d2648
...@@ -29,9 +29,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory; ...@@ -29,9 +29,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
import java.time.Duration; import java.time.Duration;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* <p> * <p>
...@@ -59,29 +57,29 @@ public class RedisCacheConfig extends CachingConfigurerSupport { ...@@ -59,29 +57,29 @@ public class RedisCacheConfig extends CachingConfigurerSupport {
}; };
} }
@Bean @Bean
public CacheManager cacheManager(RedisConnectionFactory factory) { public CacheManager cacheManager(RedisConnectionFactory factory) {
// 生成一个默认配置,通过config对象即可对缓存进行自定义配置 // 生成一个默认配置,通过config对象即可对缓存进行自定义配置
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
// 设置缓存的默认过期时间,也是使用Duration设置 // 设置缓存的默认过期时间,也是使用Duration设置
// 过期时间30分钟 // 过期时间30分钟
config = config.entryTtl(Duration.ofMinutes(30)); config = config.entryTtl(Duration.ofMinutes(30)).prefixKeysWith("cache");
// 设置一个初始化的缓存空间set集合 // 设置一个初始化的缓存空间set集合
Set<String> cacheNames = new HashSet<>(); // Set<String> cacheNames = new HashSet<>();
cacheNames.add("my-redis-cache1"); // cacheNames.add("expireKey");
cacheNames.add("my-redis-cache2"); // cacheNames.add("my-redis-cache2");
// 对每个缓存空间应用不同的配置 // 对每个缓存空间应用不同的配置
Map<String, RedisCacheConfiguration> configMap = new HashMap<>(10); Map<String, RedisCacheConfiguration> configMap = new HashMap<>(10);
configMap.put("my-redis-cache1", config); configMap.put("expireKey", config);
configMap.put("my-redis-cache2", config.entryTtl(Duration.ofSeconds(120)));
// 使用自定义的缓存配置初始化一个cacheManager // 使用自定义的缓存配置初始化一个cacheManager
RedisCacheManager cacheManager = RedisCacheManager.builder(factory) RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
// 注意这两句的调用顺序,一定要先调用该方法设置初始化的缓存名,再初始化相关的配置 // 注意这两句的调用顺序,一定要先调用该方法设置初始化的缓存名,再初始化相关的配置
.initialCacheNames(cacheNames) // .initialCacheNames(cacheNames)
.cacheDefaults(config)
.withInitialCacheConfigurations(configMap) .withInitialCacheConfigurations(configMap)
.build(); .build();
......
...@@ -4,24 +4,12 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; ...@@ -4,24 +4,12 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wecloud.dispatch.annotation.ActionMapping; import com.wecloud.dispatch.annotation.ActionMapping;
import com.wecloud.dispatch.common.BaseRequest; import com.wecloud.dispatch.common.BaseRequest;
import com.wecloud.dispatch.extend.ActionRequest; import com.wecloud.dispatch.extend.ActionRequest;
import com.wecloud.im.entity.ImApplication; import com.wecloud.im.entity.*;
import com.wecloud.im.entity.ImClient;
import com.wecloud.im.entity.ImConversationMembers;
import com.wecloud.im.entity.ImInbox;
import com.wecloud.im.entity.ImMessage;
import com.wecloud.im.entity.ImMessageOnlineSend;
import com.wecloud.im.param.ChatContentVo; import com.wecloud.im.param.ChatContentVo;
import com.wecloud.im.param.ImClientSimpleDto; import com.wecloud.im.param.ImClientSimpleDto;
import com.wecloud.im.param.ImConversationQueryVo; import com.wecloud.im.param.ImConversationQueryVo;
import com.wecloud.im.param.MsgVo; import com.wecloud.im.param.MsgVo;
import com.wecloud.im.service.ImApplicationService; import com.wecloud.im.service.*;
import com.wecloud.im.service.ImClientBlacklistService;
import com.wecloud.im.service.ImClientService;
import com.wecloud.im.service.ImConversationMembersService;
import com.wecloud.im.service.ImConversationService;
import com.wecloud.im.service.ImInboxService;
import com.wecloud.im.service.ImMessageService;
import com.wecloud.im.thousandchat.cache.ThousandChatCacheManager;
import com.wecloud.im.ws.enums.WsResponseCmdEnum; import com.wecloud.im.ws.enums.WsResponseCmdEnum;
import com.wecloud.im.ws.model.WsResponse; import com.wecloud.im.ws.model.WsResponse;
import com.wecloud.im.ws.sender.AsyncPush; import com.wecloud.im.ws.sender.AsyncPush;
...@@ -67,8 +55,6 @@ public class NormalChatAction { ...@@ -67,8 +55,6 @@ public class NormalChatAction {
@Autowired @Autowired
private ChannelSender channelSender; private ChannelSender channelSender;
@Autowired @Autowired
private ThousandChatCacheManager thousandChatCacheManager;
@Autowired
private ImClientBlacklistService imClientBlacklistService; private ImClientBlacklistService imClientBlacklistService;
@Autowired @Autowired
private ImInboxService imInboxService; private ImInboxService imInboxService;
...@@ -121,9 +107,6 @@ public class NormalChatAction { ...@@ -121,9 +107,6 @@ public class NormalChatAction {
ImMessageOnlineSend imMessageOnlineSend = assembleImMessageOnlineSend(data, imClientSender, imApplication.getId()); ImMessageOnlineSend imMessageOnlineSend = assembleImMessageOnlineSend(data, imClientSender, imApplication.getId());
// 在线用户直接发消息
sendMsgForOnline(data, imMessageOnlineSend);
// 再给所有人发 todo 需要改成批量 // 再给所有人发 todo 需要改成批量
for (ImConversationMembers conversationMembers : membersList) { for (ImConversationMembers conversationMembers : membersList) {
// 入库 保存收件箱 // 入库 保存收件箱
...@@ -137,6 +120,13 @@ public class NormalChatAction { ...@@ -137,6 +120,13 @@ public class NormalChatAction {
if (imClientReceiver == null) { if (imClientReceiver == null) {
continue; continue;
} }
if(imClientReceiver.getId().equals(imClientSender.getId())) {
// 不给自己发
continue;
}
// 在线用户直接发消息
sendMsgForOnline(imClientReceiver.getId(), imMessageOnlineSend);
// 异步推送系统通知消息 // 异步推送系统通知消息
systemPush.push(data.getPush(), imClientReceiver, imApplication); systemPush.push(data.getPush(), imClientReceiver, imApplication);
} }
...@@ -148,12 +138,10 @@ public class NormalChatAction { ...@@ -148,12 +138,10 @@ public class NormalChatAction {
/** /**
* 发送消息给在线客户 * 发送消息给在线客户
* @param data * @param receiverClientId
* @param imMessageOnlineSend * @param imMessageOnlineSend
*/ */
private void sendMsgForOnline(ChatContentVo data, ImMessageOnlineSend imMessageOnlineSend) { private void sendMsgForOnline(Long receiverClientId, ImMessageOnlineSend imMessageOnlineSend) {
Map<String /** ip **/, List<String /** client的主键ID:platform **/>> onlineIpClientMap =
thousandChatCacheManager.findOnlineHostsByThousandGroupId(data.getToConversation());
// 封装要推给接收方的消息 // 封装要推给接收方的消息
WsResponse<ImMessageOnlineSend> responseModel = new WsResponse<>(); WsResponse<ImMessageOnlineSend> responseModel = new WsResponse<>();
responseModel.setCmd(WsResponseCmdEnum.ONLINE_MSG.getCmdCode()); responseModel.setCmd(WsResponseCmdEnum.ONLINE_MSG.getCmdCode());
...@@ -162,10 +150,7 @@ public class NormalChatAction { ...@@ -162,10 +150,7 @@ public class NormalChatAction {
responseModel.setMsg(result.getMessage()); responseModel.setMsg(result.getMessage());
responseModel.setData(imMessageOnlineSend); responseModel.setData(imMessageOnlineSend);
responseModel.setReqId(null); responseModel.setReqId(null);
channelSender.sendMsg(responseModel, receiverClientId);
onlineIpClientMap.forEach((ip, clientIdAndPlatforms) -> {
channelSender.batchSendMsg(responseModel, ip, clientIdAndPlatforms);
});
} }
/** /**
......
...@@ -57,13 +57,13 @@ public interface ImConversationMapper extends BaseMapper<ImConversation> { ...@@ -57,13 +57,13 @@ public interface ImConversationMapper extends BaseMapper<ImConversation> {
List<ImConversation> getMyImConversationList(@Param("clientId") Long clientId); List<ImConversation> getMyImConversationList(@Param("clientId") Long clientId);
/** /**
* 判断两个客户端是否存在会话 * 查询已经存在的一对一会话信息
* *
* @param clientId1 * @param clientId1
* @param clientId2 * @param clientId2
* @return * @return
*/ */
Integer getRepetitionConversation(@Param("clientId1") Long clientId1, @Param("clientId2") Long clientId2); ImConversation getRepetitionConversationSingle(@Param("clientId1") Long clientId1, @Param("clientId2") Long clientId2);
/** /**
* 判断重复会话中的Attributes是否一样 * 判断重复会话中的Attributes是否一样
...@@ -75,16 +75,6 @@ public interface ImConversationMapper extends BaseMapper<ImConversation> { ...@@ -75,16 +75,6 @@ public interface ImConversationMapper extends BaseMapper<ImConversation> {
*/ */
Long getRepetitionConversationAttributes(@Param("clientId1") Long clientId1, @Param("clientId2") Long clientId2, @Param("attributes") String attributes); Long getRepetitionConversationAttributes(@Param("clientId1") Long clientId1, @Param("clientId2") Long clientId2, @Param("attributes") String attributes);
/**
* 查询已经存在的一对一会话信息
*
* @param clientId1
* @param clientId2
* @return
*/
ImConversation getRepetitionConversationInfo(@Param("clientId1") Long clientId1, @Param("clientId2") Long clientId2);
/** /**
* 增减成员数量 * 增减成员数量
* @param appId fkAppid * @param appId fkAppid
......
...@@ -137,16 +137,6 @@ public interface ImConversationService extends BaseService<ImConversation> { ...@@ -137,16 +137,6 @@ public interface ImConversationService extends BaseService<ImConversation> {
*/ */
List<ImConversation> getMyImConversationList() throws Exception; List<ImConversation> getMyImConversationList() throws Exception;
/**
* 判断两个客户端是否存在会话
*
* @param clientId1
* @param clientId2
* @return
*/
Integer getRepetitionConversation(Long clientId1, Long clientId2);
/** /**
* 判断重复会话中的Attributes是否一样 * 判断重复会话中的Attributes是否一样
* *
......
...@@ -35,23 +35,23 @@ public class ImApplicationServiceImpl extends BaseServiceImpl<ImApplicationMappe ...@@ -35,23 +35,23 @@ public class ImApplicationServiceImpl extends BaseServiceImpl<ImApplicationMappe
private ImApplicationMapper imApplicationMapper; private ImApplicationMapper imApplicationMapper;
@Override @Override
@Cacheable(key = "#p0") @Cacheable(key = "'id_'+#p0")
public ImApplication getCacheById(Long id) { public ImApplication getCacheById(Long id) {
return super.getById(id); return super.getById(id);
} }
@Override @Override
@CacheEvict(key = "#p0") @CacheEvict(key = "'id_'+#p0")
public void deleteCacheById(Long id) { public void deleteCacheById(Long id) {
} }
@Override @Override
@CacheEvict(key = "#p0") @CacheEvict(key = "'appKey_'+#p0")
public void deleteCacheByAppKey(String appKey) { public void deleteCacheByAppKey(String appKey) {
} }
@Override @Override
@Cacheable(key = "#p0") @Cacheable(key = "'appKey_'+#p0")
public ImApplication getCacheAppByAppKey(String appKey) { public ImApplication getCacheAppByAppKey(String appKey) {
ImApplication imApplication = this.getOne( ImApplication imApplication = this.getOne(
new QueryWrapper<ImApplication>().lambda() new QueryWrapper<ImApplication>().lambda()
......
...@@ -128,25 +128,22 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap ...@@ -128,25 +128,22 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
// 根据appKey查询application // 根据appKey查询application
ImApplication imApplication = imApplicationService.getCacheAppByAppKey(curentJwtToken.getAppKey()); ImApplication imApplication = imApplicationService.getCacheAppByAppKey(curentJwtToken.getAppKey());
// 该应用 是否允许创建重复一对一会话 0不允许 1允许 // 该应用 是否允许创建重复单聊类型会话 0不允许 1允许
if (imApplication.getRepeatSessionStatus() != null && imApplication.getRepeatSessionStatus() == 0) { if (imApplication.getRepeatSessionStatus() != null && imApplication.getRepeatSessionStatus() == 0) {
// 判断是否已经存在一对一会话 // 判断是否已经存在单聊类型会话
// size() == 1 为单聊不允许重复创建 两个用户如果已经创建过会话,不能重复创建会话 // size() == 1 为单聊不允许重复创建 两个用户如果已经创建过会话,不能重复创建会话
if (imConversationCreate.getClientIds().size() == 1) { if (imConversationCreate.getClientIds().size() == 1) {
ImClient client2 = imClientService.getOne(new QueryWrapper<ImClient>().lambda() ImClient client2 = imClientService.getOne(new QueryWrapper<ImClient>().lambda()
.eq(ImClient::getFkAppid, createClient.getFkAppid()) .eq(ImClient::getFkAppid, createClient.getFkAppid())
.eq(ImClient::getClientId, imConversationCreate.getClientIds().get(0))); .eq(ImClient::getClientId, imConversationCreate.getClientIds().get(0)));
// 是否存在重复一对一会话 // 如果存在重复单聊类型会话,则不会为空
Integer repetitionConversation = getRepetitionConversation(createClient.getId(), client2.getId()); ImConversation repetitionConversationInfo = imConversationMapper.getRepetitionConversationSingle(createClient.getId(), client2.getId());
if (repetitionConversation != 0) { if (repetitionConversationInfo != null) {
log.info("repetitionConversation != 0"); log.info("repetitionConversation != 0");
// 返回已存在的一对一会话id // 返回已存在的单聊类型会话id
ImConversation repetitionConversationInfo = imConversationMapper.getRepetitionConversationInfo(createClient.getId(), client2.getId());
ImConversationCreateVo imConversationCreateVo = new ImConversationCreateVo(); ImConversationCreateVo imConversationCreateVo = new ImConversationCreateVo();
imConversationCreateVo.setId(repetitionConversationInfo.getId()); imConversationCreateVo.setId(repetitionConversationInfo.getId());
return ApiResult.ok(imConversationCreateVo); return ApiResult.ok(imConversationCreateVo);
} }
} }
...@@ -848,11 +845,6 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap ...@@ -848,11 +845,6 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
} }
@Override @Override
public Integer getRepetitionConversation(Long clientId1, Long clientId2) {
return imConversationMapper.getRepetitionConversation(clientId1, clientId2);
}
@Override
public Long getRepetitionConversationAttributes(Long clientId1, Long clientId2, String attributes) { public Long getRepetitionConversationAttributes(Long clientId1, Long clientId2, String attributes) {
return imConversationMapper.getRepetitionConversationAttributes(clientId1, clientId2, attributes); return imConversationMapper.getRepetitionConversationAttributes(clientId1, clientId2, attributes);
} }
......
...@@ -56,25 +56,16 @@ ...@@ -56,25 +56,16 @@
ON imConversation.id = imConversationMembers.fk_conversation_id ON imConversation.id = imConversationMembers.fk_conversation_id
WHERE imConversationMembers.fk_client_id = #{clientId} WHERE imConversationMembers.fk_client_id = #{clientId}
</select> </select>
<select id="getRepetitionConversation" resultType="java.lang.Integer">
-- 查询是否有重复会话
SELECT COUNT(id)
FROM (
SELECT im_conversation_members.*,
(SELECT COUNT(im2.id)
FROM im_conversation_members AS im2
WHERE im2.fk_conversation_id = im_conversation_members.fk_conversation_id) AS members_count
FROM im_conversation_members
INNER JOIN (SELECT *
FROM im_conversation_members
WHERE im_conversation_members.fk_client_id = #{clientId2}) AS im_conversation_members2
ON im_conversation_members.fk_conversation_id =
im_conversation_members2.fk_conversation_id
WHERE im_conversation_members.fk_client_id = #{clientId1}
) AS r1
WHERE members_count = 2
</select>
<select id="getRepetitionConversationSingle" resultType="com.wecloud.im.entity.ImConversation">
-- 查询重复的单聊会话第一条
select con.* from im_conversation con
inner join im_conversation_members mem1
on con.id = mem1.fk_conversation_id and mem1.fk_client_id = #{clientId1}
inner join im_conversation_members mem2
on con.id = mem2.fk_conversation_id and mem2.fk_client_id = #{clientId2}
where con.chat_type = 1 and con.member_count = 2 order by con.id asc LIMIT 1
</select>
<select id="getRepetitionConversationAttributes" resultType="java.lang.Long"> <select id="getRepetitionConversationAttributes" resultType="java.lang.Long">
SELECT * SELECT *
...@@ -92,23 +83,6 @@ ...@@ -92,23 +83,6 @@
AND attributes = CAST(#{attributes} AS json) LIMIT 1 AND attributes = CAST(#{attributes} AS json) LIMIT 1
</select> </select>
<select id="getRepetitionConversationInfo" resultType="com.wecloud.im.entity.ImConversation">
SELECT im_conversation.*,
(SELECT COUNT(im2.id)
FROM im_conversation_members AS im2
WHERE im2.fk_conversation_id = im_conversation_members.fk_conversation_id) AS members_count
FROM im_conversation_members
INNER JOIN (SELECT *
FROM im_conversation_members
WHERE im_conversation_members.fk_client_id = #{clientId2}) AS im_conversation_members2
ON im_conversation_members.fk_conversation_id = im_conversation_members2.fk_conversation_id
INNER JOIN im_conversation ON im_conversation.id = im_conversation_members.fk_conversation_id
WHERE im_conversation_members.fk_client_id = #{clientId1}
HAVING members_count = 2 LIMIT 1
</select>
<update id="addMemberCount"> <update id="addMemberCount">
update im_conversation set member_count = member_count + #{addCount} where fk_appid = #{appId} and id = #{conversationId} update im_conversation set member_count = member_count + #{addCount} where fk_appid = #{appId} and id = #{conversationId}
</update> </update>
......
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