From f4baf847ca5ab08f77865407c1bee2345c919870 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Tue, 7 Feb 2023 18:43:43 +0800 Subject: [PATCH] errcode --- pkg/common/db/cache/conversation.go | 6 ++-- pkg/common/db/cache/extend_msg_set.go | 13 +++++++++ .../{redisModel_test.go => redis_test.go} | 0 pkg/common/db/localcache/group.go | 28 ++++++++++++++++--- .../db/table/unrelation/extend_msg_set.go | 16 +++++++++++ pkg/common/db/table/unrelation/super_group.go | 3 ++ 6 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 pkg/common/db/cache/extend_msg_set.go rename pkg/common/db/cache/{redisModel_test.go => redis_test.go} (100%) diff --git a/pkg/common/db/cache/conversation.go b/pkg/common/db/cache/conversation.go index 0a4dd3711..df39a342c 100644 --- a/pkg/common/db/cache/conversation.go +++ b/pkg/common/db/cache/conversation.go @@ -47,7 +47,7 @@ func (c *ConversationCache) getSuperGroupRecvNotNotifyUserIDsKey(groupID string) return superGroupRecvMsgNotNotifyUserIDsKey + groupID } -func (c *ConversationCache) GetUserConversationIDs(ctx context.Context, ownerUserID string) (conversationIDs []string, err error) { +func (c *ConversationCache) GetUserConversationIDs(ctx context.Context, ownerUserID string, f func(userID string) ([]string, error)) (conversationIDs []string, err error) { //getConversationIDs := func() (string, error) { // conversationIDs, err := relation.GetConversationIDsByUserID(ownerUserID) // if err != nil { @@ -69,11 +69,11 @@ func (c *ConversationCache) GetUserConversationIDs(ctx context.Context, ownerUse //} //return conversationIDs, nil return GetCache(c.rcClient, c.getConversationIDsKey(ownerUserID), time.Second*30*60, func() ([]string, error) { - return relation.GetConversationIDsByUserID(ownerUserID) + return f(ownerUserID) }) } -func (c *ConversationCache) GetUserConversationIDs1(ctx context.Context, ownerUserID string, fn func() (any, error)) (conversationIDs []string, err error) { +func (c *ConversationCache) GetUserConversationIDs1(ctx context.Context, ownerUserID string) (conversationIDs []string, err error) { //getConversationIDs := func() (string, error) { // conversationIDs, err := relation.GetConversationIDsByUserID(ownerUserID) // if err != nil { diff --git a/pkg/common/db/cache/extend_msg_set.go b/pkg/common/db/cache/extend_msg_set.go new file mode 100644 index 000000000..acceda2bf --- /dev/null +++ b/pkg/common/db/cache/extend_msg_set.go @@ -0,0 +1,13 @@ +package cache + +import ( + "Open_IM/pkg/common/db/relation" + "github.com/dtm-labs/rockscache" + "time" +) + +type ExtendMsgSetCache struct { + friendDB *relation.FriendGorm + expireTime time.Duration + rcClient *rockscache.Client +} diff --git a/pkg/common/db/cache/redisModel_test.go b/pkg/common/db/cache/redis_test.go similarity index 100% rename from pkg/common/db/cache/redisModel_test.go rename to pkg/common/db/cache/redis_test.go diff --git a/pkg/common/db/localcache/group.go b/pkg/common/db/localcache/group.go index 17aa53eac..5da9047f6 100644 --- a/pkg/common/db/localcache/group.go +++ b/pkg/common/db/localcache/group.go @@ -1,6 +1,7 @@ package localcache import ( + "Open_IM/pkg/common/constant" "Open_IM/pkg/proto/group" "context" "google.golang.org/grpc" @@ -27,12 +28,31 @@ func NewGroupMemberIDsLocalCache(rpc *grpc.ClientConn) GroupLocalCache { } } -func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) []string { +func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) { + g.lock.Lock() + defer g.lock.Unlock() resp, err := g.group.GetGroupAbstractInfo(ctx, &group.GetGroupAbstractInfoReq{ - GroupIDs: nil, + GroupIDs: []string{groupID}, }) if err != nil { - return nil + return nil, err } - return []string{} + if len(resp.GroupAbstractInfos) < 0 { + return nil, constant.ErrGroupIDNotFound + } + localHashInfo, ok := g.cache[groupID] + if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash { + return localHashInfo.userIDs, nil + } + groupMembersResp, err := g.group.GetGroupMemberList(ctx, &group.GetGroupMemberListReq{ + GroupID: groupID, + }) + if err != nil { + return nil, err + } + g.cache[groupID] = GroupMemberIDsHash{ + memberListHash: resp.GroupAbstractInfos[0].GroupMemberListHash, + userIDs: groupMembersResp.Members, + } + return g.cache[groupID].userIDs, nil } diff --git a/pkg/common/db/table/unrelation/extend_msg_set.go b/pkg/common/db/table/unrelation/extend_msg_set.go index 3f09878f9..197368a0c 100644 --- a/pkg/common/db/table/unrelation/extend_msg_set.go +++ b/pkg/common/db/table/unrelation/extend_msg_set.go @@ -1,6 +1,8 @@ package unrelation import ( + commonPb "Open_IM/pkg/proto/sdk_ws" + "context" "strconv" "strings" ) @@ -51,3 +53,17 @@ func (e *ExtendMsgSet) SplitSourceIDAndGetIndex() int32 { index, _ := strconv.Atoi(l[len(l)-1]) return int32(index) } + +type GetAllExtendMsgSetOpts struct { + ExcludeExtendMsgs bool +} + +type ExtendMsgSetInterface interface { + CreateExtendMsgSet(ctx context.Context, set *ExtendMsgSet) error + GetAllExtendMsgSet(ctx context.Context, ID string, opts *GetAllExtendMsgSetOpts) (sets []*ExtendMsgSet, err error) + GetExtendMsgSet(ctx context.Context, sourceID string, sessionType int32, maxMsgUpdateTime int64) (*ExtendMsgSet, error) + InsertExtendMsg(ctx context.Context, sourceID string, sessionType int32, msg *ExtendMsg) error + InsertOrUpdateReactionExtendMsgSet(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, msgFirstModifyTime int64, reactionExtensionList map[string]*commonPb.KeyValue) error + DeleteReactionExtendMsgSet(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, msgFirstModifyTime int64, reactionExtensionList map[string]*commonPb.KeyValue) error + GetExtendMsg(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, maxMsgUpdateTime int64) (extendMsg *ExtendMsg, err error) +} diff --git a/pkg/common/db/table/unrelation/super_group.go b/pkg/common/db/table/unrelation/super_group.go index db8e6fe53..d45ca9989 100644 --- a/pkg/common/db/table/unrelation/super_group.go +++ b/pkg/common/db/table/unrelation/super_group.go @@ -1,5 +1,7 @@ package unrelation +import "go.mongodb.org/mongo-driver/mongo" + const ( CSuperGroup = "super_group" CUserToSuperGroup = "user_to_super_group" @@ -24,4 +26,5 @@ func (UserToSuperGroupModel) TableName() string { } type SuperGroupModelInterface interface { + CreateSuperGroup(sCtx mongo.SessionContext, groupID string, initMemberIDs []string) error }