From df78d87ec51dab100badc86db54bf3df75560a85 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Fri, 2 Dec 2022 16:40:11 +0800 Subject: [PATCH] ex msg --- internal/cron_task/clear_msg.go | 2 +- pkg/common/db/extend_msg_mongo_model.go | 85 ++++++++++++------------- pkg/utils/strings.go | 2 + 3 files changed, 43 insertions(+), 46 deletions(-) diff --git a/internal/cron_task/clear_msg.go b/internal/cron_task/clear_msg.go index c8885a9ef..d51e798de 100644 --- a/internal/cron_task/clear_msg.go +++ b/internal/cron_task/clear_msg.go @@ -54,7 +54,7 @@ func DeleteMongoMsgAndResetRedisSeq(operationID, userID string) error { if minSeq == 0 { return nil } - log.NewDebug(operationID, utils.GetSelfFuncName(), "delMsgIDMap: ", delStruct, "minSeq", minSeq) + log.NewDebug(operationID, utils.GetSelfFuncName(), "delMsgIDStruct: ", delStruct, "minSeq", minSeq) err = db.DB.SetUserMinSeq(userID, minSeq) return utils.Wrap(err, "") } diff --git a/pkg/common/db/extend_msg_mongo_model.go b/pkg/common/db/extend_msg_mongo_model.go index 551009e07..18cf7c348 100644 --- a/pkg/common/db/extend_msg_mongo_model.go +++ b/pkg/common/db/extend_msg_mongo_model.go @@ -2,19 +2,24 @@ package db import ( "Open_IM/pkg/common/config" + "Open_IM/pkg/utils" "context" + "fmt" + "go.mongodb.org/mongo-driver/bson/primitive" "strconv" "time" + + "go.mongodb.org/mongo-driver/bson" ) const cExtendMsgSet = "extend_msg_set" type ExtendMsgSet struct { ID string `bson:"id" json:"ID"` - ExtendMsgs []*ExtendMsg `bson:"extend_msg" json:"extendMsg"` + ExtendMsgs []*ExtendMsg `bson:"extend_msgs" json:"extendMsgs"` LatestUpdateTime int32 `bson:"latest_update_time" json:"latestUpdateTime"` - AttachedInfo string `bson:"attached_info" json:"attachedInfo"` - Ex string `bson:"ex" json:"ex"` + AttachedInfo *string `bson:"attached_info" json:"attachedInfo"` + Ex *string `bson:"ex" json:"ex"` ExtendMsgNum int32 `bson:"extend_msg_num" json:"extendMsgNum"` CreateTime int32 `bson:"create_time" json:"createTime"` } @@ -30,31 +35,6 @@ type ExtendMsg struct { CreateTime int32 `bson:"create_time" json:"createTime"` } -//type Vote struct { -// Content string `bson:"content" json:"content"` -// AttachedInfo string `bson:"attached_info" json:"attachedInfo"` -// Ex string `bson:"ex" json:"ex"` -// Options []*Options `bson:"options" json:"options"` -//} -// -//type Options struct { -// Content string `bson:"content" json:"content"` -// AttachedInfo string `bson:"attached_info" json:"attachedInfo"` -// Ex string `bson:"ex" json:"ex"` -// VoteUserIDList []string `bson:"vote_user_id_list" json:"voteUserIDList"` -//} -// -//type ExtendMsgComment struct { -// UserID string `bson:"user_id" json:"userID"` -// ReplyUserID string `bson:"reply_user_id" json:"replyUserID"` -// ReplyContentID string `bson:"reply_content_id" json:"replyContentID"` -// ContentID string `bson:"content_id" json:"contentID"` -// Content string `bson:"content" json:"content"` -// CreateTime int32 `bson:"create_time" json:"createTime"` -// AttachedInfo string `bson:"attached_info" json:"attachedInfo"` -// Ex string `bson:"ex" json:"ex"` -//} - func GetExtendMsgSetID(ID string, index int32) string { return ID + ":" + strconv.Itoa(int(index)) } @@ -66,10 +46,19 @@ func (d *DataBases) CreateExtendMsgSet(set *ExtendMsgSet) error { return err } -func (d *DataBases) GetAllExtendMsgSet(ID string) ([]*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) - +func (d *DataBases) GetAllExtendMsgSet(ID string) (sets []*ExtendMsgSet, 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) + regex := fmt.Sprintf("^%s", ID) + cursor, err := c.Find(ctx, bson.M{"uid": primitive.Regex{Pattern: regex}}) + 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 } type GetExtendMsgSetOpts struct { @@ -77,24 +66,30 @@ type GetExtendMsgSetOpts struct { } func (d *DataBases) GetExtendMsgSet(ID string, index int32, opts *GetExtendMsgSetOpts) (*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) + ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) + c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cExtendMsgSet) + var set ExtendMsgSet + err := c.FindOne(ctx, bson.M{"uid": GetExtendMsgSetID(ID, index)}).Decode(&set) + return &set, err } -func (d *DataBases) InsertExtendMsg(ID string, 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) - return nil +func (d *DataBases) InsertExtendMsg(ID string, index 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) + _, err := c.UpdateOne(ctx, bson.M{"uid": GetExtendMsgSetID(ID, index)}, bson.M{"$set": bson.M{"create_time": utils.GetCurrentTimestampBySecond(), "$inc": bson.M{"extend_msg_num": 1}, "$push": bson.M{"extend_msgs": msg}}}) + return err } func (d *DataBases) UpdateOneExtendMsgSet(ID string, index, MsgIndex int32, msg *ExtendMsg, msgSet *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) - return nil + 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.UpdateOne(ctx, bson.M{"uid": GetExtendMsgSetID(ID, index)}, bson.M{}) + return err } -func (d *DataBases) GetExtendMsgList(ID string, index, msgStartIndex, msgEndIndex int32) ([]*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) - return nil, nil +func (d *DataBases) GetExtendMsgList(ID string, index, msgStartIndex, msgEndIndex int32) (extendMsgList []*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) + err = c.FindOne(ctx, bson.M{"uid": GetExtendMsgSetID(ID, index)}).Decode(&extendMsgList) + return extendMsgList, err } diff --git a/pkg/utils/strings.go b/pkg/utils/strings.go index 203af2708..f611f1a35 100644 --- a/pkg/utils/strings.go +++ b/pkg/utils/strings.go @@ -96,6 +96,8 @@ func GetConversationIDBySessionType(sourceID string, sessionType int) string { return "single_" + sourceID case constant.GroupChatType: return "group_" + sourceID + case constant.SuperGroupChatType: + return "super_group_" + sourceID case constant.NotificationChatType: return "notification_" + sourceID }