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/pkg/common/db/unrelation/msg.go

132 lines
4.2 KiB

2 years ago
package unrelation
import (
2 years ago
table "Open_IM/pkg/common/db/table/unrelation"
2 years ago
"Open_IM/pkg/proto/sdkws"
"Open_IM/pkg/utils"
"context"
"errors"
2 years ago
"fmt"
"github.com/golang/protobuf/proto"
"go.mongodb.org/mongo-driver/bson"
2 years ago
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var ErrMsgListNotExist = errors.New("user not have msg in mongoDB")
2 years ago
var ErrMsgNotFound = errors.New("msg not found")
2 years ago
type MsgMongoDriver struct {
mgoDB *mongo.Database
MsgCollection *mongo.Collection
2 years ago
msg table.MsgDocModel
2 years ago
}
func NewMsgMongoDriver(mgoDB *mongo.Database) *MsgMongoDriver {
2 years ago
return &MsgMongoDriver{mgoDB: mgoDB, MsgCollection: mgoDB.Collection(table.MsgDocModel{}.TableName())}
2 years ago
}
2 years ago
func (m *MsgMongoDriver) PushMsgsToDoc(ctx context.Context, docID string, msgsToMongo []table.MsgInfoModel) error {
filter := bson.M{"uid": docID}
return m.MsgCollection.FindOneAndUpdate(ctx, filter, bson.M{"$push": bson.M{"msg": bson.M{"$each": msgsToMongo}}}).Err()
2 years ago
}
2 years ago
func (m *MsgMongoDriver) Create(ctx context.Context, model *table.MsgDocModel) error {
_, err := m.MsgCollection.InsertOne(ctx, model)
2 years ago
return err
}
2 years ago
func (m *MsgMongoDriver) UpdateMsgStatusByIndexInOneDoc(ctx context.Context, docID string, msg *sdkws.MsgData, seqIndex int, status int32) error {
msg.Status = status
2 years ago
bytes, err := proto.Marshal(msg)
if err != nil {
return utils.Wrap(err, "")
}
2 years ago
_, err = m.MsgCollection.UpdateOne(ctx, bson.M{"uid": docID}, bson.M{"$set": bson.M{fmt.Sprintf("msg.%d.msg", seqIndex): bytes}})
2 years ago
if err != nil {
return utils.Wrap(err, "")
}
return nil
}
2 years ago
func (m *MsgMongoDriver) FindOneByDocID(ctx context.Context, docID string) (*table.MsgDocModel, error) {
doc := &table.MsgDocModel{}
err := m.MsgCollection.FindOne(ctx, bson.M{"uid": docID}).Decode(doc)
return doc, err
2 years ago
}
2 years ago
func (m *MsgMongoDriver) GetMsgsByIndex(ctx context.Context, sourceID string, index int64) (*table.MsgDocModel, error) {
2 years ago
findOpts := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"uid": 1})
2 years ago
cursor, err := m.MsgCollection.Find(ctx, bson.M{"uid": primitive.Regex{Pattern: fmt.Sprintf("^%s", sourceID)}}, findOpts)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
}
2 years ago
var msgs []table.MsgDocModel
2 years ago
err = cursor.All(context.Background(), &msgs)
if err != nil {
return nil, utils.Wrap(err, fmt.Sprintf("cursor is %s", cursor.Current.String()))
}
if len(msgs) > 0 {
return &msgs[0], nil
}
2 years ago
return nil, ErrMsgListNotExist
2 years ago
}
2 years ago
func (m *MsgMongoDriver) GetNewestMsg(ctx context.Context, sourceID string) (*table.MsgInfoModel, error) {
var msgDocs []table.MsgDocModel
cursor, err := m.MsgCollection.Find(ctx, bson.M{"uid": bson.M{"$regex": fmt.Sprintf("^%s", sourceID)}}, options.Find().SetLimit(1).SetSort(bson.M{"uid": -1}))
2 years ago
if err != nil {
2 years ago
return nil, utils.Wrap(err, "")
2 years ago
}
2 years ago
err = cursor.All(ctx, &msgDocs)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
}
2 years ago
if len(msgDocs) > 0 {
if len(msgDocs[0].Msg) > 0 {
return &msgDocs[0].Msg[len(msgDocs[0].Msg)-1], nil
2 years ago
}
2 years ago
return nil, errors.New("len(msgDocs[0].Msg) < 0")
2 years ago
}
2 years ago
return nil, ErrMsgNotFound
2 years ago
}
2 years ago
func (m *MsgMongoDriver) GetOldestMsg(ctx context.Context, sourceID string) (*table.MsgInfoModel, error) {
var msgDocs []table.MsgDocModel
cursor, err := m.MsgCollection.Find(ctx, bson.M{"uid": bson.M{"$regex": fmt.Sprintf("^%s", sourceID)}}, options.Find().SetLimit(1).SetSort(bson.M{"uid": 1}))
2 years ago
if err != nil {
return nil, err
}
2 years ago
err = cursor.All(ctx, &msgDocs)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
}
2 years ago
var oldestMsg table.MsgInfoModel
if len(msgDocs) > 0 {
for _, v := range msgDocs[0].Msg {
2 years ago
if v.SendTime != 0 {
2 years ago
oldestMsg = v
2 years ago
break
}
}
2 years ago
if len(oldestMsg.Msg) == 0 {
if len(msgDocs[0].Msg) > 0 {
oldestMsg = msgDocs[0].Msg[0]
2 years ago
}
}
2 years ago
return &oldestMsg, nil
2 years ago
}
2 years ago
return nil, ErrMsgNotFound
2 years ago
}
2 years ago
func (m *MsgMongoDriver) Delete(ctx context.Context, docIDs []string) error {
_, err := m.MsgCollection.DeleteMany(ctx, bson.M{"uid": bson.M{"$in": docIDs}})
return err
2 years ago
}
2 years ago
func (m *MsgMongoDriver) UpdateOneDoc(ctx context.Context, msg *table.MsgDocModel) error {
_, err := m.MsgCollection.UpdateOne(ctx, bson.M{"uid": msg.DocID}, bson.M{"$set": bson.M{"msg": msg.Msg}})
return err
2 years ago
}