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

85 lines
3.0 KiB

3 years ago
package group
import (
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db/table/relation"
2 years ago
"Open_IM/pkg/common/db/table/unrelation"
3 years ago
pbGroup "Open_IM/pkg/proto/group"
2 years ago
sdkws "Open_IM/pkg/proto/sdkws"
3 years ago
"Open_IM/pkg/utils"
"context"
2 years ago
"fmt"
"strings"
3 years ago
)
func (s *groupServer) GetJoinedSuperGroupList(ctx context.Context, req *pbGroup.GetJoinedSuperGroupListReq) (*pbGroup.GetJoinedSuperGroupListResp, error) {
resp := &pbGroup.GetJoinedSuperGroupListResp{}
2 years ago
joinSuperGroup, err := s.GroupInterface.FindJoinSuperGroup(ctx, req.UserID)
3 years ago
if err != nil {
return nil, err
}
2 years ago
if len(joinSuperGroup.GroupIDs) == 0 {
3 years ago
return resp, nil
}
2 years ago
owners, err := s.GroupInterface.FindGroupMember(ctx, joinSuperGroup.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(joinSuperGroup.GroupIDs, utils.Keys(ownerMap)); len(ids) > 0 {
return nil, constant.ErrData.Wrap(fmt.Sprintf("super group %s not owner", strings.Join(ids, ",")))
}
groups, err := s.GroupInterface.FindGroup(ctx, joinSuperGroup.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(joinSuperGroup.GroupIDs, utils.Keys(groupMap)); len(ids) > 0 {
return nil, constant.ErrData.Wrap(fmt.Sprintf("super group info %s not found", strings.Join(ids, ",")))
}
superGroupMembers, err := s.GroupInterface.FindSuperGroup(ctx, joinSuperGroup.GroupIDs)
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(joinSuperGroup.GroupIDs, func(groupID string) *sdkws.GroupInfo {
2 years ago
return DbToPbGroupInfo(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 {
return nil, constant.ErrArgs.Wrap("groupIDs empty")
}
2 years ago
groups, err := s.GroupInterface.FindGroup(ctx, req.GroupIDs)
if err != nil {
return nil, err
}
2 years ago
superGroupMembers, err := s.GroupInterface.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.GroupInterface.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 DbToPbGroupInfo(e, ownerMap[e.GroupID].UserID, uint32(len(superGroupMemberMap[e.GroupID])))
})
3 years ago
return resp, nil
}