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/group.go

1154 lines
41 KiB

package group
import (
"context"
"fmt"
2 years ago
"math/big"
"math/rand"
"strconv"
"strings"
"time"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation"
relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
pbConversation "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation"
pbGroup "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/check"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2 years ago
"google.golang.org/grpc"
2 years ago
"gorm.io/gorm"
)
2 years ago
func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error {
2 years ago
db, err := relation.NewGormDB()
2 years ago
if err != nil {
return err
}
2 years ago
if err := db.AutoMigrate(&relationTb.GroupModel{}, &relationTb.GroupMemberModel{}, &relationTb.GroupRequestModel{}); err != nil {
2 years ago
return err
}
2 years ago
mongo, err := unrelation.NewMongo()
2 years ago
if err != nil {
return err
}
2 years ago
rdb, err := cache.NewRedis()
2 years ago
if err != nil {
return err
}
2 years ago
pbGroup.RegisterGroupServer(server, &groupServer{
2 years ago
GroupDatabase: controller.InitGroupDatabase(db, rdb, mongo.GetDatabase()),
2 years ago
UserCheck: check.NewUserCheck(client),
ConversationChecker: check.NewConversationChecker(client),
2 years ago
})
2 years ago
return nil
3 years ago
}
2 years ago
type groupServer struct {
2 years ago
GroupDatabase controller.GroupDatabase
2 years ago
UserCheck *check.UserCheck
Notification *notification.Check
ConversationChecker *check.ConversationChecker
3 years ago
}
3 years ago
func (s *groupServer) CheckGroupAdmin(ctx context.Context, groupID string) error {
2 years ago
if !tokenverify.IsAppManagerUid(ctx) {
2 years ago
groupMember, err := s.GroupDatabase.TakeGroupMember(ctx, groupID, mcontext.GetOpUserID(ctx))
if err != nil {
return err
}
if !(groupMember.RoleLevel == constant.GroupOwner || groupMember.RoleLevel == constant.GroupAdmin) {
2 years ago
return errs.ErrNoPermission.Wrap("no group owner or admin")
}
}
return nil
}
2 years ago
func (s *groupServer) GetUsernameMap(ctx context.Context, userIDs []string, complete bool) (map[string]string, error) {
if len(userIDs) == 0 {
return map[string]string{}, nil
}
2 years ago
users, err := s.UserCheck.GetPublicUserInfos(ctx, userIDs, complete)
2 years ago
if err != nil {
return nil, err
}
2 years ago
return utils.SliceToMapAny(users, func(e *sdkws.PublicUserInfo) (string, string) {
2 years ago
return e.UserID, e.Nickname
}), nil
}
func (s *groupServer) IsNotFound(err error) bool {
return utils.Unwrap(err) == gorm.ErrRecordNotFound
}
func (s *groupServer) GenGroupID(ctx context.Context, groupID *string) error {
if *groupID != "" {
2 years ago
_, err := s.GroupDatabase.TakeGroup(ctx, *groupID)
2 years ago
if err == nil {
2 years ago
return errs.ErrGroupIDExisted.Wrap("group id existed " + *groupID)
2 years ago
} else if s.IsNotFound(err) {
return nil
} else {
return err
}
}
for i := 0; i < 10; i++ {
2 years ago
id := utils.Md5(strings.Join([]string{mcontext.GetOperationID(ctx), strconv.FormatInt(time.Now().UnixNano(), 10), strconv.Itoa(rand.Int())}, ",;,"))
2 years ago
bi := big.NewInt(0)
bi.SetString(id[0:8], 16)
id = bi.String()
2 years ago
_, err := s.GroupDatabase.TakeGroup(ctx, id)
2 years ago
if err == nil {
continue
} else if s.IsNotFound(err) {
*groupID = id
return nil
} else {
return err
}
}
2 years ago
return errs.ErrData.Wrap("group id gen error")
2 years ago
}
2 years ago
func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupReq) (*pbGroup.CreateGroupResp, error) {
2 years ago
if err := tokenverify.CheckAccessV3(ctx, req.OwnerUserID); err != nil {
2 years ago
return nil, err
2 years ago
}
if req.OwnerUserID == "" {
2 years ago
return nil, errs.ErrArgs.Wrap("no group owner")
2 years ago
}
2 years ago
userIDs := append(append(req.InitMembers, req.AdminUserIDs...), req.OwnerUserID)
if utils.Duplicate(userIDs) {
2 years ago
return nil, errs.ErrArgs.Wrap("group member repeated")
2 years ago
}
2 years ago
userMap, err := s.UserCheck.GetUsersInfoMap(ctx, userIDs, true)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
if err := CallbackBeforeCreateGroup(ctx, req); err != nil && err != errs.ErrCallbackContinue {
2 years ago
return nil, err
2 years ago
}
2 years ago
var groupMembers []*relationTb.GroupMemberModel
group := PbToDBGroupInfo(req.GroupInfo)
2 years ago
if err := s.GenGroupID(ctx, &group.GroupID); err != nil {
return nil, err
}
joinGroup := func(userID string, roleLevel int32) error {
groupMember := PbToDbGroupMember(userMap[userID])
2 years ago
groupMember.Nickname = ""
groupMember.GroupID = group.GroupID
groupMember.RoleLevel = roleLevel
2 years ago
groupMember.OperatorUserID = mcontext.GetOpUserID(ctx)
groupMember.JoinSource = constant.JoinByInvitation
2 years ago
groupMember.InviterUserID = mcontext.GetOpUserID(ctx)
2 years ago
groupMember.JoinTime = time.Now()
2 years ago
if err := CallbackBeforeMemberJoinGroup(ctx, groupMember, group.Ex); err != nil && err != errs.ErrCallbackContinue {
return err
}
groupMembers = append(groupMembers, groupMember)
return nil
}
if err := joinGroup(req.OwnerUserID, constant.GroupOwner); err != nil {
return nil, err
}
2 years ago
if req.GroupInfo.GroupType == constant.SuperGroup {
2 years ago
if err := s.GroupDatabase.CreateSuperGroup(ctx, group.GroupID, userIDs); err != nil {
2 years ago
return nil, err
}
} else {
for _, userID := range req.AdminUserIDs {
if err := joinGroup(userID, constant.GroupAdmin); err != nil {
2 years ago
return nil, err
2 years ago
}
}
2 years ago
for _, userID := range req.InitMembers {
2 years ago
if err := joinGroup(userID, constant.GroupOrdinaryUsers); err != nil {
2 years ago
return nil, err
2 years ago
}
}
}
2 years ago
if err := s.GroupDatabase.CreateGroup(ctx, []*relationTb.GroupModel{group}, groupMembers); err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
resp := &pbGroup.CreateGroupResp{GroupInfo: &sdkws.GroupInfo{}}
resp.GroupInfo = DbToPbGroupInfo(group, req.OwnerUserID, uint32(len(userIDs)))
2 years ago
resp.GroupInfo.MemberCount = uint32(len(userIDs))
2 years ago
if req.GroupInfo.GroupType == constant.SuperGroup {
2 years ago
go func() {
2 years ago
for _, userID := range userIDs {
2 years ago
s.Notification.SuperGroupNotification(ctx, userID, userID)
2 years ago
}
}()
2 years ago
} else {
2 years ago
s.Notification.GroupCreatedNotification(ctx, group.GroupID, userIDs)
2 years ago
}
return resp, nil
}
2 years ago
func (s *groupServer) GetJoinedGroupList(ctx context.Context, req *pbGroup.GetJoinedGroupListReq) (*pbGroup.GetJoinedGroupListResp, error) {
resp := &pbGroup.GetJoinedGroupListResp{}
2 years ago
if err := tokenverify.CheckAccessV3(ctx, req.FromUserID); err != nil {
2 years ago
return nil, err
}
2 years ago
total, members, err := s.GroupDatabase.PageGroupMember(ctx, nil, []string{req.FromUserID}, nil, req.Pagination.PageNumber, req.Pagination.ShowNumber)
if err != nil {
2 years ago
return nil, err
}
2 years ago
resp.Total = total
2 years ago
if len(members) == 0 {
2 years ago
return resp, nil
}
2 years ago
groupIDs := utils.Slice(members, func(e *relationTb.GroupMemberModel) string {
2 years ago
return e.GroupID
})
2 years ago
groups, err := s.GroupDatabase.FindGroup(ctx, groupIDs)
2 years ago
if err != nil {
return nil, err
}
2 years ago
groupMemberNum, err := s.GroupDatabase.MapGroupMemberNum(ctx, groupIDs)
2 years ago
if err != nil {
return nil, err
}
2 years ago
owners, err := s.GroupDatabase.FindGroupMember(ctx, groupIDs, nil, []int32{constant.GroupOwner})
2 years ago
if err != nil {
return nil, err
}
2 years ago
ownerMap := utils.SliceToMap(owners, func(e *relationTb.GroupMemberModel) string {
2 years ago
return e.GroupID
})
2 years ago
resp.Groups = utils.Slice(utils.Order(groupIDs, groups, func(group *relationTb.GroupModel) string {
2 years ago
return group.GroupID
2 years ago
}), func(group *relationTb.GroupModel) *sdkws.GroupInfo {
2 years ago
return DbToPbGroupInfo(group, ownerMap[group.GroupID].UserID, groupMemberNum[group.GroupID])
2 years ago
})
2 years ago
return resp, nil
}
2 years ago
func (s *groupServer) InviteUserToGroup(ctx context.Context, req *pbGroup.InviteUserToGroupReq) (*pbGroup.InviteUserToGroupResp, error) {
resp := &pbGroup.InviteUserToGroupResp{}
2 years ago
if len(req.InvitedUserIDs) == 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("user empty")
}
2 years ago
if utils.Duplicate(req.InvitedUserIDs) {
2 years ago
return nil, errs.ErrArgs.Wrap("userID duplicate")
2 years ago
}
2 years ago
group, err := s.GroupDatabase.TakeGroup(ctx, req.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
if group.Status == constant.GroupStatusDismissed {
2 years ago
return nil, errs.ErrDismissedAlready.Wrap()
2 years ago
}
2 years ago
members, err := s.GroupDatabase.FindGroupMember(ctx, []string{group.GroupID}, nil, nil)
2 years ago
if err != nil {
return nil, err
}
2 years ago
memberMap := utils.SliceToMap(members, func(e *relationTb.GroupMemberModel) string {
2 years ago
return e.UserID
})
2 years ago
if ids := utils.Single(req.InvitedUserIDs, utils.Keys(memberMap)); len(ids) > 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("user in group " + strings.Join(ids, ","))
}
2 years ago
userMap, err := s.UserCheck.GetUsersInfoMap(ctx, req.InvitedUserIDs, true)
2 years ago
if err != nil {
2 years ago
return nil, err
}
2 years ago
if group.NeedVerification == constant.AllNeedVerification {
2 years ago
if !tokenverify.IsAppManagerUid(ctx) {
2 years ago
opUserID := mcontext.GetOpUserID(ctx)
2 years ago
member, ok := memberMap[opUserID]
2 years ago
if !ok {
2 years ago
return nil, errs.ErrNoPermission.Wrap("not in group")
3 years ago
}
2 years ago
if !(member.RoleLevel == constant.GroupOwner || member.RoleLevel == constant.GroupAdmin) {
2 years ago
var requests []*relationTb.GroupRequestModel
2 years ago
for _, userID := range req.InvitedUserIDs {
2 years ago
requests = append(requests, &relationTb.GroupRequestModel{
2 years ago
UserID: userID,
GroupID: req.GroupID,
JoinSource: constant.JoinByInvitation,
InviterUserID: opUserID,
})
}
2 years ago
if err := s.GroupDatabase.CreateGroupRequest(ctx, requests); err != nil {
2 years ago
return nil, err
}
for _, request := range requests {
2 years ago
s.Notification.JoinGroupApplicationNotification(ctx, &pbGroup.JoinGroupReq{
2 years ago
GroupID: request.GroupID,
ReqMessage: request.ReqMsg,
JoinSource: request.JoinSource,
InviterUserID: request.InviterUserID,
})
}
return resp, nil
3 years ago
}
}
2 years ago
}
if group.GroupType == constant.SuperGroup {
2 years ago
if err := s.GroupDatabase.CreateSuperGroupMember(ctx, req.GroupID, req.InvitedUserIDs); err != nil {
2 years ago
return nil, err
}
2 years ago
for _, userID := range req.InvitedUserIDs {
2 years ago
s.Notification.SuperGroupNotification(ctx, userID, userID)
2 years ago
}
3 years ago
} else {
2 years ago
opUserID := mcontext.GetOpUserID(ctx)
2 years ago
var groupMembers []*relationTb.GroupMemberModel
2 years ago
for _, userID := range req.InvitedUserIDs {
member := PbToDbGroupMember(userMap[userID])
2 years ago
member.Nickname = ""
2 years ago
member.GroupID = req.GroupID
member.RoleLevel = constant.GroupOrdinaryUsers
2 years ago
member.OperatorUserID = opUserID
member.InviterUserID = opUserID
2 years ago
member.JoinSource = constant.JoinByInvitation
2 years ago
if err := CallbackBeforeMemberJoinGroup(ctx, member, group.Ex); err != nil && err != errs.ErrCallbackContinue {
2 years ago
return nil, err
}
groupMembers = append(groupMembers, member)
}
2 years ago
if err := s.GroupDatabase.CreateGroup(ctx, nil, groupMembers); err != nil {
2 years ago
return nil, err
}
2 years ago
s.Notification.MemberInvitedNotification(ctx, req.GroupID, req.Reason, req.InvitedUserIDs)
3 years ago
}
2 years ago
return resp, nil
}
2 years ago
func (s *groupServer) GetGroupAllMember(ctx context.Context, req *pbGroup.GetGroupAllMemberReq) (*pbGroup.GetGroupAllMemberResp, error) {
resp := &pbGroup.GetGroupAllMemberResp{}
2 years ago
group, err := s.GroupDatabase.TakeGroup(ctx, req.GroupID)
if err != nil {
2 years ago
return nil, err
}
if group.GroupType == constant.SuperGroup {
2 years ago
return nil, errs.ErrArgs.Wrap("unsupported super group")
3 years ago
}
2 years ago
members, err := s.GroupDatabase.FindGroupMember(ctx, []string{req.GroupID}, nil, nil)
if err != nil {
return nil, err
}
2 years ago
nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationTb.GroupMemberModel) (string, bool) {
2 years ago
return e.UserID, e.Nickname == ""
2 years ago
}), true)
2 years ago
if err != nil {
return nil, err
}
2 years ago
resp.Members = utils.Slice(members, func(e *relationTb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
2 years ago
if e.Nickname == "" {
e.Nickname = nameMap[e.UserID]
}
return DbToPbGroupMembersCMSResp(e)
})
2 years ago
return resp, nil
}
2 years ago
func (s *groupServer) GetGroupMemberList(ctx context.Context, req *pbGroup.GetGroupMemberListReq) (*pbGroup.GetGroupMemberListResp, error) {
resp := &pbGroup.GetGroupMemberListResp{}
2 years ago
total, members, err := s.GroupDatabase.PageGroupMember(ctx, []string{req.GroupID}, nil, utils.If(req.Filter >= 0, []int32{req.Filter}, nil), req.Pagination.PageNumber, req.Pagination.ShowNumber)
if err != nil {
2 years ago
return nil, err
}
2 years ago
resp.Total = total
2 years ago
nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationTb.GroupMemberModel) (string, bool) {
2 years ago
return e.UserID, e.Nickname == ""
2 years ago
}), true)
2 years ago
if err != nil {
return nil, err
}
2 years ago
resp.Members = utils.Slice(members, func(e *relationTb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
2 years ago
if e.Nickname == "" {
e.Nickname = nameMap[e.UserID]
}
return DbToPbGroupMembersCMSResp(e)
})
2 years ago
return resp, nil
}
2 years ago
func (s *groupServer) KickGroupMember(ctx context.Context, req *pbGroup.KickGroupMemberReq) (*pbGroup.KickGroupMemberResp, error) {
resp := &pbGroup.KickGroupMemberResp{}
2 years ago
group, err := s.GroupDatabase.TakeGroup(ctx, req.GroupID)
if err != nil {
2 years ago
return nil, err
}
2 years ago
if len(req.KickedUserIDs) == 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("KickedUserIDs empty")
2 years ago
}
if utils.IsDuplicateStringSlice(req.KickedUserIDs) {
2 years ago
return nil, errs.ErrArgs.Wrap("KickedUserIDs duplicate")
2 years ago
}
2 years ago
opUserID := mcontext.GetOpUserID(ctx)
2 years ago
if utils.IsContain(opUserID, req.KickedUserIDs) {
2 years ago
return nil, errs.ErrArgs.Wrap("opUserID in KickedUserIDs")
2 years ago
}
if group.GroupType == constant.SuperGroup {
2 years ago
if err := s.GroupDatabase.DeleteSuperGroupMember(ctx, req.GroupID, req.KickedUserIDs); err != nil {
2 years ago
return nil, err
}
2 years ago
go func() {
for _, userID := range req.KickedUserIDs {
2 years ago
s.Notification.SuperGroupNotification(ctx, userID, userID)
}
2 years ago
}()
3 years ago
} else {
2 years ago
members, err := s.GroupDatabase.FindGroupMember(ctx, []string{req.GroupID}, append(req.KickedUserIDs, opUserID), nil)
2 years ago
if err != nil {
2 years ago
return nil, err
}
2 years ago
memberMap := make(map[string]*relationTb.GroupMemberModel)
2 years ago
for i, member := range members {
memberMap[member.UserID] = members[i]
}
2 years ago
for _, userID := range req.KickedUserIDs {
if _, ok := memberMap[userID]; !ok {
2 years ago
return nil, errs.ErrUserIDNotFound.Wrap(userID)
}
}
2 years ago
if !tokenverify.IsAppManagerUid(ctx) {
2 years ago
member := memberMap[opUserID]
if member == nil {
2 years ago
return nil, errs.ErrNoPermission.Wrap(fmt.Sprintf("opUserID %s no in group", opUserID))
}
2 years ago
switch member.RoleLevel {
case constant.GroupOwner:
case constant.GroupAdmin:
for _, member := range members {
if member.UserID == opUserID {
continue
}
if member.RoleLevel == constant.GroupOwner || member.RoleLevel == constant.GroupAdmin {
2 years ago
return nil, errs.ErrNoPermission.Wrap("userID:" + member.UserID)
2 years ago
}
}
default:
2 years ago
return nil, errs.ErrNoPermission.Wrap("opUserID is OrdinaryUser")
2 years ago
}
}
2 years ago
if err := s.GroupDatabase.DeleteGroupMember(ctx, group.GroupID, req.KickedUserIDs); err != nil {
2 years ago
return nil, err
}
2 years ago
s.Notification.MemberKickedNotification(ctx, req, req.KickedUserIDs)
3 years ago
}
2 years ago
return resp, nil
}
2 years ago
func (s *groupServer) GetGroupMembersInfo(ctx context.Context, req *pbGroup.GetGroupMembersInfoReq) (*pbGroup.GetGroupMembersInfoResp, error) {
resp := &pbGroup.GetGroupMembersInfoResp{}
2 years ago
if len(req.Members) == 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("members empty")
2 years ago
}
if req.GroupID == "" {
2 years ago
return nil, errs.ErrArgs.Wrap("groupID empty")
2 years ago
}
2 years ago
members, err := s.GroupDatabase.FindGroupMember(ctx, []string{req.GroupID}, req.Members, nil)
2 years ago
if err != nil {
return nil, err
}
2 years ago
nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationTb.GroupMemberModel) (string, bool) {
2 years ago
return e.UserID, e.Nickname == ""
2 years ago
}), true)
2 years ago
if err != nil {
return nil, err
}
2 years ago
resp.Members = utils.Slice(members, func(e *relationTb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
2 years ago
if e.Nickname == "" {
e.Nickname = nameMap[e.UserID]
}
return DbToPbGroupMembersCMSResp(e)
})
return resp, nil
}
3 years ago
2 years ago
func (s *groupServer) GetGroupApplicationList(ctx context.Context, req *pbGroup.GetGroupApplicationListReq) (*pbGroup.GetGroupApplicationListResp, error) {
resp := &pbGroup.GetGroupApplicationListResp{}
2 years ago
total, groupRequests, err := s.GroupDatabase.PageGroupRequestUser(ctx, req.FromUserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
if err != nil {
2 years ago
return nil, err
}
2 years ago
resp.Total = total
2 years ago
if len(groupRequests) == 0 {
return resp, nil
}
var (
userIDs []string
groupIDs []string
)
for _, gr := range groupRequests {
userIDs = append(userIDs, gr.UserID)
groupIDs = append(groupIDs, gr.GroupID)
}
2 years ago
userIDs = utils.Distinct(userIDs)
groupIDs = utils.Distinct(groupIDs)
2 years ago
userMap, err := s.UserCheck.GetPublicUserInfoMap(ctx, userIDs, true)
2 years ago
if err != nil {
return nil, err
}
2 years ago
groups, err := s.GroupDatabase.FindGroup(ctx, utils.Distinct(groupIDs))
2 years ago
if err != nil {
return nil, err
}
2 years ago
groupMap := utils.SliceToMap(groups, func(e *relationTb.GroupModel) string {
2 years ago
return e.GroupID
})
if ids := utils.Single(utils.Keys(groupMap), groupIDs); len(ids) > 0 {
2 years ago
return nil, errs.ErrGroupIDNotFound.Wrap(strings.Join(ids, ","))
2 years ago
}
2 years ago
groupMemberNumMap, err := s.GroupDatabase.MapGroupMemberNum(ctx, groupIDs)
2 years ago
if err != nil {
return nil, err
3 years ago
}
2 years ago
owners, err := s.GroupDatabase.FindGroupMember(ctx, groupIDs, nil, []int32{constant.GroupOwner})
2 years ago
if err != nil {
2 years ago
return nil, err
}
2 years ago
ownerMap := utils.SliceToMap(owners, func(e *relationTb.GroupMemberModel) string {
2 years ago
return e.GroupID
})
2 years ago
resp.GroupRequests = utils.Slice(groupRequests, func(e *relationTb.GroupRequestModel) *sdkws.GroupRequest {
2 years ago
return DbToPbGroupRequest(e, userMap[e.UserID], DbToPbGroupInfo(groupMap[e.GroupID], ownerMap[e.GroupID].UserID, uint32(groupMemberNumMap[e.GroupID])))
})
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) GetGroupsInfo(ctx context.Context, req *pbGroup.GetGroupsInfoReq) (*pbGroup.GetGroupsInfoResp, error) {
resp := &pbGroup.GetGroupsInfoResp{}
2 years ago
if len(req.GroupIDs) == 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("groupID is empty")
2 years ago
}
2 years ago
groups, err := s.GroupDatabase.FindGroup(ctx, req.GroupIDs)
2 years ago
if err != nil {
return nil, err
}
2 years ago
groupMemberNumMap, err := s.GroupDatabase.MapGroupMemberNum(ctx, req.GroupIDs)
2 years ago
if err != nil {
return nil, err
3 years ago
}
2 years ago
owners, err := s.GroupDatabase.FindGroupMember(ctx, req.GroupIDs, nil, []int32{constant.GroupOwner})
2 years ago
if err != nil {
return nil, err
}
2 years ago
ownerMap := utils.SliceToMap(owners, func(e *relationTb.GroupMemberModel) string {
2 years ago
return e.GroupID
})
2 years ago
resp.GroupInfos = utils.Slice(groups, func(e *relationTb.GroupModel) *sdkws.GroupInfo {
2 years ago
return DbToPbGroupInfo(e, ownerMap[e.GroupID].UserID, groupMemberNumMap[e.GroupID])
2 years ago
})
2 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) GroupApplicationResponse(ctx context.Context, req *pbGroup.GroupApplicationResponseReq) (*pbGroup.GroupApplicationResponseResp, error) {
resp := &pbGroup.GroupApplicationResponseResp{}
if !utils.Contain(req.HandleResult, constant.GroupResponseAgree, constant.GroupResponseRefuse) {
2 years ago
return nil, errs.ErrArgs.Wrap("HandleResult unknown")
3 years ago
}
2 years ago
if !tokenverify.IsAppManagerUid(ctx) {
2 years ago
groupMember, err := s.GroupDatabase.TakeGroupMember(ctx, req.GroupID, req.FromUserID)
if err != nil {
return nil, err
}
if !(groupMember.RoleLevel == constant.GroupOwner || groupMember.RoleLevel == constant.GroupAdmin) {
2 years ago
return nil, errs.ErrNoPermission.Wrap("no group owner or admin")
}
}
2 years ago
group, err := s.GroupDatabase.TakeGroup(ctx, req.GroupID)
if err != nil {
2 years ago
return nil, err
}
2 years ago
groupRequest, err := s.GroupDatabase.TakeGroupRequest(ctx, req.GroupID, req.FromUserID)
if err != nil {
2 years ago
return nil, err
}
if groupRequest.HandleResult != 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("group request already processed")
}
2 years ago
var join bool
2 years ago
if _, err = s.GroupDatabase.TakeGroupMember(ctx, req.GroupID, req.FromUserID); err == nil {
2 years ago
join = true // 已经在群里了
} else if !s.IsNotFound(err) {
return nil, err
}
2 years ago
user, err := s.UserCheck.GetPublicUserInfo(ctx, req.FromUserID)
if err != nil {
return nil, err
}
2 years ago
var member *relationTb.GroupMemberModel
2 years ago
if (!join) && req.HandleResult == constant.GroupResponseAgree {
2 years ago
member = &relationTb.GroupMemberModel{
GroupID: req.GroupID,
UserID: user.UserID,
Nickname: user.Nickname,
FaceURL: user.FaceURL,
RoleLevel: constant.GroupOrdinaryUsers,
JoinTime: time.Now(),
JoinSource: groupRequest.JoinSource,
InviterUserID: groupRequest.InviterUserID,
2 years ago
OperatorUserID: mcontext.GetOpUserID(ctx),
Ex: groupRequest.Ex,
}
2 years ago
if err = CallbackBeforeMemberJoinGroup(ctx, member, group.Ex); err != nil && err != errs.ErrCallbackContinue {
2 years ago
return nil, err
3 years ago
}
}
2 years ago
if err := s.GroupDatabase.HandlerGroupRequest(ctx, req.GroupID, req.FromUserID, req.HandledMsg, req.HandleResult, member); err != nil {
return nil, err
}
2 years ago
if !join {
if req.HandleResult == constant.GroupResponseAgree {
2 years ago
s.Notification.GroupApplicationAcceptedNotification(ctx, req)
s.Notification.MemberEnterNotification(ctx, req)
2 years ago
} else if req.HandleResult == constant.GroupResponseRefuse {
2 years ago
s.Notification.GroupApplicationRejectedNotification(ctx, req)
2 years ago
}
3 years ago
}
2 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) JoinGroup(ctx context.Context, req *pbGroup.JoinGroupReq) (*pbGroup.JoinGroupResp, error) {
resp := &pbGroup.JoinGroupResp{}
2 years ago
if _, err := s.UserCheck.GetPublicUserInfo(ctx, mcontext.GetOpUserID(ctx)); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
group, err := s.GroupDatabase.TakeGroup(ctx, req.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
if group.Status == constant.GroupStatusDismissed {
2 years ago
return nil, errs.ErrDismissedAlready.Wrap()
3 years ago
}
2 years ago
if group.NeedVerification == constant.Directly {
if group.GroupType == constant.SuperGroup {
2 years ago
return nil, errs.ErrGroupTypeNotSupport.Wrap()
}
2 years ago
user, err := s.UserCheck.GetUserInfo(ctx, mcontext.GetOpUserID(ctx))
2 years ago
if err != nil {
return nil, err
}
groupMember := PbToDbGroupMember(user)
groupMember.GroupID = group.GroupID
groupMember.RoleLevel = constant.GroupOrdinaryUsers
2 years ago
groupMember.OperatorUserID = mcontext.GetOpUserID(ctx)
groupMember.JoinSource = constant.JoinByInvitation
2 years ago
groupMember.InviterUserID = mcontext.GetOpUserID(ctx)
2 years ago
if err := CallbackBeforeMemberJoinGroup(ctx, groupMember, group.Ex); err != nil && err != errs.ErrCallbackContinue {
2 years ago
return nil, err
}
2 years ago
if err := s.GroupDatabase.CreateGroup(ctx, nil, []*relationTb.GroupMemberModel{groupMember}); err != nil {
2 years ago
return nil, err
}
2 years ago
s.Notification.MemberEnterDirectlyNotification(ctx, req.GroupID, mcontext.GetOpUserID(ctx), mcontext.GetOperationID(ctx))
2 years ago
return resp, nil
}
2 years ago
groupRequest := relationTb.GroupRequestModel{
2 years ago
UserID: mcontext.GetOpUserID(ctx),
2 years ago
ReqMsg: req.ReqMessage,
GroupID: req.GroupID,
JoinSource: req.JoinSource,
ReqTime: time.Now(),
}
2 years ago
if err := s.GroupDatabase.CreateGroupRequest(ctx, []*relationTb.GroupRequestModel{&groupRequest}); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
s.Notification.JoinGroupApplicationNotification(ctx, req)
2 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) QuitGroup(ctx context.Context, req *pbGroup.QuitGroupReq) (*pbGroup.QuitGroupResp, error) {
resp := &pbGroup.QuitGroupResp{}
2 years ago
group, err := s.GroupDatabase.TakeGroup(ctx, req.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
if group.GroupType == constant.SuperGroup {
2 years ago
if err := s.GroupDatabase.DeleteSuperGroupMember(ctx, req.GroupID, []string{mcontext.GetOpUserID(ctx)}); err != nil {
2 years ago
return nil, err
}
2 years ago
s.Notification.SuperGroupNotification(ctx, mcontext.GetOpUserID(ctx), mcontext.GetOpUserID(ctx))
} else {
2 years ago
_, err := s.GroupDatabase.TakeGroupMember(ctx, req.GroupID, mcontext.GetOpUserID(ctx))
2 years ago
if err != nil {
2 years ago
return nil, err
}
2 years ago
s.Notification.MemberQuitNotification(ctx, req)
}
2 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) SetGroupInfo(ctx context.Context, req *pbGroup.SetGroupInfoReq) (*pbGroup.SetGroupInfoResp, error) {
resp := &pbGroup.SetGroupInfoResp{}
2 years ago
if !tokenverify.IsAppManagerUid(ctx) {
2 years ago
groupMember, err := s.GroupDatabase.TakeGroupMember(ctx, req.GroupInfoForSet.GroupID, mcontext.GetOpUserID(ctx))
2 years ago
if err != nil {
return nil, err
}
if !(groupMember.RoleLevel == constant.GroupOwner || groupMember.RoleLevel == constant.GroupAdmin) {
2 years ago
return nil, errs.ErrNoPermission.Wrap("no group owner or admin")
2 years ago
}
3 years ago
}
2 years ago
group, err := s.GroupDatabase.TakeGroup(ctx, req.GroupInfoForSet.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
3 years ago
if group.Status == constant.GroupStatusDismissed {
2 years ago
return nil, utils.Wrap(errs.ErrDismissedAlready, "")
3 years ago
}
2 years ago
userIDs, err := s.GroupDatabase.FindGroupMemberUserID(ctx, group.GroupID)
2 years ago
if err != nil {
return nil, err
}
data := UpdateGroupInfoMap(req.GroupInfoForSet)
2 years ago
if len(data) > 0 {
return resp, nil
}
2 years ago
if err := s.GroupDatabase.UpdateGroup(ctx, group.GroupID, data); err != nil {
2 years ago
return nil, err
}
2 years ago
group, err = s.GroupDatabase.TakeGroup(ctx, req.GroupInfoForSet.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
s.Notification.GroupInfoSetNotification(ctx, req.GroupInfoForSet.GroupID, group.GroupName, group.Notification, group.Introduction, group.FaceURL, req.GroupInfoForSet.NeedVerification)
2 years ago
if req.GroupInfoForSet.Notification != "" {
2 years ago
args := pbConversation.ModifyConversationFieldReq{
Conversation: &pbConversation.Conversation{
2 years ago
OwnerUserID: mcontext.GetOpUserID(ctx),
2 years ago
ConversationID: utils.GetConversationIDBySessionType(group.GroupID, constant.GroupChatType),
ConversationType: constant.GroupChatType,
GroupID: group.GroupID,
},
FieldType: constant.FieldGroupAtType,
UserIDList: userIDs,
}
if err := s.ConversationChecker.ModifyConversationField(ctx, &args); err != nil {
2 years ago
log.ZWarn(ctx, "modifyConversationField failed", err, "args", args)
2 years ago
}
}
2 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) TransferGroupOwner(ctx context.Context, req *pbGroup.TransferGroupOwnerReq) (*pbGroup.TransferGroupOwnerResp, error) {
resp := &pbGroup.TransferGroupOwnerResp{}
2 years ago
group, err := s.GroupDatabase.TakeGroup(ctx, req.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
if group.Status == constant.GroupStatusDismissed {
2 years ago
return nil, utils.Wrap(errs.ErrDismissedAlready, "")
3 years ago
}
3 years ago
if req.OldOwnerUserID == req.NewOwnerUserID {
2 years ago
return nil, errs.ErrArgs.Wrap("OldOwnerUserID == NewOwnerUserID")
3 years ago
}
2 years ago
members, err := s.GroupDatabase.FindGroupMember(ctx, []string{req.GroupID}, []string{req.OldOwnerUserID, req.NewOwnerUserID}, nil)
if err != nil {
2 years ago
return nil, err
}
2 years ago
memberMap := utils.SliceToMap(members, func(e *relationTb.GroupMemberModel) string { return e.UserID })
2 years ago
if ids := utils.Single([]string{req.OldOwnerUserID, req.NewOwnerUserID}, utils.Keys(memberMap)); len(ids) > 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("user not in group " + strings.Join(ids, ","))
}
2 years ago
newOwner := memberMap[req.NewOwnerUserID]
if newOwner == nil {
2 years ago
return nil, errs.ErrArgs.Wrap("NewOwnerUser not in group " + req.NewOwnerUserID)
3 years ago
}
2 years ago
oldOwner := memberMap[req.OldOwnerUserID]
2 years ago
if tokenverify.IsAppManagerUid(ctx) {
2 years ago
if oldOwner == nil {
2 years ago
oldOwner, err = s.GroupDatabase.TakeGroupOwner(ctx, req.OldOwnerUserID)
2 years ago
if err != nil {
return nil, err
}
}
} else {
if oldOwner == nil {
2 years ago
return nil, errs.ErrArgs.Wrap("OldOwnerUser not in group " + req.NewOwnerUserID)
2 years ago
}
2 years ago
if oldOwner.GroupID != mcontext.GetOpUserID(ctx) {
return nil, errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s no permission transfer group owner", mcontext.GetOpUserID(ctx)))
2 years ago
}
}
2 years ago
if err := s.GroupDatabase.TransferGroupOwner(ctx, req.GroupID, req.OldOwnerUserID, req.NewOwnerUserID, newOwner.RoleLevel); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
s.Notification.GroupOwnerTransferredNotification(ctx, req)
2 years ago
return resp, nil
3 years ago
}
3 years ago
2 years ago
func (s *groupServer) GetGroups(ctx context.Context, req *pbGroup.GetGroupsReq) (*pbGroup.GetGroupsResp, error) {
2 years ago
resp := &pbGroup.GetGroupsResp{}
var (
2 years ago
groups []*relationTb.GroupModel
2 years ago
err error
)
2 years ago
if req.GroupID != "" {
2 years ago
groups, err = s.GroupDatabase.FindGroup(ctx, []string{req.GroupID})
2 years ago
resp.Total = uint32(len(groups))
2 years ago
} else {
2 years ago
resp.Total, groups, err = s.GroupDatabase.SearchGroup(ctx, req.GroupName, req.Pagination.PageNumber, req.Pagination.ShowNumber)
2 years ago
}
if err != nil {
return nil, err
}
2 years ago
groupIDs := utils.Slice(groups, func(e *relationTb.GroupModel) string {
2 years ago
return e.GroupID
})
2 years ago
ownerMembers, err := s.GroupDatabase.FindGroupMember(ctx, groupIDs, nil, []int32{constant.GroupOwner})
2 years ago
if err != nil {
return nil, err
}
2 years ago
ownerMemberMap := utils.SliceToMap(ownerMembers, func(e *relationTb.GroupMemberModel) string {
2 years ago
return e.GroupID
})
if ids := utils.Single(groupIDs, utils.Keys(ownerMemberMap)); len(ids) > 0 {
2 years ago
return nil, errs.ErrDatabase.Wrap("group not owner " + strings.Join(ids, ","))
2 years ago
}
2 years ago
groupMemberNumMap, err := s.GroupDatabase.MapGroupMemberNum(ctx, groupIDs)
2 years ago
if err != nil {
return nil, err
}
2 years ago
resp.Groups = utils.Slice(groups, func(group *relationTb.GroupModel) *pbGroup.CMSGroup {
2 years ago
member := ownerMemberMap[group.GroupID]
return DbToPbCMSGroup(group, member.UserID, member.Nickname, uint32(groupMemberNumMap[group.GroupID]))
})
2 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) GetGroupMembersCMS(ctx context.Context, req *pbGroup.GetGroupMembersCMSReq) (*pbGroup.GetGroupMembersCMSResp, error) {
resp := &pbGroup.GetGroupMembersCMSResp{}
2 years ago
total, members, err := s.GroupDatabase.SearchGroupMember(ctx, req.UserName, []string{req.GroupID}, nil, nil, req.Pagination.PageNumber, req.Pagination.ShowNumber)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
resp.Total = total
2 years ago
nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationTb.GroupMemberModel) (string, bool) {
2 years ago
return e.UserID, e.Nickname == ""
2 years ago
}), true)
2 years ago
if err != nil {
return nil, err
}
2 years ago
resp.Members = utils.Slice(members, func(e *relationTb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
2 years ago
if e.Nickname == "" {
e.Nickname = nameMap[e.UserID]
}
return DbToPbGroupMembersCMSResp(e)
})
2 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) GetUserReqApplicationList(ctx context.Context, req *pbGroup.GetUserReqApplicationListReq) (*pbGroup.GetUserReqApplicationListResp, error) {
resp := &pbGroup.GetUserReqApplicationListResp{}
2 years ago
user, err := s.UserCheck.GetPublicUserInfo(ctx, req.UserID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
total, requests, err := s.GroupDatabase.PageGroupRequestUser(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
if err != nil {
return nil, err
}
resp.Total = total
if len(requests) == 0 {
return resp, nil
}
2 years ago
groupIDs := utils.Distinct(utils.Slice(requests, func(e *relationTb.GroupRequestModel) string {
return e.GroupID
}))
2 years ago
groups, err := s.GroupDatabase.FindGroup(ctx, groupIDs)
if err != nil {
return nil, err
}
2 years ago
groupMap := utils.SliceToMap(groups, func(e *relationTb.GroupModel) string {
return e.GroupID
})
if ids := utils.Single(groupIDs, utils.Keys(groupMap)); len(ids) > 0 {
2 years ago
return nil, errs.ErrGroupIDNotFound.Wrap(strings.Join(ids, ","))
}
2 years ago
owners, err := s.GroupDatabase.FindGroupMember(ctx, groupIDs, nil, []int32{constant.GroupOwner})
if err != nil {
return nil, err
3 years ago
}
2 years ago
ownerMap := utils.SliceToMap(owners, func(e *relationTb.GroupMemberModel) string {
return e.GroupID
})
if ids := utils.Single(groupIDs, utils.Keys(ownerMap)); len(ids) > 0 {
2 years ago
return nil, errs.ErrData.Wrap("group no owner", strings.Join(ids, ","))
}
2 years ago
groupMemberNum, err := s.GroupDatabase.MapGroupMemberNum(ctx, groupIDs)
if err != nil {
return nil, err
}
2 years ago
resp.GroupRequests = utils.Slice(requests, func(e *relationTb.GroupRequestModel) *sdkws.GroupRequest {
return DbToPbGroupRequest(e, user, DbToPbGroupInfo(groupMap[e.GroupID], ownerMap[e.GroupID].UserID, uint32(groupMemberNum[e.GroupID])))
})
2 years ago
return resp, nil
3 years ago
}
3 years ago
2 years ago
func (s *groupServer) DismissGroup(ctx context.Context, req *pbGroup.DismissGroupReq) (*pbGroup.DismissGroupResp, error) {
resp := &pbGroup.DismissGroupResp{}
if err := s.CheckGroupAdmin(ctx, req.GroupID); err != nil {
2 years ago
return nil, err
}
2 years ago
group, err := s.GroupDatabase.TakeGroup(ctx, req.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
if group.Status == constant.GroupStatusDismissed {
2 years ago
return nil, errs.ErrArgs.Wrap("group status is dismissed")
}
2 years ago
if err := s.GroupDatabase.DismissGroup(ctx, req.GroupID); err != nil {
2 years ago
return nil, err
}
if group.GroupType == constant.SuperGroup {
2 years ago
if err := s.GroupDatabase.DeleteSuperGroup(ctx, group.GroupID); err != nil {
2 years ago
return nil, err
3 years ago
}
} else {
2 years ago
s.Notification.GroupDismissedNotification(ctx, req)
3 years ago
}
2 years ago
return resp, nil
3 years ago
}
3 years ago
2 years ago
func (s *groupServer) MuteGroupMember(ctx context.Context, req *pbGroup.MuteGroupMemberReq) (*pbGroup.MuteGroupMemberResp, error) {
resp := &pbGroup.MuteGroupMemberResp{}
2 years ago
member, err := s.GroupDatabase.TakeGroupMember(ctx, req.GroupID, req.UserID)
if err != nil {
2 years ago
return nil, err
}
2 years ago
if !(mcontext.GetOpUserID(ctx) == req.UserID || tokenverify.IsAppManagerUid(ctx)) {
2 years ago
opMember, err := s.GroupDatabase.TakeGroupMember(ctx, req.GroupID, req.UserID)
if err != nil {
return nil, err
}
if opMember.RoleLevel <= member.RoleLevel {
2 years ago
return nil, errs.ErrNoPermission.Wrap(fmt.Sprintf("self RoleLevel %d target %d", opMember.RoleLevel, member.RoleLevel))
}
}
data := UpdateGroupMemberMutedTimeMap(time.Now().Add(time.Second * time.Duration(req.MutedSeconds)))
2 years ago
if err := s.GroupDatabase.UpdateGroupMember(ctx, member.GroupID, member.UserID, data); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
s.Notification.GroupMemberMutedNotification(ctx, req.GroupID, req.UserID, req.MutedSeconds)
2 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) CancelMuteGroupMember(ctx context.Context, req *pbGroup.CancelMuteGroupMemberReq) (*pbGroup.CancelMuteGroupMemberResp, error) {
resp := &pbGroup.CancelMuteGroupMemberResp{}
2 years ago
member, err := s.GroupDatabase.TakeGroupMember(ctx, req.GroupID, req.UserID)
if err != nil {
2 years ago
return nil, err
}
2 years ago
if !(mcontext.GetOpUserID(ctx) == req.UserID || tokenverify.IsAppManagerUid(ctx)) {
opMember, err := s.GroupDatabase.TakeGroupMember(ctx, req.GroupID, mcontext.GetOpUserID(ctx))
if err != nil {
return nil, err
}
if opMember.RoleLevel <= member.RoleLevel {
2 years ago
return nil, errs.ErrNoPermission.Wrap(fmt.Sprintf("self RoleLevel %d target %d", opMember.RoleLevel, member.RoleLevel))
}
}
data := UpdateGroupMemberMutedTimeMap(time.Unix(0, 0))
2 years ago
if err := s.GroupDatabase.UpdateGroupMember(ctx, member.GroupID, member.UserID, data); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
s.Notification.GroupMemberCancelMutedNotification(ctx, req.GroupID, req.UserID)
2 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) MuteGroup(ctx context.Context, req *pbGroup.MuteGroupReq) (*pbGroup.MuteGroupResp, error) {
resp := &pbGroup.MuteGroupResp{}
if err := s.CheckGroupAdmin(ctx, req.GroupID); err != nil {
2 years ago
return nil, err
}
2 years ago
if err := s.GroupDatabase.UpdateGroup(ctx, req.GroupID, UpdateGroupStatusMap(constant.GroupStatusMuted)); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
s.Notification.GroupMutedNotification(ctx, req.GroupID)
2 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) CancelMuteGroup(ctx context.Context, req *pbGroup.CancelMuteGroupReq) (*pbGroup.CancelMuteGroupResp, error) {
resp := &pbGroup.CancelMuteGroupResp{}
if err := s.CheckGroupAdmin(ctx, req.GroupID); err != nil {
2 years ago
return nil, err
}
2 years ago
if err := s.GroupDatabase.UpdateGroup(ctx, req.GroupID, UpdateGroupStatusMap(constant.GroupOk)); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
s.Notification.GroupCancelMutedNotification(ctx, req.GroupID)
2 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) SetGroupMemberInfo(ctx context.Context, req *pbGroup.SetGroupMemberInfoReq) (*pbGroup.SetGroupMemberInfoResp, error) {
resp := &pbGroup.SetGroupMemberInfoResp{}
if len(req.Members) == 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("members empty")
}
duplicateMap := make(map[[2]string]struct{})
userIDMap := make(map[string]struct{})
groupIDMap := make(map[string]struct{})
for _, member := range req.Members {
key := [...]string{member.GroupID, member.UserID}
if _, ok := duplicateMap[key]; ok {
2 years ago
return nil, errs.ErrArgs.Wrap("group user duplicate")
}
duplicateMap[key] = struct{}{}
userIDMap[member.UserID] = struct{}{}
groupIDMap[member.GroupID] = struct{}{}
}
groupIDs := utils.Keys(groupIDMap)
userIDs := utils.Keys(userIDMap)
2 years ago
members, err := s.GroupDatabase.FindGroupMember(ctx, groupIDs, append(userIDs, mcontext.GetOpUserID(ctx)), nil)
if err != nil {
2 years ago
return nil, err
}
for _, member := range members {
delete(duplicateMap, [...]string{member.GroupID, member.UserID})
}
if len(duplicateMap) > 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("group not found" + strings.Join(utils.Slice(utils.Keys(duplicateMap), func(e [2]string) string {
2 years ago
return fmt.Sprintf("[group: %s user: %s]", e[0], e[1])
}), ","))
}
memberMap := utils.SliceToMap(members, func(e *relationTb.GroupMemberModel) [2]string {
return [...]string{e.GroupID, e.UserID}
})
if !tokenverify.IsAppManagerUid(ctx) {
2 years ago
opUserID := mcontext.GetOpUserID(ctx)
for _, member := range members {
if member.UserID == opUserID {
continue
}
opMember, ok := memberMap[[...]string{member.GroupID, member.UserID}]
if !ok {
2 years ago
return nil, errs.ErrArgs.Wrap(fmt.Sprintf("user %s not in group %s", opUserID, member.GroupID))
}
if member.RoleLevel >= opMember.RoleLevel {
2 years ago
return nil, errs.ErrNoPermission.Wrap(fmt.Sprintf("group %s : %s RoleLevel %d >= %s RoleLevel %d", member.GroupID, member.UserID, member.RoleLevel, opMember.UserID, opMember.RoleLevel))
}
}
}
for _, member := range req.Members {
if member.RoleLevel == nil {
continue
}
if memberMap[[...]string{member.GroupID, member.UserID}].RoleLevel == constant.GroupOwner {
2 years ago
return nil, errs.ErrArgs.Wrap(fmt.Sprintf("group %s user %s is owner", member.GroupID, member.UserID))
}
}
for i := 0; i < len(req.Members); i++ {
if err := CallbackBeforeSetGroupMemberInfo(ctx, req.Members[i]); err != nil {
return nil, err
}
}
2 years ago
err = s.GroupDatabase.UpdateGroupMembers(ctx, utils.Slice(req.Members, func(e *pbGroup.SetGroupMemberInfo) *relationTb.BatchUpdateGroupMember {
2 years ago
return &relationTb.BatchUpdateGroupMember{
GroupID: e.GroupID,
UserID: e.UserID,
Map: UpdateGroupMemberMap(e),
}
}))
if err != nil {
return nil, err
2 years ago
}
for _, member := range req.Members {
2 years ago
s.Notification.GroupMemberInfoSetNotification(ctx, member.GroupID, member.UserID)
}
2 years ago
return resp, nil
}
2 years ago
2 years ago
func (s *groupServer) GetGroupAbstractInfo(ctx context.Context, req *pbGroup.GetGroupAbstractInfoReq) (*pbGroup.GetGroupAbstractInfoResp, error) {
resp := &pbGroup.GetGroupAbstractInfoResp{}
if len(req.GroupIDs) == 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("groupIDs empty")
}
if utils.Duplicate(req.GroupIDs) {
2 years ago
return nil, errs.ErrArgs.Wrap("groupIDs duplicate")
}
2 years ago
groups, err := s.GroupDatabase.FindGroup(ctx, req.GroupIDs)
if err != nil {
return nil, err
}
2 years ago
if ids := utils.Single(req.GroupIDs, utils.Slice(groups, func(group *relationTb.GroupModel) string {
return group.GroupID
})); len(ids) > 0 {
2 years ago
return nil, errs.ErrGroupIDNotFound.Wrap("not found group " + strings.Join(ids, ","))
2 years ago
}
2 years ago
groupUserMap, err := s.GroupDatabase.MapGroupMemberUserID(ctx, req.GroupIDs)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
if ids := utils.Single(req.GroupIDs, utils.Keys(groupUserMap)); len(ids) > 0 {
2 years ago
return nil, errs.ErrGroupIDNotFound.Wrap(fmt.Sprintf("group %s not found member", strings.Join(ids, ",")))
2 years ago
}
resp.GroupAbstractInfos = utils.Slice(groups, func(group *relationTb.GroupModel) *pbGroup.GroupAbstractInfo {
users := groupUserMap[group.GroupID]
2 years ago
return DbToPbGroupAbstractInfo(group.GroupID, users.MemberNum, users.Hash)
})
2 years ago
return resp, nil
}
func (s *groupServer) GetUserInGroupMembers(ctx context.Context, req *pbGroup.GetUserInGroupMembersReq) (*pbGroup.GetUserInGroupMembersResp, error) {
resp := &pbGroup.GetUserInGroupMembersResp{}
if len(req.GroupIDs) == 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("groupIDs empty")
}
2 years ago
members, err := s.GroupDatabase.FindGroupMember(ctx, []string{req.UserID}, req.GroupIDs, nil)
if err != nil {
return nil, err
}
2 years ago
nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationTb.GroupMemberModel) (string, bool) {
2 years ago
return e.UserID, e.Nickname == ""
2 years ago
}), true)
2 years ago
if err != nil {
return nil, err
}
2 years ago
resp.Members = utils.Slice(members, func(e *relationTb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
2 years ago
if e.Nickname == "" {
e.Nickname = nameMap[e.UserID]
}
return DbToPbGroupMembersCMSResp(e)
})
return resp, nil
}
2 years ago
2 years ago
func (s *groupServer) GetGroupMemberUserIDs(ctx context.Context, req *pbGroup.GetGroupMemberUserIDsReq) (resp *pbGroup.GetGroupMemberUserIDsResp, err error) {
resp = &pbGroup.GetGroupMemberUserIDsResp{}
2 years ago
resp.UserIDs, err = s.GroupDatabase.FindGroupMemberUserID(ctx, req.GroupID)
2 years ago
if err != nil {
return nil, err
}
return resp, nil
}
func (s *groupServer) GetGroupMemberRoleLevel(ctx context.Context, req *pbGroup.GetGroupMemberRoleLevelReq) (*pbGroup.GetGroupMemberRoleLevelResp, error) {
resp := &pbGroup.GetGroupMemberRoleLevelResp{}
if len(req.RoleLevels) == 0 {
2 years ago
return nil, errs.ErrArgs.Wrap("RoleLevels empty")
}
2 years ago
members, err := s.GroupDatabase.FindGroupMember(ctx, []string{req.GroupID}, nil, req.RoleLevels)
if err != nil {
return nil, err
}
nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationTb.GroupMemberModel) (string, bool) {
return e.UserID, e.Nickname == ""
}), true)
if err != nil {
return nil, err
}
resp.Members = utils.Slice(members, func(e *relationTb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
if e.Nickname == "" {
e.Nickname = nameMap[e.UserID]
}
return DbToPbGroupMembersCMSResp(e)
})
return resp, nil
}