Files
open-im-server/internal/rpc/group/super_group.go
T

88 lines
3.2 KiB
Go
Raw Normal View History

2022-05-30 11:55:27 +08:00
package group
import (
"context"
2023-02-08 18:30:45 +08:00
"fmt"
2023-03-27 15:05:40 +08:00
"strings"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
2023-05-12 11:37:53 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert"
2023-03-16 10:46:06 +08:00
"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"
2022-05-30 11:55:27 +08:00
)
func (s *groupServer) GetJoinedSuperGroupList(ctx context.Context, req *pbGroup.GetJoinedSuperGroupListReq) (*pbGroup.GetJoinedSuperGroupListResp, error) {
2023-02-07 15:48:18 +08:00
resp := &pbGroup.GetJoinedSuperGroupListResp{}
2023-03-27 15:05:40 +08:00
groupIDs, err := s.GroupDatabase.FindJoinSuperGroup(ctx, req.UserID)
2022-05-30 11:55:27 +08:00
if err != nil {
2023-02-07 15:48:18 +08:00
return nil, err
}
2023-03-27 15:05:40 +08:00
if len(groupIDs) == 0 {
2022-05-30 11:55:27 +08:00
return resp, nil
}
2023-03-27 15:05:40 +08:00
owners, err := s.GroupDatabase.FindGroupMember(ctx, groupIDs, nil, []int32{constant.GroupOwner})
2023-02-07 15:48:18 +08:00
if err != nil {
return nil, err
}
2023-02-08 14:41:09 +08:00
ownerMap := utils.SliceToMap(owners, func(e *relation.GroupMemberModel) string {
return e.GroupID
})
2023-03-27 15:05:40 +08:00
if ids := utils.Single(groupIDs, utils.Keys(ownerMap)); len(ids) > 0 {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrData.Wrap(fmt.Sprintf("super group %s not owner", strings.Join(ids, ",")))
2023-02-08 18:30:45 +08:00
}
2023-03-27 15:05:40 +08:00
groups, err := s.GroupDatabase.FindGroup(ctx, groupIDs)
2023-02-07 15:48:18 +08:00
if err != nil {
return nil, err
2022-05-30 15:10:26 +08:00
}
2023-02-07 15:48:18 +08:00
groupMap := utils.SliceToMap(groups, func(e *relation.GroupModel) string {
return e.GroupID
})
2023-03-27 15:05:40 +08:00
if ids := utils.Single(groupIDs, utils.Keys(groupMap)); len(ids) > 0 {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrData.Wrap(fmt.Sprintf("super group info %s not found", strings.Join(ids, ",")))
2023-02-08 18:30:45 +08:00
}
2023-03-27 15:05:40 +08:00
superGroupMembers, err := s.GroupDatabase.FindSuperGroup(ctx, groupIDs)
2023-02-08 18:30:45 +08:00
if err != nil {
return nil, err
}
superGroupMemberMap := utils.SliceToMapAny(superGroupMembers, func(e *unrelation.SuperGroupModel) (string, []string) {
return e.GroupID, e.MemberIDs
})
2023-03-27 15:05:40 +08:00
resp.Groups = utils.Slice(groupIDs, func(groupID string) *sdkws.GroupInfo {
2023-05-12 11:37:53 +08:00
return convert.Db2PbGroupInfo(groupMap[groupID], ownerMap[groupID].UserID, uint32(len(superGroupMemberMap)))
2023-02-07 15:48:18 +08:00
})
2022-05-30 11:55:27 +08:00
return resp, nil
}
2022-05-30 16:23:15 +08:00
2023-01-11 16:23:16 +08:00
func (s *groupServer) GetSuperGroupsInfo(ctx context.Context, req *pbGroup.GetSuperGroupsInfoReq) (resp *pbGroup.GetSuperGroupsInfoResp, err error) {
2023-02-07 15:48:18 +08:00
resp = &pbGroup.GetSuperGroupsInfoResp{}
if len(req.GroupIDs) == 0 {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrArgs.Wrap("groupIDs empty")
2023-02-07 15:48:18 +08:00
}
2023-02-22 11:35:24 +08:00
groups, err := s.GroupDatabase.FindGroup(ctx, req.GroupIDs)
2023-02-07 15:48:18 +08:00
if err != nil {
return nil, err
}
2023-02-22 11:35:24 +08:00
superGroupMembers, err := s.GroupDatabase.FindSuperGroup(ctx, req.GroupIDs)
2023-02-07 15:48:18 +08:00
if err != nil {
return nil, err
}
2023-02-08 18:30:45 +08:00
superGroupMemberMap := utils.SliceToMapAny(superGroupMembers, func(e *unrelation.SuperGroupModel) (string, []string) {
return e.GroupID, e.MemberIDs
})
2023-02-22 11:35:24 +08:00
owners, err := s.GroupDatabase.FindGroupMember(ctx, req.GroupIDs, nil, []int32{constant.GroupOwner})
2023-02-07 15:48:18 +08:00
if err != nil {
return nil, err
2022-05-30 16:23:15 +08:00
}
2023-02-08 14:41:09 +08:00
ownerMap := utils.SliceToMap(owners, func(e *relation.GroupMemberModel) string {
return e.GroupID
})
2023-02-09 20:36:34 +08:00
resp.GroupInfos = utils.Slice(groups, func(e *relation.GroupModel) *sdkws.GroupInfo {
2023-05-12 11:37:53 +08:00
return convert.Db2PbGroupInfo(e, ownerMap[e.GroupID].UserID, uint32(len(superGroupMemberMap[e.GroupID])))
2023-02-07 15:48:18 +08:00
})
2022-05-30 16:23:15 +08:00
return resp, nil
}