Commit a01e30ee by 罗长华

完善publishPrivateMessage接口

parent 85156e3e
package com.wecloud.im.mapper; package com.wecloud.im.mapper;
import java.io.Serializable;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
...@@ -7,11 +13,6 @@ import com.wecloud.im.entity.ImConversation; ...@@ -7,11 +13,6 @@ import com.wecloud.im.entity.ImConversation;
import com.wecloud.im.param.ImConversationPageParam; import com.wecloud.im.param.ImConversationPageParam;
import com.wecloud.im.param.ImConversationQueryVo; import com.wecloud.im.param.ImConversationQueryVo;
import com.wecloud.im.vo.ConversationVo; import com.wecloud.im.vo.ConversationVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.io.Serializable;
import java.util.List;
/** /**
* 会话表 Mapper 接口 * 会话表 Mapper 接口
...@@ -46,7 +47,7 @@ public interface ImConversationMapper extends BaseMapper<ImConversation> { ...@@ -46,7 +47,7 @@ public interface ImConversationMapper extends BaseMapper<ImConversation> {
* @param currentClientId * @param currentClientId
* @return * @return
*/ */
List<ConversationVo> getMyImConversationListAndMsgCount(@Param("currentClientId") Long currentClientId,@Param("clientId") String clientId, @Param("conversationId") Long conversationId); List<ConversationVo> getMyImConversationListAndMsgCount(@Param("currentClientId") Long currentClientId, @Param("clientId") String clientId, @Param("conversationId") Long conversationId);
/** /**
* 查询用户加入的所有会话 * 查询用户加入的所有会话
...@@ -91,4 +92,15 @@ public interface ImConversationMapper extends BaseMapper<ImConversation> { ...@@ -91,4 +92,15 @@ public interface ImConversationMapper extends BaseMapper<ImConversation> {
* @return 成功则返回true * @return 成功则返回true
*/ */
boolean upgradeToThousandChat(@Param("appId") Long appId, @Param("conversationId") Long conversationId); boolean upgradeToThousandChat(@Param("appId") Long appId, @Param("conversationId") Long conversationId);
/**
* 获取发件人和收件人会话列表
* @Author luozh
* @Date 2022年05月06日 07:15:05
* @param senderClientId 发件人im client id
* @param receiverClientIds 收件人 im client ids
* @Return
*/
List<ImConversation> getConversationBySenderAndReceivers(@Param("senderImClientId") Long senderImClientId, @Param(
"receiverImClientIds") List<Long> receiverImClientIds);
} }
package com.wecloud.im.service; package com.wecloud.im.service;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.service.BaseService;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import java.util.List;
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.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.GroupChatSettingParam;
import com.wecloud.im.param.ExitChatRoomParam; import com.wecloud.im.param.ExitChatRoomParam;
import com.wecloud.im.param.GroupChatSettingParam;
import com.wecloud.im.param.ImConversationPageParam; import com.wecloud.im.param.ImConversationPageParam;
import com.wecloud.im.param.ImConversationQueryParam; import com.wecloud.im.param.ImConversationQueryParam;
import com.wecloud.im.param.ImConversationQueryVo; import com.wecloud.im.param.ImConversationQueryVo;
...@@ -24,11 +30,6 @@ import com.wecloud.im.param.add.ImConversationNameUpdate; ...@@ -24,11 +30,6 @@ import com.wecloud.im.param.add.ImConversationNameUpdate;
import com.wecloud.im.vo.ChatRoomMemberVo; import com.wecloud.im.vo.ChatRoomMemberVo;
import com.wecloud.im.vo.ConversationVo; import com.wecloud.im.vo.ConversationVo;
import com.wecloud.im.vo.ImConversationCreateVo; import com.wecloud.im.vo.ImConversationCreateVo;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.service.BaseService;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import java.util.List;
/** /**
* 会话表 服务类 * 会话表 服务类
...@@ -245,5 +246,15 @@ public interface ImConversationService extends BaseService<ImConversation> { ...@@ -245,5 +246,15 @@ public interface ImConversationService extends BaseService<ImConversation> {
*/ */
List<ConversationVo> listConversation(ListConversationParam param); List<ConversationVo> listConversation(ListConversationParam param);
/**
*
* @Author luozh
* @Date 2022年05月06日 07:18:34
* @param senderClientId
* @param receiverClientIds
* @Return
*/
List<ImConversation> getConversationBySenderAndReceivers(Long senderImClientId, List<Long> receiverImClientIds);
} }
...@@ -1150,6 +1150,11 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap ...@@ -1150,6 +1150,11 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
return conversationVoList; return conversationVoList;
} }
@Override
public List<ImConversation> getConversationBySenderAndReceivers(Long senderImClientId, List<Long> receiverImClientIds) {
return baseMapper.getConversationBySenderAndReceivers(senderImClientId, receiverImClientIds);
}
private ImConversationCreateVo createChatRoom(ImConversationCreate imConversationCreate, ImClient createClient) { private ImConversationCreateVo createChatRoom(ImConversationCreate imConversationCreate, ImClient createClient) {
// 会话id // 会话id
Long chatRoomId = SnowflakeUtil.getId(); Long chatRoomId = SnowflakeUtil.getId();
......
...@@ -15,6 +15,7 @@ import java.util.Date; ...@@ -15,6 +15,7 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
...@@ -27,6 +28,7 @@ import com.alibaba.fastjson.JSONObject; ...@@ -27,6 +28,7 @@ import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem; import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
...@@ -663,7 +665,90 @@ public class ImMessageServiceImpl extends BaseServiceImpl<ImMessageMapper, ImMes ...@@ -663,7 +665,90 @@ public class ImMessageServiceImpl extends BaseServiceImpl<ImMessageMapper, ImMes
@Override @Override
public Boolean privateMessagePublish(PrivateChatMessageParam param) { public Boolean privateMessagePublish(PrivateChatMessageParam param) {
return null; Long appId = SecurityUtils.getCurrentAppId();
ImApplication application = imApplicationService.getCacheById(appId);
// 获取发件人信息
String senderClientId = param.getFromUserId();
ImClient sender = imClientService.getCacheImClient(appId, senderClientId);
if (sender == null) {
throw new BusinessException("id为 " + senderClientId + " 的发件人不存在");
}
// 获取收件人信息
String toUserIdsStr = param.getToUserIds();
List<String> toUserIdList = Arrays.asList(toUserIdsStr.split(","));
List<ImClient> clientList = imClientService.list(Wrappers.<ImClient>lambdaQuery().eq(ImClient::getFkAppid,
appId).in(ImClient::getClientId, toUserIdList));
if (clientList.isEmpty()) {
throw new BusinessException("收件人列表为空");
}
// 查找conversation列表
Long senderImClientId = sender.getId();
List<Long> receiverImClientIds = clientList.stream().map(ImClient::getId).collect(Collectors.toList());
List<ImConversation> conversationList =
imConversationService.getConversationBySenderAndReceivers(senderImClientId, receiverImClientIds);
Map<Long, ImConversation> conversationMapGroupById =
conversationList.stream().collect(Collectors.toMap(ImConversation::getId, Function.identity()));
// 查找会话成员
List<Long> conversationIdList = conversationList.stream().map(ImConversation::getId).collect(Collectors.toList());
List<ImConversationMembers> conversationMembersList =
imConversationMembersService.list(Wrappers.<ImConversationMembers>lambdaQuery().in(ImConversationMembers::getFkConversationId, conversationIdList));
Map<Long, List<ImConversationMembers>> conMembersMapGroupByConId =
conversationMembersList.stream().collect(Collectors.groupingBy(ImConversationMembers::getFkConversationId));
Map<Long, ImConversationMembers> receiverConversationMembersMap =
conversationMembersList.stream().filter(member -> {
return !member.getFkClientId().equals(senderImClientId);
}).collect(Collectors.toMap(ImConversationMembers::getFkClientId, Function.identity()));
// 根据收件人imClientId对会话/会话成员进行分组
Map<Long, ImConversation> conMapGroupByReceiverImClientId = new HashMap<>();
Map<Long, List<ImConversationMembers>> conMemberMap = new HashMap<>();
// // 校验群聊信息
// if (conversationList.size() <= 0) {
// throw new BusinessException("群聊ids为 " + conversationIdsStr + " 的群聊列表不存在");
// }
//
// conversationList.forEach(conversation -> {
// // 获取群成员
// List<ImConversationMembers> membersList = imConversationMembersService.list(
// new QueryWrapper<ImConversationMembers>().lambda()
// .eq(ImConversationMembers::getFkConversationId, conversation.getId()));
//
// if (param.getToUserIds() != null && !param.getToUserIds().isEmpty()) {
// // 定向发送
// membersList =
// membersList.stream().filter(members -> param.getToUserIds().contains(members.getClientId())).collect(Collectors.toList());
// }
//
// // 组装消息
// ImMessage message = assembleImMessage(appId, sender, conversation.getId(), param.getMessageType(), false, param.getContent());
// // 持久化
// this.save(message);
// // 拼装发送消息体
// ImMessageOnlineSend imMessageOnlineSend = assembleImMessageOnlineSend(message, sender, appId);
//
// // 发送消息
// for (ImConversationMembers members : membersList) {
// if (members.getClientId().equals(senderClientId) && !param.getIncludeSender()) {
// // 是否是发送者 且includeSender不为true
// continue;
// }
// // 入库 保存收件箱
// saveImInbox(application, conversation.getId(), imMessageOnlineSend.getMsgId(),
// members, SnowflakeUtil.getId());
//
// // 在线用户直接发消息
// sendMsgForOnline(members.getFkClientId(), imMessageOnlineSend);
//
// // 离线消息推送
// pushMsgToOfflineMembers(application, members, param.getPushContent(), param.getPushExt());
// }
//
// });
return true;
} }
@Override @Override
......
...@@ -46,20 +46,20 @@ ...@@ -46,20 +46,20 @@
FROM im_inbox INNER JOIN im_message ON im_inbox.fk_msg_id = im_message.id FROM im_inbox INNER JOIN im_message ON im_inbox.fk_msg_id = im_message.id
WHERE im_inbox.fk_conversation_id = imConversation.id WHERE im_inbox.fk_conversation_id = imConversation.id
AND im_inbox.receiver = #{currentClientId} AND im_inbox.receiver = #{currentClientId}
AND im_inbox.read_msg_status = 0 AND im_inbox.read_msg_status = 0
AND (FIND_IN_SET(#{clientId},im_message.`at`) > 0 or FIND_IN_SET('-1',im_message.`at`) > 0)) AS beAtCount, AND (FIND_IN_SET(#{clientId},im_message.`at`) > 0 or FIND_IN_SET('-1',im_message.`at`) > 0)) AS beAtCount,
( (
SELECT GROUP_CONCAT(im_client.client_id) SELECT GROUP_CONCAT(im_client.client_id)
FROM im_conversation_members AS im_conversation_members FROM im_conversation_members AS im_conversation_members
INNER JOIN im_client AS im_client ON im_client.id = im_conversation_members.fk_client_id INNER JOIN im_client AS im_client ON im_client.id = im_conversation_members.fk_client_id
WHERE im_conversation_members.fk_conversation_id = imConversation.id WHERE im_conversation_members.fk_conversation_id = imConversation.id
) AS members ) AS members
FROM im_conversation_members AS imConversationMembers FROM im_conversation_members AS imConversationMembers
INNER JOIN im_conversation AS imConversation INNER JOIN im_conversation AS imConversation
ON imConversation.id = imConversationMembers.fk_conversation_id ON imConversation.id = imConversationMembers.fk_conversation_id
INNER JOIN im_client AS im_client ON im_client.id = imConversation.creator INNER JOIN im_client AS im_client ON im_client.id = imConversation.creator
WHERE imConversationMembers.fk_client_id = #{currentClientId} WHERE imConversationMembers.fk_client_id = #{currentClientId}
AND imConversationMembers.display_status = 1 AND imConversationMembers.display_status = 1
<if test="conversationId != null"> <if test="conversationId != null">
AND imConversation.id = #{conversationId} AND imConversation.id = #{conversationId}
</if> </if>
...@@ -76,12 +76,16 @@ ...@@ -76,12 +76,16 @@
<select id="getRepetitionConversationSingle" resultType="com.wecloud.im.entity.ImConversation"> <select id="getRepetitionConversationSingle" resultType="com.wecloud.im.entity.ImConversation">
-- 查询重复的单聊会话第一条 -- 查询重复的单聊会话第一条
select con.* from im_conversation con select con.*
inner join im_conversation_members mem1 from im_conversation con
on con.id = mem1.fk_conversation_id and mem1.fk_client_id = #{clientId1} inner join im_conversation_members mem1
inner join im_conversation_members mem2 on con.id = mem1.fk_conversation_id and mem1.fk_client_id = #{clientId1}
on con.id = mem2.fk_conversation_id and mem2.fk_client_id = #{clientId2} inner join im_conversation_members mem2
where con.chat_type = 1 and con.member_count = 2 order by con.id asc LIMIT 1 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>
<select id="getRepetitionConversationAttributes" resultType="java.lang.Long"> <select id="getRepetitionConversationAttributes" resultType="java.lang.Long">
...@@ -97,15 +101,65 @@ ...@@ -97,15 +101,65 @@
im_conversation_members2.fk_conversation_id im_conversation_members2.fk_conversation_id
WHERE im_conversation_members.fk_client_id = #{clientId1} WHERE im_conversation_members.fk_client_id = #{clientId1}
) )
AND attributes = CAST(#{attributes} AS json) LIMIT 1 AND attributes = CAST(#{attributes} AS json)
LIMIT 1
</select> </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>
<update id="updateMemberCount"> <update id="updateMemberCount">
update im_conversation set chat_type = 3 where fk_appid = #{appId} and id = #{conversationId} update im_conversation
set chat_type = 3
where fk_appid = #{appId}
and id = #{conversationId}
</update> </update>
<select id="getConversationBySenderAndReceivers" resultType="com.wecloud.im.entity.ImConversation">
SELECT
c.id,
c.create_time,
c.update_time,
c.last_message,
c.member_count,
c.chat_type,
c.fk_appid,
c.creator,
c.name,
c.attributes,
c.system_flag,
c.muted,
c.is_forbid_add_friend as forbid_add_friend,
c.is_forbid_send_red_packets as forbid_send_red_packets,
c.is_forbid_send_pic as forbid_send_pic,
c.is_forbid_send_link as forbid_send_link
FROM
im_conversation c
LEFT JOIN im_conversation_members cm ON c.id = cm.fk_conversation_id
WHERE
c.chat_type = 1
AND (
(
c.creator = #{senderImClientId} AND cm.fk_client_id IN
<foreach collection="receiverImClientIds" item="id" index="index" open="(" close=")"
separator=",">
#{id}
</foreach>
)
OR (
c.creator IN
<foreach collection="receiverImClientIds" item="id" index="index" open="(" close=")"
separator=",">
#{id}
</foreach>
AND cm.fk_client_id = #{senderImClientId}
)
)
</select>
</mapper> </mapper>
package com.wecloud.im.sdk.sample;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.wecloud.im.sdk.WecloudIm;
import com.wecloud.im.sdk.WecloudImClientBuilder;
import com.wecloud.im.sdk.model.GroupChatMessage;
import com.wecloud.im.sdk.model.GroupChatStatusMessage;
/**
* 发布群消息
* @Author luozh
* @Date 2022年05月06日 01:37:29
* @Version 1.0
*/
public class PublishGroupChatMessageSample {
public static void main(String[] args) {
WecloudIm im = new WecloudImClientBuilder().build("http://139.159.136.86:8082",
"QizKVHcILRWp6Td2",
"287d04828099fb7de871e9dda845fa8b6b2302faf2ab3737");
String senderId = "1519984635566845952";
String toGroupIds = "1519985463165931520";
String messageType = "-1";
Map<String, Object> msg = new HashMap<>();
msg.put("toConversation", "1519985463165931520");
msg.put("type", -1);
msg.put("text", "这是测试消息: " + DateUtil.formatDateTime(new Date()));
boolean silent = true;
GroupChatMessage groupChatMessage = new GroupChatMessage();
groupChatMessage.setFromUserId(senderId);
groupChatMessage.setToGroupIds(toGroupIds);
groupChatMessage.setMessageType(messageType);
groupChatMessage.setContent(JSONUtil.toJsonStr(msg));
groupChatMessage.setPushContent("");
groupChatMessage.setIncludeSender(false);
groupChatMessage.setPersisted(true);
groupChatMessage.setMentioned(false);
groupChatMessage.setSilent(silent);
// groupChatMessage.setPushExt(new PushExtParam());
String s = " {\"content\":\"{\\\"amount\\\":1.000,\\\"blessingWords\\\":\\\"恭喜发财,大吉大利\\\"," +
"\\\"id\\\":1522526485813088256,\\\"liveStatus\\\":1,\\\"number\\\":1,\\\"payMode\\\":1,\\\"payStatus\\\":2,\\\"payTime\\\":1651833576000,\\\"redEnvelopeType\\\":2,\\\"remainingAmount\\\":0.000,\\\"remainingPacket\\\":0,\\\"sponsorAccountCode\\\":\\\"1517329604459835392\\\",\\\"targetType\\\":2}\",\"fromUserId\":\"1517329604459835392\",\"includeBlacklist\":false,\"includeSender\":true,\"messageType\":\"201\",\"toGroupIds\":\"1519985463165931520\"}";
GroupChatStatusMessage groupChatMessage1 = JSONObject.parseObject(s, GroupChatStatusMessage.class);
Boolean result = im.publishGroupChatStatusMessage(groupChatMessage1);
}
}
package com.wecloud.im.sdk.sample;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.wecloud.im.sdk.WecloudIm;
import com.wecloud.im.sdk.WecloudImClientBuilder;
import com.wecloud.im.sdk.model.PrivateChatMessage;
/**
* 发布单聊消息
* @Author luozh
* @Date 2022年05月06日 01:37:29
* @Version 1.0
*/
public class PublishPrivateChatMessageSample {
public static void main(String[] args) {
// WecloudIm im = new WecloudImClientBuilder().build("http://139.159.136.86:8082",
// "QizKVHcILRWp6Td2",
// "287d04828099fb7de871e9dda845fa8b6b2302faf2ab3737");
WecloudIm im = new WecloudImClientBuilder().build("http://127.0.0.1:8082",
"QizKVHcILRWp6Td2",
"287d04828099fb7de871e9dda845fa8b6b2302faf2ab3737");
String senderId = "1519984635566845952";
String toGroupIds = "1513691265777217536";
String messageType = "-1";
Map<String, Object> msg = new HashMap<>();
msg.put("toConversation", "1519985463165931520");
msg.put("type", -1);
msg.put("text", "这是测试消息: " + DateUtil.formatDateTime(new Date()));
boolean silent = true;
PrivateChatMessage privateChatMessage = new PrivateChatMessage();
privateChatMessage.setFromUserId(senderId);
privateChatMessage.setToUserIds(toGroupIds);
privateChatMessage.setMessageType(messageType);
privateChatMessage.setContent(JSONUtil.toJsonStr(msg));
privateChatMessage.setPushContent("");
privateChatMessage.setIncludeSender(false);
privateChatMessage.setPersisted(true);
privateChatMessage.setSilent(silent);
// groupChatMessage.setPushExt(new PushExtParam());
Boolean result = im.publishPrivateMessage(privateChatMessage);
}
}
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