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

133 lines
5.2 KiB
Go
Raw Normal View History

2022-12-01 01:40:13 +08:00
package db
2022-12-01 19:34:54 +08:00
import (
"Open_IM/pkg/common/config"
2022-12-02 16:40:11 +08:00
"Open_IM/pkg/utils"
2022-12-01 19:34:54 +08:00
"context"
2022-12-02 16:40:11 +08:00
"fmt"
"go.mongodb.org/mongo-driver/bson/primitive"
2022-12-07 21:12:48 +08:00
"go.mongodb.org/mongo-driver/mongo/options"
2022-12-01 19:34:54 +08:00
"strconv"
"time"
2022-12-02 16:40:11 +08:00
"go.mongodb.org/mongo-driver/bson"
2022-12-01 19:34:54 +08:00
)
2022-12-08 12:54:39 +08:00
const cExtendMsgSet = "extend_msgs"
2022-12-09 18:03:40 +08:00
const MaxNum = 100
2022-12-01 19:34:54 +08:00
2022-12-01 01:40:13 +08:00
type ExtendMsgSet struct {
2022-12-09 18:03:40 +08:00
SourceID string `bson:"source_id" json:"ID"`
SessionType int32 `bson:"session_type" json:"sessionType"`
2022-12-07 21:12:48 +08:00
ExtendMsgs map[string]ExtendMsg `bson:"extend_msgs" json:"extendMsgs"`
ExtendMsgNum int32 `bson:"extend_msg_num" json:"extendMsgNum"`
2022-12-08 19:50:28 +08:00
CreateTime int64 `bson:"create_time" json:"createTime"` // this block's create time
MaxMsgUpdateTime int64 `bson:"max_msg_update_time"` // index find msg
2022-12-01 01:40:13 +08:00
}
2022-12-08 19:50:28 +08:00
type KeyValue struct {
TypeKey string `bson:"type_key" json:"typeKey"`
2022-12-07 21:12:48 +08:00
Value string `bson:"value" json:"value"`
2022-12-08 19:50:28 +08:00
LatestUpdateTime int64 `bson:"latest_update_time" json:"latestUpdateTime"`
2022-12-01 19:34:54 +08:00
}
2022-12-01 01:40:13 +08:00
type ExtendMsg struct {
2022-12-09 18:03:40 +08:00
ReactionExtensionList map[string]KeyValue `bson:"reaction_extension_list" json:"reactionExtensionList"`
2022-12-08 19:50:28 +08:00
ClientMsgID string `bson:"client_msg_id" json:"clientMsgID"`
2022-12-09 18:03:40 +08:00
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"`
2022-12-01 19:34:54 +08:00
}
func GetExtendMsgSetID(ID string, index int32) string {
return ID + ":" + strconv.Itoa(int(index))
}
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
}
2022-12-07 21:12:48 +08:00
type GetAllExtendMsgSetOpts struct {
ExcludeExtendMsgs bool
}
func (d *DataBases) GetAllExtendMsgSet(ID string, opts *GetAllExtendMsgSetOpts) (sets []*ExtendMsgSet, err error) {
2022-12-02 16:40:11 +08:00
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)
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})
}
}
cursor, err := c.Find(ctx, bson.M{"uid": primitive.Regex{Pattern: regex}}, findOpts)
2022-12-02 16:40:11 +08:00
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
2022-12-01 19:34:54 +08:00
}
type GetExtendMsgSetOpts struct {
2022-12-07 21:12:48 +08:00
ExcludeExtendMsgs bool
2022-12-01 19:34:54 +08:00
}
2022-12-09 18:03:40 +08:00
// first modify msg
func (d *DataBases) InsertExtendMsg(sourceID string, sessionType int32, msg *ExtendMsg) error {
2022-12-02 16:40:11 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cExtendMsgSet)
2022-12-09 18:03:40 +08:00
result, err := c.UpdateOne(ctx, bson.M{"source_id": 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}})
if err != nil {
return utils.Wrap(err, "")
}
if result.UpsertedCount == 0 {
if err := d.CreateExtendMsgSet(&ExtendMsgSet{
SourceID: sourceID,
SessionType: sessionType,
ExtendMsgs: map[string]ExtendMsg{msg.ClientMsgID: *msg},
ExtendMsgNum: 1,
CreateTime: msg.MsgFirstModifyTime,
MaxMsgUpdateTime: msg.MsgFirstModifyTime,
}); err != nil {
return err
2022-12-07 21:12:48 +08:00
}
}
2022-12-09 18:03:40 +08:00
return nil
2022-12-01 19:34:54 +08:00
}
2022-12-08 12:54:39 +08:00
// insert or update
2022-12-09 18:03:40 +08:00
func (d *DataBases) InsertOrUpdateReactionExtendMsgSet(sourceID string, sessionType int32, clientMsgID, typeKey, value string) error {
2022-12-02 16:40:11 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cExtendMsgSet)
2022-12-08 19:50:28 +08:00
reactionExtendMsgSet := KeyValue{
2022-12-09 18:03:40 +08:00
TypeKey: typeKey,
2022-12-07 21:12:48 +08:00
Value: value,
2022-12-08 19:50:28 +08:00
LatestUpdateTime: utils.GetCurrentTimestampBySecond(),
2022-12-07 21:12:48 +08:00
}
upsert := true
opt := &options.UpdateOptions{
Upsert: &upsert,
}
2022-12-09 18:03:40 +08:00
_, err := c.UpdateOne(ctx, bson.M{"source_id": sourceID, "session_type": sessionType}, bson.M{"$set": bson.M{fmt.Sprintf("extend_msgs.%s.%s", clientMsgID, typeKey): reactionExtendMsgSet}}, opt)
2022-12-02 16:40:11 +08:00
return err
2022-12-01 19:34:54 +08:00
}
2022-12-09 18:03:40 +08:00
// delete TypeKey
func (d *DataBases) DeleteReactionExtendMsgSet(sourceID string, sessionType int32, clientMsgID []string, userID string) error {
2022-12-05 18:29:58 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cExtendMsgSet)
2022-12-09 18:03:40 +08:00
_, err := c.UpdateOne(ctx, bson.M{"source_id": sourceID, "session_type": sessionType}, bson.M{"$unset": bson.M{fmt.Sprintf("extend_msgs.%s.%s", clientMsgID, userID): ""}})
2022-12-05 18:29:58 +08:00
return err
}
2022-12-09 18:03:40 +08:00
func (d *DataBases) GetExtendMsg(sourceID string, sessionType int32, clientMsgID string, maxMsgUpdateTime int64) error {
2022-12-01 01:40:13 +08:00
}