修改聊天数据查询条件

This commit is contained in:
flswld
2023-02-12 05:23:48 +08:00
parent 100c7accc8
commit 15199d31e8
2 changed files with 32 additions and 7 deletions

View File

@@ -246,14 +246,29 @@ func (d *Dao) QueryChatMsgList() ([]*model.ChatMsg, error) {
func (d *Dao) QueryChatMsgListByUid(uid uint32) ([]*model.ChatMsg, error) {
db := d.db.Collection("chat_msg")
result := make([]*model.ChatMsg, 0)
find, err := db.Find(
context.TODO(),
bson.D{{"ToUid", uid}, {"Uid", uid}},
bson.D{{"ToUid", uid}},
)
if err != nil {
return nil, err
}
for find.Next(context.TODO()) {
item := new(model.ChatMsg)
err = find.Decode(item)
if err != nil {
return nil, err
}
result = append(result, item)
}
find, err = db.Find(
context.TODO(),
bson.D{{"Uid", uid}},
)
if err != nil {
return nil, err
}
result := make([]*model.ChatMsg, 0)
for find.Next(context.TODO()) {
item := new(model.ChatMsg)
err = find.Decode(item)

View File

@@ -470,15 +470,25 @@ func (u *UserManager) LoadUserChatMsgFromDbSync(userId uint32) map[uint32][]*mod
return chatMsgMap
}
for _, chatMsg := range chatMsgList {
msgList, exist := chatMsgMap[chatMsg.ToUid]
otherUid := uint32(0)
if chatMsg.Uid == userId {
otherUid = chatMsg.ToUid
} else if chatMsg.ToUid == userId {
otherUid = chatMsg.Uid
} else {
continue
}
msgList, exist := chatMsgMap[otherUid]
if !exist {
msgList = make([]*model.ChatMsg, 0)
}
if len(msgList) > MaxMsgListLen {
continue
}
msgList = append(msgList, chatMsg)
chatMsgMap[chatMsg.ToUid] = msgList
chatMsgMap[otherUid] = msgList
}
for otherUid, msgList := range chatMsgMap {
if len(msgList) > MaxMsgListLen {
chatMsgMap[otherUid] = msgList[len(msgList)-MaxMsgListLen:]
}
}
return chatMsgMap
}