소스 검색

获取用户消息 修改

779513719 5 년 전
부모
커밋
118f27a6f4

+ 0 - 2
jim-common/src/main/java/org/jim/common/utils/ChatKit.java

@@ -8,14 +8,12 @@ import org.apache.log4j.Logger;
 import org.jim.common.ImConst;
 import org.jim.common.ImAio;
 import org.jim.common.ImPacket;
-import org.jim.common.ImSessionContext;
 import org.jim.common.ImStatus;
 import org.jim.common.config.Config;
 import org.jim.common.http.HttpConst;
 import org.jim.common.packets.ChatBody;
 import org.jim.common.packets.Command;
 import org.jim.common.packets.RespBody;
-import org.jim.common.packets.User;
 import org.jim.common.session.id.impl.UUIDSessionIdGenerator;
 import org.tio.core.ChannelContext;
 import org.tio.utils.lock.SetWithLock;

+ 111 - 16
jim-server/src/main/java/org/jim/server/command/handler/MessageReqHandler.java

@@ -1,19 +1,25 @@
 package org.jim.server.command.handler;
 
+import com.alibaba.fastjson.JSONObject;
+import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.jim.common.ImConst;
 import org.jim.common.ImPacket;
 import org.jim.common.ImStatus;
+import org.jim.common.packets.*;
+import org.jim.server.model.ConversationRecord;
+import org.jim.server.model.GroupConversationMiddle;
 import org.tio.core.ChannelContext;
 import org.jim.common.message.MessageHelper;
