You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/cmdutils/mongo/mongo_utils.go

60 lines
1.4 KiB

3 years ago
package mongo
import (
"Open_IM/pkg/common/config"
2 years ago
sdkws "Open_IM/pkg/proto/sdkws"
3 years ago
"context"
"fmt"
"github.com/golang/protobuf/proto"
3 years ago
"go.mongodb.org/mongo-driver/mongo"
3 years ago
"gopkg.in/mgo.v2/bson"
3 years ago
"time"
3 years ago
)
3 years ago
var (
Client *mongo.Client
)
3 years ago
type MsgInfo struct {
SendTime int64
Msg []byte
}
type UserChat struct {
UID string
Msg []MsgInfo
}
3 years ago
func GetUserAllChat(uid string) {
3 years ago
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
3 years ago
collection := Client.Database(config.Config.Mongo.DBDatabase).Collection("msg")
3 years ago
var userChatList []UserChat
3 years ago
uid = uid + ":"
3 years ago
filter := bson.M{"uid": bson.M{"$regex": uid}}
//filter := bson.M{"uid": "17726378428:0"}
3 years ago
result, err := collection.Find(context.Background(), filter)
3 years ago
if err != nil {
3 years ago
fmt.Println("find error", err.Error())
3 years ago
return
}
3 years ago
if err := result.All(ctx, &userChatList); err != nil {
3 years ago
fmt.Println(err.Error())
}
for _, userChat := range userChatList {
for _, msg := range userChat.Msg {
2 years ago
msgData := &sdkws.MsgData{}
3 years ago
err := proto.Unmarshal(msg.Msg, msgData)
if err != nil {
fmt.Println(err.Error(), msg)
continue
}
3 years ago
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))
3 years ago
}
}
}