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

1085 lines
39 KiB

package group
import (
3 years ago
chat "Open_IM/internal/rpc/msg"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
2 years ago
"Open_IM/pkg/common/db/cache"
"Open_IM/pkg/common/db/controller"
2 years ago
relation_conn "Open_IM/pkg/common/db/relation"
"Open_IM/pkg/common/db/table/relation"
2 years ago
"Open_IM/pkg/common/db/unrelation"
"Open_IM/pkg/common/log"
2 years ago
"Open_IM/pkg/common/middleware"
2 years ago
promePkg "Open_IM/pkg/common/prometheus"
3 years ago
"Open_IM/pkg/common/token_verify"
2 years ago
"Open_IM/pkg/common/tracelog"
2 years ago
discoveryRegistry "Open_IM/pkg/discovery_registry"
pbGroup "Open_IM/pkg/proto/group"
2 years ago
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils"
"context"
"fmt"
"github.com/OpenIMSDK/getcdv3"
2 years ago
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
2 years ago
"math/big"
3 years ago
"net"
"strconv"
"strings"
"time"
2 years ago
2 years ago
"github.com/OpenIMSDK/openKeeper"
2 years ago
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/wrapperspb"
)
3 years ago
type groupServer struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
2 years ago
GroupInterface controller.GroupInterface
2 years ago
2 years ago
etcdConn *getcdv3.EtcdConn
//userRpc pbUser.UserClient
//conversationRpc pbConversation.ConversationClient
3 years ago
}
func NewGroupServer(port int) *groupServer {
3 years ago
log.NewPrivateLog(constant.LogFileName)
2 years ago
g := groupServer{
3 years ago
rpcPort: port,
rpcRegisterName: config.Config.RpcRegisterName.OpenImGroupName,
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
}
2 years ago
ttl := 10
2 years ago
etcdClient, err := getcdv3.NewEtcdConn(config.Config.Etcd.EtcdSchema, strings.Join(g.etcdAddr, ","), config.Config.RpcRegisterIP, config.Config.Etcd.UserName, config.Config.Etcd.Password, port, ttl)
2 years ago
if err != nil {
panic("NewEtcdConn failed" + err.Error())
}
err = etcdClient.RegisterEtcd("", g.rpcRegisterName)
if err != nil {
panic("NewEtcdConn failed" + err.Error())
}
etcdClient.SetDefaultEtcdConfig(config.Config.RpcRegisterName.OpenImUserName, config.Config.RpcPort.OpenImUserPort)
2 years ago
//conn := etcdClient.GetConn("", config.Config.RpcRegisterName.OpenImUserName)
//g.userRpc = pbUser.NewUserClient(conn)
2 years ago
etcdClient.SetDefaultEtcdConfig(config.Config.RpcRegisterName.OpenImConversationName, config.Config.RpcPort.OpenImConversationPort)
2 years ago
//conn = etcdClient.GetConn("", config.Config.RpcRegisterName.OpenImConversationName)
//g.conversationRpc = pbConversation.NewConversationClient(conn)
2 years ago
2 years ago
//mysql init
2 years ago
var mysql relation_conn.Mysql
2 years ago
var mongo unrelation.Mongo
2 years ago
var groupModel relation.GroupModel
2 years ago
var redis cache.RedisClient
2 years ago
err = mysql.InitConn().AutoMigrateModel(&groupModel)
2 years ago
if err != nil {
panic("db init err:" + err.Error())
}
if mysql.GormConn() != nil {
groupModel.DB = mysql.GormConn()
} else {
panic("db init err:" + "conn is nil")
}
2 years ago
mongo.InitMongo()
2 years ago
redis.InitRedis()
2 years ago
mongo.CreateSuperGroupIndex()
2 years ago
zkClient, err := openKeeper.NewClient([]string{"43.154.157.177:2181"}, config.Config.Etcd.EtcdSchema, 10, "", "")
if err != nil {
panic(err.Error())
}
2 years ago
registerIP, err := utils.GetRpcIP()
err = zkClient.Register(config.Config.RpcRegisterName.OpenImGroupName, registerIP, port)
if err != nil {
panic(err.Error())
}
2 years ago
var registerCenter discoveryRegistry.SvcDiscoveryRegistry = zkClient
conns, err := registerCenter.GetConns(config.Config.RpcRegisterName.OpenImConversationName)
2 years ago
g.GroupInterface = controller.NewGroupInterface(controller.NewGroupDatabase(mysql.GormConn(), redis.GetClient(), mongo.GetClient()))
2 years ago
return &g
3 years ago
}
3 years ago
3 years ago
func (s *groupServer) Run() {
3 years ago
log.NewInfo("", "group rpc start ")
3 years ago
listenIP := ""
if config.Config.ListenIP == "" {
listenIP = "0.0.0.0"
} else {
listenIP = config.Config.ListenIP
}
address := listenIP + ":" + strconv.Itoa(s.rpcPort)
3 years ago
//listener network
3 years ago
listener, err := net.Listen("tcp", address)
3 years ago
if err != nil {
3 years ago
panic("listening err:" + err.Error() + s.rpcRegisterName)
3 years ago
}
3 years ago
log.NewInfo("", "listen network success, ", address, listener)
2 years ago
3 years ago
defer listener.Close()
//grpc server
2 years ago
recvSize := 1024 * 1024 * constant.GroupRPCRecvSize
sendSize := 1024 * 1024 * constant.GroupRPCSendSize
2 years ago
var grpcOpts = []grpc.ServerOption{
2 years ago
grpc.MaxRecvMsgSize(recvSize),
grpc.MaxSendMsgSize(sendSize),
2 years ago
grpc.UnaryInterceptor(middleware.RpcServerInterceptor),
2 years ago
}
2 years ago
if config.Config.Prometheus.Enable {
2 years ago
promePkg.NewGrpcRequestCounter()
promePkg.NewGrpcRequestFailedCounter()
promePkg.NewGrpcRequestSuccessCounter()
2 years ago
grpcOpts = append(grpcOpts, []grpc.ServerOption{
2 years ago
// grpc.UnaryInterceptor(promePkg.UnaryServerInterceptorProme),
2 years ago
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpcPrometheus.UnaryServerInterceptor),
}...)
2 years ago
}
2 years ago
srv := grpc.NewServer(grpcOpts...)
3 years ago
defer srv.GracefulStop()
//Service registers with etcd
pbGroup.RegisterGroupServer(srv, s)
3 years ago
rpcRegisterIP := config.Config.RpcRegisterIP
2 years ago
if config.Config.RpcRegisterIP == "" {
3 years ago
rpcRegisterIP, err = utils.GetLocalIP()
if err != nil {
log.Error("", "GetLocalIP failed ", err.Error())
}
}
3 years ago
log.NewInfo("", "rpcRegisterIP", rpcRegisterIP)
err = getcdv3.RegisterEtcd(s.etcdSchema, strings.Join(s.etcdAddr, ","), rpcRegisterIP, s.rpcPort, s.rpcRegisterName, 10, "")
3 years ago
if err != nil {
3 years ago
log.NewError("", "RegisterEtcd failed ", err.Error())
panic(utils.Wrap(err, "register group module rpc to etcd err"))
3 years ago
}
3 years ago
log.Info("", "RegisterEtcd ", s.etcdSchema, strings.Join(s.etcdAddr, ","), rpcRegisterIP, s.rpcPort, s.rpcRegisterName)
3 years ago
err = srv.Serve(listener)
if err != nil {
3 years ago
log.NewError("", "Serve failed ", err.Error())
3 years ago
return
}
3 years ago
log.NewInfo("", "group rpc success")
3 years ago
}
func (s *groupServer) CheckGroupAdmin(ctx context.Context, groupID string) error {
if !token_verify.IsAppManagerUid(ctx) {
2 years ago
groupMember, err := s.GroupInterface.TakeGroupMember(ctx, groupID, tracelog.GetOpUserID(ctx))
if err != nil {
return err
}
if !(groupMember.RoleLevel == constant.GroupOwner || groupMember.RoleLevel == constant.GroupAdmin) {
return constant.ErrNoPermission.Wrap("no group owner or admin")
}
}
return nil
}
2 years ago
func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupReq) (*pbGroup.CreateGroupResp, error) {
2 years ago
resp := &pbGroup.CreateGroupResp{GroupInfo: &open_im_sdk.GroupInfo{}}
2 years ago
if err := token_verify.CheckAccessV3(ctx, req.OwnerUserID); err != nil {
2 years ago
return nil, err
2 years ago
}
if req.OwnerUserID == "" {
return nil, constant.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, constant.ErrArgs.Wrap("group member repeated")
2 years ago
}
2 years ago
userMap, err := GetUserInfoMap(ctx, userIDs)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
if ids := utils.Single(userIDs, utils.Keys(userMap)); len(ids) > 0 {
2 years ago
return nil, constant.ErrUserIDNotFound.Wrap(strings.Join(ids, ","))
2 years ago
}
2 years ago
if err := callbackBeforeCreateGroup(ctx, req); err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
var groupMembers []*relation.GroupMemberModel
group := PbToDBGroupInfo(req.GroupInfo)
2 years ago
group.GroupID = genGroupID(ctx, req.GroupInfo.GroupID)
joinGroup := func(userID string, roleLevel int32) error {
groupMember := PbToDbGroupMember(userMap[userID])
groupMember.GroupID = group.GroupID
groupMember.RoleLevel = roleLevel
groupMember.OperatorUserID = tracelog.GetOpUserID(ctx)
groupMember.JoinSource = constant.JoinByInvitation
groupMember.InviterUserID = tracelog.GetOpUserID(ctx)
if err := CallbackBeforeMemberJoinGroup(ctx, tracelog.GetOperationID(ctx), groupMember, group.Ex); err != nil {
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 {
if err := s.GroupInterface.CreateSuperGroup(ctx, group.GroupID, userIDs); err != nil {
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.GroupInterface.CreateGroup(ctx, []*relation.GroupModel{group}, groupMembers); err != nil {
2 years ago
return nil, err
2 years ago
}
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
chat.SuperGroupNotification(tracelog.GetOperationID(ctx), userID, userID)
2 years ago
}
}()
2 years ago
} else {
2 years ago
chat.GroupCreatedNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(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 := token_verify.CheckAccessV3(ctx, req.FromUserID); err != nil {
2 years ago
return nil, err
}
2 years ago
total, members, err := s.GroupInterface.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 *relation.GroupMemberModel) string {
2 years ago
return e.GroupID
})
2 years ago
groups, err := s.GroupInterface.FindGroup(ctx, groupIDs)
2 years ago
if err != nil {
return nil, err
}
2 years ago
groupMemberNum, err := s.GroupInterface.MapGroupMemberNum(ctx, groupIDs)
2 years ago
if err != nil {
return nil, err
}
2 years ago
owners, err := s.GroupInterface.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 {
2 years ago
return e.GroupID
})
2 years ago
resp.Groups = utils.Slice(utils.Order(groupIDs, groups, func(group *relation.GroupModel) string {
2 years ago
return group.GroupID
2 years ago
}), func(group *relation.GroupModel) *open_im_sdk.GroupInfo {
2 years ago
return DbToPbGroupInfo(group, ownerMap[group.GroupID].UserID, uint32(groupMemberNum[group.GroupID]))
})
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, constant.ErrArgs.Wrap("user empty")
}
2 years ago
if utils.Duplicate(req.InvitedUserIDs) {
2 years ago
return nil, constant.ErrArgs.Wrap("userID duplicate")
}
2 years ago
group, err := s.GroupInterface.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 {
return nil, constant.ErrDismissedAlready.Wrap()
}
2 years ago
members, err := s.GroupInterface.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 *relation.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, constant.ErrArgs.Wrap("user in group " + strings.Join(ids, ","))
}
2 years ago
userMap, err := GetUserInfoMap(ctx, req.InvitedUserIDs)
2 years ago
if err != nil {
2 years ago
return nil, err
}
2 years ago
if ids := utils.Single(req.InvitedUserIDs, utils.Keys(userMap)); len(ids) > 0 {
2 years ago
return nil, constant.ErrArgs.Wrap("user not found " + strings.Join(ids, ","))
2 years ago
}
if group.NeedVerification == constant.AllNeedVerification {
if !token_verify.IsAppManagerUid(ctx) {
2 years ago
opUserID := tracelog.GetOpUserID(ctx)
2 years ago
member, ok := memberMap[opUserID]
2 years ago
if !ok {
2 years ago
return nil, constant.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 []*relation.GroupRequestModel
2 years ago
for _, userID := range req.InvitedUserIDs {
2 years ago
requests = append(requests, &relation.GroupRequestModel{
2 years ago
UserID: userID,
GroupID: req.GroupID,
JoinSource: constant.JoinByInvitation,
InviterUserID: opUserID,
})
}
if err := s.GroupInterface.CreateGroupRequest(ctx, requests); err != nil {
return nil, err
}
for _, request := range requests {
chat.JoinGroupApplicationNotification(ctx, &pbGroup.JoinGroupReq{
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.GroupInterface.CreateSuperGroupMember(ctx, req.GroupID, req.InvitedUserIDs); err != nil {
2 years ago
return nil, err
}
2 years ago
for _, userID := range req.InvitedUserIDs {
chat.SuperGroupNotification(tracelog.GetOperationID(ctx), userID, userID)
2 years ago
}
3 years ago
} else {
2 years ago
opUserID := tracelog.GetOpUserID(ctx)
2 years ago
var groupMembers []*relation.GroupMemberModel
2 years ago
for _, userID := range req.InvitedUserIDs {
member := PbToDbGroupMember(userMap[userID])
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
if err := CallbackBeforeMemberJoinGroup(ctx, tracelog.GetOperationID(ctx), member, group.Ex); err != nil {
2 years ago
return nil, err
}
groupMembers = append(groupMembers, member)
}
2 years ago
if err := s.GroupInterface.CreateGroup(ctx, nil, groupMembers); err != nil {
2 years ago
return nil, err
}
2 years ago
chat.MemberInvitedNotification(tracelog.GetOperationID(ctx), req.GroupID, tracelog.GetOpUserID(ctx), 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.GroupInterface.TakeGroup(ctx, req.GroupID)
if err != nil {
2 years ago
return nil, err
}
if group.GroupType == constant.SuperGroup {
return nil, constant.ErrArgs.Wrap("unsupported super group")
3 years ago
}
2 years ago
members, err := s.GroupInterface.FindGroupMember(ctx, []string{req.GroupID}, nil, nil)
if err != nil {
return nil, err
}
2 years ago
resp.Members = utils.Slice(members, func(e *relation.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
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.GroupInterface.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
resp.Members = utils.Slice(members, func(e *relation.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
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.GroupInterface.TakeGroup(ctx, req.GroupID)
if err != nil {
2 years ago
return nil, err
}
2 years ago
if len(req.KickedUserIDs) == 0 {
return nil, constant.ErrArgs.Wrap("KickedUserIDs empty")
}
if utils.IsDuplicateStringSlice(req.KickedUserIDs) {
return nil, constant.ErrArgs.Wrap("KickedUserIDs duplicate")
}
opUserID := tracelog.GetOpUserID(ctx)
if utils.IsContain(opUserID, req.KickedUserIDs) {
return nil, constant.ErrArgs.Wrap("opUserID in KickedUserIDs")
}
if group.GroupType == constant.SuperGroup {
2 years ago
if err := s.GroupInterface.DeleteSuperGroupMember(ctx, req.GroupID, req.KickedUserIDs); err != nil {
2 years ago
return nil, err
}
2 years ago
go func() {
for _, userID := range req.KickedUserIDs {
chat.SuperGroupNotification(tracelog.GetOperationID(ctx), userID, userID)
}
2 years ago
}()
3 years ago
} else {
2 years ago
members, err := s.GroupInterface.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]*relation.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 {
return nil, constant.ErrUserIDNotFound.Wrap(userID)
}
}
2 years ago
if !token_verify.IsAppManagerUid(ctx) {
member := memberMap[opUserID]
if member == nil {
return nil, constant.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 {
return nil, constant.ErrNoPermission.Wrap("userID:" + member.UserID)
}
}
default:
return nil, constant.ErrNoPermission.Wrap("opUserID is OrdinaryUser")
}
}
2 years ago
if err := s.GroupInterface.DeleteGroupMember(ctx, group.GroupID, req.KickedUserIDs); err != nil {
2 years ago
return nil, err
}
chat.MemberKickedNotification(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 {
return nil, constant.ErrArgs.Wrap("members empty")
}
if req.GroupID == "" {
return nil, constant.ErrArgs.Wrap("groupID empty")
}
members, err := s.GroupInterface.FindGroupMember(ctx, []string{req.GroupID}, req.Members, nil)
2 years ago
if err != nil {
return nil, err
}
2 years ago
resp.Members = utils.Slice(members, func(e *relation.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
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.GroupInterface.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)
userMap, err := GetPublicUserInfoMap(ctx, userIDs)
2 years ago
if err != nil {
return nil, err
}
2 years ago
if ids := utils.Single(utils.Keys(userMap), userIDs); len(ids) > 0 {
return nil, constant.ErrUserIDNotFound.Wrap(strings.Join(ids, ","))
2 years ago
}
2 years ago
groups, err := s.GroupInterface.FindGroup(ctx, utils.Distinct(groupIDs))
2 years ago
if err != nil {
return nil, err
}
2 years ago
groupMap := utils.SliceToMap(groups, func(e *relation.GroupModel) string {
2 years ago
return e.GroupID
})
if ids := utils.Single(utils.Keys(groupMap), groupIDs); len(ids) > 0 {
return nil, constant.ErrGroupIDNotFound.Wrap(strings.Join(ids, ","))
2 years ago
}
2 years ago
groupMemberNumMap, err := s.GroupInterface.MapGroupMemberNum(ctx, groupIDs)
2 years ago
if err != nil {
return nil, err
3 years ago
}
2 years ago
owners, err := s.GroupInterface.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 *relation.GroupMemberModel) string {
2 years ago
return e.GroupID
})
2 years ago
resp.GroupRequests = utils.Slice(groupRequests, func(e *relation.GroupRequestModel) *open_im_sdk.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 {
return nil, constant.ErrArgs.Wrap("groupID is empty")
}
2 years ago
groups, err := s.GroupInterface.FindGroup(ctx, req.GroupIDs)
2 years ago
if err != nil {
return nil, err
}
2 years ago
groupMemberNumMap, err := s.GroupInterface.MapGroupMemberNum(ctx, req.GroupIDs)
2 years ago
if err != nil {
return nil, err
3 years ago
}
2 years ago
owners, err := s.GroupInterface.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 *relation.GroupMemberModel) string {
2 years ago
return e.GroupID
})
2 years ago
resp.GroupInfos = utils.Slice(groups, func(e *relation.GroupModel) *open_im_sdk.GroupInfo {
2 years ago
return DbToPbGroupInfo(e, ownerMap[e.GroupID].UserID, uint32(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) {
return nil, constant.ErrArgs.Wrap("HandleResult unknown")
3 years ago
}
if !token_verify.IsAppManagerUid(ctx) {
2 years ago
groupMember, err := s.GroupInterface.TakeGroupMember(ctx, req.GroupID, req.FromUserID)
if err != nil {
return nil, err
}
if !(groupMember.RoleLevel == constant.GroupOwner || groupMember.RoleLevel == constant.GroupAdmin) {
return nil, constant.ErrNoPermission.Wrap("no group owner or admin")
}
}
2 years ago
group, err := s.GroupInterface.TakeGroup(ctx, req.GroupID)
if err != nil {
2 years ago
return nil, err
}
groupRequest, err := s.GroupInterface.TakeGroupRequest(ctx, req.GroupID, req.FromUserID)
if err != nil {
2 years ago
return nil, err
}
if groupRequest.HandleResult != 0 {
return nil, constant.ErrArgs.Wrap("group request already processed")
}
2 years ago
if _, err := s.GroupInterface.TakeGroupMember(ctx, req.GroupID, req.FromUserID); err != nil {
if !IsNotFound(err) {
2 years ago
return nil, err
}
} else {
return nil, constant.ErrArgs.Wrap("already in group")
}
user, err := GetPublicUserInfoOne(ctx, req.FromUserID)
if err != nil {
return nil, err
}
2 years ago
var member *relation.GroupMemberModel
if req.HandleResult == constant.GroupResponseAgree {
2 years ago
member = &relation.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,
OperatorUserID: tracelog.GetOpUserID(ctx),
Ex: groupRequest.Ex,
}
if err = CallbackBeforeMemberJoinGroup(ctx, tracelog.GetOperationID(ctx), member, group.Ex); err != nil {
2 years ago
return nil, err
3 years ago
}
}
if err := s.GroupInterface.HandlerGroupRequest(ctx, req.GroupID, req.FromUserID, req.HandledMsg, req.HandleResult, member); err != nil {
return nil, err
}
if req.HandleResult == constant.GroupResponseAgree {
3 years ago
chat.GroupApplicationAcceptedNotification(req)
3 years ago
chat.MemberEnterNotification(req)
3 years ago
} else if req.HandleResult == constant.GroupResponseRefuse {
chat.GroupApplicationRejectedNotification(req)
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 := GetPublicUserInfoOne(ctx, tracelog.GetOpUserID(ctx)); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
group, err := s.GroupInterface.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 {
return nil, constant.ErrDismissedAlready.Wrap()
3 years ago
}
2 years ago
if group.NeedVerification == constant.Directly {
if group.GroupType == constant.SuperGroup {
return nil, constant.ErrGroupTypeNotSupport.Wrap()
}
2 years ago
user, err := relation_conn.GetUserByUserID(tracelog.GetOpUserID(ctx))
2 years ago
if err != nil {
return nil, err
}
groupMember := PbToDbGroupMember(user)
groupMember.GroupID = group.GroupID
groupMember.RoleLevel = constant.GroupOrdinaryUsers
groupMember.OperatorUserID = tracelog.GetOpUserID(ctx)
groupMember.JoinSource = constant.JoinByInvitation
groupMember.InviterUserID = tracelog.GetOpUserID(ctx)
if err := CallbackBeforeMemberJoinGroup(ctx, tracelog.GetOperationID(ctx), groupMember, group.Ex); err != nil {
2 years ago
return nil, err
}
2 years ago
if err := s.GroupInterface.CreateGroup(ctx, nil, []*relation.GroupMemberModel{groupMember}); err != nil {
2 years ago
return nil, err
}
chat.MemberEnterDirectlyNotification(req.GroupID, tracelog.GetOpUserID(ctx), tracelog.GetOperationID(ctx))
return resp, nil
}
2 years ago
groupRequest := relation.GroupRequestModel{
2 years ago
UserID: tracelog.GetOpUserID(ctx),
ReqMsg: req.ReqMessage,
GroupID: req.GroupID,
JoinSource: req.JoinSource,
ReqTime: time.Now(),
}
2 years ago
if err := s.GroupInterface.CreateGroupRequest(ctx, []*relation.GroupRequestModel{&groupRequest}); err != nil {
2 years ago
return nil, err
3 years ago
}
chat.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.GroupInterface.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.GroupInterface.DeleteSuperGroupMember(ctx, req.GroupID, []string{tracelog.GetOpUserID(ctx)}); err != nil {
2 years ago
return nil, err
}
2 years ago
chat.SuperGroupNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(ctx), tracelog.GetOpUserID(ctx))
} else {
2 years ago
_, err := s.GroupInterface.TakeGroupMember(ctx, req.GroupID, tracelog.GetOpUserID(ctx))
2 years ago
if err != nil {
2 years ago
return nil, err
}
chat.MemberQuitNotification(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 !token_verify.IsAppManagerUid(ctx) {
2 years ago
groupMember, err := s.GroupInterface.TakeGroupMember(ctx, req.GroupInfoForSet.GroupID, tracelog.GetOpUserID(ctx))
2 years ago
if err != nil {
return nil, err
}
if !(groupMember.RoleLevel == constant.GroupOwner || groupMember.RoleLevel == constant.GroupAdmin) {
return nil, constant.ErrNoPermission.Wrap("no group owner or admin")
}
3 years ago
}
2 years ago
group, err := s.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(constant.ErrDismissedAlready, "")
3 years ago
}
data := UpdateGroupInfoMap(req.GroupInfoForSet)
2 years ago
if len(data) > 0 {
return resp, nil
}
2 years ago
if err := s.GroupInterface.UpdateGroup(ctx, group.GroupID, data); err != nil {
2 years ago
return nil, err
}
2 years ago
group, err = s.TakeGroup(ctx, req.GroupInfoForSet.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
chat.GroupInfoSetNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(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
GroupNotification(ctx, group.GroupID)
}
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.GroupInterface.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(constant.ErrDismissedAlready, "")
3 years ago
}
3 years ago
if req.OldOwnerUserID == req.NewOwnerUserID {
2 years ago
return nil, constant.ErrArgs.Wrap("OldOwnerUserID == NewOwnerUserID")
3 years ago
}
2 years ago
members, err := s.GroupInterface.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 *relation.GroupMemberModel) string { return e.UserID })
2 years ago
if ids := utils.Single([]string{req.OldOwnerUserID, req.NewOwnerUserID}, utils.Keys(memberMap)); len(ids) > 0 {
return nil, constant.ErrArgs.Wrap("user not in group " + strings.Join(ids, ","))
}
2 years ago
newOwner := memberMap[req.NewOwnerUserID]
if newOwner == nil {
return nil, constant.ErrArgs.Wrap("NewOwnerUser not in group " + req.NewOwnerUserID)
3 years ago
}
2 years ago
oldOwner := memberMap[req.OldOwnerUserID]
if token_verify.IsAppManagerUid(ctx) {
if oldOwner == nil {
2 years ago
oldOwner, err = s.GroupInterface.TakeGroupOwner(ctx, req.OldOwnerUserID)
2 years ago
if err != nil {
return nil, err
}
}
} else {
if oldOwner == nil {
return nil, constant.ErrArgs.Wrap("OldOwnerUser not in group " + req.NewOwnerUserID)
}
if oldOwner.GroupID != tracelog.GetOpUserID(ctx) {
return nil, constant.ErrNoPermission.Wrap(fmt.Sprintf("user %s no permission transfer group owner", tracelog.GetOpUserID(ctx)))
}
}
2 years ago
if err := s.GroupInterface.TransferGroupOwner(ctx, req.GroupID, req.OldOwnerUserID, req.NewOwnerUserID, newOwner.RoleLevel); err != nil {
2 years ago
return nil, err
3 years ago
}
3 years ago
chat.GroupOwnerTransferredNotification(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 []*relation.GroupModel
2 years ago
err error
)
2 years ago
if req.GroupID != "" {
2 years ago
groups, err = s.GroupInterface.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.GroupInterface.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 *relation.GroupModel) string {
2 years ago
return e.GroupID
})
2 years ago
ownerMembers, err := s.GroupInterface.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 *relation.GroupMemberModel) string {
2 years ago
return e.GroupID
})
if ids := utils.Single(groupIDs, utils.Keys(ownerMemberMap)); len(ids) > 0 {
return nil, constant.ErrDB.Wrap("group not owner " + strings.Join(ids, ","))
}
2 years ago
groupMemberNumMap, err := s.GroupInterface.MapGroupMemberNum(ctx, groupIDs)
2 years ago
if err != nil {
return nil, err
}
2 years ago
resp.Groups = utils.Slice(groups, func(group *relation.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.GroupInterface.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
resp.Members = utils.Slice(members, func(e *relation.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
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{}
user, err := GetPublicUserInfoOne(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.GroupInterface.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 *relation.GroupRequestModel) string {
return e.GroupID
}))
2 years ago
groups, err := s.GroupInterface.FindGroup(ctx, groupIDs)
if err != nil {
return nil, err
}
2 years ago
groupMap := utils.SliceToMap(groups, func(e *relation.GroupModel) string {
return e.GroupID
})
if ids := utils.Single(groupIDs, utils.Keys(groupMap)); len(ids) > 0 {
return nil, constant.ErrGroupIDNotFound.Wrap(strings.Join(ids, ","))
}
2 years ago
owners, err := s.GroupInterface.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 *relation.GroupMemberModel) string {
return e.GroupID
})
if ids := utils.Single(groupIDs, utils.Keys(ownerMap)); len(ids) > 0 {
return nil, constant.ErrData.Wrap("group no owner", strings.Join(ids, ","))
}
2 years ago
groupMemberNum, err := s.GroupInterface.MapGroupMemberNum(ctx, groupIDs)
if err != nil {
return nil, err
}
2 years ago
resp.GroupRequests = utils.Slice(requests, func(e *relation.GroupRequestModel) *open_im_sdk.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.GroupInterface.TakeGroup(ctx, req.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
if group.Status == constant.GroupStatusDismissed {
return nil, constant.ErrArgs.Wrap("group status is dismissed")
}
if err := s.GroupInterface.DismissGroup(ctx, req.GroupID); err != nil {
2 years ago
return nil, err
}
if group.GroupType == constant.SuperGroup {
if err := s.GroupInterface.DeleteSuperGroup(ctx, group.GroupID); err != nil {
2 years ago
return nil, err
3 years ago
}
} else {
chat.GroupDismissedNotification(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.GroupInterface.TakeGroupMember(ctx, req.GroupID, req.UserID)
if err != nil {
2 years ago
return nil, err
}
if !(tracelog.GetOpUserID(ctx) == req.UserID || token_verify.IsAppManagerUid(ctx)) {
2 years ago
opMember, err := s.GroupInterface.TakeGroupMember(ctx, req.GroupID, req.UserID)
if err != nil {
return nil, err
}
if opMember.RoleLevel <= member.RoleLevel {
return nil, constant.ErrNoPermission.Wrap(fmt.Sprintf("self RoleLevel %d target %d", opMember.RoleLevel, member.RoleLevel))
}
}
data := UpdateGroupMemberMutedTimeMap(time.Now().Add(time.Second * time.Duration(req.MutedSeconds)))
if err := s.GroupInterface.UpdateGroupMember(ctx, member.GroupID, member.UserID, data); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
chat.GroupMemberMutedNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(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.GroupInterface.TakeGroupMember(ctx, req.GroupID, req.UserID)
if err != nil {
2 years ago
return nil, err
}
if !(tracelog.GetOpUserID(ctx) == req.UserID || token_verify.IsAppManagerUid(ctx)) {
2 years ago
opMember, err := s.GroupInterface.TakeGroupMember(ctx, req.GroupID, tracelog.GetOpUserID(ctx))
if err != nil {
return nil, err
}
if opMember.RoleLevel <= member.RoleLevel {
return nil, constant.ErrNoPermission.Wrap(fmt.Sprintf("self RoleLevel %d target %d", opMember.RoleLevel, member.RoleLevel))
}
}
data := UpdateGroupMemberMutedTimeMap(time.Unix(0, 0))
if err := s.GroupInterface.UpdateGroupMember(ctx, member.GroupID, member.UserID, data); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
chat.GroupMemberCancelMutedNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(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
}
if err := s.GroupInterface.UpdateGroup(ctx, req.GroupID, UpdateGroupStatusMap(constant.GroupStatusMuted)); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
chat.GroupMutedNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(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
}
if err := s.GroupInterface.UpdateGroup(ctx, req.GroupID, UpdateGroupStatusMap(constant.GroupOk)); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
chat.GroupCancelMutedNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(ctx), req.GroupID)
2 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *groupServer) SetGroupMemberNickname(ctx context.Context, req *pbGroup.SetGroupMemberNicknameReq) (*pbGroup.SetGroupMemberNicknameResp, error) {
_, err := s.SetGroupMemberInfo(ctx, &pbGroup.SetGroupMemberInfoReq{GroupID: req.GroupID, UserID: req.UserID, Nickname: wrapperspb.String(req.Nickname)})
if err != nil {
2 years ago
return nil, err
}
return &pbGroup.SetGroupMemberNicknameResp{}, nil
}
2 years ago
func (s *groupServer) SetGroupMemberInfo(ctx context.Context, req *pbGroup.SetGroupMemberInfoReq) (*pbGroup.SetGroupMemberInfoResp, error) {
resp := &pbGroup.SetGroupMemberInfoResp{}
if req.RoleLevel != nil && req.RoleLevel.Value == constant.GroupOwner {
return nil, constant.ErrNoPermission.Wrap("set group owner")
}
2 years ago
group, err := s.GroupInterface.TakeGroup(ctx, req.GroupID)
if err != nil {
2 years ago
return nil, err
}
if group.Status == constant.GroupStatusDismissed {
return nil, constant.ErrArgs.Wrap("group status is dismissed")
}
2 years ago
member, err := s.GroupInterface.TakeGroupMember(ctx, req.GroupID, req.UserID)
if err != nil {
return nil, err
}
if tracelog.GetOpUserID(ctx) == req.UserID {
if req.RoleLevel != nil {
return nil, constant.ErrArgs.Wrap("update role level")
}
} else if !token_verify.IsAppManagerUid(ctx) {
2 years ago
opMember, err := s.GroupInterface.TakeGroupMember(ctx, req.GroupID, tracelog.GetOpUserID(ctx))
if err != nil {
return nil, err
}
if opMember.RoleLevel <= member.RoleLevel {
return nil, constant.ErrNoPermission.Wrap(fmt.Sprintf("self RoleLevel %d target %d", opMember.RoleLevel, member.RoleLevel))
}
}
if err := CallbackBeforeSetGroupMemberInfo(ctx, req); err != nil {
2 years ago
return nil, err
}
if err := s.GroupInterface.UpdateGroupMember(ctx, req.GroupID, req.UserID, UpdateGroupMemberMap(req)); err != nil {
return nil, err
2 years ago
}
chat.GroupMemberInfoSetNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(ctx), req.GroupID, req.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 {
return nil, constant.ErrArgs.Wrap("groupIDs empty")
}
if utils.Duplicate(req.GroupIDs) {
return nil, constant.ErrArgs.Wrap("groupIDs duplicate")
}
2 years ago
groups, err := s.GroupInterface.FindGroup(ctx, req.GroupIDs)
if err != nil {
return nil, err
}
2 years ago
groupUserMap, err := s.GroupInterface.MapGroupMemberUserID(ctx, req.GroupIDs)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
resp.GroupAbstractInfos = utils.Slice(groups, func(e *relation.GroupModel) *pbGroup.GroupAbstractInfo {
2 years ago
userIDs := groupUserMap[e.GroupID]
utils.Sort(userIDs, true)
bi := big.NewInt(0)
bi.SetString(utils.Md5(strings.Join(userIDs, ";;"))[0:8], 16)
return DbToPbGroupAbstractInfo(e.GroupID, int32(len(userIDs)), bi.Uint64())
})
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 {
return nil, constant.ErrArgs.Wrap("groupIDs empty")
}
2 years ago
members, err := s.GroupInterface.FindGroupMember(ctx, []string{req.UserID}, req.GroupIDs, nil)
if err != nil {
return nil, err
}
2 years ago
resp.Members = utils.Slice(members, func(e *relation.GroupMemberModel) *open_im_sdk.GroupMemberFullInfo {
return DbToPbGroupMembersCMSResp(e)
})
return resp, nil
}