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.
132 lines
4.0 KiB
132 lines
4.0 KiB
2 years ago
|
package check
|
||
|
|
||
|
import (
|
||
2 years ago
|
"context"
|
||
2 years ago
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||
|
discoveryRegistry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
|
||
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
||
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group"
|
||
|
sdkws "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
|
||
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||
2 years ago
|
"google.golang.org/grpc"
|
||
|
"strings"
|
||
2 years ago
|
)
|
||
|
|
||
2 years ago
|
type GroupChecker struct {
|
||
|
zk discoveryRegistry.SvcDiscoveryRegistry
|
||
|
}
|
||
|
|
||
|
func NewGroupChecker(zk discoveryRegistry.SvcDiscoveryRegistry) *GroupChecker {
|
||
|
return &GroupChecker{
|
||
|
zk: zk,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (g *GroupChecker) getConn() (*grpc.ClientConn, error) {
|
||
|
return g.zk.GetConn(config.Config.RpcRegisterName.OpenImGroupName)
|
||
|
}
|
||
|
|
||
|
func (g *GroupChecker) GetGroupInfos(ctx context.Context, groupIDs []string, complete bool) ([]*sdkws.GroupInfo, error) {
|
||
|
cc, err := g.getConn()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
resp, err := group.NewGroupClient(cc).GetGroupsInfo(ctx, &group.GetGroupsInfoReq{
|
||
|
GroupIDs: groupIDs,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if complete {
|
||
|
if ids := utils.Single(groupIDs, utils.Slice(resp.GroupInfos, func(e *sdkws.GroupInfo) string {
|
||
|
return e.GroupID
|
||
|
})); len(ids) > 0 {
|
||
2 years ago
|
return nil, errs.ErrGroupIDNotFound.Wrap(strings.Join(ids, ","))
|
||
2 years ago
|
}
|
||
|
}
|
||
|
return resp.GroupInfos, nil
|
||
|
}
|
||
|
|
||
|
func (g *GroupChecker) GetGroupInfo(ctx context.Context, groupID string) (*sdkws.GroupInfo, error) {
|
||
|
groups, err := g.GetGroupInfos(ctx, []string{groupID}, true)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return groups[0], nil
|
||
|
}
|
||
|
|
||
|
func (g *GroupChecker) GetGroupInfoMap(ctx context.Context, groupIDs []string, complete bool) (map[string]*sdkws.GroupInfo, error) {
|
||
|
groups, err := g.GetGroupInfos(ctx, groupIDs, complete)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return utils.SliceToMap(groups, func(e *sdkws.GroupInfo) string {
|
||
|
return e.GroupID
|
||
|
}), nil
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (g *GroupChecker) GetGroupMemberInfos(ctx context.Context, groupID string, userIDs []string, complete bool) ([]*sdkws.GroupMemberFullInfo, error) {
|
||
|
cc, err := g.getConn()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
resp, err := group.NewGroupClient(cc).GetGroupMembersInfo(ctx, &group.GetGroupMembersInfoReq{
|
||
|
GroupID: groupID,
|
||
|
Members: userIDs,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if complete {
|
||
|
if ids := utils.Single(userIDs, utils.Slice(resp.Members, func(e *sdkws.GroupMemberFullInfo) string {
|
||
|
return e.UserID
|
||
|
})); len(ids) > 0 {
|
||
2 years ago
|
return nil, errs.ErrNotInGroupYet.Wrap(strings.Join(ids, ","))
|
||
2 years ago
|
}
|
||
|
}
|
||
|
return resp.Members, nil
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (g *GroupChecker) GetGroupMemberInfo(ctx context.Context, groupID string, userID string) (*sdkws.GroupMemberFullInfo, error) {
|
||
|
members, err := g.GetGroupMemberInfos(ctx, groupID, []string{userID}, true)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return members[0], nil
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (g *GroupChecker) GetGroupMemberInfoMap(ctx context.Context, groupID string, userIDs []string, complete bool) (map[string]*sdkws.GroupMemberFullInfo, error) {
|
||
|
members, err := g.GetGroupMemberInfos(ctx, groupID, userIDs, true)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return utils.SliceToMap(members, func(e *sdkws.GroupMemberFullInfo) string {
|
||
|
return e.UserID
|
||
|
}), nil
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
func (g *GroupChecker) GetOwnerAndAdminInfos(ctx context.Context, groupID string) ([]*sdkws.GroupMemberFullInfo, error) {
|
||
2 years ago
|
cc, err := g.getConn()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
resp, err := group.NewGroupClient(cc).GetGroupMemberRoleLevel(ctx, &group.GetGroupMemberRoleLevelReq{
|
||
|
GroupID: groupID,
|
||
|
RoleLevels: []int32{constant.GroupOwner, constant.GroupAdmin},
|
||
|
})
|
||
|
return resp.Members, err
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
func (g *GroupChecker) GetOwnerInfo(ctx context.Context, groupID string) (*sdkws.GroupMemberFullInfo, error) {
|
||
|
cc, err := g.getConn()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
resp, err := group.NewGroupClient(cc).GetGroupMemberRoleLevel(ctx, &group.GetGroupMemberRoleLevelReq{
|
||
|
GroupID: groupID,
|
||
|
RoleLevels: []int32{constant.GroupOwner},
|
||
|
})
|
||
|
return resp.Members[0], err
|
||
|
}
|