modify searchmessage

Signed-off-by: hanzhixiao <709674996@qq.com>
pull/761/head
hanzhixiao 2 years ago
parent f592a3abd7
commit 88b91ab15f

@ -361,3 +361,7 @@ func (m *MessageApi) GetActiveGroup(c *gin.Context) {
func (m *MessageApi) SearchMsg(c *gin.Context) { func (m *MessageApi) SearchMsg(c *gin.Context) {
a2r.Call(msg.MsgClient.SearchMessage, m.Client, c) a2r.Call(msg.MsgClient.SearchMessage, m.Client, c)
} }
//func (m *MessageApi) OnlineUserCount(c *gin.Context) {
// a2r.Call(msg.MsgClient, m.Client, c)
//}

@ -201,6 +201,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive
statisticsGroup.POST("/user/active", m.GetActiveUser) statisticsGroup.POST("/user/active", m.GetActiveUser)
statisticsGroup.POST("/group/create", g.GroupCreateCount) statisticsGroup.POST("/group/create", g.GroupCreateCount)
statisticsGroup.POST("/group/active", m.GetActiveGroup) statisticsGroup.POST("/group/active", m.GetActiveGroup)
//statisticsGroup.POST("/user/online", m.OnlineUserCount)
} }
return r return r
} }

@ -115,41 +115,75 @@ func (m *msgServer) SearchMessage(ctx context.Context, req *msg.SearchMessageReq
if total, chatLogs, err = m.MsgDatabase.SearchMessage(ctx, req); err != nil { if total, chatLogs, err = m.MsgDatabase.SearchMessage(ctx, req); err != nil {
return nil, err 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 { for _, chatLog := range chatLogs {
pbChatLog := &msg.ChatLog{} pbChatLog := &msg.ChatLog{}
utils.CopyStructFields(pbChatLog, chatLog) utils.CopyStructFields(pbChatLog, chatLog)
pbChatLog.SendTime = chatLog.SendTime pbChatLog.SendTime = chatLog.SendTime
pbChatLog.CreateTime = chatLog.CreateTime pbChatLog.CreateTime = chatLog.CreateTime
if chatLog.SenderNickname == "" { if chatLog.SenderNickname == "" {
sendUser, err := m.User.GetUserInfo(ctx, chatLog.SendID) pbChatLog.SenderNickname = sendMap[chatLog.SendID]
if err != nil {
return nil, err
}
pbChatLog.SenderNickname = sendUser.Nickname
} }
switch chatLog.SessionType { switch chatLog.SessionType {
case constant.SingleChatType: case constant.SingleChatType:
recvUser, err := m.User.GetUserInfo(ctx, chatLog.RecvID) pbChatLog.RecvNickname = recvMap[chatLog.RecvID]
if err != nil {
return nil, err
}
pbChatLog.RecvNickname = recvUser.Nickname
case constant.GroupChatType, constant.SuperGroupChatType: case constant.GroupChatType, constant.SuperGroupChatType:
group, err := m.Group.GetGroupInfo(ctx, chatLog.GroupID) pbChatLog.SenderFaceURL = groupMap[chatLog.GroupID].FaceURL
if err != nil { pbChatLog.GroupMemberCount = groupMap[chatLog.GroupID].MemberCount
return nil, err pbChatLog.RecvID = groupMap[chatLog.GroupID].GroupID
} pbChatLog.GroupName = groupMap[chatLog.GroupID].GroupName
pbChatLog.SenderFaceURL = group.FaceURL pbChatLog.GroupOwner = groupMap[chatLog.GroupID].OwnerUserID
pbChatLog.GroupMemberCount = group.MemberCount pbChatLog.GroupType = groupMap[chatLog.GroupID].GroupType
pbChatLog.RecvID = group.GroupID
pbChatLog.GroupName = group.GroupName
pbChatLog.GroupOwner = group.OwnerUserID
pbChatLog.GroupType = group.GroupType
} }
resp.ChatLogs = append(resp.ChatLogs, pbChatLog) resp.ChatLogs = append(resp.ChatLogs, pbChatLog)
} }
resp.ChatLogsNum = total resp.ChatLogsNum = total
return resp, nil return resp, nil
} }

@ -44,3 +44,7 @@ func (s *userServer) UserRegisterCount(
} }
return &pbuser.UserRegisterCountResp{Total: total, Before: before, Count: count}, nil return &pbuser.UserRegisterCountResp{Total: total, Before: before, Count: count}, nil
} }
//func (s *userServer) OnlineUserCount(ctx context.Context) error {
// s.
//}

