From 3ecd33aded12bac51b2bfe890e79b10426d768e5 Mon Sep 17 00:00:00 2001 From: Alan <68671759+hanzhixiao@users.noreply.github.com> Date: Wed, 9 Aug 2023 10:53:48 +0800 Subject: [PATCH] 744 (#766) Signed-off-by: hanzhixiao <709674996@qq.com> --- internal/rpc/msg/sync_msg.go | 76 ++++++++++++++++++++--------- pkg/common/db/controller/msg.go | 3 ++ pkg/common/db/unrelation/msg.go | 84 +++++++++++++++++---------------- scripts/start.bat | 12 +++++ 4 files changed, 113 insertions(+), 62 deletions(-) create mode 100644 scripts/start.bat diff --git a/internal/rpc/msg/sync_msg.go b/internal/rpc/msg/sync_msg.go index 87f5d8ca8..390299dbd 100644 --- a/internal/rpc/msg/sync_msg.go +++ b/internal/rpc/msg/sync_msg.go @@ -116,41 +116,75 @@ func (m *msgServer) SearchMessage(ctx context.Context, req *msg.SearchMessageReq if total, chatLogs, err = m.MsgDatabase.SearchMessage(ctx, req); err != nil { return nil, err } + + var ( + sendIDs []string + recvIDs []string + groupIDs []string + sendMap = make(map[string]string) + recvMap = make(map[string]string) + groupMap = make(map[string]*sdkws.GroupInfo) + ) + for _, chatLog := range chatLogs { + if chatLog.SenderNickname == "" { + sendIDs = append(sendIDs, chatLog.SendID) + } + switch chatLog.SessionType { + case constant.SingleChatType: + recvIDs = append(recvIDs, chatLog.RecvID) + case constant.GroupChatType, constant.SuperGroupChatType: + groupIDs = append(groupIDs, chatLog.GroupID) + } + } + if len(sendIDs) != 0 { + sendInfos, err := m.User.GetUsersInfo(ctx, sendIDs) + if err != nil { + return nil, err + } + for _, sendInfo := range sendInfos { + sendMap[sendInfo.UserID] = sendInfo.Nickname + } + } + if len(recvIDs) != 0 { + recvInfos, err := m.User.GetUsersInfo(ctx, recvIDs) + if err != nil { + return nil, err + } + for _, recvInfo := range recvInfos { + recvMap[recvInfo.UserID] = recvInfo.Nickname + } + } + if len(groupIDs) != 0 { + groupInfos, err := m.Group.GetGroupInfos(ctx, groupIDs, true) + if err != nil { + return nil, err + } + for _, groupInfo := range groupInfos { + groupMap[groupInfo.GroupID] = groupInfo + } + } for _, chatLog := range chatLogs { pbChatLog := &msg.ChatLog{} utils.CopyStructFields(pbChatLog, chatLog) pbChatLog.SendTime = chatLog.SendTime pbChatLog.CreateTime = chatLog.CreateTime if chatLog.SenderNickname == "" { - sendUser, err := m.User.GetUserInfo(ctx, chatLog.SendID) - if err != nil { - return nil, err - } - pbChatLog.SenderNickname = sendUser.Nickname + pbChatLog.SenderNickname = sendMap[chatLog.SendID] } switch chatLog.SessionType { case constant.SingleChatType: - recvUser, err := m.User.GetUserInfo(ctx, chatLog.RecvID) - if err != nil { - return nil, err - } - pbChatLog.RecvNickname = recvUser.Nickname + pbChatLog.RecvNickname = recvMap[chatLog.RecvID] case constant.GroupChatType, constant.SuperGroupChatType: - group, err := m.Group.GetGroupInfo(ctx, chatLog.GroupID) - if err != nil { - return nil, err - } - pbChatLog.SenderFaceURL = group.FaceURL - pbChatLog.GroupMemberCount = group.MemberCount - pbChatLog.RecvID = group.GroupID - pbChatLog.GroupName = group.GroupName - pbChatLog.GroupOwner = group.OwnerUserID - pbChatLog.GroupType = group.GroupType + pbChatLog.SenderFaceURL = groupMap[chatLog.GroupID].FaceURL + pbChatLog.GroupMemberCount = groupMap[chatLog.GroupID].MemberCount + pbChatLog.RecvID = groupMap[chatLog.GroupID].GroupID + pbChatLog.GroupName = groupMap[chatLog.GroupID].GroupName + pbChatLog.GroupOwner = groupMap[chatLog.GroupID].OwnerUserID + pbChatLog.GroupType = groupMap[chatLog.GroupID].GroupType } resp.ChatLogs = append(resp.ChatLogs, pbChatLog) } - resp.ChatLogsNum = total return resp, nil } diff --git a/pkg/common/db/controller/msg.go b/pkg/common/db/controller/msg.go index 418fe7a4a..2e062efdd 100644 --- a/pkg/common/db/controller/msg.go +++ b/pkg/common/db/controller/msg.go @@ -962,6 +962,9 @@ func (db *commonMsgDatabase) SearchMessage(ctx context.Context, req *pbMsg.Searc return 0, nil, err } for _, msg := range msgs { + if msg.IsRead { + msg.Msg.IsRead = true + } totalMsgs = append(totalMsgs, convert.MsgDB2Pb(msg.Msg)) } return total, totalMsgs, nil diff --git a/pkg/common/db/unrelation/msg.go b/pkg/common/db/unrelation/msg.go index bdd189f38..ce51ca49c 100644 --- a/pkg/common/db/unrelation/msg.go +++ b/pkg/common/db/unrelation/msg.go @@ -1073,11 +1073,6 @@ func (m *MsgMongoDriver) SearchMessage(ctx context.Context, req *msg.SearchMessa if err != nil { return 0, nil, err } - for _, msg1 := range msgs { - if msg1.IsRead { - msg1.Msg.IsRead = true - } - } return total, msgs, nil } @@ -1151,13 +1146,22 @@ func (m *MsgMongoDriver) searchMessage(ctx context.Context, req *msg.SearchMessa {"doc_id", 1}, }}, }, + { + {"$unwind", bson.M{"path": "$msgs"}}, + }, + { + {"$sort", bson.M{"msgs.msg.send_time": -1}}, + }, } cursor, err := m.MsgCollection.Aggregate(ctx, pipe) if err != nil { return 0, nil, err } - - var msgsDocs []table.MsgDocModel + type docModel struct { + DocID string `bson:"doc_id"` + Msg *table.MsgInfoModel `bson:"msgs"` + } + var msgsDocs []docModel err = cursor.All(ctx, &msgsDocs) if err != nil { return 0, nil, err @@ -1167,41 +1171,39 @@ func (m *MsgMongoDriver) searchMessage(ctx context.Context, req *msg.SearchMessa } msgs := make([]*table.MsgInfoModel, 0) for index := range msgsDocs { - for i := range msgsDocs[index].Msg { - msg := msgsDocs[index].Msg[i] - if msg == nil || msg.Msg == nil { - continue - } - if msg.Revoke != nil { - revokeContent := sdkws.MessageRevokedContent{ - RevokerID: msg.Revoke.UserID, - RevokerRole: msg.Revoke.Role, - ClientMsgID: msg.Msg.ClientMsgID, - RevokerNickname: msg.Revoke.Nickname, - RevokeTime: msg.Revoke.Time, - SourceMessageSendTime: msg.Msg.SendTime, - SourceMessageSendID: msg.Msg.SendID, - SourceMessageSenderNickname: msg.Msg.SenderNickname, - SessionType: msg.Msg.SessionType, - Seq: msg.Msg.Seq, - Ex: msg.Msg.Ex, - } - data, err := json.Marshal(&revokeContent) - if err != nil { - return 0, nil, err - } - elem := sdkws.NotificationElem{ - Detail: string(data), - } - content, err := json.Marshal(&elem) - if err != nil { - return 0, nil, err - } - msg.Msg.ContentType = constant.MsgRevokeNotification - msg.Msg.Content = string(content) - } - msgs = append(msgs, msg) + msgInfo := msgsDocs[index].Msg + if msgInfo == nil || msgInfo.Msg == nil { + continue } + if msgInfo.Revoke != nil { + revokeContent := sdkws.MessageRevokedContent{ + RevokerID: msgInfo.Revoke.UserID, + RevokerRole: msgInfo.Revoke.Role, + ClientMsgID: msgInfo.Msg.ClientMsgID, + RevokerNickname: msgInfo.Revoke.Nickname, + RevokeTime: msgInfo.Revoke.Time, + SourceMessageSendTime: msgInfo.Msg.SendTime, + SourceMessageSendID: msgInfo.Msg.SendID, + SourceMessageSenderNickname: msgInfo.Msg.SenderNickname, + SessionType: msgInfo.Msg.SessionType, + Seq: msgInfo.Msg.Seq, + Ex: msgInfo.Msg.Ex, + } + data, err := json.Marshal(&revokeContent) + if err != nil { + return 0, nil, err + } + elem := sdkws.NotificationElem{ + Detail: string(data), + } + content, err := json.Marshal(&elem) + if err != nil { + return 0, nil, err + } + msgInfo.Msg.ContentType = constant.MsgRevokeNotification + msgInfo.Msg.Content = string(content) + } + msgs = append(msgs, msgInfo) } start := (req.Pagination.PageNumber - 1) * req.Pagination.ShowNumber n := int32(len(msgs)) diff --git a/scripts/start.bat b/scripts/start.bat new file mode 100644 index 000000000..8054acdc4 --- /dev/null +++ b/scripts/start.bat @@ -0,0 +1,12 @@ +cd %~p0../_output/bin/platforms/windows +start api.exe -p 10002 +start auth.exe -p 10060 +start conversation.exe -p 10080 +start friend.exe -p 10020 +start group.exe -p 10050 +start msg.exe -p 10030 +start msggateway.exe -p 10040 -w 10001 +start msgtransfer.exe +start third.exe -p 10090 +start push.exe -p 10070 +start user.exe -p 10010 \ No newline at end of file