Files
open-im-server/pkg/common/db/unrelation/msg.go
T

241 lines
7.0 KiB
Go
Raw Normal View History

2023-02-09 20:36:34 +08:00
package unrelation
import (
"context"
"errors"
2023-02-15 15:52:32 +08:00
"fmt"
2023-04-28 18:33:33 +08:00
2023-03-16 10:46:06 +08:00
table "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
2023-05-26 11:11:34 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2023-05-05 12:19:04 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2023-02-15 15:52:32 +08:00
"go.mongodb.org/mongo-driver/bson"
2023-02-09 20:36:34 +08:00
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
2023-04-28 18:33:33 +08:00
"google.golang.org/protobuf/proto"
2023-02-09 20:36:34 +08:00
)
var ErrMsgListNotExist = errors.New("user not have msg in mongoDB")
type MsgMongoDriver struct {
MsgCollection *mongo.Collection
2023-05-26 11:11:34 +08:00
model table.MsgDocModel
2023-02-09 20:36:34 +08:00
}
2023-03-03 17:42:26 +08:00
func NewMsgMongoDriver(database *mongo.Database) table.MsgDocModelInterface {
2023-05-25 14:57:15 +08:00
collection := database.Collection(table.MsgDocModel{}.TableName())
return &MsgMongoDriver{MsgCollection: collection}
2023-02-09 20:36:34 +08:00
}
2023-02-15 15:52:32 +08:00
func (m *MsgMongoDriver) PushMsgsToDoc(ctx context.Context, docID string, msgsToMongo []table.MsgInfoModel) error {
2023-05-10 20:50:46 +08:00
return m.MsgCollection.FindOneAndUpdate(ctx, bson.M{"doc_id": docID}, bson.M{"$push": bson.M{"msgs": bson.M{"$each": msgsToMongo}}}).Err()
2023-02-10 15:46:29 +08:00
}
2023-02-15 15:52:32 +08:00
func (m *MsgMongoDriver) Create(ctx context.Context, model *table.MsgDocModel) error {
_, err := m.MsgCollection.InsertOne(ctx, model)
2023-02-10 15:46:29 +08:00
return err
}
2023-05-25 12:24:21 +08:00
func (m *MsgMongoDriver) UpdateMsg(ctx context.Context, docID string, index int64, key string, value any) (*mongo.UpdateResult, error) {
var field string
if key == "" {
field = fmt.Sprintf("msgs.%d", index)
} else {
field = fmt.Sprintf("msgs.%d.%s", index, key)
}
filter := bson.M{"doc_id": docID}
update := bson.M{"$set": bson.M{field: value}}
res, err := m.MsgCollection.UpdateOne(ctx, filter, update)
2023-05-22 18:36:49 +08:00
if err != nil {
2023-05-25 12:24:21 +08:00
return nil, utils.Wrap(err, "")
2023-05-22 18:36:49 +08:00
}
2023-05-25 12:24:21 +08:00
return res, nil
}
// PushUnique value must slice
func (m *MsgMongoDriver) PushUnique(ctx context.Context, docID string, index int64, key string, value any) (*mongo.UpdateResult, error) {
var field string
if key == "" {
field = fmt.Sprintf("msgs.%d", index)
} else {
field = fmt.Sprintf("msgs.%d.%s", index, key)
}
filter := bson.M{"doc_id": docID}
update := bson.M{
"$addToSet": bson.M{
field: bson.M{"$each": value},
},
}
res, err := m.MsgCollection.UpdateOne(ctx, filter, update)
if err != nil {
return nil, utils.Wrap(err, "")
}
return res, nil
2023-05-22 18:36:49 +08:00
}
2023-05-23 17:11:56 +08:00
func (m *MsgMongoDriver) UpdateMsgContent(ctx context.Context, docID string, index int64, msg []byte) error {
_, err := m.MsgCollection.UpdateOne(ctx, bson.M{"doc_id": docID}, bson.M{"$set": bson.M{fmt.Sprintf("msgs.%d.msg", index): msg}})
if err != nil {
return utils.Wrap(err, "")
}
return nil
}
2023-02-15 15:52:32 +08:00
func (m *MsgMongoDriver) UpdateMsgStatusByIndexInOneDoc(ctx context.Context, docID string, msg *sdkws.MsgData, seqIndex int, status int32) error {
msg.Status = status
2023-02-09 20:36:34 +08:00
bytes, err := proto.Marshal(msg)
if err != nil {
return utils.Wrap(err, "")
}
2023-05-10 20:50:46 +08:00
_, err = m.MsgCollection.UpdateOne(ctx, bson.M{"doc_id": docID}, bson.M{"$set": bson.M{fmt.Sprintf("msgs.%d.msg", seqIndex): bytes}})
2023-02-09 20:36:34 +08:00
if err != nil {
return utils.Wrap(err, "")
}
return nil
}
2023-02-15 15:52:32 +08:00
func (m *MsgMongoDriver) FindOneByDocID(ctx context.Context, docID string) (*table.MsgDocModel, error) {
doc := &table.MsgDocModel{}
2023-05-10 20:27:39 +08:00
err := m.MsgCollection.FindOne(ctx, bson.M{"doc_id": docID}).Decode(doc)
2023-02-15 15:52:32 +08:00
return doc, err
2023-02-09 20:36:34 +08:00
}
2023-05-26 11:11:34 +08:00
func (m *MsgMongoDriver) GetMsgDocModelByIndex(ctx context.Context, conversationID string, index, sort int64) (*table.MsgDocModel, error) {
if sort != 1 && sort != -1 {
return nil, errs.ErrArgs.Wrap("mongo sort must be 1 or -1")
}
findOpts := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"doc_id": sort})
2023-05-10 20:27:39 +08:00
cursor, err := m.MsgCollection.Find(ctx, bson.M{"doc_id": primitive.Regex{Pattern: fmt.Sprintf("^%s:", conversationID)}}, findOpts)
2023-02-09 20:36:34 +08:00
if err != nil {
return nil, utils.Wrap(err, "")
}
2023-02-15 15:52:32 +08:00
var msgs []table.MsgDocModel
2023-05-26 11:11:34 +08:00
err = cursor.All(ctx, &msgs)
2023-02-09 20:36:34 +08:00
if err != nil {
return nil, utils.Wrap(err, fmt.Sprintf("cursor is %s", cursor.Current.String()))
}
if len(msgs) > 0 {
return &msgs[0], nil
}
2023-02-15 15:52:32 +08:00
return nil, ErrMsgListNotExist
2023-02-09 20:36:34 +08:00
}
2023-05-04 15:06:23 +08:00
func (m *MsgMongoDriver) GetNewestMsg(ctx context.Context, conversationID string) (*table.MsgInfoModel, error) {
2023-05-26 11:11:34 +08:00
var skip int64 = 0
for {
msgDocModel, err := m.GetMsgDocModelByIndex(ctx, conversationID, skip, -1)
if err != nil {
return nil, err
}
for i := len(msgDocModel.Msg) - 1; i >= 0; i-- {
if msgDocModel.Msg[i].Msg != nil {
return msgDocModel.Msg[i], nil
}
2023-02-09 20:36:34 +08:00
}
2023-05-26 11:11:34 +08:00
skip++
2023-02-09 20:36:34 +08:00
}
}
2023-05-04 15:06:23 +08:00
func (m *MsgMongoDriver) GetOldestMsg(ctx context.Context, conversationID string) (*table.MsgInfoModel, error) {
2023-05-26 11:11:34 +08:00
var skip int64 = 0
for {
msgDocModel, err := m.GetMsgDocModelByIndex(ctx, conversationID, skip, 1)
if err != nil {
return nil, err
}
for i, v := range msgDocModel.Msg {
if v.Msg != nil {
return msgDocModel.Msg[i], nil
}
}
skip++
}
2023-02-09 20:36:34 +08:00
}
2023-05-26 11:11:48 +08:00
func (m *MsgMongoDriver) DeleteMsgsInOneDocByIndex(ctx context.Context, docID string, indexes []int) error {
updates := bson.M{
"$set": bson.M{},
}
for _, index := range indexes {
updates["$set"].(bson.M)[fmt.Sprintf("msgs.%d", index)] = bson.M{
"msg": nil,
}
}
_, err := m.MsgCollection.UpdateMany(ctx, bson.M{"doc_id": docID}, updates)
if err != nil {
return utils.Wrap(err, "")
}
return nil
}
2023-05-26 11:11:34 +08:00
func (m *MsgMongoDriver) DeleteDocs(ctx context.Context, docIDs []string) error {
2023-03-10 12:20:01 +08:00
if docIDs == nil {
return nil
}
2023-05-10 20:27:39 +08:00
_, err := m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": bson.M{"$in": docIDs}})
2023-02-15 15:52:32 +08:00
return err
2023-02-09 20:36:34 +08:00
}
2023-05-26 18:45:00 +08:00
func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc(ctx context.Context, userID, docID string, seqs []int64) (msgs []*table.MsgInfoModel, err error) {
2023-05-26 11:11:34 +08:00
beginSeq, endSeq := utils.GetSeqsBeginEnd(seqs)
beginIndex := m.model.GetMsgIndex(beginSeq)
num := endSeq - beginSeq + 1
2023-05-26 17:56:34 +08:00
log.ZInfo(ctx, "GetMsgBySeqIndexIn1Doc", "docID", docID, "seqs", seqs, "beginSeq", beginSeq, "endSeq", endSeq, "beginIndex", beginIndex, "num", num)
2023-05-26 11:11:34 +08:00
pipeline := bson.A{
bson.M{
"$match": bson.M{"doc_id": docID},
},
bson.M{
"$project": bson.M{
"msgs": bson.M{
"$slice": bson.A{"$msgs", beginIndex, num},
},
},
},
}
cursor, err := m.MsgCollection.Aggregate(ctx, pipeline)
if err != nil {
return nil, errs.Wrap(err)
}
defer cursor.Close(ctx)
var doc table.MsgDocModel
2023-05-26 18:44:39 +08:00
if cursor.Next(ctx) {
if err := cursor.Decode(&doc); err != nil {
2023-05-26 11:11:34 +08:00
return nil, err
}
}
2023-05-26 18:44:39 +08:00
////i := 0
//for cursor.Next(ctx) {
// err := cursor.Decode(&doc)
// if err != nil {
// return nil, err
// }
// //if i == 0 {
// // break
// //}
//}
2023-05-26 11:11:34 +08:00
log.ZDebug(ctx, "msgInfos", "num", len(doc.Msg), "docID", docID)
for _, v := range doc.Msg {
2023-05-26 18:44:39 +08:00
if v.Msg == nil {
continue
}
2023-05-26 11:11:34 +08:00
if v.Msg.Seq >= beginSeq && v.Msg.Seq <= endSeq {
log.ZDebug(ctx, "find msg", "msg", v.Msg)
msgs = append(msgs, v)
} else {
log.ZWarn(ctx, "this msg is at wrong position", nil, "msg", v.Msg)
}
}
2023-05-15 11:44:51 +08:00
return msgs, nil
2023-05-05 12:19:04 +08:00
}
2023-05-22 18:36:49 +08:00
func (m *MsgMongoDriver) IsExistDocID(ctx context.Context, docID string) (bool, error) {
count, err := m.MsgCollection.CountDocuments(ctx, bson.M{"doc_id": docID})
if err != nil {
return false, errs.Wrap(err)
}
return count > 0, nil
}