@ -960,6 +960,9 @@ func (db *commonMsgDatabase) SearchMessage(ctx context.Context, req *pbMsg.Searc
return 0, nil, err return 0, nil, err
} }
for _, msg := range msgs { for _, msg := range msgs {
if msg.IsRead {
msg.Msg.IsRead = true
}
totalMsgs = append(totalMsgs, convert.MsgDB2Pb(msg.Msg)) totalMsgs = append(totalMsgs, convert.MsgDB2Pb(msg.Msg))
} }
return total, totalMsgs, nil return total, totalMsgs, nil

@ -61,6 +61,8 @@ type UserDatabase interface {
GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error)
// SetUserStatus Set the user status and store the user status in redis // SetUserStatus Set the user status and store the user status in redis
SetUserStatus(ctx context.Context, list []*user.OnlineStatus) error SetUserStatus(ctx context.Context, list []*user.OnlineStatus) error
//OnlineUserCount(ctx context.Context, start time.Time, end time.Time) (int64, map[string]int64, error)
} }
type userDatabase struct { type userDatabase struct {
@ -176,7 +178,7 @@ func (u *userDatabase) CountRangeEverydayTotal(ctx context.Context, start time.T
return u.userDB.CountRangeEverydayTotal(ctx, start, end) return u.userDB.CountRangeEverydayTotal(ctx, start, end)
} }
//SubscribeOrCancelUsersStatus Subscribe or unsubscribe a user's presence status // SubscribeOrCancelUsersStatus Subscribe or unsubscribe a user's presence status
func (u *userDatabase) SubscribeOrCancelUsersStatus(ctx context.Context, userID string, userIDs []string, genre int32) error { func (u *userDatabase) SubscribeOrCancelUsersStatus(ctx context.Context, userID string, userIDs []string, genre int32) error {
var err error var err error
if genre == constant.SubscriberUser { if genre == constant.SubscriberUser {

@ -1072,11 +1072,6 @@ func (m *MsgMongoDriver) SearchMessage(ctx context.Context, req *msg.SearchMessa
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
} }
for _, msg1 := range msgs {
if msg1.IsRead {
msg1.Msg.IsRead = true
}
}
return total, msgs, nil return total, msgs, nil
} }
@ -1150,13 +1145,22 @@ func (m *MsgMongoDriver) searchMessage(ctx context.Context, req *msg.SearchMessa
{"doc_id", 1}, {"doc_id", 1},
}}, }},
}, },
{
{"$unwind", bson.M{"path": "$msgs"}},
},
{
{"$sort", bson.M{"msgs.msg.send_time": -1}},
},
} }
cursor, err := m.MsgCollection.Aggregate(ctx, pipe) cursor, err := m.MsgCollection.Aggregate(ctx, pipe)
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
} }
type docModel struct {
var msgsDocs []table.MsgDocModel DocID string `bson:"doc_id"`
Msg *table.MsgInfoModel `bson:"msgs"`
}
var msgsDocs []docModel
err = cursor.All(ctx, &msgsDocs) err = cursor.All(ctx, &msgsDocs)
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
@ -1166,41 +1170,39 @@ func (m *MsgMongoDriver) searchMessage(ctx context.Context, req *msg.SearchMessa
} }
msgs := make([]*table.MsgInfoModel, 0) msgs := make([]*table.MsgInfoModel, 0)
for index := range msgsDocs { for index := range msgsDocs {
for i := range msgsDocs[index].Msg { msgInfo := msgsDocs[index].Msg
msg := msgsDocs[index].Msg[i] if msgInfo == nil || msgInfo.Msg == nil {
if msg == nil || msg.Msg == nil { continue
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,
} }
if msg.Revoke != nil { data, err := json.Marshal(&revokeContent)
revokeContent := sdkws.MessageRevokedContent{ if err != nil {
RevokerID: msg.Revoke.UserID, return 0, nil, err
RevokerRole: msg.Revoke.Role, }
ClientMsgID: msg.Msg.ClientMsgID, elem := sdkws.NotificationElem{
RevokerNickname: msg.Revoke.Nickname, Detail: string(data),
RevokeTime: msg.Revoke.Time, }
SourceMessageSendTime: msg.Msg.SendTime, content, err := json.Marshal(&elem)
SourceMessageSendID: msg.Msg.SendID, if err != nil {
SourceMessageSenderNickname: msg.Msg.SenderNickname, return 0, nil, err
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.Msg.ContentType = constant.MsgRevokeNotification
msgInfo.Msg.Content = string(content)
} }
msgs = append(msgs, msgInfo)
} }
start := (req.Pagination.PageNumber - 1) * req.Pagination.ShowNumber start := (req.Pagination.PageNumber - 1) * req.Pagination.ShowNumber
n := int32(len(msgs)) n := int32(len(msgs))

@ -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
Loading…
Cancel
Save