openim-server/test/mongo/mongo_utils.go

60 lines
1.4 KiB
Go
Raw Normal View History

2022-05-25 17:38:49 +08:00
package mongo
import (
"Open_IM/pkg/common/config"
server_api_params "Open_IM/pkg/proto/sdk_ws"
"context"
"fmt"
"github.com/golang/protobuf/proto"
2022-05-25 17:46:02 +08:00
"go.mongodb.org/mongo-driver/mongo"
2022-05-25 17:38:49 +08:00
"gopkg.in/mgo.v2/bson"
2022-05-25 17:58:05 +08:00
"time"
2022-05-25 17:38:49 +08:00
)
2022-05-25 17:46:02 +08:00
var (
Client *mongo.Client
)
2022-05-25 20:17:17 +08:00
type MsgInfo struct {
SendTime int64
Msg []byte
}
type UserChat struct {
UID string
Msg []MsgInfo
}
2022-05-25 17:38:49 +08:00
func GetUserAllChat(uid string) {
2022-05-25 17:58:05 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
2022-05-25 17:46:02 +08:00
collection := Client.Database(config.Config.Mongo.DBDatabase).Collection("msg")
2022-05-25 20:17:17 +08:00
var userChatList []UserChat
2022-05-25 19:15:15 +08:00
uid = uid + ":"
2022-05-25 18:26:12 +08:00
filter := bson.M{"uid": bson.M{"$regex": uid}}
//filter := bson.M{"uid": "17726378428:0"}
2022-05-25 18:25:09 +08:00
result, err := collection.Find(context.Background(), filter)
2022-05-25 17:38:49 +08:00
if err != nil {
2022-05-25 17:58:05 +08:00
fmt.Println("find error", err.Error())
2022-05-25 17:38:49 +08:00
return
}
2022-05-25 17:58:05 +08:00
if err := result.All(ctx, &userChatList); err != nil {
2022-05-25 17:38:49 +08:00
fmt.Println(err.Error())
}
for _, userChat := range userChatList {
for _, msg := range userChat.Msg {
msgData := &server_api_params.MsgData{}
err := proto.Unmarshal(msg.Msg, msgData)
if err != nil {
fmt.Println(err.Error(), msg)
continue
}
2022-06-01 11:18:28 +08:00
fmt.Println("seq: ", msgData.Seq, "status: ", msgData.Status,
"sendID: ", msgData.SendID, "recvID: ", msgData.RecvID,
"sendTime: ", msgData.SendTime,
"clientMsgID: ", msgData.ClientMsgID,
"serverMsgID: ", msgData.ServerMsgID,
"content: ", string(msgData.Content))
2022-05-25 17:38:49 +08:00
}
}
}