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.
Open-IM-Server/internal/rpc/group/super_group.go

88 lines
3.2 KiB

3 years ago
package group
import (
"context"
2 years ago
"fmt"
2 years ago
"strings"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
pbGroup "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"
3 years ago
)
func (s *groupServer) GetJoinedSuperGroupList(ctx context.Context, req *pbGroup.GetJoinedSuperGroupListReq) (*pbGroup.GetJoinedSuperGroupListResp, error) {
resp := &pbGroup.GetJoinedSuperGroupListResp{}
2 years ago
groupIDs, err := s.GroupDatabase.FindJoinSuperGroup(ctx, req.UserID)
3 years ago
if err != nil {
return nil, err
}
2 years ago
if len(groupIDs) == 0 {
3 years ago
return resp, nil
}
2 years ago
owners, err := s.GroupDatabase.FindGroupMember(ctx, groupIDs, nil, []int32{constant.GroupOwner})
if err != nil {
return nil, err
}
2 years ago
ownerMap := utils.SliceToMap(owners, func(e *relation.GroupMemberModel) string {
return e.GroupID
})
2 years ago
if ids := utils.Single(groupIDs, utils.Keys(ownerMap)); len(ids) > 0 {
2 years ago
return nil, errs.ErrData.Wrap(fmt.Sprintf("super group %s not owner", strings.Join(ids, ",")))
2 years ago
}
2 years ago
groups, err := s.GroupDatabase.FindGroup(ctx, groupIDs)
if err != nil {
return nil, err
3 years ago
}
groupMap := utils.SliceToMap(groups, func(e *relation.GroupModel) string {
return e.GroupID
})
2 years ago
if ids := utils.Single(groupIDs, utils.Keys(groupMap)); len(ids) > 0 {
2 years ago
return nil, errs.ErrData.Wrap(fmt.Sprintf("super group info %s not found", strings.Join(ids, ",")))
2 years ago
}
2 years ago
superGroupMembers, err := s.GroupDatabase.FindSuperGroup(ctx, groupIDs)
2 years ago
if err != nil {
return nil, err
}
superGroupMemberMap := utils.SliceToMapAny(superGroupMembers, func(e *unrelation.SuperGroupModel) (string, []string) {
return e.GroupID, e.MemberIDs
})
2 years ago
resp.Groups = utils.Slice(groupIDs, func(groupID string) *sdkws.GroupInfo {
2 years ago
return convert.Db2PbGroupInfo(groupMap[groupID], ownerMap[groupID].UserID, uint32(len(superGroupMemberMap)))
})
3 years ago
return resp, nil
}
3 years ago
2 years ago
func (s *groupServer) GetSuperGroupsInfo(ctx context.Context, req *pbGroup.GetSuperGroupsInfoReq) (resp *pbGroup.GetSuperGroupsInfoResp, err error) {
resp = &pbGroup.GetSuperGroupsInfoResp{}
if len(req.GroupIDs) == 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("groupIDs empty")
}
2 years ago
groups, err := s.GroupDatabase.FindGroup(ctx, req.GroupIDs)
if err != nil {
return nil, err
}
2 years ago
superGroupMembers, err := s.GroupDatabase.FindSuperGroup(ctx, req.GroupIDs)
if err != nil {
return nil, err
}
2 years ago
superGroupMemberMap := utils.SliceToMapAny(superGroupMembers, func(e *unrelation.SuperGroupModel) (string, []string) {
return e.GroupID, e.MemberIDs
})
2 years ago
owners, err := s.GroupDatabase.FindGroupMember(ctx, req.GroupIDs, nil, []int32{constant.GroupOwner})
if err != nil {
return nil, err
3 years ago
}
2 years ago
ownerMap := utils.SliceToMap(owners, func(e *relation.GroupMemberModel) string {
return e.GroupID
})
2 years ago
resp.GroupInfos = utils.Slice(groups, func(e *relation.GroupModel) *sdkws.GroupInfo {
2 years ago
return convert.Db2PbGroupInfo(e, ownerMap[e.GroupID].UserID, uint32(len(superGroupMemberMap[e.GroupID])))
})
3 years ago
return resp, nil
}