Переглянути джерело

会话列表与结构微调

xuqm 1 рік тому
батько
коміт
fb4f76c9e4

+ 8 - 0
src/main/java/cn/org/bjca/trust/java/imserver/bean/group/ConversationData.java

@@ -0,0 +1,8 @@
+package cn.org.bjca.trust.java.imserver.bean.group;
+
+import lombok.Data;
+
+@Data
+public class ConversationData {
+    private String userId;
+}

+ 25 - 0
src/main/java/cn/org/bjca/trust/java/imserver/controller/conversation/v1/ConversationV1Controller.java

@@ -0,0 +1,25 @@
+package cn.org.bjca.trust.java.imserver.controller.conversation.v1;
+
+import cn.org.bjca.trust.java.imserver.HttpResult;
+import cn.org.bjca.trust.java.imserver.im.msg.ConversationMessage;
+import cn.org.bjca.trust.java.imserver.request.RequestManager;
+import org.springframework.http.HttpHeaders;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+import static cn.org.bjca.trust.java.imserver.request.RequestConstant.ConversationList;
+
+@RestController
+@RequestMapping("conversation/v1")
+public class ConversationV1Controller {
+
+    @PostMapping("/getConversation")
+    public HttpResult<List<ConversationMessage>> getConversation(@RequestHeader HttpHeaders headers) throws Exception {
+        return RequestManager.getInstance().request(ConversationList, null, headers);
+    }
+
+}

+ 4 - 2
src/main/java/cn/org/bjca/trust/java/imserver/controller/message/v1/MessageV1Controller.java

@@ -1,12 +1,14 @@
 package cn.org.bjca.trust.java.imserver.controller.message.v1;
 
 import cn.org.bjca.trust.java.imserver.HttpResult;
-import cn.org.bjca.trust.java.imserver.bean.group.GroupCreateData;
 import cn.org.bjca.trust.java.imserver.bean.group.MsgHistoryForCountData;
+import cn.org.bjca.trust.java.imserver.im.msg.msg.SZYXMessage;
 import cn.org.bjca.trust.java.imserver.request.RequestManager;
 import org.springframework.http.HttpHeaders;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.List;
+
 import static cn.org.bjca.trust.java.imserver.request.RequestConstant.MsgHistoryForCount;
 
 @RestController
