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

1389 lines
49 KiB

package group
import (
"Open_IM/internal/rpc/fault_tolerant"
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
"Open_IM/pkg/common/db/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
"fmt"
2 years ago
"Open_IM/pkg/getcdv3"
pbConversation "Open_IM/pkg/proto/conversation"
pbGroup "Open_IM/pkg/proto/group"
2 years ago
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
pbUser "Open_IM/pkg/proto/user"
"Open_IM/pkg/utils"
"context"
2 years ago
"errors"
2 years ago
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
3 years ago
"net"
"strconv"
"strings"
"time"
2 years ago
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/wrapperspb"
2 years ago
"gorm.io/gorm"
)
3 years ago
type groupServer struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
controller.GroupInterface
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
//mysql init
2 years ago
var mysql relation.Mysql
var mongo unrelation.Mongo
var groupModel relation.Group
2 years ago
var redis cache.RedisClient
2 years ago
err := mysql.InitConn().AutoMigrateModel(&groupModel)
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
g.GroupInterface = controller.NewGroupController(groupModel.DB, 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
}
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
}
var userIDs []string
2 years ago
for _, userID := range req.InitMembers {
2 years ago
userIDs = append(userIDs, userID)
2 years ago
}
2 years ago
for _, userID := range req.AdminUserIDs {
userIDs = append(userIDs, userID)
2 years ago
}
2 years ago
userIDs = append(userIDs, req.OwnerUserID)
if utils.IsDuplicateID(userIDs) {
2 years ago
return nil, constant.ErrArgs.Wrap("group member repeated")
2 years ago
}
users, err := getUsersInfo(ctx, userIDs)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
userMap := make(map[string]*open_im_sdk.UserInfo)
2 years ago
for i, user := range users {
userMap[user.UserID] = users[i]
}
2 years ago
for _, userID := range userIDs {
if userMap[userID] == nil {
return nil, constant.ErrUserIDNotFound.Wrap(userID)
}
}
2 years ago
if err := callbackBeforeCreateGroup(ctx, req); err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
var group relation.Group
var groupMembers []*relation.GroupMember
utils.CopyStructFields(&group, req.GroupInfo)
group.GroupID = genGroupID(ctx, req.GroupInfo.GroupID)
if req.GroupInfo.GroupType == constant.SuperGroup {
if err := s.GroupInterface.CreateSuperGroup(ctx, group.GroupID, userIDs); err != nil {
return nil, err
}
} else {
2 years ago
joinGroup := func(userID string, roleLevel int32) error {
user := userMap[userID]
2 years ago
groupMember := &relation.GroupMember{GroupID: group.GroupID, RoleLevel: roleLevel, OperatorUserID: tracelog.GetOpUserID(ctx), JoinSource: constant.JoinByInvitation, InviterUserID: tracelog.GetOpUserID(ctx)}
2 years ago
utils.CopyStructFields(&groupMember, user)
2 years ago
if err := CallbackBeforeMemberJoinGroup(ctx, tracelog.GetOperationID(ctx), groupMember, group.Ex); err != nil {
2 years ago
return err
}
groupMembers = append(groupMembers, groupMember)
return nil
}
if err := joinGroup(req.OwnerUserID, constant.GroupOwner); err != nil {
return nil, err
}
2 years ago
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.Group{&group}, groupMembers); err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
utils.CopyStructFields(resp.GroupInfo, group)
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
groups, err := s.GroupInterface.GetJoinedGroupList(ctx, req.FromUserID)
if err != nil {
2 years ago
return nil, err
}
2 years ago
if len(groups) == 0 {
return resp, nil
}
var groupIDs []string
for _, group := range groups {
groupIDs = append(groupIDs, group.GroupID)
}
groupMemberNum, err := s.GroupInterface.GetGroupMemberNum(ctx, groupIDs)
if err != nil {
return nil, err
}
groupOwnerUserID, err := s.GroupInterface.GetGroupOwnerUserID(ctx, groupIDs)
if err != nil {
return nil, err
}
for _, group := range groups {
if group.Status == constant.GroupStatusDismissed || group.GroupType == constant.SuperGroup {
continue
}
2 years ago
var groupNode open_im_sdk.GroupInfo
utils.CopyStructFields(&groupNode, group)
2 years ago
groupNode.MemberCount = uint32(groupMemberNum[group.GroupID])
groupNode.OwnerUserID = groupOwnerUserID[group.GroupID]
groupNode.CreateTime = group.CreateTime.UnixMilli()
groupNode.NotificationUpdateTime = group.NotificationUpdateTime.UnixMilli()
2 years ago
resp.Groups = append(resp.Groups, &groupNode)
}
2 years ago
resp.Total = int32(len(resp.Groups))
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.IsDuplicateID(req.InvitedUserIDs) {
2 years ago
return nil, constant.ErrArgs.Wrap("userID duplicate")
}
group, err := s.GroupInterface.TakeGroupByID(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()
}
members, err := s.GroupInterface.GetGroupMemberList(ctx, group.GroupID)
if err != nil {
return nil, err
}
memberMap := make(map[string]*relation.GroupMember)
for i, member := range members {
memberMap[member.GroupID] = members[i]
}
2 years ago
for _, userID := range req.InvitedUserIDs {
2 years ago
if _, ok := memberMap[userID]; ok {
return nil, constant.ErrArgs.Wrap("user in group " + userID)
}
}
2 years ago
userMap, err := getUserMap(ctx, req.InvitedUserIDs)
2 years ago
if err != nil {
2 years ago
return nil, err
}
2 years ago
for _, userID := range req.InvitedUserIDs {
2 years ago
if _, ok := userMap[userID]; !ok {
return nil, constant.ErrUserIDNotFound.Wrap(userID)
}
}
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) {
var requests []*relation.GroupRequest
2 years ago
for _, userID := range req.InvitedUserIDs {
2 years ago
requests = append(requests, &relation.GroupRequest{
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.AddUserToSuperGroup(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.GroupMember
2 years ago
for _, userID := range req.InvitedUserIDs {
2 years ago
user := userMap[userID]
var member relation.GroupMember
utils.CopyStructFields(&member, user)
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, tracelog.GetOperationID(ctx), &member, group.Ex); err != nil {
2 years ago
return nil, err
}
2 years ago
groupMembers = append(groupMembers, &member)
}
2 years ago
if err := s.GroupInterface.CreateGroupMember(ctx, groupMembers); err != nil {
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.TakeGroupByID(ctx, req.GroupID)
if err != nil {
2 years ago
return nil, err
}
2 years ago
if group.GroupType != constant.SuperGroup {
members, err := s.GroupInterface.GetGroupMemberList(ctx, req.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
var userIDs []string
for _, member := range members {
userIDs = append(userIDs, member.UserID)
}
for _, member := range members {
3 years ago
var node open_im_sdk.GroupMemberFullInfo
2 years ago
utils.CopyStructFields(&node, member)
2 years ago
resp.Members = append(resp.Members, &node)
3 years ago
}
3 years ago
}
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
members, err := s.GroupInterface.GetGroupMemberFilterList(ctx, req.GroupID, req.Filter, req.Pagination.PageNumber, req.Pagination.ShowNumber)
if err != nil {
2 years ago
return nil, err
}
2 years ago
for _, member := range members {
var info open_im_sdk.GroupMemberFullInfo
utils.CopyStructFields(&info, &member)
resp.Members = append(resp.Members, &info)
}
2 years ago
return resp, nil
}
func (s *groupServer) getGroupUserLevel(groupID, userID string) (int, error) {
opFlag := 0
if !token_verify.IsManagerUserID(userID) {
2 years ago
opInfo, err := relation.GetGroupMemberInfoByGroupIDAndUserID(groupID, userID)
if err != nil {
return opFlag, utils.Wrap(err, "")
}
if opInfo.RoleLevel == constant.GroupOrdinaryUsers {
opFlag = 0
} else if opInfo.RoleLevel == constant.GroupOwner {
2 years ago
opFlag = 2 // owner
} else {
2 years ago
opFlag = 3 // admin
}
} else {
2 years ago
opFlag = 1 // app manager
}
return opFlag, 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.TakeGroupByID(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 {
if err := s.GroupInterface.DelSuperGroupMember(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.FindGroupMembersByID(ctx, req.GroupID, append(req.KickedUserIDs, opUserID))
if err != nil {
2 years ago
return nil, err
}
2 years ago
memberMap := make(map[string]*relation.GroupMember)
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")
}
}
if err := s.GroupInterface.DelGroupMember(ctx, group.GroupID, req.KickedUserIDs); err != nil {
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
members, err := s.GroupInterface.GetGroupMemberListByUserID(ctx, req.GroupID, req.Members)
2 years ago
if err != nil {
return nil, err
}
for _, member := range members {
2 years ago
var memberNode open_im_sdk.GroupMemberFullInfo
2 years ago
utils.CopyStructFields(&memberNode, member)
memberNode.JoinTime = member.JoinTime.UnixMilli()
2 years ago
resp.Members = append(resp.Members, &memberNode)
}
return resp, nil
}
3 years ago
func FillGroupInfoByGroupID(operationID, groupID string, groupInfo *open_im_sdk.GroupInfo) error {
2 years ago
group, err := relation.TakeGroupInfoByGroupID(groupID)
if err != nil {
log.Error(operationID, "TakeGroupInfoByGroupID failed ", err.Error(), groupID)
return utils.Wrap(err, "")
}
if group.Status == constant.GroupStatusDismissed {
log.Debug(operationID, " group constant.GroupStatusDismissed ", group.GroupID)
2 years ago
return utils.Wrap(constant.ErrDismissedAlready, "")
}
return utils.Wrap(cp.GroupDBCopyOpenIM(groupInfo, group), "")
}
func FillPublicUserInfoByUserID(operationID, userID string, userInfo *open_im_sdk.PublicUserInfo) error {
2 years ago
user, err := relation.TakeUserByUserID(userID)
3 years ago
if err != nil {
log.Error(operationID, "TakeUserByUserID failed ", err.Error(), userID)
return utils.Wrap(err, "")
3 years ago
}
cp.UserDBCopyOpenIMPublicUser(userInfo, user)
return 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
groupRequests, err := s.GroupInterface.GetGroupRecvApplicationList(ctx, req.FromUserID)
if err != nil {
2 years ago
return nil, err
}
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)
}
userMap, err := getUserMap(ctx, userIDs)
if err != nil {
return nil, err
}
for _, userID := range userIDs {
if _, ok := userMap[userID]; !ok {
return nil, constant.ErrUserIDNotFound.Wrap(userID)
}
}
groups, err := s.GroupInterface.FindGroupsByID(ctx, groupIDs)
if err != nil {
return nil, err
}
groupMap := make(map[string]*relation.Group)
for i, group := range groups {
groupMap[group.GroupID] = groups[i]
}
for _, gr := range groupRequests {
groupRequest := open_im_sdk.GroupRequest{UserInfo: &open_im_sdk.PublicUserInfo{}, GroupInfo: &open_im_sdk.GroupInfo{}}
utils.CopyStructFields(&groupRequest, gr)
getUserMap()
}
2 years ago
var errResult error
2 years ago
tracelog.SetCtxInfo(ctx, "GetRecvGroupApplicationList", nil, " FromUserID: ", req.FromUserID, "GroupApplicationList: ", groupRequests)
for _, v := range groupRequests {
3 years ago
node := open_im_sdk.GroupRequest{UserInfo: &open_im_sdk.PublicUserInfo{}, GroupInfo: &open_im_sdk.GroupInfo{}}
2 years ago
err := FillGroupInfoByGroupID(tracelog.GetOperationID(ctx), v.GroupID, node.GroupInfo)
3 years ago
if err != nil {
2 years ago
if !errors.Is(errors.Unwrap(err), constant.ErrDismissedAlready) {
errResult = err
}
continue
}
2 years ago
tracelog.SetCtxInfo(ctx, "FillGroupInfoByGroupID ", nil, " groupID: ", v.GroupID, " groupInfo: ", node.GroupInfo)
2 years ago
err = FillPublicUserInfoByUserID(tracelog.GetOperationID(ctx), v.UserID, node.UserInfo)
3 years ago
if err != nil {
errResult = err
3 years ago
continue
}
3 years ago
cp.GroupRequestDBCopyOpenIM(&node, &v)
3 years ago
resp.GroupRequestList = append(resp.GroupRequestList, &node)
3 years ago
}
if errResult != nil && len(resp.GroupRequestList) == 0 {
2 years ago
return nil, err
}
2 years ago
tracelog.SetRpcRespInfo(ctx, utils.GetSelfFuncName(), resp.String())
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{}
3 years ago
groupsInfoList := make([]*open_im_sdk.GroupInfo, 0)
3 years ago
for _, groupID := range req.GroupIDList {
groupInfoFromRedis, err := rocksCache.GetGroupInfoFromCache(ctx, groupID)
3 years ago
if err != nil {
continue
}
3 years ago
var groupInfo open_im_sdk.GroupInfo
2 years ago
cp.GroupDBCopyOpenIM(&groupInfo, groupInfoFromRedis)
groupInfo.NeedVerification = groupInfoFromRedis.NeedVerification
3 years ago
groupsInfoList = append(groupsInfoList, &groupInfo)
}
resp.GroupInfoList = groupsInfoList
2 years ago
return resp, nil
3 years ago
}
func CheckPermission(ctx context.Context, groupID string, userID string) (err error) {
defer func() {
2 years ago
tracelog.SetCtxInfo(ctx, utils.GetSelfFuncName(), err, "groupID", groupID, "userID", userID)
}()
2 years ago
if !token_verify.IsManagerUserID(userID) && !relation.IsGroupOwnerAdmin(groupID, userID) {
2 years ago
return utils.Wrap(constant.ErrNoPermission, utils.GetSelfFuncName())
}
return nil
}
2 years ago
func (s *groupServer) GroupApplicationResponse(ctx context.Context, req *pbGroup.GroupApplicationResponseReq) (*pbGroup.GroupApplicationResponseResp, error) {
resp := &pbGroup.GroupApplicationResponseResp{}
2 years ago
2 years ago
if err := CheckPermission(ctx, req.GroupID, tracelog.GetOpUserID(ctx)); err != nil {
2 years ago
return nil, err
3 years ago
}
groupRequest := getDBGroupRequest(ctx, req)
2 years ago
if err := (&relation.GroupRequest{}).Update(ctx, []*relation.GroupRequest{groupRequest}); err != nil {
2 years ago
return nil, err
}
groupInfo, err := rocksCache.GetGroupInfoFromCache(ctx, req.GroupID)
if err != nil {
2 years ago
return nil, err
}
3 years ago
if req.HandleResult == constant.GroupResponseAgree {
member, err := getDBGroupMember(ctx, req.GroupID, req.FromUserID)
3 years ago
if err != nil {
2 years ago
return nil, err
}
2 years ago
err = CallbackBeforeMemberJoinGroup(ctx, tracelog.GetOperationID(ctx), member, groupInfo.Ex)
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
err = (&relation.GroupMember{}).Create(ctx, []*relation.GroupMember{member})
3 years ago
if err != nil {
2 years ago
return nil, err
}
2 years ago
etcdCacheConn, err := fault_tolerant.GetDefaultConn(config.Config.RpcRegisterName.OpenImCacheName, tracelog.GetOperationID(ctx))
if err != nil {
2 years ago
return nil, err
}
3 years ago
cacheClient := pbCache.NewCacheClient(etcdCacheConn)
2 years ago
cacheResp, err := cacheClient.DelGroupMemberIDListFromCache(context.Background(), &pbCache.DelGroupMemberIDListFromCacheReq{OperationID: tracelog.GetOperationID(ctx), GroupID: req.GroupID})
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
if cacheResp.CommonResp.ErrCode != 0 {
2 years ago
return nil, utils.Wrap(&constant.ErrInfo{
2 years ago
ErrCode: cacheResp.CommonResp.ErrCode,
ErrMsg: cacheResp.CommonResp.ErrMsg,
}, "")
3 years ago
}
2 years ago
_ = rocksCache.DelGroupMemberListHashFromCache(ctx, req.GroupID)
_ = rocksCache.DelJoinedGroupIDListFromCache(ctx, req.FromUserID)
_ = rocksCache.DelGroupMemberNumFromCache(ctx, req.GroupID)
3 years ago
chat.GroupApplicationAcceptedNotification(req)
3 years ago
chat.MemberEnterNotification(req)
3 years ago
} else if req.HandleResult == constant.GroupResponseRefuse {
chat.GroupApplicationRejectedNotification(req)
} else {
2 years ago
//return nil, utils.Wrap(constant.ErrArgs, "")
2 years ago
return nil, constant.ErrArgs.Wrap()
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
2 years ago
if _, err := relation.GetUserByUserID(tracelog.GetOpUserID(ctx)); err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
groupInfo, err := rocksCache.GetGroupInfoFromCache(ctx, req.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
if groupInfo.Status == constant.GroupStatusDismissed {
2 years ago
return nil, utils.Wrap(constant.ErrDismissedAlready, "")
3 years ago
}
if groupInfo.NeedVerification == constant.Directly {
if groupInfo.GroupType != constant.SuperGroup {
2 years ago
us, err := relation.GetUserByUserID(tracelog.GetOpUserID(ctx))
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
//to group member
2 years ago
groupMember := relation.GroupMember{GroupID: req.GroupID, RoleLevel: constant.GroupOrdinaryUsers, OperatorUserID: tracelog.GetOpUserID(ctx)}
2 years ago
utils.CopyStructFields(&groupMember, us)
2 years ago
if err := CallbackBeforeMemberJoinGroup(ctx, tracelog.GetOperationID(ctx), &groupMember, groupInfo.Ex); err != nil {
2 years ago
return nil, err
}
2 years ago
if err := s.DelGroupAndUserCache(ctx, req.GroupID, []string{tracelog.GetOpUserID(ctx)}); err != nil {
2 years ago
return nil, err
}
2 years ago
err = relation.InsertIntoGroupMember(groupMember)
if err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
var sessionType int
if groupInfo.GroupType == constant.NormalGroup {
sessionType = constant.GroupChatType
} else {
sessionType = constant.SuperGroupChatType
}
2 years ago
var reqPb pbUser.SetConversationReq
var c pbConversation.Conversation
2 years ago
reqPb.OperationID = tracelog.GetOperationID(ctx)
c.OwnerUserID = tracelog.GetOpUserID(ctx)
2 years ago
c.ConversationID = utils.GetConversationIDBySessionType(req.GroupID, sessionType)
c.ConversationType = int32(sessionType)
2 years ago
c.GroupID = req.GroupID
c.IsNotInGroup = false
c.UpdateUnreadCountTime = utils.GetCurrentTimestampByMill()
reqPb.Conversation = &c
2 years ago
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImUserName)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
client := pbUser.NewUserClient(etcdConn)
respPb, err := client.SetConversation(context.Background(), &reqPb)
2 years ago
tracelog.SetCtxInfo(ctx, "SetConversation", err, "req", reqPb, "resp", respPb)
2 years ago
chat.MemberEnterDirectlyNotification(req.GroupID, tracelog.GetOpUserID(ctx), tracelog.GetOperationID(ctx))
2 years ago
return resp, nil
} else {
2 years ago
constant.SetErrorForResp(constant.ErrGroupTypeNotSupport, resp.CommonResp)
2 years ago
return resp, nil
}
}
2 years ago
var groupRequest relation.GroupRequest
2 years ago
groupRequest.UserID = tracelog.GetOpUserID(ctx)
3 years ago
groupRequest.ReqMsg = req.ReqMessage
groupRequest.GroupID = req.GroupID
groupRequest.JoinSource = req.JoinSource
2 years ago
err = relation.InsertIntoGroupRequest(groupRequest)
3 years ago
if 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
2 years ago
groupInfo, err := relation.GetGroupInfoByGroupID(req.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
if groupInfo.GroupType != constant.SuperGroup {
2 years ago
_, err = rocksCache.GetGroupMemberInfoFromCache(ctx, req.GroupID, tracelog.GetOpUserID(ctx))
if err != nil {
2 years ago
return nil, err
}
2 years ago
if err := s.DelGroupAndUserCache(ctx, req.GroupID, []string{tracelog.GetOpUserID(ctx)}); err != nil {
2 years ago
return nil, err
}
2 years ago
err = relation.DeleteGroupMemberByGroupIDAndUserID(req.GroupID, tracelog.GetOpUserID(ctx))
if err != nil {
2 years ago
return nil, err
}
} else {
2 years ago
okUserIDList := []string{tracelog.GetOpUserID(ctx)}
if err := db.DB.RemoverUserFromSuperGroup(req.GroupID, okUserIDList); err != nil {
2 years ago
return nil, err
}
}
3 years ago
if groupInfo.GroupType != constant.SuperGroup {
2 years ago
_ = rocksCache.DelGroupMemberInfoFromCache(ctx, req.GroupID, tracelog.GetOpUserID(ctx))
chat.MemberQuitNotification(req)
} else {
2 years ago
_ = rocksCache.DelJoinedSuperGroupIDListFromCache(ctx, tracelog.GetOpUserID(ctx))
2 years ago
_ = rocksCache.DelGroupMemberListHashFromCache(ctx, req.GroupID)
2 years ago
chat.SuperGroupNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(ctx), tracelog.GetOpUserID(ctx))
}
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
3 years ago
if !hasAccess(req) {
2 years ago
return nil, utils.Wrap(constant.ErrIdentity, "")
3 years ago
}
2 years ago
group, err := relation.GetGroupInfoByGroupID(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
}
3 years ago
var changedType int32
2 years ago
groupName := ""
notification := ""
introduction := ""
faceURL := ""
2 years ago
if group.GroupName != req.GroupInfoForSet.GroupName && req.GroupInfoForSet.GroupName != "" {
3 years ago
changedType = 1
2 years ago
groupName = req.GroupInfoForSet.GroupName
3 years ago
}
2 years ago
if group.Notification != req.GroupInfoForSet.Notification && req.GroupInfoForSet.Notification != "" {
3 years ago
changedType = changedType | (1 << 1)
2 years ago
notification = req.GroupInfoForSet.Notification
3 years ago
}
2 years ago
if group.Introduction != req.GroupInfoForSet.Introduction && req.GroupInfoForSet.Introduction != "" {
3 years ago
changedType = changedType | (1 << 2)
2 years ago
introduction = req.GroupInfoForSet.Introduction
3 years ago
}
2 years ago
if group.FaceURL != req.GroupInfoForSet.FaceURL && req.GroupInfoForSet.FaceURL != "" {
3 years ago
changedType = changedType | (1 << 3)
2 years ago
faceURL = req.GroupInfoForSet.FaceURL
3 years ago
}
2 years ago
if req.GroupInfoForSet.NeedVerification != nil {
changedType = changedType | (1 << 4)
m := make(map[string]interface{})
2 years ago
m["need_verification"] = req.GroupInfoForSet.NeedVerification.Value
2 years ago
if err := relation.UpdateGroupInfoDefaultZero(req.GroupInfoForSet.GroupID, m); err != nil {
2 years ago
return nil, err
}
}
2 years ago
if req.GroupInfoForSet.LookMemberInfo != nil {
changedType = changedType | (1 << 5)
m := make(map[string]interface{})
m["look_member_info"] = req.GroupInfoForSet.LookMemberInfo.Value
2 years ago
if err := relation.UpdateGroupInfoDefaultZero(req.GroupInfoForSet.GroupID, m); err != nil {
2 years ago
return nil, err
2 years ago
}
}
if req.GroupInfoForSet.ApplyMemberFriend != nil {
changedType = changedType | (1 << 6)
m := make(map[string]interface{})
m["apply_member_friend"] = req.GroupInfoForSet.ApplyMemberFriend.Value
2 years ago
if err := relation.UpdateGroupInfoDefaultZero(req.GroupInfoForSet.GroupID, m); err != nil {
2 years ago
return nil, err
2 years ago
}
}
3 years ago
//only administrators can set group information
2 years ago
var groupInfo relation.Group
2 years ago
utils.CopyStructFields(&groupInfo, req.GroupInfoForSet)
if req.GroupInfoForSet.Notification != "" {
2 years ago
groupInfo.NotificationUserID = tracelog.GetOpUserID(ctx)
groupInfo.NotificationUpdateTime = time.Now()
}
2 years ago
if err := rocksCache.DelGroupInfoFromCache(ctx, req.GroupInfoForSet.GroupID); err != nil {
2 years ago
return nil, err
}
2 years ago
err = relation.SetGroupInfo(groupInfo)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
if changedType != 0 {
2 years ago
chat.GroupInfoSetNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(ctx), req.GroupInfoForSet.GroupID, groupName, notification, introduction, faceURL, req.GroupInfoForSet.NeedVerification)
3 years ago
}
2 years ago
if req.GroupInfoForSet.Notification != "" {
//get group member user id
2 years ago
getGroupMemberIDListFromCacheReq := &pbCache.GetGroupMemberIDListFromCacheReq{OperationID: tracelog.GetOperationID(ctx), GroupID: req.GroupInfoForSet.GroupID}
2 years ago
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName)
2 years ago
if err != nil {
2 years ago
return nil, err
}
client := pbCache.NewCacheClient(etcdConn)
2 years ago
cacheResp, err := client.GetGroupMemberIDListFromCache(ctx, getGroupMemberIDListFromCacheReq)
if err != nil {
2 years ago
return nil, err
}
2 years ago
if err = constant.CommonResp2Err(cacheResp.CommonResp); err != nil {
2 years ago
return nil, err
}
var conversationReq pbConversation.ModifyConversationFieldReq
conversation := pbConversation.Conversation{
2 years ago
OwnerUserID: tracelog.GetOpUserID(ctx),
2 years ago
ConversationID: utils.GetConversationIDBySessionType(req.GroupInfoForSet.GroupID, constant.GroupChatType),
ConversationType: constant.GroupChatType,
2 years ago
GroupID: req.GroupInfoForSet.GroupID,
}
conversationReq.Conversation = &conversation
2 years ago
conversationReq.OperationID = tracelog.GetOperationID(ctx)
conversationReq.FieldType = constant.FieldGroupAtType
conversation.GroupAtType = constant.GroupNotification
conversationReq.UserIDList = cacheResp.UserIDList
2 years ago
nEtcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImConversationName)
2 years ago
if err != nil {
2 years ago
return nil, err
}
nClient := pbConversation.NewConversationClient(nEtcdConn)
conversationReply, err := nClient.ModifyConversationField(context.Background(), &conversationReq)
2 years ago
tracelog.SetCtxInfo(ctx, "ModifyConversationField", err, "req", &conversationReq, "resp", conversationReply)
}
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{}
3 years ago
2 years ago
groupInfo, err := relation.GetGroupInfoByGroupID(req.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
if groupInfo.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, err
3 years ago
}
2 years ago
err = rocksCache.DelGroupMemberInfoFromCache(ctx, req.GroupID, req.NewOwnerUserID)
if err != nil {
2 years ago
return nil, err
}
2 years ago
err = rocksCache.DelGroupMemberInfoFromCache(ctx, req.GroupID, req.OldOwnerUserID)
if err != nil {
2 years ago
return nil, err
}
2 years ago
groupMemberInfo := relation.GroupMember{GroupID: req.GroupID, UserID: req.OldOwnerUserID, RoleLevel: constant.GroupOrdinaryUsers}
err = relation.UpdateGroupMemberInfo(groupMemberInfo)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
groupMemberInfo = relation.GroupMember{GroupID: req.GroupID, UserID: req.NewOwnerUserID, RoleLevel: constant.GroupOwner}
err = relation.UpdateGroupMemberInfo(groupMemberInfo)
3 years ago
if 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) {
resp := &pbGroup.GetGroupsResp{
2 years ago
Groups: []*pbGroup.CMSGroup{},
2 years ago
Pagination: &open_im_sdk.ResponsePagination{CurrentPage: req.Pagination.PageNumber, ShowNumber: req.Pagination.ShowNumber},
3 years ago
}
2 years ago
2 years ago
if req.GroupID != "" {
2 years ago
groupInfoDB, err := relation.GetGroupInfoByGroupID(req.GroupID)
3 years ago
if err != nil {
2 years ago
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp, nil
}
2 years ago
return nil, err
3 years ago
}
2 years ago
resp.GroupNum = 1
groupInfo := &open_im_sdk.GroupInfo{}
utils.CopyStructFields(groupInfo, groupInfoDB)
2 years ago
groupMember, err := relation.GetGroupOwnerInfoByGroupID(req.GroupID)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
2 years ago
memberNum, err := relation.GetGroupMembersCount(req.GroupID, "")
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
groupInfo.MemberCount = uint32(memberNum)
2 years ago
groupInfo.CreateTime = uint32(groupInfoDB.CreateTime.Unix())
2 years ago
resp.Groups = append(resp.Groups, &pbGroup.CMSGroup{GroupInfo: groupInfo, GroupOwnerUserName: groupMember.Nickname, GroupOwnerUserID: groupMember.UserID})
2 years ago
} else {
2 years ago
groups, count, err := relation.GetGroupsByName(req.GroupName, req.Pagination.PageNumber, req.Pagination.ShowNumber)
3 years ago
if err != nil {
2 years ago
tracelog.SetCtxInfo(ctx, "GetGroupsByName", err, "GroupName", req.GroupName, "PageNumber", req.Pagination.PageNumber, "ShowNumber", req.Pagination.ShowNumber)
2 years ago
}
for _, v := range groups {
group := &pbGroup.CMSGroup{GroupInfo: &open_im_sdk.GroupInfo{}}
utils.CopyStructFields(group.GroupInfo, v)
2 years ago
groupMember, err := relation.GetGroupOwnerInfoByGroupID(v.GroupID)
2 years ago
if err != nil {
2 years ago
tracelog.SetCtxInfo(ctx, "GetGroupOwnerInfoByGroupID", err, "GroupID", v.GroupID)
2 years ago
continue
}
group.GroupInfo.CreateTime = uint32(v.CreateTime.Unix())
group.GroupOwnerUserID = groupMember.UserID
group.GroupOwnerUserName = groupMember.Nickname
2 years ago
resp.Groups = append(resp.Groups, group)
2 years ago
}
2 years ago
resp.GroupNum = int32(count)
3 years ago
}
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
groupMembers, err := relation.GetGroupMembersByGroupIdCMS(req.GroupID, req.UserName, req.Pagination.ShowNumber, req.Pagination.PageNumber)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
groupMembersCount, err := relation.GetGroupMembersCount(req.GroupID, req.UserName)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
log.NewInfo(tracelog.GetOperationID(ctx), groupMembersCount)
2 years ago
resp.MemberNums = int32(groupMembersCount)
3 years ago
for _, groupMember := range groupMembers {
2 years ago
member := open_im_sdk.GroupMemberFullInfo{}
utils.CopyStructFields(&member, groupMember)
2 years ago
member.JoinTime = int32(groupMember.JoinTime.Unix())
member.MuteEndTime = uint32(groupMember.MuteEndTime.Unix())
2 years ago
resp.Members = append(resp.Members, &member)
3 years ago
}
3 years ago
resp.Pagination = &open_im_sdk.ResponsePagination{
CurrentPage: req.Pagination.PageNumber,
ShowNumber: req.Pagination.ShowNumber,
}
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
groupRequests, err := relation.GetUserReqGroupByUserID(req.UserID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
for _, groupReq := range groupRequests {
node := open_im_sdk.GroupRequest{UserInfo: &open_im_sdk.PublicUserInfo{}, GroupInfo: &open_im_sdk.GroupInfo{}}
2 years ago
group, err := relation.GetGroupInfoByGroupID(groupReq.GroupID)
3 years ago
if err != nil {
2 years ago
tracelog.SetCtxInfo(ctx, "GetGroupInfoByGroupID", err, "GroupID", groupReq.GroupID)
3 years ago
continue
}
2 years ago
user, err := relation.GetUserByUserID(groupReq.UserID)
3 years ago
if err != nil {
2 years ago
tracelog.SetCtxInfo(ctx, "GetUserByUserID", err, "UserID", groupReq.UserID)
3 years ago
continue
}
cp.GroupRequestDBCopyOpenIM(&node, &groupReq)
cp.UserDBCopyOpenIMPublicUser(node.UserInfo, user)
cp.GroupDBCopyOpenIM(node.GroupInfo, group)
resp.GroupRequestList = append(resp.GroupRequestList, &node)
}
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{}
2 years ago
2 years ago
if !token_verify.IsManagerUserID(tracelog.GetOpUserID(ctx)) && !relation.IsGroupOwnerAdmin(req.GroupID, tracelog.GetOpUserID(ctx)) {
2 years ago
return nil, utils.Wrap(constant.ErrIdentity, "")
3 years ago
}
2 years ago
if err := rocksCache.DelGroupInfoFromCache(ctx, req.GroupID); err != nil {
2 years ago
return nil, err
}
2 years ago
if err := s.DelGroupAndUserCache(ctx, req.GroupID, nil); err != nil {
2 years ago
return nil, err
}
2 years ago
err := relation.OperateGroupStatus(req.GroupID, constant.GroupStatusDismissed)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
2 years ago
groupInfo, err := relation.GetGroupInfoByGroupID(req.GroupID)
if err != nil {
2 years ago
return nil, err
}
3 years ago
if groupInfo.GroupType != constant.SuperGroup {
2 years ago
memberList, err := relation.GetGroupMemberListByGroupID(req.GroupID)
if err != nil {
2 years ago
tracelog.SetCtxInfo(ctx, "GetGroupMemberListByGroupID", err, "groupID", req.GroupID)
}
3 years ago
//modify quitter conversation info
var reqPb pbUser.SetConversationReq
2 years ago
var c pbConversation.Conversation
3 years ago
for _, v := range memberList {
2 years ago
reqPb.OperationID = tracelog.GetOperationID(ctx)
3 years ago
c.OwnerUserID = v.UserID
c.ConversationID = utils.GetConversationIDBySessionType(req.GroupID, constant.GroupChatType)
c.ConversationType = constant.GroupChatType
c.GroupID = req.GroupID
c.IsNotInGroup = true
reqPb.Conversation = &c
2 years ago
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImUserName)
3 years ago
client := pbUser.NewUserClient(etcdConn)
respPb, err := client.SetConversation(context.Background(), &reqPb)
2 years ago
tracelog.SetCtxInfo(ctx, "SetConversation", err, "req", &reqPb, "resp", respPb)
3 years ago
}
2 years ago
err = relation.DeleteGroupMemberByGroupID(req.GroupID)
3 years ago
if err != nil {
2 years ago
return nil, err
3 years ago
}
chat.GroupDismissedNotification(req)
} else {
err = db.DB.DeleteSuperGroup(req.GroupID)
if err != nil {
2 years ago
return nil, err
3 years ago
}
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
2 years ago
opFlag, err := s.getGroupUserLevel(req.GroupID, tracelog.GetOpUserID(ctx))
if err != nil {
2 years ago
return nil, err
3 years ago
}
if opFlag == 0 {
2 years ago
return nil, err
}
2 years ago
mutedInfo, err := rocksCache.GetGroupMemberInfoFromCache(ctx, req.GroupID, req.UserID)
if err != nil {
2 years ago
return nil, err
}
if mutedInfo.RoleLevel == constant.GroupOwner && opFlag != 1 {
2 years ago
return nil, err
}
if mutedInfo.RoleLevel == constant.GroupAdmin && opFlag == 3 {
2 years ago
return nil, err
}
2 years ago
if err := rocksCache.DelGroupMemberInfoFromCache(ctx, req.GroupID, req.UserID); err != nil {
2 years ago
return nil, err
}
2 years ago
groupMemberInfo := relation.GroupMember{GroupID: req.GroupID, UserID: req.UserID}
3 years ago
groupMemberInfo.MuteEndTime = time.Unix(int64(time.Now().Second())+int64(req.MutedSeconds), time.Now().UnixNano())
2 years ago
err = relation.UpdateGroupMemberInfo(groupMemberInfo)
3 years ago
if 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
opFlag, err := s.getGroupUserLevel(req.GroupID, tracelog.GetOpUserID(ctx))
if err != nil {
2 years ago
return nil, err
}
if opFlag == 0 {
2 years ago
return nil, err
3 years ago
}
2 years ago
mutedInfo, err := relation.GetGroupMemberInfoByGroupIDAndUserID(req.GroupID, req.UserID)
if err != nil {
2 years ago
return nil, err
}
if mutedInfo.RoleLevel == constant.GroupOwner && opFlag != 1 {
2 years ago
return nil, err
}
if mutedInfo.RoleLevel == constant.GroupAdmin && opFlag == 3 {
2 years ago
return nil, err
}
2 years ago
if err := rocksCache.DelGroupMemberInfoFromCache(ctx, req.GroupID, req.UserID); err != nil {
2 years ago
return nil, err
}
2 years ago
groupMemberInfo := relation.GroupMember{GroupID: req.GroupID, UserID: req.UserID}
3 years ago
groupMemberInfo.MuteEndTime = time.Unix(0, 0)
2 years ago
err = relation.UpdateGroupMemberInfo(groupMemberInfo)
3 years ago
if 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{}
2 years ago
2 years ago
opFlag, err := s.getGroupUserLevel(req.GroupID, tracelog.GetOpUserID(ctx))
if err != nil {
2 years ago
return nil, err
}
if opFlag == 0 {
2 years ago
//errMsg := req.OperationID + "opFlag == 0 " + req.GroupID + req.OpUserID
//log.Error(req.OperationID, errMsg)
//return &pbGroup.MuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}}, nil
2 years ago
return nil, utils.Wrap(constant.ErrNoPermission, "")
}
2 years ago
//mutedInfo, err := relation.GetGroupMemberInfoByGroupIDAndUserID(req.GroupID, req.UserID)
//if err != nil {
// errMsg := req.OperationID + " GetGroupMemberInfoByGroupIDAndUserID failed " + req.GroupID + req.OpUserID + err.Error()
// return &pbGroup.MuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}}, nil
//}
//if mutedInfo.RoleLevel == constant.GroupOwner && opFlag != 1 {
// errMsg := req.OperationID + " mutedInfo.RoleLevel == constant.GroupOwner " + req.GroupID + req.OpUserID + err.Error()
// return &pbGroup.MuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}}, nil
//}
//if mutedInfo.RoleLevel == constant.GroupAdmin && opFlag == 3 {
// errMsg := req.OperationID + " mutedInfo.RoleLevel == constant.GroupAdmin " + req.GroupID + req.OpUserID + err.Error()
// return &pbGroup.MuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}}, nil
//}
2 years ago
if err := rocksCache.DelGroupInfoFromCache(ctx, req.GroupID); err != nil {
2 years ago
return nil, err
}
2 years ago
err = relation.OperateGroupStatus(req.GroupID, constant.GroupStatusMuted)
3 years ago
if 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{}
2 years ago
2 years ago
opFlag, err := s.getGroupUserLevel(req.GroupID, tracelog.GetOpUserID(ctx))
if err != nil {
2 years ago
return nil, err
}
if opFlag == 0 {
2 years ago
return nil, err
}
2 years ago
//mutedInfo, err := relation.GetGroupMemberInfoByGroupIDAndUserID(req.GroupID, req.)
//if err != nil {
// errMsg := req.OperationID + " GetGroupMemberInfoByGroupIDAndUserID failed " + req.GroupID + req.OpUserID + err.Error()
// return &pbGroup.CancelMuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}}, nil
//}
//if mutedInfo.RoleLevel == constant.GroupOwner && opFlag != 1 {
// errMsg := req.OperationID + " mutedInfo.RoleLevel == constant.GroupOwner " + req.GroupID + req.OpUserID + err.Error()
// return &pbGroup.CancelMuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}}, nil
//}
//if mutedInfo.RoleLevel == constant.GroupAdmin && opFlag == 3 {
// errMsg := req.OperationID + " mutedInfo.RoleLevel == constant.GroupAdmin " + req.GroupID + req.OpUserID + err.Error()
// return &pbGroup.CancelMuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}}, nil
//}
2 years ago
log.Debug(tracelog.GetOperationID(ctx), "UpdateGroupInfoDefaultZero ", req.GroupID, map[string]interface{}{"status": constant.GroupOk})
2 years ago
if err := rocksCache.DelGroupInfoFromCache(ctx, req.GroupID); err != nil {
2 years ago
return nil, err
}
2 years ago
err = relation.UpdateGroupInfoDefaultZero(req.GroupID, map[string]interface{}{"status": constant.GroupOk})
3 years ago
if 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) {
resp := &pbGroup.SetGroupMemberNicknameResp{}
2 years ago
if tracelog.GetOpUserID(ctx) != req.UserID && !token_verify.IsManagerUserID(tracelog.GetOpUserID(ctx)) {
2 years ago
return nil, utils.Wrap(constant.ErrIdentity, "")
}
cbReq := &pbGroup.SetGroupMemberInfoReq{
GroupID: req.GroupID,
UserID: req.UserID,
Nickname: &wrapperspb.StringValue{Value: req.Nickname},
}
2 years ago
if err := CallbackBeforeSetGroupMemberInfo(ctx, cbReq); err != nil {
2 years ago
return nil, err
}
nickName := cbReq.Nickname.Value
2 years ago
groupMemberInfo := relation.GroupMember{}
groupMemberInfo.UserID = req.UserID
groupMemberInfo.GroupID = req.GroupID
if nickName == "" {
2 years ago
userNickname, err := relation.GetUserNameByUserID(groupMemberInfo.UserID)
if err != nil {
2 years ago
return nil, err
}
groupMemberInfo.Nickname = userNickname
} else {
groupMemberInfo.Nickname = nickName
}
2 years ago
if err := rocksCache.DelGroupMemberInfoFromCache(ctx, req.GroupID, req.UserID); err != nil {
2 years ago
return nil, err
}
2 years ago
2 years ago
if err := relation.UpdateGroupMemberInfo(groupMemberInfo); err != nil {
2 years ago
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
func (s *groupServer) SetGroupMemberInfo(ctx context.Context, req *pbGroup.SetGroupMemberInfoReq) (*pbGroup.SetGroupMemberInfoResp, error) {
resp := &pbGroup.SetGroupMemberInfoResp{}
2 years ago
2 years ago
if err := rocksCache.DelGroupMemberInfoFromCache(ctx, req.GroupID, req.UserID); err != nil {
2 years ago
return nil, err
}
2 years ago
if err := CallbackBeforeSetGroupMemberInfo(ctx, req); err != nil {
2 years ago
return nil, err
}
2 years ago
groupMember := relation.GroupMember{
GroupID: req.GroupID,
UserID: req.UserID,
}
m := make(map[string]interface{})
if req.RoleLevel != nil {
m["role_level"] = req.RoleLevel.Value
}
if req.FaceURL != nil {
m["user_group_face_url"] = req.FaceURL.Value
}
if req.Nickname != nil {
m["nickname"] = req.Nickname.Value
}
if req.Ex != nil {
m["ex"] = req.Ex.Value
} else {
m["ex"] = nil
}
2 years ago
if err := relation.UpdateGroupMemberInfoByMap(groupMember, m); err != nil {
2 years ago
return nil, err
}
2 years ago
if req.RoleLevel != nil {
switch req.RoleLevel.Value {
case constant.GroupOrdinaryUsers:
2 years ago
//msg.GroupMemberRoleLevelChangeNotification(req.OperationID, req.OpUserID, req.GroupID, req.UserID, constant.GroupMemberSetToOrdinaryUserNotification)
2 years ago
chat.GroupMemberInfoSetNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(ctx), req.GroupID, req.UserID)
2 years ago
case constant.GroupAdmin, constant.GroupOwner:
2 years ago
//msg.GroupMemberRoleLevelChangeNotification(req.OperationID, req.OpUserID, req.GroupID, req.UserID, constant.GroupMemberSetToAdminNotification)
2 years ago
chat.GroupMemberInfoSetNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(ctx), req.GroupID, req.UserID)
2 years ago
}
} else {
2 years ago
chat.GroupMemberInfoSetNotification(tracelog.GetOperationID(ctx), tracelog.GetOpUserID(ctx), req.GroupID, req.UserID)
2 years ago
}
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{}
2 years ago
2 years ago
hashCode, err := rocksCache.GetGroupMemberListHashFromCache(ctx, req.GroupID)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
resp.GroupMemberListHash = hashCode
2 years ago
num, err := rocksCache.GetGroupMemberNumFromCache(ctx, req.GroupID)
2 years ago
if err != nil {
2 years ago
return nil, err
2 years ago
}
resp.GroupMemberNumber = int32(num)
return resp, nil
}
2 years ago
func (s *groupServer) DelGroupAndUserCache(ctx context.Context, groupID string, userIDList []string) error {
2 years ago
operationID := tracelog.GetOperationID(ctx)
if groupID != "" {
2 years ago
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName)
2 years ago
if err != nil {
return err
}
cacheClient := pbCache.NewCacheClient(etcdConn)
cacheResp, err := cacheClient.DelGroupMemberIDListFromCache(context.Background(), &pbCache.DelGroupMemberIDListFromCacheReq{
GroupID: groupID,
OperationID: operationID,
})
if err != nil {
log.NewError(operationID, "DelGroupMemberIDListFromCache rpc call failed ", err.Error())
return utils.Wrap(err, "")
}
2 years ago
err = constant.CommonResp2Err(cacheResp.CommonResp)
err = rocksCache.DelGroupMemberListHashFromCache(ctx, groupID)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), groupID, err.Error())
return utils.Wrap(err, "")
}
2 years ago
err = rocksCache.DelGroupMemberNumFromCache(ctx, groupID)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupID)
return utils.Wrap(err, "")
}
}
if userIDList != nil {
for _, userID := range userIDList {
2 years ago
err := rocksCache.DelJoinedGroupIDListFromCache(ctx, userID)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
return utils.Wrap(err, "")
}
}
}
return nil
}