From d9becad8e97c158dcc17150faa8aacd5972abe75 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Sat, 28 Jan 2023 13:19:36 +0800 Subject: [PATCH] errCode --- internal/rpc/friend/friend.go | 30 +-- internal/rpc/group/group.go | 2 +- pkg/common/db/cache/group.go | 12 +- pkg/common/db/cache/rockscache.go | 52 ++--- pkg/common/db/{model => controller}/black.go | 16 +- pkg/common/db/{model => controller}/friend.go | 17 +- .../{model => controller}/friend_request.go | 16 +- pkg/common/db/controller/group.go | 127 ++++++++++++ pkg/common/db/{model => controller}/user.go | 10 +- pkg/common/db/model/group.go | 99 ---------- pkg/common/db/mongoDB/office.go | 1 - pkg/common/db/mongoDB/super_group.go | 181 ------------------ pkg/common/db/{mysql => relation}/black.go | 2 +- .../db/{mysql => relation}/chat_log_model.go | 2 +- .../{mysql => relation}/conversation_model.go | 2 +- .../db/{mysql => relation}/demo_model.go | 2 +- .../db/{mysql => relation}/file_model.go | 2 +- .../db/{mysql => relation}/friend_model.go | 2 +- .../db/{mysql => relation}/friend_model_k.go | 2 +- .../friend_request_model.go | 2 +- .../{mysql => relation}/group_member_model.go | 2 +- .../group_member_model_k.go | 2 +- .../db/{mysql => relation}/group_model.go | 2 +- .../db/{mysql => relation}/group_model_k.go | 37 ++-- .../group_request_model.go | 2 +- .../group_request_model_k.go | 2 +- .../db/{mysql => relation}/init_mysql.go | 2 +- .../db/{mysql => relation}/model_struct.go | 2 +- .../{mysql => relation}/statistics_model.go | 2 +- .../db/{mysql => relation}/user_model.go | 2 +- .../db/{mysql => relation}/user_model_k.go | 2 +- .../batch_insert_chat.go | 2 +- .../extend_msg_mongo_model.go | 2 +- .../db/{mongoDB => unrelation}/init_mongo.go | 7 +- .../db/{mongoDB => unrelation}/mongo_model.go | 23 ++- pkg/common/db/unrelation/office.go | 1 + pkg/common/db/unrelation/super_group.go | 164 ++++++++++++++++ 37 files changed, 417 insertions(+), 418 deletions(-) rename pkg/common/db/{model => controller}/black.go (65%) rename pkg/common/db/{model => controller}/friend.go (75%) rename pkg/common/db/{model => controller}/friend_request.go (73%) create mode 100644 pkg/common/db/controller/group.go rename pkg/common/db/{model => controller}/user.go (61%) delete mode 100644 pkg/common/db/model/group.go delete mode 100644 pkg/common/db/mongoDB/office.go delete mode 100644 pkg/common/db/mongoDB/super_group.go rename pkg/common/db/{mysql => relation}/black.go (99%) rename pkg/common/db/{mysql => relation}/chat_log_model.go (99%) rename pkg/common/db/{mysql => relation}/conversation_model.go (99%) rename pkg/common/db/{mysql => relation}/demo_model.go (98%) rename pkg/common/db/{mysql => relation}/file_model.go (98%) rename pkg/common/db/{mysql => relation}/friend_model.go (99%) rename pkg/common/db/{mysql => relation}/friend_model_k.go (99%) rename pkg/common/db/{mysql => relation}/friend_request_model.go (99%) rename pkg/common/db/{mysql => relation}/group_member_model.go (99%) rename pkg/common/db/{mysql => relation}/group_member_model_k.go (99%) rename pkg/common/db/{mysql => relation}/group_model.go (99%) rename pkg/common/db/{mysql => relation}/group_model_k.go (65%) rename pkg/common/db/{mysql => relation}/group_request_model.go (99%) rename pkg/common/db/{mysql => relation}/group_request_model_k.go (99%) rename pkg/common/db/{mysql => relation}/init_mysql.go (99%) rename pkg/common/db/{mysql => relation}/model_struct.go (99%) rename pkg/common/db/{mysql => relation}/statistics_model.go (99%) rename pkg/common/db/{mysql => relation}/user_model.go (99%) rename pkg/common/db/{mysql => relation}/user_model_k.go (99%) rename pkg/common/db/{mongoDB => unrelation}/batch_insert_chat.go (99%) rename pkg/common/db/{mongoDB => unrelation}/extend_msg_mongo_model.go (99%) rename pkg/common/db/{mongoDB => unrelation}/init_mongo.go (97%) rename pkg/common/db/{mongoDB => unrelation}/mongo_model.go (99%) create mode 100644 pkg/common/db/unrelation/office.go create mode 100644 pkg/common/db/unrelation/super_group.go diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 4d8244719..3eb7b6e65 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -34,9 +34,9 @@ type friendServer struct { rpcRegisterName string etcdSchema string etcdAddr []string - friendModel *model.FriendModel - friendRequestModel *model.FriendRequestModel - blackModel *model.BlackModel + friendModel *controller.FriendModel + friendRequestModel *controller.FriendRequestModel + blackModel *controller.BlackModel } func NewFriendServer(port int) *friendServer { @@ -50,7 +50,7 @@ func NewFriendServer(port int) *friendServer { } func (s *friendServer) Run() { - db := mysql.ConnectToDB() + db := relation.ConnectToDB() //s.friendModel = mysql.NewFriend(db) //s.friendRequestModel = mysql.NewFriendRequest(db) //s.blackModel = mysql.NewBlack(db) @@ -114,8 +114,8 @@ func (s *friendServer) AddBlacklist(ctx context.Context, req *pbFriend.AddBlackl if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - black := mysql.Black{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)} - if err := s.blackModel.Create(ctx, []*mysql.Black{&black}); err != nil { + black := relation.Black{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)} + if err := s.blackModel.Create(ctx, []*relation.Black{&black}); err != nil { return nil, err } chat.BlackAddedNotification(req) @@ -155,14 +155,14 @@ func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq if _, err := GetUserInfo(ctx, req.ToUserID); err != nil { return nil, err } - friendRequest := mysql.FriendRequest{ + friendRequest := relation.FriendRequest{ FromUserID: req.FromUserID, ToUserID: req.ToUserID, HandleResult: 0, ReqMsg: req.ReqMsg, CreateTime: time.Now(), } - if err := s.friendRequestModel.Create(ctx, []*mysql.FriendRequest{&friendRequest}); err != nil { + if err := s.friendRequestModel.Create(ctx, []*relation.FriendRequest{&friendRequest}); err != nil { return nil, err } chat.FriendApplicationNotification(req) @@ -179,7 +179,7 @@ func (s *friendServer) ImportFriend(ctx context.Context, req *pbFriend.ImportFri return nil, err } - var friends []*mysql.Friend + var friends []*relation.Friend for _, userID := range utils.RemoveDuplicateElement(req.FriendUserIDList) { if _, err := GetUserInfo(ctx, userID); err != nil { return nil, err @@ -191,12 +191,12 @@ func (s *friendServer) ImportFriend(ctx context.Context, req *pbFriend.ImportFri switch len(fs) { case 1: if fs[0].OwnerUserID == req.FromUserID { - friends = append(friends, &mysql.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID}) + friends = append(friends, &relation.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID}) } else { - friends = append(friends, &mysql.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID}) + friends = append(friends, &relation.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID}) } case 0: - friends = append(friends, &mysql.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID}, &mysql.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID}) + friends = append(friends, &relation.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID}, &relation.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID}) default: continue } @@ -223,7 +223,7 @@ func (s *friendServer) AddFriendResponse(ctx context.Context, req *pbFriend.AddF friendRequest.HandleTime = time.Now() friendRequest.HandleMsg = req.HandleMsg friendRequest.HandlerUserID = tools.OpUserID(ctx) - err = mysql.UpdateFriendApplication(friendRequest) + err = relation.UpdateFriendApplication(friendRequest) if err != nil { return nil, err } @@ -233,7 +233,7 @@ func (s *friendServer) AddFriendResponse(ctx context.Context, req *pbFriend.AddF //Establish friendship after find friend relationship not exists _, err := s.friendModel.Take(ctx, req.FromUserID, req.ToUserID) if errors.Is(err, gorm.ErrRecordNotFound) { - if err := s.friendModel.Create(ctx, []*mysql.Friend{{OwnerUserID: req.FromUserID, FriendUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)}}); err != nil { + if err := s.friendModel.Create(ctx, []*relation.Friend{{OwnerUserID: req.FromUserID, FriendUserID: req.ToUserID, OperatorUserID: tools.OpUserID(ctx)}}); err != nil { return nil, err } chat.FriendAddedNotification(tools.OperationID(ctx), tools.OpUserID(ctx), req.FromUserID, req.ToUserID) @@ -302,7 +302,7 @@ func (s *friendServer) RemoveBlacklist(ctx context.Context, req *pbFriend.Remove if err := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } - if err := s.blackModel.Delete(ctx, []*mysql.Black{{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID}}); err != nil { + if err := s.blackModel.Delete(ctx, []*relation.Black{{OwnerUserID: req.FromUserID, BlockUserID: req.ToUserID}}); err != nil { return nil, err } chat.BlackDeletedNotification(req) diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index 2bdb3ea56..165542cf6 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -176,7 +176,7 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR return nil, err } } - if err := model. + if err := controller. if err := (*imdb.GroupMember)(nil).Create(ctx, groupMembers); err != nil { return nil, err } diff --git a/pkg/common/db/cache/group.go b/pkg/common/db/cache/group.go index 425d7255a..b8daa8c9f 100644 --- a/pkg/common/db/cache/group.go +++ b/pkg/common/db/cache/group.go @@ -1,7 +1,7 @@ package cache import ( - "Open_IM/pkg/common/db/mysql" + "Open_IM/pkg/common/db/relation" "Open_IM/pkg/common/trace_log" "Open_IM/pkg/utils" "context" @@ -15,13 +15,13 @@ const GroupExpireTime = time.Second * 60 * 60 * 12 const groupInfoCacheKey = "GROUP_INFO_CACHE:" type GroupCache struct { - db mysql.GroupModelInterface + db *relation.Group expireTime time.Duration redisClient *RedisClient rcClient *rockscache.Client } -func NewGroupCache(rdb redis.UniversalClient, db mysql.GroupModelInterface, opts rockscache.Options) *GroupCache { +func NewGroupCache(rdb redis.UniversalClient, db *relation.Group, opts rockscache.Options) *GroupCache { return &GroupCache{rcClient: rockscache.NewClient(rdb, opts), expireTime: GroupExpireTime, db: db, redisClient: NewRedisClient(rdb)} } @@ -29,7 +29,7 @@ func (g *GroupCache) getRedisClient() *RedisClient { return g.redisClient } -func (g *GroupCache) GetGroupsInfoFromCache(ctx context.Context, groupIDs []string) (groups []*mysql.Group, err error) { +func (g *GroupCache) GetGroupsInfoFromCache(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error) { for _, groupID := range groupIDs { group, err := g.GetGroupInfoFromCache(ctx, groupID) if err != nil { @@ -40,7 +40,7 @@ func (g *GroupCache) GetGroupsInfoFromCache(ctx context.Context, groupIDs []stri return groups, nil } -func (g *GroupCache) GetGroupInfoFromCache(ctx context.Context, groupID string) (group *mysql.Group, err error) { +func (g *GroupCache) GetGroupInfoFromCache(ctx context.Context, groupID string) (group *relation.Group, err error) { getGroup := func() (string, error) { groupInfo, err := g.db.Take(ctx, groupID) if err != nil { @@ -52,7 +52,7 @@ func (g *GroupCache) GetGroupInfoFromCache(ctx context.Context, groupID string) } return string(bytes), nil } - group = &mysql.Group{} + group = &relation.Group{} defer func() { trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", *group) }() diff --git a/pkg/common/db/cache/rockscache.go b/pkg/common/db/cache/rockscache.go index 52dd29480..09d4c94f7 100644 --- a/pkg/common/db/cache/rockscache.go +++ b/pkg/common/db/cache/rockscache.go @@ -74,7 +74,7 @@ func (rc *RcClient) DelKeys() { func (rc *Client) GetFriendIDListFromCache(ctx context.Context, userID string) (friendIDList []string, err error) { getFriendIDList := func() (string, error) { - friendIDList, err := mysql.GetFriendIDListByUserID(userID) + friendIDList, err := relation.GetFriendIDListByUserID(userID) if err != nil { return "", utils.Wrap(err, "") } @@ -104,7 +104,7 @@ func DelFriendIDListFromCache(ctx context.Context, userID string) (err error) { func GetBlackListFromCache(ctx context.Context, userID string) (blackIDs []string, err error) { getBlackIDList := func() (string, error) { - blackIDs, err := mysql.GetBlackIDListByUserID(userID) + blackIDs, err := relation.GetBlackIDListByUserID(userID) if err != nil { return "", utils.Wrap(err, "") } @@ -134,7 +134,7 @@ func DelBlackIDListFromCache(ctx context.Context, userID string) (err error) { func GetJoinedGroupIDListFromCache(ctx context.Context, userID string) (joinedGroupList []string, err error) { getJoinedGroupIDList := func() (string, error) { - joinedGroupList, err := mysql.GetJoinedGroupIDListByUserID(userID) + joinedGroupList, err := relation.GetJoinedGroupIDListByUserID(userID) if err != nil { return "", utils.Wrap(err, "") } @@ -176,7 +176,7 @@ func GetGroupMemberIDListFromCache(ctx context.Context, groupID string) (groupMe } groupMemberIDList = superGroup.MemberIDList } else { - groupMemberIDList, err = mysql.GetGroupMemberIDListByGroupID(groupID) + groupMemberIDList, err = relation.GetGroupMemberIDListByGroupID(groupID) if err != nil { return "", utils.Wrap(err, "") } @@ -205,9 +205,9 @@ func DelGroupMemberIDListFromCache(ctx context.Context, groupID string) (err err return db.DB.Rc.TagAsDeleted(groupCache + groupID) } -func GetUserInfoFromCache(ctx context.Context, userID string) (userInfo *mysql.User, err error) { +func GetUserInfoFromCache(ctx context.Context, userID string) (userInfo *relation.User, err error) { getUserInfo := func() (string, error) { - userInfo, err := mysql.GetUserByUserID(userID) + userInfo, err := relation.GetUserByUserID(userID) if err != nil { return "", utils.Wrap(err, "") } @@ -224,13 +224,13 @@ func GetUserInfoFromCache(ctx context.Context, userID string) (userInfo *mysql.U if err != nil { return nil, utils.Wrap(err, "") } - userInfo = &mysql.User{} + userInfo = &relation.User{} err = json.Unmarshal([]byte(userInfoStr), userInfo) return userInfo, utils.Wrap(err, "") } -func GetUserInfoFromCacheBatch(ctx context.Context, userIDs []string) ([]*mysql.User, error) { - var users []*mysql.User +func GetUserInfoFromCacheBatch(ctx context.Context, userIDs []string) ([]*relation.User, error) { + var users []*relation.User for _, userID := range userIDs { user, err := GetUserInfoFromCache(ctx, userID) if err != nil { @@ -248,9 +248,9 @@ func DelUserInfoFromCache(ctx context.Context, userID string) (err error) { return db.DB.Rc.TagAsDeleted(userInfoCache + userID) } -func GetGroupMemberInfoFromCache(ctx context.Context, groupID, userID string) (groupMember *mysql.GroupMember, err error) { +func GetGroupMemberInfoFromCache(ctx context.Context, groupID, userID string) (groupMember *relation.GroupMember, err error) { getGroupMemberInfo := func() (string, error) { - groupMemberInfo, err := mysql.GetGroupMemberInfoByGroupIDAndUserID(groupID, userID) + groupMemberInfo, err := relation.GetGroupMemberInfoByGroupIDAndUserID(groupID, userID) if err != nil { return "", utils.Wrap(err, "") } @@ -267,7 +267,7 @@ func GetGroupMemberInfoFromCache(ctx context.Context, groupID, userID string) (g if err != nil { return nil, utils.Wrap(err, "") } - groupMember = &mysql.GroupMember{} + groupMember = &relation.GroupMember{} err = json.Unmarshal([]byte(groupMemberInfoStr), groupMember) return groupMember, utils.Wrap(err, "") } @@ -279,7 +279,7 @@ func DelGroupMemberInfoFromCache(ctx context.Context, groupID, userID string) (e return db.DB.Rc.TagAsDeleted(groupMemberInfoCache + groupID + "-" + userID) } -func GetGroupMembersInfoFromCache(ctx context.Context, count, offset int32, groupID string) (groupMembers []*mysql.GroupMember, err error) { +func GetGroupMembersInfoFromCache(ctx context.Context, count, offset int32, groupID string) (groupMembers []*relation.GroupMember, err error) { defer func() { trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "count", count, "offset", offset, "groupID", groupID, "groupMember", groupMembers) }() @@ -290,7 +290,7 @@ func GetGroupMembersInfoFromCache(ctx context.Context, count, offset int32, grou if count < 0 || offset < 0 { return nil, nil } - var groupMemberList []*mysql.GroupMember + var groupMemberList []*relation.GroupMember var start, stop int32 start = offset stop = offset + count @@ -326,12 +326,12 @@ func GetGroupMembersInfoFromCache(ctx context.Context, count, offset int32, grou return groupMemberList, nil } -func GetAllGroupMembersInfoFromCache(ctx context.Context, groupID string) (groupMembers []*mysql.GroupMember, err error) { +func GetAllGroupMembersInfoFromCache(ctx context.Context, groupID string) (groupMembers []*relation.GroupMember, err error) { defer func() { trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "groupMembers", groupMembers) }() getGroupMemberInfo := func() (string, error) { - groupMembers, err := mysql.GetGroupMemberListByGroupID(groupID) + groupMembers, err := relation.GetGroupMemberListByGroupID(groupID) if err != nil { return "", utils.Wrap(err, "") } @@ -387,9 +387,9 @@ func DelAllGroupMembersInfoFromCache(ctx context.Context, groupID string) (err e // return db.DB.Rc.TagAsDeleted(groupInfoCache + groupID) //} -func GetAllFriendsInfoFromCache(ctx context.Context, userID string) (friends []*mysql.Friend, err error) { +func GetAllFriendsInfoFromCache(ctx context.Context, userID string) (friends []*relation.Friend, err error) { getAllFriendInfo := func() (string, error) { - friendInfoList, err := mysql.GetFriendListByUserID(userID) + friendInfoList, err := relation.GetFriendListByUserID(userID) if err != nil { return "", utils.Wrap(err, "") } @@ -489,7 +489,7 @@ func DelGroupMemberListHashFromCache(ctx context.Context, groupID string) (err e func GetGroupMemberNumFromCache(ctx context.Context, groupID string) (num int, err error) { getGroupMemberNum := func() (string, error) { - num, err := mysql.GetGroupMemberNumByGroupID(groupID) + num, err := relation.GetGroupMemberNumByGroupID(groupID) if err != nil { return "", utils.Wrap(err, "") } @@ -514,7 +514,7 @@ func DelGroupMemberNumFromCache(ctx context.Context, groupID string) (err error) func GetUserConversationIDListFromCache(ctx context.Context, userID string) (conversationIDs []string, err error) { getConversationIDList := func() (string, error) { - conversationIDList, err := mysql.GetConversationIDListByUserID(userID) + conversationIDList, err := relation.GetConversationIDListByUserID(userID) if err != nil { return "", utils.Wrap(err, "getConversationIDList failed") } @@ -543,9 +543,9 @@ func DelUserConversationIDListFromCache(ctx context.Context, userID string) (err return utils.Wrap(db.DB.Rc.TagAsDeleted(conversationIDListCache+userID), "DelUserConversationIDListFromCache err") } -func GetConversationFromCache(ctx context.Context, ownerUserID, conversationID string) (conversation *mysql.Conversation, err error) { +func GetConversationFromCache(ctx context.Context, ownerUserID, conversationID string) (conversation *relation.Conversation, err error) { getConversation := func() (string, error) { - conversation, err := mysql.GetConversation(ownerUserID, conversationID) + conversation, err := relation.GetConversation(ownerUserID, conversationID) if err != nil { return "", utils.Wrap(err, "get failed") } @@ -562,12 +562,12 @@ func GetConversationFromCache(ctx context.Context, ownerUserID, conversationID s if err != nil { return nil, utils.Wrap(err, "Fetch failed") } - conversation = &mysql.Conversation{} + conversation = &relation.Conversation{} err = json.Unmarshal([]byte(conversationStr), &conversation) return conversation, utils.Wrap(err, "Unmarshal failed") } -func GetConversationsFromCache(ctx context.Context, ownerUserID string, conversationIDs []string) (conversations []mysql.Conversation, err error) { +func GetConversationsFromCache(ctx context.Context, ownerUserID string, conversationIDs []string) (conversations []relation.Conversation, err error) { defer func() { trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "conversationIDs", conversationIDs, "conversations", conversations) }() @@ -581,7 +581,7 @@ func GetConversationsFromCache(ctx context.Context, ownerUserID string, conversa return conversations, nil } -func GetUserAllConversationList(ctx context.Context, ownerUserID string) (conversations []mysql.Conversation, err error) { +func GetUserAllConversationList(ctx context.Context, ownerUserID string) (conversations []relation.Conversation, err error) { defer func() { trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "conversations", conversations) }() @@ -589,7 +589,7 @@ func GetUserAllConversationList(ctx context.Context, ownerUserID string) (conver if err != nil { return nil, err } - var conversationList []mysql.Conversation + var conversationList []relation.Conversation log.NewDebug("", utils.GetSelfFuncName(), IDList) for _, conversationID := range IDList { conversation, err := GetConversationFromCache(ctx, ownerUserID, conversationID) diff --git a/pkg/common/db/model/black.go b/pkg/common/db/controller/black.go similarity index 65% rename from pkg/common/db/model/black.go rename to pkg/common/db/controller/black.go index 29ef1753e..3ec2f154f 100644 --- a/pkg/common/db/model/black.go +++ b/pkg/common/db/controller/black.go @@ -1,4 +1,4 @@ -package model +package controller import ( "Open_IM/pkg/common/db/cache" @@ -9,15 +9,15 @@ import ( ) type BlackModel struct { - db *mysql.Black + db *relation.Black cache *cache.GroupCache } -func (b *BlackModel) Create(ctx context.Context, blacks []*mysql.Black) (err error) { +func (b *BlackModel) Create(ctx context.Context, blacks []*relation.Black) (err error) { return b.db.Create(ctx, blacks) } -func (b *BlackModel) Delete(ctx context.Context, blacks []*mysql.Black) (err error) { +func (b *BlackModel) Delete(ctx context.Context, blacks []*relation.Black) (err error) { return b.db.Delete(ctx, blacks) } @@ -25,19 +25,19 @@ func (b *BlackModel) UpdateByMap(ctx context.Context, ownerUserID, blockUserID s return b.db.UpdateByMap(ctx, ownerUserID, blockUserID, args) } -func (b *BlackModel) Update(ctx context.Context, blacks []*mysql.Black) (err error) { +func (b *BlackModel) Update(ctx context.Context, blacks []*relation.Black) (err error) { return b.db.Update(ctx, blacks) } -func (b *BlackModel) Find(ctx context.Context, blacks []*mysql.Black) (blackList []*mysql.Black, err error) { +func (b *BlackModel) Find(ctx context.Context, blacks []*relation.Black) (blackList []*relation.Black, err error) { return b.db.Find(ctx, blacks) } -func (b *BlackModel) Take(ctx context.Context, ownerUserID, blockUserID string) (black *mysql.Black, err error) { +func (b *BlackModel) Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.Black, err error) { return b.db.Take(ctx, ownerUserID, blockUserID) } -func (b *BlackModel) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*mysql.Black, err error) { +func (b *BlackModel) FindByOwnerUserID(ctx context.Context, ownerUserID string) (blackList []*relation.Black, err error) { return b.db.FindByOwnerUserID(ctx, ownerUserID) } diff --git a/pkg/common/db/model/friend.go b/pkg/common/db/controller/friend.go similarity index 75% rename from pkg/common/db/model/friend.go rename to pkg/common/db/controller/friend.go index 32c4fdb75..957158d5c 100644 --- a/pkg/common/db/model/friend.go +++ b/pkg/common/db/controller/friend.go @@ -1,19 +1,20 @@ -package model +package controller import ( "Open_IM/pkg/common/db/cache" "Open_IM/pkg/common/db/mysql" + "Open_IM/pkg/common/db/relation" "context" "errors" "gorm.io/gorm" ) type FriendModel struct { - db *mysql.Friend + db *relation.Friend cache *cache.GroupCache } -func (f *FriendModel) Create(ctx context.Context, friends []*mysql.Friend) (err error) { +func (f *FriendModel) Create(ctx context.Context, friends []*relation.Friend) (err error) { return f.db.Create(ctx, friends) } @@ -25,7 +26,7 @@ func (f *FriendModel) UpdateByMap(ctx context.Context, ownerUserID string, args return f.db.UpdateByMap(ctx, ownerUserID, args) } -func (f *FriendModel) Update(ctx context.Context, friends []*mysql.Friend) (err error) { +func (f *FriendModel) Update(ctx context.Context, friends []*relation.Friend) (err error) { return f.db.Update(ctx, friends) } @@ -33,19 +34,19 @@ func (f *FriendModel) UpdateRemark(ctx context.Context, ownerUserID, friendUserI return f.db.UpdateRemark(ctx, ownerUserID, friendUserID, remark) } -func (f *FriendModel) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*mysql.Friend, err error) { +func (f *FriendModel) FindOwnerUserID(ctx context.Context, ownerUserID string) (friends []*relation.Friend, err error) { return f.db.FindOwnerUserID(ctx, ownerUserID) } -func (f *FriendModel) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*mysql.Friend, err error) { +func (f *FriendModel) FindFriendUserID(ctx context.Context, friendUserID string) (friends []*relation.Friend, err error) { return f.db.FindFriendUserID(ctx, friendUserID) } -func (f *FriendModel) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *mysql.Friend, err error) { +func (f *FriendModel) Take(ctx context.Context, ownerUserID, friendUserID string) (friend *relation.Friend, err error) { return f.db.Take(ctx, ownerUserID, friendUserID) } -func (f *FriendModel) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*mysql.Friend, err error) { +func (f *FriendModel) FindUserState(ctx context.Context, userID1, userID2 string) (friends []*relation.Friend, err error) { return f.db.FindUserState(ctx, userID1, userID2) } diff --git a/pkg/common/db/model/friend_request.go b/pkg/common/db/controller/friend_request.go similarity index 73% rename from pkg/common/db/model/friend_request.go rename to pkg/common/db/controller/friend_request.go index 55e5422c0..647016152 100644 --- a/pkg/common/db/model/friend_request.go +++ b/pkg/common/db/controller/friend_request.go @@ -1,4 +1,4 @@ -package model +package controller import ( "Open_IM/pkg/common/db/cache" @@ -7,11 +7,11 @@ import ( ) type FriendRequestModel struct { - db *mysql.FriendRequest + db *relation.FriendRequest cache *cache.GroupCache } -func (f *FriendRequestModel) Create(ctx context.Context, friends []*mysql.FriendRequest) (err error) { +func (f *FriendRequestModel) Create(ctx context.Context, friends []*relation.FriendRequest) (err error) { return f.db.Create(ctx, friends) } @@ -23,22 +23,22 @@ func (f *FriendRequestModel) UpdateByMap(ctx context.Context, ownerUserID string return f.db.UpdateByMap(ctx, ownerUserID, args) } -func (f *FriendRequestModel) Update(ctx context.Context, friends []*mysql.FriendRequest) (err error) { +func (f *FriendRequestModel) Update(ctx context.Context, friends []*relation.FriendRequest) (err error) { return f.db.Update(ctx, friends) } -func (f *FriendRequestModel) Find(ctx context.Context, ownerUserID string) (friends []*mysql.FriendRequest, err error) { +func (f *FriendRequestModel) Find(ctx context.Context, ownerUserID string) (friends []*relation.FriendRequest, err error) { return f.db.Find(ctx, ownerUserID) } -func (f *FriendRequestModel) Take(ctx context.Context, fromUserID, toUserID string) (friend *mysql.FriendRequest, err error) { +func (f *FriendRequestModel) Take(ctx context.Context, fromUserID, toUserID string) (friend *relation.FriendRequest, err error) { return f.db.Take(ctx, fromUserID, toUserID) } -func (f *FriendRequestModel) FindToUserID(ctx context.Context, toUserID string) (friends []*mysql.FriendRequest, err error) { +func (f *FriendRequestModel) FindToUserID(ctx context.Context, toUserID string) (friends []*relation.FriendRequest, err error) { return f.db.FindToUserID(ctx, toUserID) } -func (f *FriendRequestModel) FindFromUserID(ctx context.Context, fromUserID string) (friends []*mysql.FriendRequest, err error) { +func (f *FriendRequestModel) FindFromUserID(ctx context.Context, fromUserID string) (friends []*relation.FriendRequest, err error) { return f.db.FindFromUserID(ctx, fromUserID) } diff --git a/pkg/common/db/controller/group.go b/pkg/common/db/controller/group.go new file mode 100644 index 000000000..93b6b9ffc --- /dev/null +++ b/pkg/common/db/controller/group.go @@ -0,0 +1,127 @@ +package controller + +import ( + "Open_IM/pkg/common/db/cache" + "Open_IM/pkg/common/db/relation" + "Open_IM/pkg/common/db/unrelation" + "context" + "github.com/dtm-labs/rockscache" + _ "github.com/dtm-labs/rockscache" + "github.com/go-redis/redis/v8" + "go.mongodb.org/mongo-driver/mongo" + "gorm.io/gorm" +) + +type GroupInterface interface { + FindGroupsByID(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error) + CreateGroup(ctx context.Context, groups []*relation.Group) error + DeleteGroupByIDs(ctx context.Context, groupIDs []string) error + TakeGroupByID(ctx context.Context, groupID string) (group *relation.Group, err error) + + //mongo + GetSuperGroupByID(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error) +} + +type GroupController struct { + database DataBase +} + +func NewGroupController(db *gorm.DB, rdb redis.UniversalClient, mgoDB *mongo.Database) GroupInterface { + groupController := &GroupController{database: newGroupDatabase(db, rdb, mgoDB)} + return groupController +} + +func (g *GroupController) FindGroupsByID(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error) { + return g.database.Find(ctx, groupIDs) +} + +func (g *GroupController) CreateGroup(ctx context.Context, groups []*relation.Group) error { + return g.database.Create(ctx, groups) +} + +func (g *GroupController) DeleteGroupByIDs(ctx context.Context, groupIDs []string) error { + return g.database.Delete(ctx, groupIDs) +} + +func (g *GroupController) TakeGroupByID(ctx context.Context, groupID string) (group *relation.Group, err error) { + return g.database.Take(ctx, groupID) +} + +func (g *GroupController) GetSuperGroupByID(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error) { + return g.database.GetSuperGroup(ctx, groupID) +} + +type DataBase interface { + Find(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error) + Create(ctx context.Context, groups []*relation.Group) error + Delete(ctx context.Context, groupIDs []string) error + Take(ctx context.Context, groupID string) (group *relation.Group, err error) + GetSuperGroup(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error) +} + +type GroupDataBase struct { + sqlDB *relation.Group + cache *cache.GroupCache + mongoDB *unrelation.SuperGroupMgo +} + +func newGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, mgoDB *mongo.Database) DataBase { + sqlDB := relation.NewGroupDB(db) + database := &GroupDataBase{ + sqlDB: sqlDB, + cache: cache.NewGroupCache(rdb, sqlDB, rockscache.Options{ + RandomExpireAdjustment: 0.2, + DisableCacheRead: false, + DisableCacheDelete: false, + StrongConsistency: true, + }), + mongoDB: unrelation.NewSuperGroupMgoDB(mgoDB), + } + return database +} + +func (g *GroupDataBase) Find(ctx context.Context, groupIDs []string) (groups []*relation.Group, err error) { + return g.cache.GetGroupsInfoFromCache(ctx, groupIDs) +} + +func (g *GroupDataBase) Create(ctx context.Context, groups []*relation.Group) error { + return g.sqlDB.Create(ctx, groups) +} + +func (g *GroupDataBase) Delete(ctx context.Context, groupIDs []string) error { + err := g.sqlDB.DB.Transaction(func(tx *gorm.DB) error { + if err := g.sqlDB.Delete(ctx, groupIDs, tx); err != nil { + return err + } + if err := g.cache.DelGroupsInfoFromCache(ctx, groupIDs); err != nil { + return err + } + return nil + }) + return err +} + +func (g *GroupDataBase) Take(ctx context.Context, groupID string) (group *relation.Group, err error) { + return g.cache.GetGroupInfoFromCache(ctx, groupID) +} + +func (g *GroupDataBase) Update(ctx context.Context, groups []*relation.Group) error { + err := g.sqlDB.DB.Transaction(func(tx *gorm.DB) error { + if err := g.sqlDB.Update(ctx, groups, tx); err != nil { + return err + } + var groupIDs []string + for _, group := range groups { + groupIDs = append(groupIDs, group.GroupID) + } + if err := g.cache.DelGroupsInfoFromCache(ctx, groupIDs); err != nil { + return err + } + return nil + }) + return err +} + +func (g *GroupDataBase) GetSuperGroup(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error) { + return g.mongoDB.GetSuperGroup(ctx, groupID) +} diff --git a/pkg/common/db/model/user.go b/pkg/common/db/controller/user.go similarity index 61% rename from pkg/common/db/model/user.go rename to pkg/common/db/controller/user.go index 26ea4b2a3..ca9625564 100644 --- a/pkg/common/db/model/user.go +++ b/pkg/common/db/controller/user.go @@ -1,4 +1,4 @@ -package model +package controller import ( "Open_IM/pkg/common/db/mysql" @@ -6,19 +6,19 @@ import ( ) type UserModel struct { - db *mysql.User + db *relation.User } func NewGroupUser(ctx context.Context) *UserModel { var userModel UserModel - userModel.db = mysql.NewUserDB() + userModel.db = relation.NewUserDB() return &userModel } -func (u *UserModel) Find(ctx context.Context, userIDs []string) (users []*mysql.User, err error) { +func (u *UserModel) Find(ctx context.Context, userIDs []string) (users []*relation.User, err error) { return u.db.Find(ctx, userIDs) } -func (u *UserModel) Create(ctx context.Context, users []*mysql.User) error { +func (u *UserModel) Create(ctx context.Context, users []*relation.User) error { return u.db.Create(ctx, users) } diff --git a/pkg/common/db/model/group.go b/pkg/common/db/model/group.go deleted file mode 100644 index 0f765683e..000000000 --- a/pkg/common/db/model/group.go +++ /dev/null @@ -1,99 +0,0 @@ -package model - -import ( - "Open_IM/pkg/common/db/cache" - "Open_IM/pkg/common/db/mongoDB" - "Open_IM/pkg/common/db/mysql" - "context" - _ "github.com/dtm-labs/rockscache" - "github.com/go-redis/redis/v8" - "go.mongodb.org/mongo-driver/mongo" - "gorm.io/gorm" - //"time" -) - -type GroupInterface interface { - Find(ctx context.Context, groupIDs []string) (groups []*mysql.Group, err error) - Create(ctx context.Context, groups []*mysql.Group) error - Delete(ctx context.Context, groupIDs []string) error - Take(ctx context.Context, groupID string) (group *mysql.Group, err error) -} - -type GroupController struct { - db DataBase - cache *cache.GroupCache - mongo *mongoDB.Client -} -type DataBase interface { - Find(ctx context.Context, groupIDs []string) (groups []*mysql.Group, err error) - Create(ctx context.Context, groups []*mysql.Group) error - Delete(ctx context.Context, groupIDs []string) error - Take(ctx context.Context, groupID string) (group *mysql.Group, err error) - DeleteTx(ctx context.Context, groupIDs []string) error -} -type MySqlDatabase struct { - mysql.GroupModelInterface -} - -func (m *MySqlDatabase) Delete(ctx context.Context, groupIDs []string) error { - panic("implement me") -} - -func NewMySqlDatabase(db mysql.GroupModelInterface) DataBase { - return &MySqlDatabase{db} -} -func (m *MySqlDatabase) DeleteTx(ctx context.Context, groupIDs []string) error { - return nil -} - -func NewGroupController(groupModel mysql.GroupModelInterface, rdb redis.UniversalClient, mdb *mongo.Client) *GroupController { - return &GroupController{db: NewMySqlDatabase(groupModel)} - //groupModel.cache = cache.NewGroupCache(rdb, db, rockscache.Options{ - // DisableCacheRead: false, - // StrongConsistency: true, - //}) - //groupModel.mongo = mongoDB.NewMongoClient(mdb) - //return &groupModel -} - -func (g *GroupController) Find(ctx context.Context, groupIDs []string) (groups []*mysql.Group, err error) { - return g.cache.GetGroupsInfoFromCache(ctx, groupIDs) -} - -func (g *GroupController) Create(ctx context.Context, groups []*mysql.Group) error { - return g.db.Create(ctx, groups) -} - -func (g *GroupController) Delete(ctx context.Context, groupIDs []string) error { - err := g.db.DB.Transaction(func(tx *gorm.DB) error { - if err := g.db.Delete(ctx, groupIDs, tx); err != nil { - return err - } - if err := g.cache.DelGroupsInfoFromCache(ctx, groupIDs); err != nil { - return err - } - return nil - }) - return err -} - -func (g *GroupController) Take(ctx context.Context, groupID string) (group *mysql.Group, err error) { - return g.cache.GetGroupInfoFromCache(ctx, groupID) -} - -func (g *GroupController) Update(ctx context.Context, groups []*mysql.Group) error { - err := g.db.DB.Transaction(func(tx *gorm.DB) error { - if err := g.db.Update(ctx, groups, tx); err != nil { - return err - } - var groupIDs []string - for _, group := range groups { - groupIDs = append(groupIDs, group.GroupID) - } - if err := g.cache.DelGroupsInfoFromCache(ctx, groupIDs); err != nil { - return err - } - return nil - }) - return err -} diff --git a/pkg/common/db/mongoDB/office.go b/pkg/common/db/mongoDB/office.go deleted file mode 100644 index 9b5669132..000000000 --- a/pkg/common/db/mongoDB/office.go +++ /dev/null @@ -1 +0,0 @@ -package mongoDB diff --git a/pkg/common/db/mongoDB/super_group.go b/pkg/common/db/mongoDB/super_group.go deleted file mode 100644 index 980e39490..000000000 --- a/pkg/common/db/mongoDB/super_group.go +++ /dev/null @@ -1,181 +0,0 @@ -package mongoDB - -import ( - "Open_IM/pkg/utils" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" -) - -type SuperGroup struct { - GroupID string `bson:"group_id" json:"groupID"` - MemberIDList []string `bson:"member_id_list" json:"memberIDList"` -} - -type UserToSuperGroup struct { - UserID string `bson:"user_id" json:"userID"` - GroupIDList []string `bson:"group_id_list" json:"groupIDList"` -} - -func New - -func (d *db.DataBases) CreateSuperGroup(groupID string, initMemberIDList []string, memberNumCount int) error { - ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) - c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup) - session, err := d.mongoClient.StartSession() - if err != nil { - return utils.Wrap(err, "start session failed") - } - defer session.EndSession(ctx) - sCtx := mongo.NewSessionContext(ctx, session) - superGroup := SuperGroup{ - GroupID: groupID, - MemberIDList: initMemberIDList, - } - _, err = c.InsertOne(sCtx, superGroup) - if err != nil { - _ = session.AbortTransaction(ctx) - return utils.Wrap(err, "transaction failed") - } - var users []UserToSuperGroup - for _, v := range initMemberIDList { - users = append(users, UserToSuperGroup{ - UserID: v, - }) - } - upsert := true - opts := &options.UpdateOptions{ - Upsert: &upsert, - } - c = d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cUserToSuperGroup) - //_, err = c.UpdateMany(sCtx, bson.M{"user_id": bson.M{"$in": initMemberIDList}}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts) - //if err != nil { - // session.AbortTransaction(ctx) - // return utils.Wrap(err, "transaction failed") - //} - for _, userID := range initMemberIDList { - _, err = c.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts) - if err != nil { - _ = session.AbortTransaction(ctx) - return utils.Wrap(err, "transaction failed") - } - - } - return err -} - -func (d *db.DataBases) GetSuperGroup(groupID string) (SuperGroup, error) { - ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) - c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup) - superGroup := SuperGroup{} - err := c.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&superGroup) - return superGroup, err -} - -func (d *db.DataBases) AddUserToSuperGroup(groupID string, userIDList []string) error { - ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) - c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup) - session, err := d.mongoClient.StartSession() - if err != nil { - return utils.Wrap(err, "start session failed") - } - defer session.EndSession(ctx) - sCtx := mongo.NewSessionContext(ctx, session) - if err != nil { - return utils.Wrap(err, "start transaction failed") - } - _, err = c.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDList}}}) - if err != nil { - _ = session.AbortTransaction(ctx) - return utils.Wrap(err, "transaction failed") - } - c = d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cUserToSuperGroup) - var users []UserToSuperGroup - for _, v := range userIDList { - users = append(users, UserToSuperGroup{ - UserID: v, - }) - } - upsert := true - opts := &options.UpdateOptions{ - Upsert: &upsert, - } - for _, userID := range userIDList { - _, err = c.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts) - if err != nil { - _ = session.AbortTransaction(ctx) - return utils.Wrap(err, "transaction failed") - } - } - _ = session.CommitTransaction(ctx) - return err -} - -func (d *db.DataBases) RemoverUserFromSuperGroup(groupID string, userIDList []string) error { - ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) - c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup) - session, err := d.mongoClient.StartSession() - if err != nil { - return utils.Wrap(err, "start session failed") - } - defer session.EndSession(ctx) - sCtx := mongo.NewSessionContext(ctx, session) - _, err = c.UpdateOne(ctx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDList}}}) - if err != nil { - _ = session.AbortTransaction(ctx) - return utils.Wrap(err, "transaction failed") - } - err = d.RemoveGroupFromUser(ctx, sCtx, groupID, userIDList) - if err != nil { - _ = session.AbortTransaction(ctx) - return utils.Wrap(err, "transaction failed") - } - _ = session.CommitTransaction(ctx) - return err -} - -func (d *db.DataBases) GetSuperGroupByUserID(userID string) (UserToSuperGroup, error) { - ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) - c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cUserToSuperGroup) - var user UserToSuperGroup - _ = c.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user) - return user, nil -} - -func (d *db.DataBases) DeleteSuperGroup(groupID string) error { - ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) - c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup) - session, err := d.mongoClient.StartSession() - if err != nil { - return utils.Wrap(err, "start session failed") - } - defer session.EndSession(ctx) - sCtx := mongo.NewSessionContext(ctx, session) - superGroup := &SuperGroup{} - result := c.FindOneAndDelete(sCtx, bson.M{"group_id": groupID}) - err = result.Decode(superGroup) - if err != nil { - session.AbortTransaction(ctx) - return utils.Wrap(err, "transaction failed") - } - if err = d.RemoveGroupFromUser(ctx, sCtx, groupID, superGroup.MemberIDList); err != nil { - session.AbortTransaction(ctx) - return utils.Wrap(err, "transaction failed") - } - session.CommitTransaction(ctx) - return nil -} - -func (d *db.DataBases) RemoveGroupFromUser(ctx, sCtx context.Context, groupID string, userIDList []string) error { - var users []UserToSuperGroup - for _, v := range userIDList { - users = append(users, UserToSuperGroup{ - UserID: v, - }) - } - c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cUserToSuperGroup) - _, err := c.UpdateOne(sCtx, bson.M{"user_id": bson.M{"$in": userIDList}}, bson.M{"$pull": bson.M{"group_id_list": groupID}}) - if err != nil { - return utils.Wrap(err, "UpdateOne transaction failed") - } - return err -} \ No newline at end of file diff --git a/pkg/common/db/mysql/black.go b/pkg/common/db/relation/black.go similarity index 99% rename from pkg/common/db/mysql/black.go rename to pkg/common/db/relation/black.go index a7e712767..8ba3cbe44 100644 --- a/pkg/common/db/mysql/black.go +++ b/pkg/common/db/relation/black.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "Open_IM/pkg/common/trace_log" diff --git a/pkg/common/db/mysql/chat_log_model.go b/pkg/common/db/relation/chat_log_model.go similarity index 99% rename from pkg/common/db/mysql/chat_log_model.go rename to pkg/common/db/relation/chat_log_model.go index ccc53ceae..d2d3a36ff 100644 --- a/pkg/common/db/mysql/chat_log_model.go +++ b/pkg/common/db/relation/chat_log_model.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "Open_IM/pkg/common/constant" diff --git a/pkg/common/db/mysql/conversation_model.go b/pkg/common/db/relation/conversation_model.go similarity index 99% rename from pkg/common/db/mysql/conversation_model.go rename to pkg/common/db/relation/conversation_model.go index c487fb697..de2e92663 100644 --- a/pkg/common/db/mysql/conversation_model.go +++ b/pkg/common/db/relation/conversation_model.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "gorm.io/gorm" diff --git a/pkg/common/db/mysql/demo_model.go b/pkg/common/db/relation/demo_model.go similarity index 98% rename from pkg/common/db/mysql/demo_model.go rename to pkg/common/db/relation/demo_model.go index e1ead3baf..08cde761e 100644 --- a/pkg/common/db/mysql/demo_model.go +++ b/pkg/common/db/relation/demo_model.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "gorm.io/gorm" diff --git a/pkg/common/db/mysql/file_model.go b/pkg/common/db/relation/file_model.go similarity index 98% rename from pkg/common/db/mysql/file_model.go rename to pkg/common/db/relation/file_model.go index 36c03ea37..e1b93b72b 100644 --- a/pkg/common/db/mysql/file_model.go +++ b/pkg/common/db/relation/file_model.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "gorm.io/gorm" diff --git a/pkg/common/db/mysql/friend_model.go b/pkg/common/db/relation/friend_model.go similarity index 99% rename from pkg/common/db/mysql/friend_model.go rename to pkg/common/db/relation/friend_model.go index f5d08eb7f..1050b4958 100644 --- a/pkg/common/db/mysql/friend_model.go +++ b/pkg/common/db/relation/friend_model.go @@ -1,4 +1,4 @@ -package mysql +package relation //import ( // "fmt" diff --git a/pkg/common/db/mysql/friend_model_k.go b/pkg/common/db/relation/friend_model_k.go similarity index 99% rename from pkg/common/db/mysql/friend_model_k.go rename to pkg/common/db/relation/friend_model_k.go index af62c3b0b..ff727f60c 100644 --- a/pkg/common/db/mysql/friend_model_k.go +++ b/pkg/common/db/relation/friend_model_k.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "Open_IM/pkg/common/trace_log" diff --git a/pkg/common/db/mysql/friend_request_model.go b/pkg/common/db/relation/friend_request_model.go similarity index 99% rename from pkg/common/db/mysql/friend_request_model.go rename to pkg/common/db/relation/friend_request_model.go index 6db3e5f38..9b98355f2 100644 --- a/pkg/common/db/mysql/friend_request_model.go +++ b/pkg/common/db/relation/friend_request_model.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "Open_IM/pkg/common/trace_log" diff --git a/pkg/common/db/mysql/group_member_model.go b/pkg/common/db/relation/group_member_model.go similarity index 99% rename from pkg/common/db/mysql/group_member_model.go rename to pkg/common/db/relation/group_member_model.go index 4755bb78b..959d29bc4 100644 --- a/pkg/common/db/mysql/group_member_model.go +++ b/pkg/common/db/relation/group_member_model.go @@ -1,4 +1,4 @@ -package mysql +package relation //type GroupMember struct { // GroupID string `gorm:"column:group_id;primaryKey;"` diff --git a/pkg/common/db/mysql/group_member_model_k.go b/pkg/common/db/relation/group_member_model_k.go similarity index 99% rename from pkg/common/db/mysql/group_member_model_k.go rename to pkg/common/db/relation/group_member_model_k.go index 57d808a5b..405c8eff0 100644 --- a/pkg/common/db/mysql/group_member_model_k.go +++ b/pkg/common/db/relation/group_member_model_k.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "Open_IM/pkg/common/constant" diff --git a/pkg/common/db/mysql/group_model.go b/pkg/common/db/relation/group_model.go similarity index 99% rename from pkg/common/db/mysql/group_model.go rename to pkg/common/db/relation/group_model.go index 66b53abe7..3bc238cd9 100644 --- a/pkg/common/db/mysql/group_model.go +++ b/pkg/common/db/relation/group_model.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "Open_IM/pkg/common/constant" diff --git a/pkg/common/db/mysql/group_model_k.go b/pkg/common/db/relation/group_model_k.go similarity index 65% rename from pkg/common/db/mysql/group_model_k.go rename to pkg/common/db/relation/group_model_k.go index 7f7a626bf..59885d843 100644 --- a/pkg/common/db/mysql/group_model_k.go +++ b/pkg/common/db/relation/group_model_k.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "Open_IM/pkg/common/trace_log" @@ -8,18 +8,6 @@ import ( "time" ) -type GroupModelInterface interface { - //mysql - Create(ctx context.Context, groups []*Group) (err error) - Delete(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (err error) - UpdateByMap(ctx context.Context, groupID string, args map[string]interface{}) (err error) - Update(ctx context.Context, groups []*Group) (err error) - Find(ctx context.Context, groupIDs []string) (groups []*Group, err error) - Take(ctx context.Context, groupID string) (group *Group, err error) - - //mongo -} - type Group struct { GroupID string `gorm:"column:group_id;primary_key;size:64" json:"groupID" binding:"required"` GroupName string `gorm:"column:name;size:255" json:"groupName"` @@ -39,18 +27,17 @@ type Group struct { DB *gorm.DB } -func NewGroupDB() *Group { +func NewGroupDB(db *gorm.DB) *Group { var group Group - db := ConnectToDB() - db = InitModel(db, &group) + group.DB = db return &group } -func (*Group) Create(ctx context.Context, groups []*Group) (err error) { +func (g *Group) Create(ctx context.Context, groups []*Group) (err error) { defer func() { trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups) }() - err = utils.Wrap(GroupDB.Create(&groups).Error, "") + err = utils.Wrap(g.DB.Create(&groups).Error, "") return err } @@ -61,11 +48,11 @@ func (g *Group) Delete(ctx context.Context, groupIDs []string, tx ...*gorm.DB) ( return utils.Wrap(getDBConn(g.DB, tx...).Where("group_id in (?)", groupIDs).Delete(&Group{}).Error, "") } -func (*Group) UpdateByMap(ctx context.Context, groupID string, args map[string]interface{}) (err error) { +func (g *Group) UpdateByMap(ctx context.Context, groupID string, args map[string]interface{}) (err error) { defer func() { trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "args", args) }() - return utils.Wrap(GroupDB.Where("group_id = ?", groupID).Updates(args).Error, "") + return utils.Wrap(g.DB.Where("group_id = ?", groupID).Updates(args).Error, "") } func (g *Group) Update(ctx context.Context, groups []*Group, tx ...*gorm.DB) (err error) { @@ -75,22 +62,22 @@ func (g *Group) Update(ctx context.Context, groups []*Group, tx ...*gorm.DB) (er return utils.Wrap(getDBConn(g.DB, tx...).Updates(&groups).Error, "") } -func (*Group) Find(ctx context.Context, groupIDs []string) (groups []*Group, err error) { +func (g *Group) Find(ctx context.Context, groupIDs []string) (groups []*Group, err error) { defer func() { trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groups", groups) }() - err = utils.Wrap(GroupDB.Where("group_id in (?)", groupIDs).Find(&groups).Error, "") + err = utils.Wrap(g.DB.Where("group_id in (?)", groupIDs).Find(&groups).Error, "") return groups, err } -func (*Group) Take(ctx context.Context, groupID string) (group *Group, err error) { +func (g *Group) Take(ctx context.Context, groupID string) (group *Group, err error) { group = &Group{} defer func() { trace_log.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", *group) }() - err = utils.Wrap(GroupDB.Where("group_id = ?", groupID).Take(group).Error, "") + err = utils.Wrap(g.DB.Where("group_id = ?", groupID).Take(group).Error, "") return group, err } -func (*Group) DeleteTx(ctx context.Context, groupIDs []string) error { +func (g *Group) DeleteTx(ctx context.Context, groupIDs []string) error { return nil } diff --git a/pkg/common/db/mysql/group_request_model.go b/pkg/common/db/relation/group_request_model.go similarity index 99% rename from pkg/common/db/mysql/group_request_model.go rename to pkg/common/db/relation/group_request_model.go index f292ce50d..ef54d8037 100644 --- a/pkg/common/db/mysql/group_request_model.go +++ b/pkg/common/db/relation/group_request_model.go @@ -1,4 +1,4 @@ -package mysql +package relation // //func UpdateGroupRequest(groupRequest GroupRequest) error { diff --git a/pkg/common/db/mysql/group_request_model_k.go b/pkg/common/db/relation/group_request_model_k.go similarity index 99% rename from pkg/common/db/mysql/group_request_model_k.go rename to pkg/common/db/relation/group_request_model_k.go index 21409f62e..2394e2bd6 100644 --- a/pkg/common/db/mysql/group_request_model_k.go +++ b/pkg/common/db/relation/group_request_model_k.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "Open_IM/pkg/common/trace_log" diff --git a/pkg/common/db/mysql/init_mysql.go b/pkg/common/db/relation/init_mysql.go similarity index 99% rename from pkg/common/db/mysql/init_mysql.go rename to pkg/common/db/relation/init_mysql.go index 839902dfb..14713d2d6 100644 --- a/pkg/common/db/mysql/init_mysql.go +++ b/pkg/common/db/relation/init_mysql.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "Open_IM/pkg/common/config" diff --git a/pkg/common/db/mysql/model_struct.go b/pkg/common/db/relation/model_struct.go similarity index 99% rename from pkg/common/db/mysql/model_struct.go rename to pkg/common/db/relation/model_struct.go index 2e00f3107..d6b070b3d 100644 --- a/pkg/common/db/mysql/model_struct.go +++ b/pkg/common/db/relation/model_struct.go @@ -1,4 +1,4 @@ -package mysql +package relation //type Register struct { // Account string `gorm:"column:account;primary_key;type:char(255)" json:"account"` diff --git a/pkg/common/db/mysql/statistics_model.go b/pkg/common/db/relation/statistics_model.go similarity index 99% rename from pkg/common/db/mysql/statistics_model.go rename to pkg/common/db/relation/statistics_model.go index 328fbc9bb..8964731d0 100644 --- a/pkg/common/db/mysql/statistics_model.go +++ b/pkg/common/db/relation/statistics_model.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "Open_IM/pkg/common/constant" diff --git a/pkg/common/db/mysql/user_model.go b/pkg/common/db/relation/user_model.go similarity index 99% rename from pkg/common/db/mysql/user_model.go rename to pkg/common/db/relation/user_model.go index 757b446fa..32353dd94 100644 --- a/pkg/common/db/mysql/user_model.go +++ b/pkg/common/db/relation/user_model.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "Open_IM/pkg/common/config" diff --git a/pkg/common/db/mysql/user_model_k.go b/pkg/common/db/relation/user_model_k.go similarity index 99% rename from pkg/common/db/mysql/user_model_k.go rename to pkg/common/db/relation/user_model_k.go index f529ea414..1ecead80c 100644 --- a/pkg/common/db/mysql/user_model_k.go +++ b/pkg/common/db/relation/user_model_k.go @@ -1,4 +1,4 @@ -package mysql +package relation import ( "Open_IM/pkg/common/trace_log" diff --git a/pkg/common/db/mongoDB/batch_insert_chat.go b/pkg/common/db/unrelation/batch_insert_chat.go similarity index 99% rename from pkg/common/db/mongoDB/batch_insert_chat.go rename to pkg/common/db/unrelation/batch_insert_chat.go index c2e5ca33e..fdfe1f3b0 100644 --- a/pkg/common/db/mongoDB/batch_insert_chat.go +++ b/pkg/common/db/unrelation/batch_insert_chat.go @@ -1,4 +1,4 @@ -package mongoDB +package unrelation import ( "Open_IM/pkg/common/config" diff --git a/pkg/common/db/mongoDB/extend_msg_mongo_model.go b/pkg/common/db/unrelation/extend_msg_mongo_model.go similarity index 99% rename from pkg/common/db/mongoDB/extend_msg_mongo_model.go rename to pkg/common/db/unrelation/extend_msg_mongo_model.go index 1ca56bc59..b9a9040d5 100644 --- a/pkg/common/db/mongoDB/extend_msg_mongo_model.go +++ b/pkg/common/db/unrelation/extend_msg_mongo_model.go @@ -1,4 +1,4 @@ -package mongoDB +package unrelation import ( "Open_IM/pkg/common/config" diff --git a/pkg/common/db/mongoDB/init_mongo.go b/pkg/common/db/unrelation/init_mongo.go similarity index 97% rename from pkg/common/db/mongoDB/init_mongo.go rename to pkg/common/db/unrelation/init_mongo.go index 9333e75e9..4b9faf84f 100644 --- a/pkg/common/db/mongoDB/init_mongo.go +++ b/pkg/common/db/unrelation/init_mongo.go @@ -1,4 +1,4 @@ -package mongoDB +package unrelation import ( "Open_IM/pkg/common/config" @@ -21,7 +21,7 @@ func NewMongoClient(mdb *mongo.Client) *Client { return &Client{mongo: mdb} } -func initMongo() *mongo.Client { +func initMongo() *mongo.Database { uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority" if config.Config.Mongo.DBUri != "" { // example: mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize @@ -58,7 +58,8 @@ func initMongo() *mongo.Client { panic(err.Error() + " mongo.Connect failed " + uri) } } - return mongoClient + + return mongoClient.Database(config.Config.Mongo.DBDatabase) } func GetCollection(mongoClient *mongo.Client) { diff --git a/pkg/common/db/mongoDB/mongo_model.go b/pkg/common/db/unrelation/mongo_model.go similarity index 99% rename from pkg/common/db/mongoDB/mongo_model.go rename to pkg/common/db/unrelation/mongo_model.go index 3b6b7aa27..3de74f686 100644 --- a/pkg/common/db/mongoDB/mongo_model.go +++ b/pkg/common/db/unrelation/mongo_model.go @@ -1,4 +1,4 @@ -package mongoDB +package unrelation import ( "Open_IM/pkg/common/config" @@ -34,8 +34,7 @@ const cTag = "tag" const cSendLog = "send_log" const cWorkMoment = "work_moment" const cCommentMsg = "comment_msg" -const cSuperGroup = "super_group" -const cUserToSuperGroup = "user_to_super_group" + const singleGocMsgNum = 5000 func GetSingleGocMsgNum() int { @@ -1090,15 +1089,15 @@ func (d *db.DataBases) GetUserFriendWorkMoments(showNumber, pageNumber int32, us return workMomentList, err } -type SuperGroup struct { - GroupID string `bson:"group_id" json:"groupID"` - MemberIDList []string `bson:"member_id_list" json:"memberIDList"` -} - -type UserToSuperGroup struct { - UserID string `bson:"user_id" json:"userID"` - GroupIDList []string `bson:"group_id_list" json:"groupIDList"` -} +//type SuperGroup struct { +// GroupID string `bson:"group_id" json:"groupID"` +// MemberIDList []string `bson:"member_id_list" json:"memberIDList"` +//} +// +//type UserToSuperGroup struct { +// UserID string `bson:"user_id" json:"userID"` +// GroupIDList []string `bson:"group_id_list" json:"groupIDList"` +//} func (d *db.DataBases) CreateSuperGroup(groupID string, initMemberIDList []string, memberNumCount int) error { ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) diff --git a/pkg/common/db/unrelation/office.go b/pkg/common/db/unrelation/office.go new file mode 100644 index 000000000..5cad2078d --- /dev/null +++ b/pkg/common/db/unrelation/office.go @@ -0,0 +1 @@ +package unrelation diff --git a/pkg/common/db/unrelation/super_group.go b/pkg/common/db/unrelation/super_group.go new file mode 100644 index 000000000..0c9ef9341 --- /dev/null +++ b/pkg/common/db/unrelation/super_group.go @@ -0,0 +1,164 @@ +package unrelation + +import ( + "Open_IM/pkg/common/config" + "Open_IM/pkg/utils" + "context" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/mongo/readconcern" + "go.mongodb.org/mongo-driver/x/mongo/driver/session" + "time" +) + +const ( + cSuperGroup = "super_group" + cUserToSuperGroup = "user_to_super_group" +) + +type SuperGroupMgo struct { + mgoDB *mongo.Database + superGroupCollection *mongo.Collection + userToSuperGroupCollection *mongo.Collection +} + +type SuperGroup struct { + GroupID string `bson:"group_id" json:"groupID"` + MemberIDList []string `bson:"member_id_list" json:"memberIDList"` +} + +type UserToSuperGroup struct { + UserID string `bson:"user_id" json:"userID"` + GroupIDList []string `bson:"group_id_list" json:"groupIDList"` +} + +func NewSuperGroupMgoDB(mgoDB *mongo.Database) *SuperGroupMgo { + return &SuperGroupMgo{mgoDB: mgoDB, superGroupCollection: mgoDB.Collection(cSuperGroup), userToSuperGroupCollection: mgoDB.Collection(cUserToSuperGroup)} +} + +func (db *SuperGroupMgo) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string, memberNumCount int) error { + //ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) + //c := db.mgoDB.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup) + opts := options.Session().SetDefaultReadConcern(readconcern.Majority()) + return db.mgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error { + err := sCtx.StartTransaction() + if err != nil { + return err + } + superGroup := SuperGroup{ + GroupID: groupID, + MemberIDList: initMemberIDList, + } + _, err = db.superGroupCollection.InsertOne(sCtx, superGroup) + if err != nil { + _ = sCtx.AbortTransaction(ctx) + return err + } + upsert := true + opts := &options.UpdateOptions{ + Upsert: &upsert, + } + for _, userID := range initMemberIDList { + _, err = db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts) + if err != nil { + _ = sCtx.AbortTransaction(ctx) + return err + } + } + return sCtx.CommitTransaction(context.Background()) + }) +} + +func (db *SuperGroupMgo) GetSuperGroup(ctx context.Context, groupID string) (*SuperGroup, error) { + superGroup := SuperGroup{} + err := db.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&superGroup) + return &superGroup, err +} + +func (db *SuperGroupMgo) AddUserToSuperGroup(ctx context.Context, groupID string, userIDList []string) error { + opts := options.Session().SetDefaultReadConcern(readconcern.Majority()) + return db.mgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error { + _, err := db.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDList}}}) + if err != nil { + _ = sCtx.AbortTransaction(ctx) + return err + } + upsert := true + opts := &options.UpdateOptions{ + Upsert: &upsert, + } + for _, userID := range userIDList { + _, err = db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts) + if err != nil { + _ = sCtx.AbortTransaction(ctx) + return utils.Wrap(err, "transaction failed") + } + } + return sCtx.CommitTransaction(context.Background()) + }) +} + +func (d *SuperGroupMgo) RemoverUserFromSuperGroup(groupID string, userIDList []string) error { + ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) + c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup) + session, err := d.mongoClient.StartSession() + if err != nil { + return utils.Wrap(err, "start session failed") + } + defer session.EndSession(ctx) + sCtx := mongo.NewSessionContext(ctx, session) + _, err = c.UpdateOne(ctx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDList}}}) + if err != nil { + _ = session.AbortTransaction(ctx) + return utils.Wrap(err, "transaction failed") + } + err = d.RemoveGroupFromUser(ctx, sCtx, groupID, userIDList) + if err != nil { + _ = session.AbortTransaction(ctx) + return utils.Wrap(err, "transaction failed") + } + _ = session.CommitTransaction(ctx) + return err +} + +func (d *SuperGroupMgo) GetSuperGroupByUserID(userID string) (UserToSuperGroup, error) { + ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) + c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cUserToSuperGroup) + var user UserToSuperGroup + _ = c.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user) + return user, nil +} + +func (d *SuperGroupMgo) DeleteSuperGroup(groupID string) error { + ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) + c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup) + session, err := d.mongoClient.StartSession() + if err != nil { + return utils.Wrap(err, "start session failed") + } + defer session.EndSession(ctx) + sCtx := mongo.NewSessionContext(ctx, session) + superGroup := &SuperGroup{} + result := c.FindOneAndDelete(sCtx, bson.M{"group_id": groupID}) + err = result.Decode(superGroup) + if err != nil { + session.AbortTransaction(ctx) + return utils.Wrap(err, "transaction failed") + } + if err = d.RemoveGroupFromUser(ctx, sCtx, groupID, superGroup.MemberIDList); err != nil { + session.AbortTransaction(ctx) + return utils.Wrap(err, "transaction failed") + } + session.CommitTransaction(ctx) + return nil +} + +func (d *SuperGroupMgo) RemoveGroupFromUser(ctx, sCtx context.Context, groupID string, userIDList []string) error { + c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cUserToSuperGroup) + _, err := c.UpdateOne(sCtx, bson.M{"user_id": bson.M{"$in": userIDList}}, bson.M{"$pull": bson.M{"group_id_list": groupID}}) + if err != nil { + return utils.Wrap(err, "UpdateOne transaction failed") + } + return err +}