@@ -14,7 +16,7 @@ import static cn.org.bjca.trust.java.imserver.request.RequestConstant.MsgHistory
 public class MessageV1Controller {
 
     @PostMapping("/getHistoryForCount")
-    public HttpResult<String> getHistoryForCount(@RequestBody MsgHistoryForCountData groupCreateData, @RequestHeader HttpHeaders headers) throws Exception {
+    public HttpResult<List<SZYXMessage>> getHistoryForCount(@RequestBody MsgHistoryForCountData groupCreateData, @RequestHeader HttpHeaders headers) throws Exception {
         return RequestManager.getInstance().request(MsgHistoryForCount, groupCreateData, headers);
     }
 

+ 0 - 7
src/main/java/cn/org/bjca/trust/java/imserver/entitys/GroupEntity.java

@@ -2,16 +2,9 @@ package cn.org.bjca.trust.java.imserver.entitys;
 
 
 import cn.org.bjca.trust.java.imserver.entitys.converter.AbstractBaseTimeEntity;
-import cn.org.bjca.trust.java.imserver.entitys.converter.AbstractBaseTimeEntityNoKey;
-import cn.org.bjca.trust.java.imserver.entitys.converter.StringListConverter;
-import jakarta.persistence.Column;
-import jakarta.persistence.Convert;
 import jakarta.persistence.Entity;
-import jakarta.persistence.Id;
 import lombok.Data;
 
-import java.util.List;
-
 @Entity
 @Data
 public class GroupEntity extends AbstractBaseTimeEntity {

+ 23 - 22
src/main/java/cn/org/bjca/trust/java/imserver/im/ImManager.java

@@ -4,7 +4,6 @@ package cn.org.bjca.trust.java.imserver.im;
 import cn.org.bjca.trust.java.imserver.common.SpringUtilsAuTo;
 import cn.org.bjca.trust.java.imserver.common.json.GsonImplHelp;
 import cn.org.bjca.trust.java.imserver.entitys.GroupEntity;
-import cn.org.bjca.trust.java.imserver.entitys.UserInfo;
 import cn.org.bjca.trust.java.imserver.enums.PacketType;
 import cn.org.bjca.trust.java.imserver.im.bean.PacketMessage;
 import cn.org.bjca.trust.java.imserver.im.msg.ConnectMessage;
@@ -18,6 +17,7 @@ import org.eclipse.paho.client.mqttv3.*;
 import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
 
 import java.nio.charset.StandardCharsets;
+import java.util.List;
 
 public class ImManager {
 
@@ -50,17 +50,17 @@ public class ImManager {
 
         try {
             mqttClient = new MqttClient("tcp://114.115.203.60:18883",
-                    "server1" + System.currentTimeMillis(), new MemoryPersistence());
+                    "server" + System.currentTimeMillis(), new MemoryPersistence());
             mqttClient.setCallback(new MqttCallbackExtended() {
                 @Override
                 public void connectComplete(boolean reconnect, String serverURI) {
                     System.out.println("======>connectComplete:reconnect?" + reconnect + "::" + serverURI);
                     if (reconnect) return;
                     try {
-                        mqttClient.subscribe("server1" + PacketType.CONNECT, 2);
-                        mqttClient.subscribe("server1" + PacketType.SEND, 2);
-                        mqttClient.subscribe("server1" + PacketType.PING, 2);
-                        mqttClient.subscribe("server1" + PacketType.REQUEST, 2);
+                        mqttClient.subscribe("server" + PacketType.CONNECT, 2);
+                        mqttClient.subscribe("server" + PacketType.SEND, 2);
+                        mqttClient.subscribe("server" + PacketType.PING, 2);
+                        mqttClient.subscribe("server" + PacketType.REQUEST, 2);
                     } catch (MqttException ignored) {
                     }
                 }
@@ -74,12 +74,11 @@ public class ImManager {
                 public void messageArrived(String topic, MqttMessage message) throws Exception {
                     String msg = new String(message.getPayload());
                     System.out.println("======>messageArrived:\n" + topic + "::" + msg);
-                    if (topic.equals("server1" + PacketType.CONNECT)) {
+                    if (topic.equals("server" + PacketType.CONNECT)) {
                         sendConnectedMsg(GsonImplHelp.get().toObject(msg, ConnectMessage.class));
-                    } else if (topic.equals("server1" + PacketType.SEND)) {
+                    } else if (topic.equals("server" + PacketType.SEND)) {
                         SZYXMessage szyxMessage = GsonImplHelp.get().toObject(msg, SZYXMessage.class);
                         szyxMessage.setStatus(0);
-                        szyxMessage.setFromClientId(szyxMessage.getFromClient().getUserId());
                         saveConversation(szyxMessage);
 
                         SZYXMessage message1 = szyxMessage.copy();
@@ -101,7 +100,7 @@ public class ImManager {
                         sendAckMsg(szyxMessage);
 
                     }// 数据请求
-                    else if (topic.equals("server1" + PacketType.REQUEST)) {
+                    else if (topic.equals("server" + PacketType.REQUEST)) {
                         sendPacketMessage(PacketType.RESULT, RequestManager.getInstance().request(msg));
                     }
 
@@ -120,11 +119,18 @@ public class ImManager {
 
     private void saveConversation(SZYXMessage szyxMessage) {
         try {
-            ConversationMessage conversation = new ConversationMessage();
+            List<ConversationMessage> l = conversationRepository.getConversation(szyxMessage.getAppId(), szyxMessage.getUserName(),
+                    szyxMessage.isArrive() ? szyxMessage.getFromClientId() : szyxMessage.getToClientId(), szyxMessage.getGroupID());
+            ConversationMessage conversation;
+            if (l.size() == 0) {
+                conversation = new ConversationMessage();
+            } else {
+                conversation = l.get(0);
+            }
+//            ConversationMessage conversation = new ConversationMessage();
             conversation.setGroup(szyxMessage.isGroup());
             conversation.setSender(!szyxMessage.isArrive());
             conversation.setMessage(szyxMessage);
-            conversation.setSelf(userRepository.findFirstByUserIdAndOsTypeAndAppId(szyxMessage.getUserName(), szyxMessage.getOsType(), szyxMessage.getAppId()));
 
             conversation.setAppId(szyxMessage.getAppId());
             conversation.setUserName(szyxMessage.getUserName());
@@ -134,18 +140,10 @@ public class ImManager {
             if (szyxMessage.isGroup()) {
                 conversation.setGroupEntity(groupRepository.findFirstByAppIdAndGroupId(szyxMessage.getAppId(), szyxMessage.getGroupID()));
             } else {
-                UserInfo userInfo;
-                if (szyxMessage.isArrive()) {
-                    userInfo = userRepository.findFirstByUserIdAndOsTypeAndAppId(szyxMessage.getFromClientId(), szyxMessage.getOsType(), szyxMessage.getAppId());
-                } else {
-                    userInfo = userRepository.findFirstByUserIdAndOsTypeAndAppId(szyxMessage.getToClientId(), szyxMessage.getOsType(), szyxMessage.getAppId());
-                }
-                conversation.setUserInfo(userInfo);
+                conversation.setUserId(szyxMessage.isArrive() ? szyxMessage.getFromClientId() : szyxMessage.getToClientId());
             }
 
             conversationRepository.save(conversation);
-            conversation.getSelf().setPassword("");
-            conversation.getUserInfo().setPassword("");
             sendPacketMessage(PacketType.CONVERSATION, conversation);
         } catch (Exception e) {
             System.out.println("--------------" + e.getMessage());
@@ -155,7 +153,10 @@ public class ImManager {
     public void connect() {
         new Thread(() -> {
             try {
-                if (!mqttClient.isConnected()) {
+                if (null != mqttClient) {
+                    if (mqttClient.isConnected()) {
+                        mqttClient.disconnect();
+                    }
                     mqttClient.connect(connectOptions);
                 }
             } catch (MqttException e) {

+ 1 - 6
src/main/java/cn/org/bjca/trust/java/imserver/im/msg/ConversationMessage.java

@@ -29,10 +29,5 @@ public class ConversationMessage extends PacketMessage {
     @OneToOne
     @JoinColumn
     private GroupEntity groupEntity;
-    @OneToOne
-    @JoinColumn
-    private UserInfo userInfo;
-    @OneToOne
-    @JoinColumn
-    private UserInfo self;
+    private String userId;
 }

+ 0 - 6
src/main/java/cn/org/bjca/trust/java/imserver/im/msg/msg/SZYXMessage.java

@@ -1,7 +1,6 @@
 package cn.org.bjca.trust.java.imserver.im.msg.msg;
 
 
-import cn.org.bjca.trust.java.imserver.entitys.UserInfo;
 import cn.org.bjca.trust.java.imserver.enums.MsgType;
 import cn.org.bjca.trust.java.imserver.enums.PacketType;
 import cn.org.bjca.trust.java.imserver.im.bean.PacketMessage;
@@ -25,9 +24,6 @@ public class SZYXMessage extends PacketMessage {
     /**
      * 发送者信息
      */
-    @OneToOne
-    @JoinColumn
-    private UserInfo fromClient;
     private String fromClientId;
     /**
      * 接收者信息
@@ -82,8 +78,6 @@ public class SZYXMessage extends PacketMessage {
         szyxMessage.setStatus(this.getStatus());
         szyxMessage.setGroupID(this.getGroupID());
 
-        szyxMessage.setFromClient(this.getFromClient().copy());
-
         SZYXTextMessage tm = new SZYXTextMessage();
         tm.setText(this.getTextMessage().getText());
 

+ 9 - 0
src/main/java/cn/org/bjca/trust/java/imserver/repository/ConversationRepository.java

@@ -2,9 +2,18 @@ package cn.org.bjca.trust.java.imserver.repository;
 
 import cn.org.bjca.trust.java.imserver.im.msg.ConversationMessage;
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
 import org.springframework.stereotype.Repository;
 
+import java.util.List;
+
 @Repository
 public interface ConversationRepository extends JpaRepository<ConversationMessage, String> {
 
+    @Query(value = "select cs from ConversationMessage AS cs " +
+            "where cs.appId = ?1 and cs.userName = ?2 and (cs.userId = ?3 or cs.groupEntity._id = ?4)")
+    List<ConversationMessage> getConversation(String appId, String userName, String userId, String groupId);
+
+    List<ConversationMessage> findAllByAppIdAndUserName(String appId, String userName);
+
 }

+ 2 - 0
src/main/java/cn/org/bjca/trust/java/imserver/request/RequestConstant.java

@@ -8,4 +8,6 @@ public class RequestConstant {
     public static final String MsgHistoryForCount = "/message/v1/getHistoryForCount";
     public static final String MsgListForTime = "/message/v1/getListForTime";
     public static final String MsgListForCount = "/message/v1/getListForCount";
+
+    public static final String ConversationList = "/conversation/v1/getConversation";
 }

+ 17 - 3
src/main/java/cn/org/bjca/trust/java/imserver/request/RequestManager.java

@@ -7,9 +7,11 @@ import cn.org.bjca.trust.java.imserver.common.SpringUtilsAuTo;
 import cn.org.bjca.trust.java.imserver.common.TimeHelper;
 import cn.org.bjca.trust.java.imserver.common.json.GsonImplHelp;
 import cn.org.bjca.trust.java.imserver.entitys.GroupEntity;
+import cn.org.bjca.trust.java.imserver.im.msg.ConversationMessage;
 import cn.org.bjca.trust.java.imserver.im.msg.RequestMessage;
 import cn.org.bjca.trust.java.imserver.im.msg.ResultMessage;
 import cn.org.bjca.trust.java.imserver.im.msg.msg.SZYXMessage;
+import cn.org.bjca.trust.java.imserver.repository.ConversationRepository;
 import cn.org.bjca.trust.java.imserver.repository.GroupRepository;
 import cn.org.bjca.trust.java.imserver.repository.MessageRepository;
 import cn.org.bjca.trust.java.imserver.repository.UserRepository;
@@ -19,12 +21,11 @@ import org.springframework.data.domain.Pageable;
 import org.springframework.data.domain.Sort;
 import org.springframework.http.HttpHeaders;
 
-import java.util.Comparator;
+import java.time.ZoneOffset;
 import java.util.List;
 import java.util.UUID;
 
-import static cn.org.bjca.trust.java.imserver.request.RequestConstant.Group_create_v1;
-import static cn.org.bjca.trust.java.imserver.request.RequestConstant.MsgHistoryForCount;
+import static cn.org.bjca.trust.java.imserver.request.RequestConstant.*;
 
 public class RequestManager {
 
@@ -33,6 +34,8 @@ public class RequestManager {
     private final ApplicationRepository applicationRepository = SpringUtilsAuTo.getBean(ApplicationRepository.class);
     private final MessageRepository messageRepository = SpringUtilsAuTo.getBean(MessageRepository.class);
 
+    private final ConversationRepository conversationRepository = SpringUtilsAuTo.getBean(ConversationRepository.class);
+
     private static final class RequestManagerHolder {
         static final RequestManager instance = new RequestManager();
     }
@@ -79,9 +82,12 @@ public class RequestManager {
                             GsonImplHelp.get().toObject((String) data, GroupCreateData.class)
                             : (GroupCreateData) data, appId, userId);
                 case MsgHistoryForCount:
+                    if(null == data) return new HttpResult<>(201, "参数为空", null);
                     return (HttpResult<T>) this.getHistoryForCount(data instanceof String ?
                             GsonImplHelp.get().toObject((String) data, MsgHistoryForCountData.class)
                             : (MsgHistoryForCountData) data, appId, userId);
+                case ConversationList:
+                    return (HttpResult<T>) this.getConversation(appId, userId);
                 default:
                     return new HttpResult<>(201, "路径错误" + path, null);
             }
@@ -91,6 +97,14 @@ public class RequestManager {
     }
 
 
+    private HttpResult<List<ConversationMessage>> getConversation(String appid, String userid) {
+
+        List<ConversationMessage> l = conversationRepository.findAllByAppIdAndUserName(appid, userid);
+        l.sort((o1, o2) -> (int) (o1.getUpdateTime().toEpochSecond(ZoneOffset.UTC) - o2.getUpdateTime().toEpochSecond(ZoneOffset.UTC)));
+
+        return new HttpResult<>(200, "查询成功", l);
+    }
+
     private HttpResult<List<SZYXMessage>> getHistoryForCount(MsgHistoryForCountData data, String appid, String userid) {
 
         Pageable pageable = PageRequest.of(0, 10, Sort.by("_id").descending());