-import org.jim.common.packets.Command;
-import org.jim.common.packets.RespBody;
-import org.jim.common.packets.UserMessageData;
-import org.jim.common.packets.MessageReqBody;
 import org.jim.common.utils.ImKit;
 import org.jim.common.utils.JsonKit;
 import org.jim.server.command.AbstractCmdHandler;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 /**
  * 获取聊天消息命令处理器
  * @author WChao
@@ -37,6 +43,103 @@ public class MessageReqHandler extends AbstractCmdHandler {
 			//用户消息格式不正确
 			return getMessageFailedPacket(channelContext);
 		}
+		//当前用户ID;
+		String userId = messageReqBody.getUserId();
+		//群组ID;
+		String groupId = messageReqBody.getGroupId();
+		//消息类型;
+		int type = messageReqBody.getType();
+		//如果用户ID为空或者type格式不正确或者群组id为空,获取消息失败;
+		if( StringUtils.isEmpty(userId) || StringUtils.isEmpty(groupId) || (0 != type && 1 != type) ){
+			return getMessageFailedPacket(channelContext);
+		}
+		if(type == 0){
+			resPacket = new RespBody(Command.COMMAND_GET_MESSAGE_RESP,ImStatus.C10016);
+		}else{
+			resPacket = new RespBody(Command.COMMAND_GET_MESSAGE_RESP,ImStatus.C10018);
+		}
+		//群组ID不为空获取用户该群组消息;
+		if(ImConst.ON.equals(imConfig.getIsStore())){
+			return this.disposeRedisData(messageReqBody, channelContext, resPacket);
+		}else{
+			//从 数据库 中 获取 数据
+			return this.disposeMysqlData(groupId,userId,channelContext,resPacket);
+		}
+	}
+
+	/**
+	 * 从数据库中获取数据
+	 * @param groupId
+ 	 * @param userId
+ 	 * @param channelContext
+ 	 * @param resPacket
+	 * @return {@link {@link ImPacket}}
+	 * @author Darren
+	 * @date 2020/2/19 17:01
+	 */
+	private ImPacket disposeMysqlData(
+			String groupId, String userId, ChannelContext channelContext, RespBody resPacket
+	){
+		Map<String, List<ChatBody>> groups = new HashMap<>();
+		groups.put(groupId,this.getChatBodyListByGroupId(groupId));
+		UserMessageData messageData = new UserMessageData(userId);
+		messageData.setGroups(groups);
+		resPacket.setData(messageData);
+		return ImKit.ConvertRespPacket(resPacket, channelContext);
+	}
+
+	/**
+	 * 根据groupId 获取 chatBodyList
+	 * @param groupId
+	 * @return {@link {@link List< ChatBody>}}
+	 * @author Darren
+	 * @date 2020/2/19 17:11
+	 */
+	private List<ChatBody> getChatBodyListByGroupId(String groupId){
+		List<ChatBody> chatBodyList = new ArrayList<>();
+		List<GroupConversationMiddle> groupConversationMiddleList = GroupConversationMiddle.dao
+				.find("SELECT * FROM sw_group_conversation_middle WHERE group_id = ? order by create_time ASC ", groupId);
+		if(CollectionUtils.isNotEmpty(groupConversationMiddleList)){
+			for (GroupConversationMiddle conversationMiddle : groupConversationMiddleList) {
+				List<ConversationRecord> conversationRecords = ConversationRecord.dao
+						.find(
+								"SELECT * FROM sw_conversation_record WHERE conversation_id = ? order by create_time ASC "
+								, conversationMiddle.getLong("conversation_id")
+						);
+				if(CollectionUtils.isNotEmpty(conversationRecords)){
+					for (ConversationRecord conversationRecord : conversationRecords) {
+						ChatBody chatBody = ChatBody.newBuilder().build();
+						chatBody.setId(conversationRecord.getStr("chat_body_id"));
+						chatBody.setCreateTime(conversationRecord.getTimestamp("create_time").getTime());
+						chatBody.setFrom(conversationRecord.getStr("chat_body_from"));
+						chatBody.setTo(conversationRecord.getStr("chat_body_to"));
+						chatBody.setMsgType(conversationRecord.getInt("chat_body_msg_type"));
+						chatBody.setGroup_id(groupId);
+						chatBody.setContent(conversationRecord.getStr("content"));
+						chatBody.setChatType(ChatType.CHAT_TYPE_PUBLIC.getNumber());
+						Map<String, Object> map = new HashMap<>();
+						map.put("type",conversationRecord.getStr("type"));
+						chatBody.setExtras(new JSONObject(map));
+						chatBodyList.add(chatBody);
+					}
+				}
+			}
+		}
+		return chatBodyList;
+	}
+
+	/**
+	 * 获取redis中的数据
+ 	 * @param messageReqBody
+ 	 * @param channelContext
+ 	 * @param resPacket
+	 * @return {@link {@link ImPacket}}
+	 * @author Darren
+	 * @date 2020/2/19 15:24
+	 */
+	private ImPacket disposeRedisData(
+			MessageReqBody messageReqBody, ChannelContext channelContext, RespBody resPacket
+	){
 		UserMessageData messageData = null;
 		MessageHelper messageHelper = imConfig.getMessageHelper();
 		//群组ID;
@@ -55,21 +158,11 @@ public class MessageReqHandler extends AbstractCmdHandler {
 		Integer count = messageReqBody.getCount();
 		//消息类型;
 		int type = messageReqBody.getType();
-		//如果用户ID为空或者type格式不正确,获取消息失败;
-		if(StringUtils.isEmpty(userId) || (0 != type && 1 != type) || !ImConst.ON.equals(imConfig.getIsStore())){
-			return getMessageFailedPacket(channelContext);
-		}
-		if(type == 0){
-			resPacket = new RespBody(Command.COMMAND_GET_MESSAGE_RESP,ImStatus.C10016);
-		}else{
-			resPacket = new RespBody(Command.COMMAND_GET_MESSAGE_RESP,ImStatus.C10018);
-		}
-		//群组ID不为空获取用户该群组消息;
 		if(!StringUtils.isEmpty(groupId)){
 			//离线消息;
 			if(0 == type){
 				messageData = messageHelper.getGroupOfflineMessage(userId,groupId);
-			//历史消息;
+				//历史消息;
 			}else if(1 == type){
 				messageData = messageHelper.getGroupHistoryMessage(userId, groupId,beginTime,endTime,offset,count);
 			}
@@ -84,7 +177,7 @@ public class MessageReqHandler extends AbstractCmdHandler {
 			//获取与指定用户离线消息;
 			if(0 == type){
 				messageData = messageHelper.getFriendsOfflineMessage(userId, fromUserId);
-			//获取与指定用户历史消息;
+				//获取与指定用户历史消息;
 			}else if(1 == type){
 				messageData = messageHelper.getFriendHistoryMessage(userId, fromUserId,beginTime,endTime,offset,count);
 			}
@@ -92,6 +185,7 @@ public class MessageReqHandler extends AbstractCmdHandler {
 		resPacket.setData(messageData);
 		return ImKit.ConvertRespPacket(resPacket, channelContext);
 	}
+
 	/**
 	 * 获取用户消息失败响应包;
 	 * @param channelContext
@@ -101,4 +195,5 @@ public class MessageReqHandler extends AbstractCmdHandler {
 		RespBody resPacket = new RespBody(Command.COMMAND_GET_MESSAGE_RESP,ImStatus.C10015);
 		return ImKit.ConvertRespPacket(resPacket, channelContext);
 	}
+
 }

+ 16 - 5
server-chat/src/main/java/com/cn/processor/IMChatAsyncChatMessageProcessor.java

@@ -72,13 +72,15 @@ public class IMChatAsyncChatMessageProcessor extends DefaultAsyncChatMessageProc
                     Conversation conversation = Conversation.dao.findById(groupConversationMiddle.getLong("conversation_id"));
                     if( null != conversation){
                         ConversationRecord.dao
-                                ._setAttrs(this.getConversationRecordAttrs(chatBody,conversation,groupConversationMiddle))
+                                ._setAttrs(this.getConversationRecordAttrs(chatBody,conversation,groupConversationMiddle,group))
                                 .save();
                     }
                 }
             }
-            //延长redis数据过期时间
-            this.extendRedisData(chatBody,channelContext);
+            if(ON.equals(imConfig.getIsStore())){
+                //延长redis数据过期时间
+                this.extendRedisData(chatBody,channelContext);
+            }
         }
         log.info("聊天记录持久化... 结束:" + sdf.format(new Date()));
     }
@@ -113,14 +115,21 @@ public class IMChatAsyncChatMessageProcessor extends DefaultAsyncChatMessageProc
      * @param conversation
      * @return
      */
-    private Map<String, Object> getConversationRecordAttrs(ChatBody chatBody, Conversation conversation, GroupConversationMiddle groupConversationMiddle){
+    private Map<String, Object> getConversationRecordAttrs(
+              ChatBody chatBody
+            , Conversation conversation
+            , GroupConversationMiddle groupConversationMiddle
+            , ChatGroup group
+    ){
         Map<String, Object> conversationRecordAttrs = new HashMap<>();
         conversationRecordAttrs.put("id", SnowflakeIdUtils.getInstance().nextId());
         conversationRecordAttrs.put("chat_body_id",chatBody.getId());
         conversationRecordAttrs.put("content",chatBody.getContent());
+        conversationRecordAttrs.put("chat_body_msg_type",chatBody.getMsgType());
         conversationRecordAttrs.put("is_recall",0);
         conversationRecordAttrs.put("create_time",sdf.format(chatBody.getCreateTime()));
         String fromId = chatBody.getFrom();
+        conversationRecordAttrs.put("chat_body_from",fromId);
         // customerDepartmentId  visitorDepartmentId  serviceAccountRoleDepartmentId
         User user = ImAio.getUser(fromId);
         String visitType = user.getVisitType();
@@ -130,18 +139,20 @@ public class IMChatAsyncChatMessageProcessor extends DefaultAsyncChatMessageProc
             conversationRecordAttrs.put("from_id",Long.valueOf(companyId));
             conversationRecordAttrs.put("to_id",conversation.getLong("service_account_id"));
             conversationRecordAttrs.put("type",0);
-
+            conversationRecordAttrs.put("chat_body_to", group.getStr("service_account_role_department_middle_id"));
         }else if(VisitTypeEnum.VISITOR.getKey().equals(visitType)){
             // VISITOR 游客  发送的消息
             String visitorId = user.getExtras().getString("visitorId");
             conversationRecordAttrs.put("from_id",Long.valueOf(visitorId));
             conversationRecordAttrs.put("to_id",conversation.getLong("service_account_id"));
             conversationRecordAttrs.put("type",0);
+            conversationRecordAttrs.put("chat_body_to", group.getStr("service_account_role_department_middle_id"));
         }else{
             //SERVICEACCOUNT 客服  发送的消息
             String serviceAccountId = user.getExtras().getString("serviceAccountId");
             conversationRecordAttrs.put("from_id",Long.valueOf(serviceAccountId));
             conversationRecordAttrs.put("to_id",conversation.getLong("user_id"));
+            conversationRecordAttrs.put("chat_body_to", group.getStr("consumer_id"));
             JSONObject jsonObject = chatBody.getExtras();
             if(
                        null != jsonObject

+ 4 - 0
server-chat/src/main/java/com/cn/service/IMChatLoginServiceProcessor.java

@@ -436,6 +436,7 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
         attrs.put("id", conversationId );
         //会话状态 0:游客会话 1:客户会话  群组类型 0:游客-客服 类型 1:客户-客服 类型
         attrs.put("state", group.getGroupType());
+        attrs.put("consumer_id",group.getConsumerId());
         if(VisitTypeEnum.CUSTOMER.getKey().equals(user.getVisitType())){
             attrs.put("user_id", Long.valueOf(user.getExtras().getString("customerId")));
         }
@@ -449,6 +450,7 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
         ServiceAccountRoleDepartmentMiddle pojo
                 = ServiceAccountRoleDepartmentMiddle.dao.findById(group.getServiceAccountRoleDepartmentId());
         attrs.put("service_account_id", pojo.getLong("service_account_id"));
+        attrs.put("service_account_role_department_middle_id",group.getServiceAccountRoleDepartmentId());
         Conversation.dao._setAttrs(attrs).save();
         // 添加 sw_group_conversation_middle 数据
         GroupConversationMiddle.dao
@@ -743,6 +745,7 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
         attrs.put("id", conversationId );
         //会话状态 0:游客会话 1:客户会话  群组类型 0:游客-客服 类型 1:客户-客服 类型
         attrs.put("state", group.getGroupType());
+        attrs.put("consumer_id", group.getConsumerId());
         attrs.put("user_id", user.getExtras().getString("customerId"));
         attrs.put("user_nickname", user.getExtras().getString("customerNickname"));
         attrs.put("user_phone", user.getExtras().getString("customerPhone"));
@@ -751,6 +754,7 @@ public class IMChatLoginServiceProcessor implements LoginCmdProcessor {
         ServiceAccountRoleDepartmentMiddle pojo
                 = ServiceAccountRoleDepartmentMiddle.dao.findById(group.getServiceAccountRoleDepartmentId());
         attrs.put("service_account_id", pojo.getLong("service_account_id"));
+        attrs.put("service_account_role_department_middle_id", group.getServiceAccountRoleDepartmentId());
         Conversation.dao._setAttrs(attrs).save();
         // 添加 sw_group_conversation_middle 数据
         GroupConversationMiddle.dao

+ 1 - 1
server-chat/src/main/resources/jim.properties

@@ -6,7 +6,7 @@ jim.port = 8888
 #\u5FC3\u8DF3\u8D85\u65F6\u65F6\u957F
 jim.heartbeat.timeout = 0
 #\u662F\u5426\u5F00\u542F\u6D88\u606F\u6301\u4E45\u5316(on:\u5F00\u542F,off:\u4E0D\u5F00\u542F)
-jim.store = on
+jim.store = off
 #\u662F\u5426\u5F00\u542F\u96C6\u7FA4(on:\u5F00\u542F,off:\u4E0D\u5F00\u542F)
 jim.cluster = off
 #\u662F\u5426\u5F00\u542FSSL(on:\u5F00\u542F,off:\u4E0D\u5F00\u542F)