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

206 lines
8.1 KiB

2 years ago
package db
2 years ago
import (
"Open_IM/pkg/common/config"
2 years ago
server_api_params "Open_IM/pkg/proto/sdk_ws"
2 years ago
"Open_IM/pkg/utils"
2 years ago
"context"
2 years ago
"errors"
2 years ago
"fmt"
"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
"strconv"
2 years ago
"strings"
2 years ago
"time"
2 years ago
"go.mongodb.org/mongo-driver/bson"
2 years ago
)
2 years ago
const cExtendMsgSet = "extend_msgs"
2 years ago
const MaxNum = 100
2 years ago
2 years ago
type ExtendMsgSet struct {
2 years ago
SourceID string `bson:"source_id" json:"sourceID"`
2 years ago
SessionType int32 `bson:"session_type" json:"sessionType"`
2 years ago
ExtendMsgs map[string]ExtendMsg `bson:"extend_msgs" json:"extendMsgs"`
ExtendMsgNum int32 `bson:"extend_msg_num" json:"extendMsgNum"`
2 years ago
CreateTime int64 `bson:"create_time" json:"createTime"` // this block's create time
MaxMsgUpdateTime int64 `bson:"max_msg_update_time" json:"maxMsgUpdateTime"` // index find msg
2 years ago
}
2 years ago
type KeyValue struct {
TypeKey string `bson:"type_key" json:"typeKey"`
2 years ago
Value string `bson:"value" json:"value"`
2 years ago
LatestUpdateTime int64 `bson:"latest_update_time" json:"latestUpdateTime"`
2 years ago
}
2 years ago
type ExtendMsg struct {
2 years ago
ReactionExtensionList map[string]KeyValue `bson:"reaction_extension_list" json:"reactionExtensionList"`
2 years ago
ClientMsgID string `bson:"client_msg_id" json:"clientMsgID"`
2 years ago
MsgFirstModifyTime int64 `bson:"msg_first_modify_time" json:"msgFirstModifyTime"` // this extendMsg create time
AttachedInfo string `bson:"attached_info" json:"attachedInfo"`
Ex string `bson:"ex" json:"ex"`
2 years ago
}
2 years ago
func GetExtendMsgMaxNum() int32 {
return MaxNum
}
func GetExtendMsgSourceID(ID string, index int32) string {
2 years ago
return ID + ":" + strconv.Itoa(int(index))
}
2 years ago
func SplitSourceIDAndGetIndex(sourceID string) int32 {
l := strings.Split(sourceID, ":")
index, _ := strconv.Atoi(l[len(l)-1])
return int32(index)
}
2 years ago
func (d *DataBases) CreateExtendMsgSet(set *ExtendMsgSet) error {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cExtendMsgSet)
_, err := c.InsertOne(ctx, set)
return err
}
2 years ago
type GetAllExtendMsgSetOpts struct {
ExcludeExtendMsgs bool
}
func (d *DataBases) GetAllExtendMsgSet(ID string, opts *GetAllExtendMsgSetOpts) (sets []*ExtendMsgSet, err error) {
2 years ago
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cExtendMsgSet)
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})
}
}
cursor, err := c.Find(ctx, bson.M{"uid": primitive.Regex{Pattern: regex}}, findOpts)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
}
err = cursor.All(context.Background(), &sets)
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 (d *DataBases) GetExtendMsgSet(ctx context.Context, sourceID string, sessionType int32, maxMsgUpdateTime int64, c *mongo.Collection) (*ExtendMsgSet, error) {
2 years ago
regex := fmt.Sprintf("^%s", sourceID)
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: regex}, "session_type": sessionType}
if maxMsgUpdateTime > 0 {
find["max_msg_update_time"] = maxMsgUpdateTime
}
result, err := c.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 []ExtendMsgSet
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
func (d *DataBases) InsertExtendMsg(sourceID string, sessionType int32, msg *ExtendMsg) error {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cExtendMsgSet)
set, err := d.GetExtendMsgSet(ctx, sourceID, sessionType, 0, c)
if err != nil {
2 years ago
return utils.Wrap(err, "")
}
2 years ago
if set == nil || set.ExtendMsgNum >= GetExtendMsgMaxNum() {
2 years ago
var index int32
2 years ago
if set != nil {
index = SplitSourceIDAndGetIndex(set.SourceID)
2 years ago
}
err = d.CreateExtendMsgSet(&ExtendMsgSet{
SourceID: GetExtendMsgSourceID(sourceID, index),
2 years ago
SessionType: sessionType,
ExtendMsgs: map[string]ExtendMsg{msg.ClientMsgID: *msg},
ExtendMsgNum: 1,
CreateTime: msg.MsgFirstModifyTime,
MaxMsgUpdateTime: msg.MsgFirstModifyTime,
2 years ago
})
} else {
2 years ago
_, err = c.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 (d *DataBases) InsertOrUpdateReactionExtendMsgSet(sourceID string, sessionType int32, clientMsgID string, msgFirstModifyTime int64, reactionExtensionList map[string]*server_api_params.KeyValue) error {
2 years ago
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cExtendMsgSet)
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 := d.GetExtendMsgSet(ctx, sourceID, sessionType, msgFirstModifyTime, c)
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 = c.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 (d *DataBases) DeleteReactionExtendMsgSet(sourceID string, sessionType int32, clientMsgID string, msgFirstModifyTime int64, reactionExtensionList map[string]*server_api_params.KeyValue) error {
2 years ago
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cExtendMsgSet)
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 := d.GetExtendMsgSet(ctx, sourceID, sessionType, msgFirstModifyTime, c)
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 = c.UpdateOne(ctx, bson.M{"source_id": set.SourceID, "session_type": sessionType}, bson.M{"$unset": updateBson})
2 years ago
return err
}
2 years ago
func (d *DataBases) GetExtendMsg(sourceID string, sessionType int32, clientMsgID string, maxMsgUpdateTime int64) (extendMsg *ExtendMsg, err error) {
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cExtendMsgSet)
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 := c.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 []ExtendMsgSet
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
}