You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.1 KiB
79 lines
2.1 KiB
// Copyright © 2023 OpenIM. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package localcache
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"github.com/OpenIMSDK/protocol/group"
|
|
"github.com/OpenIMSDK/tools/errs"
|
|
|
|
"github.com/openimsdk/open-im-server/v3/pkg/rpcclient"
|
|
)
|
|
|
|
type GroupLocalCache struct {
|
|
lock sync.Mutex
|
|
cache map[string]GroupMemberIDsHash
|
|
client *rpcclient.GroupRpcClient
|
|
}
|
|
|
|
type GroupMemberIDsHash struct {
|
|
memberListHash uint64
|
|
userIDs []string
|
|
}
|
|
|
|
func NewGroupLocalCache(client *rpcclient.GroupRpcClient) *GroupLocalCache {
|
|
return &GroupLocalCache{
|
|
cache: make(map[string]GroupMemberIDsHash, 0),
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) {
|
|
resp, err := g.client.Client.GetGroupAbstractInfo(ctx, &group.GetGroupAbstractInfoReq{
|
|
GroupIDs: []string{groupID},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(resp.GroupAbstractInfos) < 1 {
|
|
return nil, errs.ErrGroupIDNotFound
|
|
}
|
|
|
|
g.lock.Lock()
|
|
localHashInfo, ok := g.cache[groupID]
|
|
if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash {
|
|
g.lock.Unlock()
|
|
return localHashInfo.userIDs, nil
|
|
}
|
|
g.lock.Unlock()
|
|
|
|
groupMembersResp, err := g.client.Client.GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{
|
|
GroupID: groupID,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
g.lock.Lock()
|
|
defer g.lock.Unlock()
|
|
g.cache[groupID] = GroupMemberIDsHash{
|
|
memberListHash: resp.GroupAbstractInfos[0].GroupMemberListHash,
|
|
userIDs: groupMembersResp.UserIDs,
|
|
}
|
|
return g.cache[groupID].userIDs, nil
|
|
}
|