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/extend_msg.go

153 lines
6.1 KiB

2 years ago
package unrelation
2 years ago
2 years ago
import (
"context"
2 years ago
"errors"
2 years ago
"fmt"
2 years ago
unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"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"
2 years ago
"go.mongodb.org/mongo-driver/mongo"
2 years ago
"go.mongodb.org/mongo-driver/mongo/options"
2 years ago
)
2 years ago
type ExtendMsgSetMongoDriver struct {
mgoDB *mongo.Database
ExtendMsgSetCollection *mongo.Collection
2 years ago
}
2 years ago
func NewExtendMsgSetMongoDriver(mgoDB *mongo.Database) unRelationTb.ExtendMsgSetModelInterface {
2 years ago
return &ExtendMsgSetMongoDriver{mgoDB: mgoDB, ExtendMsgSetCollection: mgoDB.Collection(unRelationTb.CExtendMsgSet)}
2 years ago
}
2 years ago
func (e *ExtendMsgSetMongoDriver) CreateExtendMsgSet(ctx context.Context, set *unRelationTb.ExtendMsgSetModel) error {
2 years ago
_, err := e.ExtendMsgSetCollection.InsertOne(ctx, set)
2 years ago
return err
}
2 years ago
func (e *ExtendMsgSetMongoDriver) GetAllExtendMsgSet(ctx context.Context, ID string, opts *unRelationTb.GetAllExtendMsgSetOpts) (sets []*unRelationTb.ExtendMsgSetModel, err error) {
2 years ago
regex := fmt.Sprintf("^%s", ID)
2 years ago
var findOpts *options.FindOptions
if opts != nil {
if opts.ExcludeExtendMsgs {
findOpts = &options.FindOptions{}
findOpts.SetProjection(bson.M{"extend_msgs": 0})
}
}
2 years ago
cursor, err := e.ExtendMsgSetCollection.Find(ctx, bson.M{"uid": primitive.Regex{Pattern: regex}}, findOpts)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
}
2 years ago
err = cursor.All(ctx, &sets)
2 years ago
if err != nil {
return nil, utils.Wrap(err, fmt.Sprintf("cursor is %s", cursor.Current.String()))
}
return sets, nil
2 years ago
}
2 years ago
func (e *ExtendMsgSetMongoDriver) GetExtendMsgSet(ctx context.Context, sourceID string, sessionType int32, maxMsgUpdateTime int64) (*unRelationTb.ExtendMsgSetModel, error) {
2 years ago
var err error
findOpts := options.Find().SetLimit(1).SetSkip(0).SetSort(bson.M{"source_id": -1}).SetProjection(bson.M{"extend_msgs": 0})
// update newest
2 years ago
find := bson.M{"source_id": primitive.Regex{Pattern: fmt.Sprintf("^%s", sourceID)}, "session_type": sessionType}
2 years ago
if maxMsgUpdateTime > 0 {
find["max_msg_update_time"] = maxMsgUpdateTime
}
2 years ago
result, err := e.ExtendMsgSetCollection.Find(ctx, find, findOpts)
2 years ago
if err != nil {
2 years ago
return nil, utils.Wrap(err, "")
2 years ago
}
2 years ago
var setList []unRelationTb.ExtendMsgSetModel
2 years ago
if err := result.All(ctx, &setList); err != nil {
2 years ago
return nil, utils.Wrap(err, "")
}
if len(setList) == 0 {
return nil, nil
}
return &setList[0], nil
}
// first modify msg
2 years ago
func (e *ExtendMsgSetMongoDriver) InsertExtendMsg(ctx context.Context, sourceID string, sessionType int32, msg *unRelationTb.ExtendMsgModel) error {
2 years ago
set, err := e.GetExtendMsgSet(ctx, sourceID, sessionType, 0)
2 years ago
if err != nil {
2 years ago
return utils.Wrap(err, "")
}
2 years ago
if set == nil || set.ExtendMsgNum >= set.GetExtendMsgMaxNum() {
2 years ago
var index int32
2 years ago
if set != nil {
2 years ago
index = set.SplitSourceIDAndGetIndex()
2 years ago
}
2 years ago
err = e.CreateExtendMsgSet(ctx, &unRelationTb.ExtendMsgSetModel{
2 years ago
SourceID: set.GetSourceID(sourceID, index),
2 years ago
SessionType: sessionType,
2 years ago
ExtendMsgs: map[string]unRelationTb.ExtendMsgModel{msg.ClientMsgID: *msg},
2 years ago
ExtendMsgNum: 1,
CreateTime: msg.MsgFirstModifyTime,
MaxMsgUpdateTime: msg.MsgFirstModifyTime,
2 years ago
})
} else {
2 years ago
_, err = e.ExtendMsgSetCollection.UpdateOne(ctx, bson.M{"source_id": set.SourceID, "session_type": sessionType}, bson.M{"$set": bson.M{"max_msg_update_time": msg.MsgFirstModifyTime, "$inc": bson.M{"extend_msg_num": 1}, fmt.Sprintf("extend_msgs.%s", msg.ClientMsgID): msg}})
2 years ago
}
2 years ago
return utils.Wrap(err, "")
2 years ago
}
2 years ago
// insert or update
2 years ago
func (e *ExtendMsgSetMongoDriver) InsertOrUpdateReactionExtendMsgSet(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, msgFirstModifyTime int64, reactionExtensionList map[string]*unRelationTb.KeyValueModel) error {
2 years ago
var updateBson = bson.M{}
for _, v := range reactionExtensionList {
updateBson[fmt.Sprintf("extend_msgs.%s.%s", clientMsgID, v.TypeKey)] = v
2 years ago
}
upsert := true
opt := &options.UpdateOptions{
Upsert: &upsert,
}
2 years ago
set, err := e.GetExtendMsgSet(ctx, sourceID, sessionType, msgFirstModifyTime)
2 years ago
if err != nil {
return utils.Wrap(err, "")
}
2 years ago
if set == nil {
return errors.New(fmt.Sprintf("sourceID %s has no set", sourceID))
2 years ago
}
2 years ago
_, err = e.ExtendMsgSetCollection.UpdateOne(ctx, bson.M{"source_id": set.SourceID, "session_type": sessionType}, bson.M{"$set": updateBson}, opt)
2 years ago
return utils.Wrap(err, "")
2 years ago
}
2 years ago
// delete TypeKey
2 years ago
func (e *ExtendMsgSetMongoDriver) DeleteReactionExtendMsgSet(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, msgFirstModifyTime int64, reactionExtensionList map[string]*unRelationTb.KeyValueModel) error {
2 years ago
var updateBson = bson.M{}
for _, v := range reactionExtensionList {
updateBson[fmt.Sprintf("extend_msgs.%s.%s", clientMsgID, v.TypeKey)] = ""
}
2 years ago
set, err := e.GetExtendMsgSet(ctx, sourceID, sessionType, msgFirstModifyTime)
2 years ago
if err != nil {
return utils.Wrap(err, "")
}
2 years ago
if set == nil {
return errors.New(fmt.Sprintf("sourceID %s has no set", sourceID))
2 years ago
}
2 years ago
_, err = e.ExtendMsgSetCollection.UpdateOne(ctx, bson.M{"source_id": set.SourceID, "session_type": sessionType}, bson.M{"$unset": updateBson})
2 years ago
return err
}
2 years ago
func (e *ExtendMsgSetMongoDriver) TakeExtendMsg(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, maxMsgUpdateTime int64) (extendMsg *unRelationTb.ExtendMsgModel, err error) {
2 years ago
findOpts := options.Find().SetLimit(1).SetSkip(0).SetSort(bson.M{"source_id": -1}).SetProjection(bson.M{fmt.Sprintf("extend_msgs.%s", clientMsgID): 1})
regex := fmt.Sprintf("^%s", sourceID)
2 years ago
result, err := e.ExtendMsgSetCollection.Find(ctx, bson.M{"source_id": primitive.Regex{Pattern: regex}, "session_type": sessionType, "max_msg_update_time": bson.M{"$lte": maxMsgUpdateTime}}, findOpts)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
}
2 years ago
var setList []unRelationTb.ExtendMsgSetModel
2 years ago
if err := result.All(ctx, &setList); err != nil {
2 years ago
return nil, utils.Wrap(err, "")
}
2 years ago
if len(setList) == 0 {
2 years ago
return nil, utils.Wrap(errors.New("GetExtendMsg failed, len(setList) == 0"), "")
}
2 years ago
if v, ok := setList[0].ExtendMsgs[clientMsgID]; ok {
return &v, nil
}
return nil, errors.New(fmt.Sprintf("cant find client msg id: %s", clientMsgID))
2 years ago
}