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

154 lines
6.2 KiB
Go
Raw Normal View History

2023-01-28 13:19:36 +08:00
package unrelation
2022-12-01 01:40:13 +08:00
2022-12-01 19:34:54 +08:00
import (
"context"
2022-12-09 19:54:49 +08:00
"errors"
2022-12-02 16:40:11 +08:00
"fmt"
2023-05-04 15:06:23 +08:00
2023-03-16 10:46:06 +08:00
unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2023-02-02 16:11:24 +08:00
"go.mongodb.org/mongo-driver/bson"
2022-12-02 16:40:11 +08:00
"go.mongodb.org/mongo-driver/bson/primitive"
2022-12-12 19:22:50 +08:00
"go.mongodb.org/mongo-driver/mongo"
2022-12-07 21:12:48 +08:00
"go.mongodb.org/mongo-driver/mongo/options"
2022-12-01 19:34:54 +08:00
)
2023-02-02 16:11:24 +08:00
type ExtendMsgSetMongoDriver struct {
mgoDB *mongo.Database
ExtendMsgSetCollection *mongo.Collection
2022-12-01 01:40:13 +08:00
}
2023-03-03 17:42:26 +08:00
func NewExtendMsgSetMongoDriver(mgoDB *mongo.Database) unRelationTb.ExtendMsgSetModelInterface {
2023-02-15 15:52:32 +08:00
return &ExtendMsgSetMongoDriver{mgoDB: mgoDB, ExtendMsgSetCollection: mgoDB.Collection(unRelationTb.CExtendMsgSet)}
2022-12-01 19:34:54 +08:00
}
2023-02-15 15:52:32 +08:00
func (e *ExtendMsgSetMongoDriver) CreateExtendMsgSet(ctx context.Context, set *unRelationTb.ExtendMsgSetModel) error {
2023-02-02 16:11:24 +08:00
_, err := e.ExtendMsgSetCollection.InsertOne(ctx, set)
2022-12-01 19:34:54 +08:00
return err
}
2023-02-15 15:52:32 +08:00
func (e *ExtendMsgSetMongoDriver) GetAllExtendMsgSet(ctx context.Context, ID string, opts *unRelationTb.GetAllExtendMsgSetOpts) (sets []*unRelationTb.ExtendMsgSetModel, err error) {
2022-12-02 16:40:11 +08:00
regex := fmt.Sprintf("^%s", ID)
2022-12-07 21:12:48 +08:00
var findOpts *options.FindOptions
if opts != nil {
if opts.ExcludeExtendMsgs {
findOpts = &options.FindOptions{}
findOpts.SetProjection(bson.M{"extend_msgs": 0})
}
}
2023-05-10 20:27:39 +08:00
cursor, err := e.ExtendMsgSetCollection.Find(ctx, bson.M{"doc_id": primitive.Regex{Pattern: regex}}, findOpts)
2022-12-02 16:40:11 +08:00
if err != nil {
return nil, utils.Wrap(err, "")
}
2023-03-09 16:49:46 +08:00
err = cursor.All(ctx, &sets)
2022-12-02 16:40:11 +08:00
if err != nil {
return nil, utils.Wrap(err, fmt.Sprintf("cursor is %s", cursor.Current.String()))
}
return sets, nil
2022-12-01 19:34:54 +08:00
}
2023-05-04 15:06:23 +08:00
func (e *ExtendMsgSetMongoDriver) GetExtendMsgSet(ctx context.Context, conversationID string, sessionType int32, maxMsgUpdateTime int64) (*unRelationTb.ExtendMsgSetModel, error) {
2022-12-09 19:54:49 +08:00
var err error
findOpts := options.Find().SetLimit(1).SetSkip(0).SetSort(bson.M{"source_id": -1}).SetProjection(bson.M{"extend_msgs": 0})
// update newest
2023-05-04 15:06:23 +08:00
find := bson.M{"source_id": primitive.Regex{Pattern: fmt.Sprintf("^%s", conversationID)}, "session_type": sessionType}
2022-12-12 19:22:50 +08:00
if maxMsgUpdateTime > 0 {
find["max_msg_update_time"] = maxMsgUpdateTime
}
2023-02-02 16:11:24 +08:00
result, err := e.ExtendMsgSetCollection.Find(ctx, find, findOpts)
2022-12-09 18:03:40 +08:00
if err != nil {
2022-12-12 19:22:50 +08:00
return nil, utils.Wrap(err, "")
2022-12-09 18:03:40 +08:00
}
2023-02-15 15:52:32 +08:00
var setList []unRelationTb.ExtendMsgSetModel
2022-12-09 19:54:49 +08:00
if err := result.All(ctx, &setList); err != nil {
2022-12-12 19:22:50 +08:00
return nil, utils.Wrap(err, "")
}
if len(setList) == 0 {
return nil, nil
}
return &setList[0], nil
}
// first modify msg
2023-05-04 15:06:23 +08:00
func (e *ExtendMsgSetMongoDriver) InsertExtendMsg(ctx context.Context, conversationID string, sessionType int32, msg *unRelationTb.ExtendMsgModel) error {
set, err := e.GetExtendMsgSet(ctx, conversationID, sessionType, 0)
2022-12-12 19:22:50 +08:00
if err != nil {
2022-12-09 19:54:49 +08:00
return utils.Wrap(err, "")
}
2023-02-02 16:11:24 +08:00
if set == nil || set.ExtendMsgNum >= set.GetExtendMsgMaxNum() {
2022-12-09 19:54:49 +08:00
var index int32
2022-12-12 19:22:50 +08:00
if set != nil {
2023-05-04 15:06:23 +08:00
index = set.SplitConversationIDAndGetIndex()
2022-12-09 19:54:49 +08:00
}
2023-02-15 15:52:32 +08:00
err = e.CreateExtendMsgSet(ctx, &unRelationTb.ExtendMsgSetModel{
2023-05-04 15:06:23 +08:00
ConversationID: set.GetConversationID(conversationID, index),
2022-12-09 18:03:40 +08:00
SessionType: sessionType,
2023-02-15 15:52:32 +08:00
ExtendMsgs: map[string]unRelationTb.ExtendMsgModel{msg.ClientMsgID: *msg},
2022-12-09 18:03:40 +08:00
ExtendMsgNum: 1,
CreateTime: msg.MsgFirstModifyTime,
MaxMsgUpdateTime: msg.MsgFirstModifyTime,
2022-12-09 19:54:49 +08:00
})
} else {
2023-05-04 15:06:23 +08:00
_, err = e.ExtendMsgSetCollection.UpdateOne(ctx, bson.M{"conversation_id": set.ConversationID, "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}})
2022-12-07 21:12:48 +08:00
}
2022-12-09 19:54:49 +08:00
return utils.Wrap(err, "")
2022-12-01 19:34:54 +08:00
}
2022-12-08 12:54:39 +08:00
// insert or update
2023-05-04 15:06:23 +08:00
func (e *ExtendMsgSetMongoDriver) InsertOrUpdateReactionExtendMsgSet(ctx context.Context, conversationID string, sessionType int32, clientMsgID string, msgFirstModifyTime int64, reactionExtensionList map[string]*unRelationTb.KeyValueModel) error {
2022-12-09 19:54:49 +08:00
var updateBson = bson.M{}
for _, v := range reactionExtensionList {
updateBson[fmt.Sprintf("extend_msgs.%s.%s", clientMsgID, v.TypeKey)] = v
2022-12-07 21:12:48 +08:00
}
upsert := true
opt := &options.UpdateOptions{
Upsert: &upsert,
}
2023-05-04 15:06:23 +08:00
set, err := e.GetExtendMsgSet(ctx, conversationID, sessionType, msgFirstModifyTime)
2022-12-09 19:54:49 +08:00
if err != nil {
return utils.Wrap(err, "")
}
2022-12-12 19:22:50 +08:00
if set == nil {
2023-05-04 15:06:23 +08:00
return errors.New(fmt.Sprintf("conversationID %s has no set", conversationID))
2022-12-09 19:54:49 +08:00
}
2023-05-04 15:06:23 +08:00
_, err = e.ExtendMsgSetCollection.UpdateOne(ctx, bson.M{"source_id": set.ConversationID, "session_type": sessionType}, bson.M{"$set": updateBson}, opt)
2022-12-09 19:54:49 +08:00
return utils.Wrap(err, "")
2022-12-01 19:34:54 +08:00
}
2022-12-09 18:03:40 +08:00
// delete TypeKey
2023-05-04 15:06:23 +08:00
func (e *ExtendMsgSetMongoDriver) DeleteReactionExtendMsgSet(ctx context.Context, conversationID string, sessionType int32, clientMsgID string, msgFirstModifyTime int64, reactionExtensionList map[string]*unRelationTb.KeyValueModel) error {
2022-12-09 19:54:49 +08:00
var updateBson = bson.M{}
for _, v := range reactionExtensionList {
updateBson[fmt.Sprintf("extend_msgs.%s.%s", clientMsgID, v.TypeKey)] = ""
}
2023-05-04 15:06:23 +08:00
set, err := e.GetExtendMsgSet(ctx, conversationID, sessionType, msgFirstModifyTime)
2022-12-09 19:54:49 +08:00
if err != nil {
return utils.Wrap(err, "")
}
2022-12-12 19:22:50 +08:00
if set == nil {
2023-05-04 15:06:23 +08:00
return errors.New(fmt.Sprintf("conversationID %s has no set", conversationID))
2022-12-09 19:54:49 +08:00
}
2023-05-04 15:06:23 +08:00
_, err = e.ExtendMsgSetCollection.UpdateOne(ctx, bson.M{"source_id": set.ConversationID, "session_type": sessionType}, bson.M{"$unset": updateBson})
2022-12-05 18:29:58 +08:00
return err
}
2023-05-04 15:06:23 +08:00
func (e *ExtendMsgSetMongoDriver) TakeExtendMsg(ctx context.Context, conversationID string, sessionType int32, clientMsgID string, maxMsgUpdateTime int64) (extendMsg *unRelationTb.ExtendMsgModel, err error) {
2022-12-09 19:54:49 +08:00
findOpts := options.Find().SetLimit(1).SetSkip(0).SetSort(bson.M{"source_id": -1}).SetProjection(bson.M{fmt.Sprintf("extend_msgs.%s", clientMsgID): 1})
2023-05-04 15:06:23 +08:00
regex := fmt.Sprintf("^%s", conversationID)
2023-02-02 16:11:24 +08:00
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)
2022-12-09 19:54:49 +08:00
if err != nil {
return nil, utils.Wrap(err, "")
}
2023-02-15 15:52:32 +08:00
var setList []unRelationTb.ExtendMsgSetModel
2022-12-12 19:22:50 +08:00
if err := result.All(ctx, &setList); err != nil {
2022-12-09 19:54:49 +08:00
return nil, utils.Wrap(err, "")
}
2022-12-12 19:22:50 +08:00
if len(setList) == 0 {
2022-12-09 19:54:49 +08:00
return nil, utils.Wrap(errors.New("GetExtendMsg failed, len(setList) == 0"), "")
}
2022-12-12 19:22:50 +08:00
if v, ok := setList[0].ExtendMsgs[clientMsgID]; ok {
return &v, nil
}
return nil, errors.New(fmt.Sprintf("cant find client msg id: %s", clientMsgID))
2022-12-01 01:40:13 +08:00
}