From 727bf623b311e1afcf0fa1cf1e140b14a5eb15d0 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Tue, 17 Jan 2023 17:05:34 +0800 Subject: [PATCH] Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode # Conflicts: # pkg/common/db/model/group.go --- internal/cron_task/clear_msg.go | 6 ++--- .../msg_transfer/logic/modify_msg_handler.go | 6 ++--- internal/rpc/office/office.go | 22 +++++++++---------- pkg/common/db/cache/group.go | 2 +- pkg/common/db/cache/rockscache.go | 4 ++-- pkg/common/db/model/group.go | 16 +++++++------- pkg/common/db/mongo/office.go | 1 - .../{mongo => mongoDB}/batch_insert_chat.go | 2 +- .../extend_msg_mongo_model.go | 2 +- .../db/{mongo => mongoDB}/init_mongo.go | 8 +++---- .../db/{mongo => mongoDB}/mongo_model.go | 2 +- pkg/common/db/mongoDB/office.go | 1 + 12 files changed, 35 insertions(+), 37 deletions(-) delete mode 100644 pkg/common/db/mongo/office.go rename pkg/common/db/{mongo => mongoDB}/batch_insert_chat.go (99%) rename pkg/common/db/{mongo => mongoDB}/extend_msg_mongo_model.go (99%) rename pkg/common/db/{mongo => mongoDB}/init_mongo.go (97%) rename pkg/common/db/{mongo => mongoDB}/mongo_model.go (99%) create mode 100644 pkg/common/db/mongoDB/office.go diff --git a/internal/cron_task/clear_msg.go b/internal/cron_task/clear_msg.go index 043398422..3ccfb4819 100644 --- a/internal/cron_task/clear_msg.go +++ b/internal/cron_task/clear_msg.go @@ -90,7 +90,7 @@ func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMs msgs, err := db.DB.GetUserMsgListByIndex(ID, index) if err != nil || msgs.UID == "" { if err != nil { - if err == mongo.ErrMsgListNotExist { + if err == mongoDB.ErrMsgListNotExist { log.NewInfo(operationID, utils.GetSelfFuncName(), "ID:", ID, "index:", index, err.Error()) } else { log.NewError(operationID, utils.GetSelfFuncName(), "GetUserMsgListByIndex failed", err.Error(), index, ID) @@ -104,7 +104,7 @@ func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMs return delStruct.getSetMinSeq() + 1, nil } log.NewDebug(operationID, "ID:", ID, "index:", index, "uid:", msgs.UID, "len:", len(msgs.Msg)) - if len(msgs.Msg) > mongo.GetSingleGocMsgNum() { + if len(msgs.Msg) > mongoDB.GetSingleGocMsgNum() { log.NewWarn(operationID, utils.GetSelfFuncName(), "msgs too large", len(msgs.Msg), msgs.UID) } if msgs.Msg[len(msgs.Msg)-1].SendTime+(int64(config.Config.Mongo.DBRetainChatRecords)*24*60*60*1000) > utils.GetCurrentTimestampByMill() && msgListIsFull(msgs) { @@ -150,7 +150,7 @@ func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMs return seq, utils.Wrap(err, "deleteMongoMsg failed") } -func msgListIsFull(chat *mongo.UserChat) bool { +func msgListIsFull(chat *mongoDB.UserChat) bool { index, _ := strconv.Atoi(strings.Split(chat.UID, ":")[1]) if index == 0 { if len(chat.Msg) >= 4999 { diff --git a/internal/msg_transfer/logic/modify_msg_handler.go b/internal/msg_transfer/logic/modify_msg_handler.go index 847bf8761..4cdff43fc 100644 --- a/internal/msg_transfer/logic/modify_msg_handler.go +++ b/internal/msg_transfer/logic/modify_msg_handler.go @@ -71,14 +71,14 @@ func (mmc *ModifyMsgConsumerHandler) ModifyMsg(cMsg *sarama.ConsumerMessage, msg } if !notification.IsReact { // first time to modify - var reactionExtensionList = make(map[string]mongo.KeyValue) - extendMsg := mongo.ExtendMsg{ + var reactionExtensionList = make(map[string]mongoDB.KeyValue) + extendMsg := mongoDB.ExtendMsg{ ReactionExtensionList: reactionExtensionList, ClientMsgID: notification.ClientMsgID, MsgFirstModifyTime: notification.MsgFirstModifyTime, } for _, v := range notification.SuccessReactionExtensionList { - reactionExtensionList[v.TypeKey] = mongo.KeyValue{ + reactionExtensionList[v.TypeKey] = mongoDB.KeyValue{ TypeKey: v.TypeKey, Value: v.Value, LatestUpdateTime: v.LatestUpdateTime, diff --git a/internal/rpc/office/office.go b/internal/rpc/office/office.go index ce3938036..583656e70 100644 --- a/internal/rpc/office/office.go +++ b/internal/rpc/office/office.go @@ -297,7 +297,7 @@ func (s *officeServer) SendMsg2Tag(_ context.Context, req *pbOffice.SendMsg2TagR } } - var tagSendLogs mongo.TagSendLog + var tagSendLogs mongoDB.TagSendLog var wg sync.WaitGroup wg.Add(len(successUserIDList)) var lock sync.Mutex @@ -310,7 +310,7 @@ func (s *officeServer) SendMsg2Tag(_ context.Context, req *pbOffice.SendMsg2TagR return } lock.Lock() - tagSendLogs.UserList = append(tagSendLogs.UserList, mongo.TagUser{ + tagSendLogs.UserList = append(tagSendLogs.UserList, mongoDB.TagUser{ UserID: userID, UserName: userName, }) @@ -389,10 +389,10 @@ func (s *officeServer) GetUserTagByID(_ context.Context, req *pbOffice.GetUserTa func (s *officeServer) CreateOneWorkMoment(_ context.Context, req *pbOffice.CreateOneWorkMomentReq) (resp *pbOffice.CreateOneWorkMomentResp, err error) { log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String()) resp = &pbOffice.CreateOneWorkMomentResp{CommonResp: &pbOffice.CommonResp{}} - workMoment := mongo.WorkMoment{ - Comments: []*mongo.Comment{}, - LikeUserList: []*mongo.WorkMomentUser{}, - PermissionUserList: []*mongo.WorkMomentUser{}, + workMoment := mongoDB.WorkMoment{ + Comments: []*mongoDB.Comment{}, + LikeUserList: []*mongoDB.WorkMomentUser{}, + PermissionUserList: []*mongoDB.WorkMomentUser{}, } createUser, err := imdb.GetUserByUserID(req.WorkMoment.UserID) if err != nil { @@ -406,14 +406,14 @@ func (s *officeServer) CreateOneWorkMoment(_ context.Context, req *pbOffice.Crea workMoment.UserName = createUser.Nickname workMoment.FaceURL = createUser.FaceURL workMoment.PermissionUserIDList = s.getPermissionUserIDList(req.OperationID, req.WorkMoment.PermissionGroupList, req.WorkMoment.PermissionUserList) - workMoment.PermissionUserList = []*mongo.WorkMomentUser{} + workMoment.PermissionUserList = []*mongoDB.WorkMomentUser{} for _, userID := range workMoment.PermissionUserIDList { userName, err := imdb.GetUserNameByUserID(userID) if err != nil { log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUserNameByUserID failed", err.Error()) continue } - workMoment.PermissionUserList = append(workMoment.PermissionUserList, &mongo.WorkMomentUser{ + workMoment.PermissionUserList = append(workMoment.PermissionUserList, &mongoDB.WorkMomentUser{ UserID: userID, UserName: userName, }) @@ -503,7 +503,7 @@ func (s *officeServer) DeleteOneWorkMoment(_ context.Context, req *pbOffice.Dele return resp, nil } -func isUserCanSeeWorkMoment(userID string, workMoment mongo.WorkMoment) bool { +func isUserCanSeeWorkMoment(userID string, workMoment mongoDB.WorkMoment) bool { if userID != workMoment.UserID { switch workMoment.Permission { case constant.WorkMomentPublic: @@ -570,7 +570,7 @@ func (s *officeServer) CommentOneWorkMoment(_ context.Context, req *pbOffice.Com return resp, nil } } - comment := &mongo.Comment{ + comment := &mongoDB.Comment{ UserID: req.UserID, UserName: commentUser.Nickname, ReplyUserID: req.ReplyUserID, @@ -644,7 +644,7 @@ func (s *officeServer) GetUserWorkMoments(_ context.Context, req *pbOffice.GetUs log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String()) resp = &pbOffice.GetUserWorkMomentsResp{CommonResp: &pbOffice.CommonResp{}, WorkMoments: []*pbOffice.WorkMoment{}} resp.Pagination = &pbCommon.ResponsePagination{CurrentPage: req.Pagination.PageNumber, ShowNumber: req.Pagination.ShowNumber} - var workMoments []mongo.WorkMoment + var workMoments []mongoDB.WorkMoment if req.UserID == req.OpUserID { workMoments, err = db.DB.GetUserSelfWorkMoments(req.UserID, req.Pagination.ShowNumber, req.Pagination.PageNumber) } else { diff --git a/pkg/common/db/cache/group.go b/pkg/common/db/cache/group.go index ab91f8aa2..06c3256fc 100644 --- a/pkg/common/db/cache/group.go +++ b/pkg/common/db/cache/group.go @@ -23,7 +23,7 @@ type GroupCache struct { func NewGroupCache(rdb redis.UniversalClient, db *mysql.Group, opts rockscache.Options) *GroupCache { rcClient := &rockscache.Client{ - Options: opts, + Options: rockscache.Options{}, } redisClient := NewRedisClient(rdb) return &GroupCache{rcClient: rcClient, expireTime: GroupExpireTime, db: db, redisClient: redisClient} diff --git a/pkg/common/db/cache/rockscache.go b/pkg/common/db/cache/rockscache.go index c17de7527..52dd29480 100644 --- a/pkg/common/db/cache/rockscache.go +++ b/pkg/common/db/cache/rockscache.go @@ -608,7 +608,7 @@ func DelConversationFromCache(ctx context.Context, ownerUserID, conversationID s return utils.Wrap(db.DB.Rc.TagAsDeleted(conversationCache+ownerUserID+":"+conversationID), "DelConversationFromCache err") } -func GetExtendMsg(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, firstModifyTime int64) (extendMsg *mongo.ExtendMsg, err error) { +func GetExtendMsg(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, firstModifyTime int64) (extendMsg *mongoDB.ExtendMsg, err error) { getExtendMsg := func() (string, error) { extendMsg, err := db.DB.GetExtendMsg(sourceID, sessionType, clientMsgID, firstModifyTime) if err != nil { @@ -628,7 +628,7 @@ func GetExtendMsg(ctx context.Context, sourceID string, sessionType int32, clien if err != nil { return nil, utils.Wrap(err, "Fetch failed") } - extendMsg = &mongo.ExtendMsg{} + extendMsg = &mongoDB.ExtendMsg{} err = json.Unmarshal([]byte(extendMsgStr), extendMsg) return extendMsg, utils.Wrap(err, "Unmarshal failed") } diff --git a/pkg/common/db/model/group.go b/pkg/common/db/model/group.go index e0ac19450..3cdcc7d97 100644 --- a/pkg/common/db/model/group.go +++ b/pkg/common/db/model/group.go @@ -2,13 +2,13 @@ package model import ( "Open_IM/pkg/common/db/cache" - "Open_IM/pkg/common/db/mongo" + "Open_IM/pkg/common/db/mongoDB" "Open_IM/pkg/common/db/mysql" - "Open_IM/pkg/common/trace_log" "Open_IM/pkg/utils" "context" - "encoding/json" "github.com/dtm-labs/rockscache" + "github.com/go-redis/redis/v8" + "go.mongodb.org/mongo-driver/mongo" "gorm.io/gorm" //"time" ) @@ -16,17 +16,17 @@ import ( type GroupModel struct { db *mysql.Group cache *cache.GroupCache - mongo *mongo.Client + mongo *mongoDB.Client } -func NewGroupModel() *GroupModel { +func NewGroupModel(db *mysql.Group, rdb redis.UniversalClient, mdb *mongo.Client) *GroupModel { var groupModel GroupModel - groupModel.db = mysql.NewGroupDB() - groupModel.cache = cache.NewGroupCache(cache.InitRedis(), groupModel.db, rockscache.Options{ + groupModel.db = db + groupModel.cache = cache.NewGroupCache(rdb, db, rockscache.Options{ DisableCacheRead: false, StrongConsistency: true, }) - groupModel.mongo = mongo.NewMongoClient() + groupModel.mongo = mongoDB.NewMongoClient(mdb) return &groupModel } diff --git a/pkg/common/db/mongo/office.go b/pkg/common/db/mongo/office.go deleted file mode 100644 index af2a1bf5b..000000000 --- a/pkg/common/db/mongo/office.go +++ /dev/null @@ -1 +0,0 @@ -package mongo diff --git a/pkg/common/db/mongo/batch_insert_chat.go b/pkg/common/db/mongoDB/batch_insert_chat.go similarity index 99% rename from pkg/common/db/mongo/batch_insert_chat.go rename to pkg/common/db/mongoDB/batch_insert_chat.go index e81e3e800..c2e5ca33e 100644 --- a/pkg/common/db/mongo/batch_insert_chat.go +++ b/pkg/common/db/mongoDB/batch_insert_chat.go @@ -1,4 +1,4 @@ -package mongo +package mongoDB import ( "Open_IM/pkg/common/config" diff --git a/pkg/common/db/mongo/extend_msg_mongo_model.go b/pkg/common/db/mongoDB/extend_msg_mongo_model.go similarity index 99% rename from pkg/common/db/mongo/extend_msg_mongo_model.go rename to pkg/common/db/mongoDB/extend_msg_mongo_model.go index 582c43f96..1ca56bc59 100644 --- a/pkg/common/db/mongo/extend_msg_mongo_model.go +++ b/pkg/common/db/mongoDB/extend_msg_mongo_model.go @@ -1,4 +1,4 @@ -package mongo +package mongoDB import ( "Open_IM/pkg/common/config" diff --git a/pkg/common/db/mongo/init_mongo.go b/pkg/common/db/mongoDB/init_mongo.go similarity index 97% rename from pkg/common/db/mongo/init_mongo.go rename to pkg/common/db/mongoDB/init_mongo.go index 3ffb0adb0..b0775256a 100644 --- a/pkg/common/db/mongo/init_mongo.go +++ b/pkg/common/db/mongoDB/init_mongo.go @@ -1,4 +1,4 @@ -package mongo +package mongoDB import ( "Open_IM/pkg/common/config" @@ -17,10 +17,8 @@ type Client struct { mongo *mongo.Client } -func NewMongoClient() *Client { - var client Client - client.mongo = initMongo() - return &client +func NewMongoClient(mdb *mongo.Client) *Client { + return &Client{mongo: mdb} } func initMongo() *mongo.Client { diff --git a/pkg/common/db/mongo/mongo_model.go b/pkg/common/db/mongoDB/mongo_model.go similarity index 99% rename from pkg/common/db/mongo/mongo_model.go rename to pkg/common/db/mongoDB/mongo_model.go index 1f46b3156..5f2aeddb4 100644 --- a/pkg/common/db/mongo/mongo_model.go +++ b/pkg/common/db/mongoDB/mongo_model.go @@ -1,4 +1,4 @@ -package mongo +package mongoDB import ( "Open_IM/pkg/common/config" diff --git a/pkg/common/db/mongoDB/office.go b/pkg/common/db/mongoDB/office.go new file mode 100644 index 000000000..9b5669132 --- /dev/null +++ b/pkg/common/db/mongoDB/office.go @@ -0,0 +1 @@ +package mongoDB