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

180 lines
5.7 KiB

2 years ago
package unrelation
import (
"context"
"errors"
2 years ago
"fmt"
2 years ago
2 years ago
table "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2 years ago
"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"
2 years ago
"google.golang.org/protobuf/proto"
2 years ago
)
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 {
MsgCollection *mongo.Collection
2 years ago
msg table.MsgDocModel
2 years ago
}
2 years ago
func NewMsgMongoDriver(database *mongo.Database) table.MsgDocModelInterface {
return &MsgMongoDriver{MsgCollection: database.Collection(table.MsgDocModel{}.TableName())}
2 years ago
}
2 years ago
func (m *MsgMongoDriver) PushMsgsToDoc(ctx context.Context, docID string, msgsToMongo []table.MsgInfoModel) error {
2 years ago
return m.MsgCollection.FindOneAndUpdate(ctx, bson.M{"doc_id": docID}, bson.M{"$push": bson.M{"msgs": 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{"doc_id": docID}, bson.M{"$set": bson.M{fmt.Sprintf("msgs.%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{}
2 years ago
err := m.MsgCollection.FindOne(ctx, bson.M{"doc_id": docID}).Decode(doc)
2 years ago
return doc, err
2 years ago
}
func (m *MsgMongoDriver) GetMsgsByIndex(ctx context.Context, conversationID string, index int64) (*table.MsgDocModel, error) {
2 years ago
findOpts := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"doc_id": 1})
cursor, err := m.MsgCollection.Find(ctx, bson.M{"doc_id": primitive.Regex{Pattern: fmt.Sprintf("^%s:", conversationID)}}, 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
}
func (m *MsgMongoDriver) GetNewestMsg(ctx context.Context, conversationID string) (*table.MsgInfoModel, error) {
2 years ago
var msgDocs []table.MsgDocModel
2 years ago
cursor, err := m.MsgCollection.Find(ctx, bson.M{"doc_id": bson.M{"$regex": fmt.Sprintf("^%s:", conversationID)}}, options.Find().SetLimit(1).SetSort(bson.M{"doc_id": -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, errs.ErrRecordNotFound.Wrap("len(msgDocs[0].Msgs) < 0")
2 years ago
}
2 years ago
return nil, ErrMsgNotFound
2 years ago
}
func (m *MsgMongoDriver) GetOldestMsg(ctx context.Context, conversationID string) (*table.MsgInfoModel, error) {
2 years ago
var msgDocs []table.MsgDocModel
2 years ago
cursor, err := m.MsgCollection.Find(ctx, bson.M{"doc_id": bson.M{"$regex": fmt.Sprintf("^%s:", conversationID)}}, options.Find().SetLimit(1).SetSort(bson.M{"doc_id": 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 {
2 years ago
if docIDs == nil {
return nil
}
2 years ago
_, err := m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": bson.M{"$in": docIDs}})
2 years ago
return err
2 years ago
}
2 years ago
func (m *MsgMongoDriver) UpdateOneDoc(ctx context.Context, msg *table.MsgDocModel) error {
2 years ago
_, err := m.MsgCollection.UpdateOne(ctx, bson.M{"doc_id": msg.DocID}, bson.M{"$set": bson.M{"msgs": msg.Msg}})
2 years ago
return err
2 years ago
}
2 years ago
2 years ago
func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc(ctx context.Context, docID string, beginSeq, endSeq int64) (msgs []*sdkws.MsgData, seqs []int64, err error) {
beginIndex := m.msg.GetMsgIndex(beginSeq)
num := endSeq - beginSeq + 1
log.ZDebug(ctx, "beginIndex", beginIndex, "num", num)
2 years ago
pipeline := bson.A{
bson.M{
"$match": bson.M{"doc_id": docID},
},
bson.M{
"$project": bson.M{
"msgs": bson.M{
"$slice": []interface{}{"$msgs", beginIndex, num},
},
},
},
}
cursor, err := m.MsgCollection.Aggregate(ctx, pipeline)
2 years ago
if err != nil {
2 years ago
return nil, nil, errs.Wrap(err)
2 years ago
}
var msgInfos []table.MsgInfoModel
2 years ago
if err := cursor.All(ctx, &msgInfos); err != nil {
2 years ago
return nil, nil, err
2 years ago
}
if len(msgInfos) < 1 {
2 years ago
return nil, nil, errs.ErrRecordNotFound.Wrap("mongo GetMsgBySeqIndex failed, len is 0")
2 years ago
}
2 years ago
log.ZDebug(ctx, "msgInfos", "num", len(msgInfos))
2 years ago
for _, v := range msgInfos {
var msg sdkws.MsgData
if err := proto.Unmarshal(v.Msg, &msg); err != nil {
return nil, nil, err
}
if msg.Seq >= beginSeq && msg.Seq <= endSeq {
2 years ago
log.ZDebug(ctx, "find msg", "msg", &msg)
2 years ago
msgs = append(msgs, &msg)
seqs = append(seqs, msg.Seq)
} else {
log.ZWarn(ctx, "this msg is at wrong position", nil, "msg", &msg)
}
2 years ago
}
2 years ago
return msgs, seqs, nil
2 years ago
}