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

1399 lines
63 KiB

package group
import (
3 years ago
chat "Open_IM/internal/rpc/msg"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db"
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
3 years ago
"Open_IM/pkg/common/http"
"Open_IM/pkg/common/log"
3 years ago
"Open_IM/pkg/common/token_verify"
3 years ago
cp "Open_IM/pkg/common/utils"
3 years ago
"Open_IM/pkg/grpc-etcdv3/getcdv3"
3 years ago
pbCache "Open_IM/pkg/proto/cache"
pbGroup "Open_IM/pkg/proto/group"
3 years ago
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
pbUser "Open_IM/pkg/proto/user"
"Open_IM/pkg/utils"
"context"
3 years ago
"net"
"strconv"
"strings"
"time"
3 years ago
"google.golang.org/grpc"
)
3 years ago
type groupServer struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
}
func NewGroupServer(port int) *groupServer {
3 years ago
log.NewPrivateLog(constant.LogFileName)
3 years ago
return &groupServer{
rpcPort: port,
rpcRegisterName: config.Config.RpcRegisterName.OpenImGroupName,
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
}
}
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)
3 years ago
defer listener.Close()
//grpc server
srv := grpc.NewServer()
defer srv.GracefulStop()
//Service registers with etcd
pbGroup.RegisterGroupServer(srv, s)
3 years ago
rpcRegisterIP := ""
if config.Config.RpcRegisterIP == "" {
rpcRegisterIP, err = utils.GetLocalIP()
if err != nil {
log.Error("", "GetLocalIP failed ", err.Error())
}
}
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())
3 years ago
return
}
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) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupReq) (*pbGroup.CreateGroupResp, error) {
3 years ago
log.NewInfo(req.OperationID, "CreateGroup, args ", req.String())
3 years ago
if !token_verify.CheckAccess(req.OpUserID, req.OwnerUserID) {
log.NewError(req.OperationID, "CheckAccess false ", req.OpUserID, req.OwnerUserID)
return &pbGroup.CreateGroupResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
3 years ago
}
3 years ago
canCreate, err := callbackBeforeCreateGroup(req)
if err != nil || !canCreate {
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "callbackBeforeCreateGroup failed")
3 years ago
}
}
3 years ago
//Time stamp + MD5 to generate group chat id
3 years ago
groupId := utils.Md5(strconv.FormatInt(time.Now().UnixNano(), 10))
3 years ago
//to group
3 years ago
groupInfo := db.Group{}
3 years ago
utils.CopyStructFields(&groupInfo, req.GroupInfo)
3 years ago
groupInfo.CreatorUserID = req.OpUserID
3 years ago
groupInfo.GroupID = groupId
3 years ago
err = imdb.InsertIntoGroup(groupInfo)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "InsertIntoGroup failed, ", err.Error(), groupInfo)
3 years ago
return &pbGroup.CreateGroupResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, http.WrapError(constant.ErrDB)
3 years ago
}
3 years ago
us, err := imdb.GetUserByUserID(req.OwnerUserID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetUserByUserID failed ", err.Error(), req.OwnerUserID)
3 years ago
return &pbGroup.CreateGroupResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, http.WrapError(constant.ErrDB)
3 years ago
}
3 years ago
//to group member
3 years ago
groupMember := db.GroupMember{GroupID: groupId, RoleLevel: constant.GroupOwner, OperatorUserID: req.OpUserID}
3 years ago
utils.CopyStructFields(&groupMember, us)
3 years ago
err = imdb.InsertIntoGroupMember(groupMember)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "InsertIntoGroupMember failed ", err.Error(), groupMember)
3 years ago
return &pbGroup.CreateGroupResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, http.WrapError(constant.ErrDB)
3 years ago
}
3 years ago
err = db.DB.AddGroupMember(groupId, req.OwnerUserID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "AddGroupMember failed ", err.Error(), groupId, req.OwnerUserID)
3 years ago
}
3 years ago
var okUserIDList []string
3 years ago
//to group member
3 years ago
for _, user := range req.InitMemberList {
3 years ago
us, err := imdb.GetUserByUserID(user.UserID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetUserByUserID failed ", err.Error(), user.UserID)
3 years ago
continue
}
3 years ago
if user.RoleLevel == constant.GroupOwner {
3 years ago
log.NewError(req.OperationID, "only one owner, failed ", user)
continue
}
3 years ago
groupMember.RoleLevel = user.RoleLevel
3 years ago
utils.CopyStructFields(&groupMember, us)
3 years ago
err = imdb.InsertIntoGroupMember(groupMember)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "InsertIntoGroupMember failed ", err.Error(), groupMember)
3 years ago
continue
3 years ago
}
3 years ago
3 years ago
okUserIDList = append(okUserIDList, user.UserID)
3 years ago
// mongoDB method
//err = db.DB.AddGroupMember(groupId, user.UserID)
//if err != nil {
// log.NewError(req.OperationID, "add mongo group member failed, db.DB.AddGroupMember failed ", err.Error())
//}
3 years ago
}
3 years ago
resp := &pbGroup.CreateGroupResp{GroupInfo: &open_im_sdk.GroupInfo{}}
3 years ago
group, err := imdb.GetGroupInfoByGroupID(groupId)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetGroupInfoByGroupID failed ", err.Error(), groupId)
3 years ago
resp.ErrCode = constant.ErrDB.ErrCode
resp.ErrMsg = constant.ErrDB.ErrMsg
3 years ago
return resp, nil
}
utils.CopyStructFields(resp.GroupInfo, group)
3 years ago
resp.GroupInfo.MemberCount, err = imdb.GetGroupMemberNumByGroupID(groupId)
if err != nil {
log.NewError(req.OperationID, "GetGroupMemberNumByGroupID failed ", err.Error(), groupId)
resp.ErrCode = constant.ErrDB.ErrCode
resp.ErrMsg = constant.ErrDB.ErrMsg
return resp, nil
}
3 years ago
resp.GroupInfo.OwnerUserID = req.OwnerUserID
3 years ago
okUserIDList = append(okUserIDList, req.OwnerUserID)
3 years ago
addGroupMemberToCacheReq := &pbCache.AddGroupMemberToCacheReq{
UserIDList: okUserIDList,
3 years ago
GroupID: groupId,
3 years ago
OperationID: req.OperationID,
}
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName)
cacheClient := pbCache.NewCacheClient(etcdConn)
cacheResp, err := cacheClient.AddGroupMemberToCache(context.Background(), addGroupMemberToCacheReq)
if err != nil {
3 years ago
log.NewError(req.OperationID, "AddGroupMemberToCache rpc call failed ", err.Error())
3 years ago
return &pbGroup.CreateGroupResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
}
if cacheResp.CommonResp.ErrCode != 0 {
3 years ago
log.NewError(req.OperationID, "AddGroupMemberToCache rpc logic call failed ", cacheResp.String())
3 years ago
return &pbGroup.CreateGroupResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
}
3 years ago
3 years ago
log.NewInfo(req.OperationID, "rpc CreateGroup return ", resp.String())
3 years ago
chat.GroupCreatedNotification(req.OperationID, req.OpUserID, groupId, okUserIDList)
3 years ago
return resp, nil
3 years ago
}
func (s *groupServer) GetJoinedGroupList(ctx context.Context, req *pbGroup.GetJoinedGroupListReq) (*pbGroup.GetJoinedGroupListResp, error) {
3 years ago
log.NewInfo(req.OperationID, "GetJoinedGroupList, args ", req.String())
if !token_verify.CheckAccess(req.OpUserID, req.FromUserID) {
log.NewError(req.OperationID, "CheckAccess false ", req.OpUserID, req.FromUserID)
3 years ago
return &pbGroup.GetJoinedGroupListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
}
3 years ago
//group list
3 years ago
joinedGroupList, err := imdb.GetJoinedGroupIDListByUserID(req.FromUserID)
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetJoinedGroupIDListByUserID failed ", err.Error(), req.FromUserID)
3 years ago
return &pbGroup.GetJoinedGroupListResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
}
var resp pbGroup.GetJoinedGroupListResp
for _, v := range joinedGroupList {
3 years ago
var groupNode open_im_sdk.GroupInfo
3 years ago
num, err := imdb.GetGroupMemberNumByGroupID(v)
3 years ago
owner, err2 := imdb.GetGroupOwnerInfoByGroupID(v)
group, err := imdb.GetGroupInfoByGroupID(v)
3 years ago
if num > 0 && owner != nil && err2 == nil && group != nil && err == nil {
utils.CopyStructFields(&groupNode, group)
3 years ago
groupNode.CreateTime = uint32(group.CreateTime.Unix())
3 years ago
groupNode.MemberCount = uint32(num)
3 years ago
groupNode.OwnerUserID = owner.UserID
resp.GroupList = append(resp.GroupList, &groupNode)
3 years ago
} else {
log.NewError(req.OperationID, "check nil ", num, owner, err, group)
continue
}
3 years ago
log.NewDebug(req.OperationID, "joinedGroup ", groupNode)
}
3 years ago
log.NewInfo(req.OperationID, "GetJoinedGroupList rpc return ", resp.String())
return &resp, nil
}
func (s *groupServer) InviteUserToGroup(ctx context.Context, req *pbGroup.InviteUserToGroupReq) (*pbGroup.InviteUserToGroupResp, error) {
3 years ago
log.NewInfo(req.OperationID, "InviteUserToGroup args ", req.String())
3 years ago
if !imdb.IsExistGroupMember(req.GroupID, req.OpUserID) && !token_verify.IsManagerUserID(req.OpUserID) {
3 years ago
log.NewError(req.OperationID, "no permission InviteUserToGroup ", req.GroupID, req.OpUserID)
3 years ago
return &pbGroup.InviteUserToGroupResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
}
3 years ago
groupInfo, err := imdb.GetGroupInfoByGroupID(req.GroupID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetGroupInfoByGroupID failed ", req.GroupID, err)
3 years ago
return &pbGroup.InviteUserToGroupResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
}
if groupInfo.Status == constant.GroupStatusDismissed {
errMsg := " group status is dismissed "
return &pbGroup.InviteUserToGroupResp{ErrCode: constant.ErrStatus.ErrCode, ErrMsg: errMsg}, nil
}
//
//from User: invite: applicant
//to user: invite: invited
var resp pbGroup.InviteUserToGroupResp
3 years ago
var okUserIDList []string
3 years ago
for _, v := range req.InvitedUserIDList {
var resultNode pbGroup.Id2Result
3 years ago
resultNode.UserID = v
resultNode.Result = 0
3 years ago
toUserInfo, err := imdb.GetUserByUserID(v)
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetUserByUserID failed ", err.Error(), v)
resultNode.Result = -1
3 years ago
resp.Id2ResultList = append(resp.Id2ResultList, &resultNode)
continue
}
if imdb.IsExistGroupMember(req.GroupID, v) {
3 years ago
log.NewError(req.OperationID, "IsExistGroupMember ", req.GroupID, v)
resultNode.Result = -1
3 years ago
resp.Id2ResultList = append(resp.Id2ResultList, &resultNode)
continue
}
3 years ago
var toInsertInfo db.GroupMember
3 years ago
utils.CopyStructFields(&toInsertInfo, toUserInfo)
toInsertInfo.GroupID = req.GroupID
3 years ago
toInsertInfo.RoleLevel = constant.GroupOrdinaryUsers
3 years ago
toInsertInfo.OperatorUserID = req.OpUserID
3 years ago
err = imdb.InsertIntoGroupMember(toInsertInfo)
if err != nil {
3 years ago
log.NewError(req.OperationID, "InsertIntoGroupMember failed ", req.GroupID, toUserInfo.UserID, toUserInfo.Nickname, toUserInfo.FaceURL)
resultNode.Result = -1
3 years ago
resp.Id2ResultList = append(resp.Id2ResultList, &resultNode)
continue
}
3 years ago
okUserIDList = append(okUserIDList, v)
3 years ago
err = db.DB.AddGroupMember(req.GroupID, toUserInfo.UserID)
if err != nil {
3 years ago
log.NewError(req.OperationID, "AddGroupMember failed ", err.Error(), req.GroupID, toUserInfo.UserID)
}
3 years ago
resp.Id2ResultList = append(resp.Id2ResultList, &resultNode)
}
var haveConUserID []string
conversations, err := imdb.GetConversationsByConversationIDMultipleOwner(okUserIDList, utils.GetConversationIDBySessionType(req.GroupID, constant.GroupChatType))
for _, v := range conversations {
haveConUserID = append(haveConUserID, v.OwnerUserID)
}
var reqPb pbUser.SetConversationReq
var c pbUser.Conversation
for _, v := range conversations {
reqPb.OperationID = req.OperationID
c.OwnerUserID = v.OwnerUserID
c.ConversationID = utils.GetConversationIDBySessionType(req.GroupID, constant.GroupChatType)
c.RecvMsgOpt = v.RecvMsgOpt
c.ConversationType = constant.GroupChatType
c.GroupID = req.GroupID
c.IsPinned = v.IsPinned
c.AttachedInfo = v.AttachedInfo
c.IsPrivateChat = v.IsPrivateChat
c.GroupAtType = v.GroupAtType
c.IsNotInGroup = false
c.Ex = v.Ex
reqPb.Conversation = &c
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
client := pbUser.NewUserClient(etcdConn)
respPb, err := client.SetConversation(context.Background(), &reqPb)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "SetConversation rpc failed, ", reqPb.String(), err.Error(), v.OwnerUserID)
} else {
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "SetConversation success", respPb.String(), v.OwnerUserID)
}
}
for _, v := range utils.DifferenceString(haveConUserID, okUserIDList) {
reqPb.OperationID = req.OperationID
c.OwnerUserID = v
c.ConversationID = utils.GetConversationIDBySessionType(req.GroupID, constant.GroupChatType)
c.ConversationType = constant.GroupChatType
c.GroupID = req.GroupID
c.IsNotInGroup = false
reqPb.Conversation = &c
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
client := pbUser.NewUserClient(etcdConn)
respPb, err := client.SetConversation(context.Background(), &reqPb)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "SetConversation rpc failed, ", reqPb.String(), err.Error(), v)
} else {
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "SetConversation success", respPb.String(), v)
}
}
3 years ago
addGroupMemberToCacheReq := &pbCache.AddGroupMemberToCacheReq{
UserIDList: okUserIDList,
GroupID: req.GroupID,
OperationID: req.OperationID,
}
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName)
cacheClient := pbCache.NewCacheClient(etcdConn)
cacheResp, err := cacheClient.AddGroupMemberToCache(context.Background(), addGroupMemberToCacheReq)
if err != nil {
3 years ago
log.NewError(req.OperationID, "AddGroupMemberToCache rpc call failed ", err.Error())
3 years ago
return &pbGroup.InviteUserToGroupResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
}
if cacheResp.CommonResp.ErrCode != 0 {
3 years ago
log.NewError(req.OperationID, "AddGroupMemberToCache rpc logic call failed ", cacheResp.String())
3 years ago
return &pbGroup.InviteUserToGroupResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
}
3 years ago
chat.MemberInvitedNotification(req.OperationID, req.GroupID, req.OpUserID, req.Reason, okUserIDList)
3 years ago
resp.ErrCode = 0
3 years ago
log.NewInfo(req.OperationID, "InviteUserToGroup rpc return ", resp.String())
return &resp, nil
}
func (s *groupServer) GetGroupAllMember(ctx context.Context, req *pbGroup.GetGroupAllMemberReq) (*pbGroup.GetGroupAllMemberResp, error) {
3 years ago
log.NewInfo(req.OperationID, "GetGroupAllMember, args ", req.String())
var resp pbGroup.GetGroupAllMemberResp
3 years ago
memberList, err := imdb.GetGroupMemberListByGroupID(req.GroupID)
if err != nil {
3 years ago
resp.ErrCode = constant.ErrDB.ErrCode
resp.ErrMsg = constant.ErrDB.ErrMsg
3 years ago
log.NewError(req.OperationID, "GetGroupMemberListByGroupID failed,", err.Error(), req.GroupID)
return &resp, nil
}
3 years ago
for _, v := range memberList {
3 years ago
log.Debug(req.OperationID, v)
3 years ago
var node open_im_sdk.GroupMemberFullInfo
cp.GroupMemberDBCopyOpenIM(&node, &v)
3 years ago
log.Debug(req.OperationID, "db value:", v.MuteEndTime, "seconds: ", v.MuteEndTime.Unix())
3 years ago
log.Debug(req.OperationID, "cp value: ", node)
resp.MemberList = append(resp.MemberList, &node)
3 years ago
}
3 years ago
log.NewInfo(req.OperationID, "GetGroupAllMember rpc return ", resp.String())
return &resp, nil
}
func (s *groupServer) GetGroupMemberList(ctx context.Context, req *pbGroup.GetGroupMemberListReq) (*pbGroup.GetGroupMemberListResp, error) {
3 years ago
log.NewInfo(req.OperationID, "GetGroupMemberList args ", req.String())
var resp pbGroup.GetGroupMemberListResp
3 years ago
memberList, err := imdb.GetGroupMemberByGroupID(req.GroupID, req.Filter, req.NextSeq, 30)
if err != nil {
3 years ago
resp.ErrCode = constant.ErrDB.ErrCode
3 years ago
resp.ErrMsg = constant.ErrDB.ErrMsg
3 years ago
log.NewError(req.OperationID, "GetGroupMemberByGroupId failed,", req.GroupID, req.Filter, req.NextSeq, 30)
return &resp, nil
}
for _, v := range memberList {
3 years ago
var node open_im_sdk.GroupMemberFullInfo
3 years ago
utils.CopyStructFields(&node, &v)
resp.MemberList = append(resp.MemberList, &node)
}
//db operate get db sorted by join time
if int32(len(memberList)) < 30 {
resp.NextSeq = 0
} else {
resp.NextSeq = req.NextSeq + int32(len(memberList))
}
3 years ago
resp.ErrCode = 0
3 years ago
log.NewInfo(req.OperationID, "GetGroupMemberList rpc return ", resp.String())
return &resp, nil
}
func (s *groupServer) KickGroupMember(ctx context.Context, req *pbGroup.KickGroupMemberReq) (*pbGroup.KickGroupMemberResp, error) {
3 years ago
log.NewInfo(req.OperationID, "KickGroupMember args ", req.String())
3 years ago
ownerList, err := imdb.GetOwnerManagerByGroupID(req.GroupID)
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetOwnerManagerByGroupId failed ", err.Error(), req.GroupID)
3 years ago
return &pbGroup.KickGroupMemberResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
}
//op is group owner?
var flag = 0
for _, v := range ownerList {
3 years ago
if v.UserID == req.OpUserID {
flag = 1
3 years ago
log.NewDebug(req.OperationID, "is group owner ", req.OpUserID, req.GroupID)
break
}
}
3 years ago
3 years ago
//op is app manager
if flag != 1 {
3 years ago
if token_verify.IsManagerUserID(req.OpUserID) {
flag = 1
3 years ago
log.NewDebug(req.OperationID, "is app manager ", req.OpUserID)
}
}
if flag != 1 {
3 years ago
log.NewError(req.OperationID, "failed, no access kick ", req.OpUserID)
3 years ago
return &pbGroup.KickGroupMemberResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
}
3 years ago
if len(req.KickedUserIDList) == 0 {
3 years ago
log.NewError(req.OperationID, "failed, kick list 0")
3 years ago
return &pbGroup.KickGroupMemberResp{ErrCode: constant.ErrArgs.ErrCode, ErrMsg: constant.ErrArgs.ErrMsg}, nil
}
3 years ago
3 years ago
groupOwnerUserID := ""
for _, v := range ownerList {
3 years ago
if v.RoleLevel == constant.GroupOwner {
3 years ago
groupOwnerUserID = v.UserID
}
}
3 years ago
var okUserIDList []string
//remove
var resp pbGroup.KickGroupMemberResp
3 years ago
for _, v := range req.KickedUserIDList {
3 years ago
//owner cant kicked
3 years ago
if v == groupOwnerUserID {
log.NewError(req.OperationID, "failed, can't kick owner ", v)
resp.Id2ResultList = append(resp.Id2ResultList, &pbGroup.Id2Result{UserID: v, Result: -1})
continue
}
3 years ago
err := imdb.RemoveGroupMember(req.GroupID, v)
if err != nil {
3 years ago
log.NewError(req.OperationID, "RemoveGroupMember failed ", err.Error(), req.GroupID, v)
resp.Id2ResultList = append(resp.Id2ResultList, &pbGroup.Id2Result{UserID: v, Result: -1})
} else {
3 years ago
log.NewDebug(req.OperationID, "kicked ", v)
3 years ago
resp.Id2ResultList = append(resp.Id2ResultList, &pbGroup.Id2Result{UserID: v, Result: 0})
3 years ago
okUserIDList = append(okUserIDList, v)
}
3 years ago
//err = db.DB.DelGroupMember(req.GroupID, v)
//if err != nil {
// log.NewError(req.OperationID, "DelGroupMember failed ", err.Error(), req.GroupID, v)
//}
}
var reqPb pbUser.SetConversationReq
var c pbUser.Conversation
for _, v := range okUserIDList {
reqPb.OperationID = req.OperationID
c.OwnerUserID = v
c.ConversationID = utils.GetConversationIDBySessionType(req.GroupID, constant.GroupChatType)
c.ConversationType = constant.GroupChatType
c.GroupID = req.GroupID
c.IsNotInGroup = true
reqPb.Conversation = &c
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
client := pbUser.NewUserClient(etcdConn)
respPb, err := client.SetConversation(context.Background(), &reqPb)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "SetConversation rpc failed, ", reqPb.String(), err.Error(), v)
} else {
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "SetConversation success", respPb.String(), v)
}
}
3 years ago
reduceGroupMemberFromCacheReq := &pbCache.ReduceGroupMemberFromCacheReq{
UserIDList: okUserIDList,
GroupID: req.GroupID,
OperationID: req.OperationID,
}
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName)
cacheClient := pbCache.NewCacheClient(etcdConn)
cacheResp, err := cacheClient.ReduceGroupMemberFromCache(context.Background(), reduceGroupMemberFromCacheReq)
if err != nil {
log.NewError(req.OperationID, "ReduceGroupMemberFromCache rpc call failed ", err.Error())
return &pbGroup.KickGroupMemberResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
}
if cacheResp.CommonResp.ErrCode != 0 {
log.NewError(req.OperationID, "ReduceGroupMemberFromCache rpc logic call failed ", cacheResp.String())
return &pbGroup.KickGroupMemberResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
}
3 years ago
chat.MemberKickedNotification(req, okUserIDList)
log.NewInfo(req.OperationID, "GetGroupMemberList rpc return ", resp.String())
return &resp, nil
}
func (s *groupServer) GetGroupMembersInfo(ctx context.Context, req *pbGroup.GetGroupMembersInfoReq) (*pbGroup.GetGroupMembersInfoResp, error) {
3 years ago
log.NewInfo(req.OperationID, "GetGroupMembersInfo args ", req.String())
var resp pbGroup.GetGroupMembersInfoResp
for _, v := range req.MemberList {
3 years ago
var memberNode open_im_sdk.GroupMemberFullInfo
3 years ago
memberInfo, err := imdb.GetMemberInfoByID(req.GroupID, v)
3 years ago
memberNode.UserID = v
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetMemberInfoById failed ", err.Error(), req.GroupID, v)
continue
3 years ago
} else {
utils.CopyStructFields(&memberNode, memberInfo)
3 years ago
memberNode.JoinTime = int32(memberInfo.JoinTime.Unix())
3 years ago
resp.MemberList = append(resp.MemberList, &memberNode)
}
}
3 years ago
resp.ErrCode = 0
3 years ago
log.NewInfo(req.OperationID, "GetGroupMembersInfo rpc return ", resp.String())
return &resp, nil
}
3 years ago
3 years ago
func (s *groupServer) GetGroupApplicationList(_ context.Context, req *pbGroup.GetGroupApplicationListReq) (*pbGroup.GetGroupApplicationListResp, error) {
log.NewInfo(req.OperationID, "GetGroupMembersInfo args ", req.String())
3 years ago
reply, err := imdb.GetGroupApplicationList(req.FromUserID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetGroupApplicationList failed ", err.Error(), req.FromUserID)
3 years ago
return &pbGroup.GetGroupApplicationListResp{ErrCode: 701, ErrMsg: "GetGroupApplicationList failed"}, nil
}
3 years ago
3 years ago
log.NewDebug(req.OperationID, "GetGroupApplicationList reply ", reply)
3 years ago
resp := pbGroup.GetGroupApplicationListResp{}
for _, v := range reply {
3 years ago
node := open_im_sdk.GroupRequest{UserInfo: &open_im_sdk.PublicUserInfo{}, GroupInfo: &open_im_sdk.GroupInfo{}}
group, err := imdb.GetGroupInfoByGroupID(v.GroupID)
if err != nil {
log.Error(req.OperationID, "GetGroupInfoByGroupID failed ", err.Error(), v.GroupID)
continue
}
user, err := imdb.GetUserByUserID(v.UserID)
if err != nil {
log.Error(req.OperationID, "GetUserByUserID failed ", err.Error(), v.UserID)
continue
}
3 years ago
cp.GroupRequestDBCopyOpenIM(&node, &v)
3 years ago
cp.UserDBCopyOpenIMPublicUser(node.UserInfo, user)
3 years ago
cp.GroupDBCopyOpenIM(node.GroupInfo, group)
3 years ago
log.NewDebug(req.OperationID, "node ", node, "v ", v)
3 years ago
resp.GroupRequestList = append(resp.GroupRequestList, &node)
3 years ago
}
log.NewInfo(req.OperationID, "GetGroupMembersInfo rpc return ", resp)
return &resp, nil
3 years ago
}
func (s *groupServer) GetGroupsInfo(ctx context.Context, req *pbGroup.GetGroupsInfoReq) (*pbGroup.GetGroupsInfoResp, error) {
3 years ago
log.NewInfo(req.OperationID, "GetGroupsInfo args ", req.String())
groupsInfoList := make([]*open_im_sdk.GroupInfo, 0)
3 years ago
for _, groupID := range req.GroupIDList {
3 years ago
groupInfoFromMysql, err := imdb.GetGroupInfoByGroupID(groupID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetGroupInfoByGroupID failed ", err.Error(), groupID)
3 years ago
continue
}
3 years ago
var groupInfo open_im_sdk.GroupInfo
3 years ago
cp.GroupDBCopyOpenIM(&groupInfo, groupInfoFromMysql)
3 years ago
groupsInfoList = append(groupsInfoList, &groupInfo)
}
3 years ago
resp := pbGroup.GetGroupsInfoResp{GroupInfoList: groupsInfoList}
log.NewInfo(req.OperationID, "GetGroupsInfo rpc return ", resp)
return &resp, nil
3 years ago
}
3 years ago
func (s *groupServer) GroupApplicationResponse(_ context.Context, req *pbGroup.GroupApplicationResponseReq) (*pbGroup.GroupApplicationResponseResp, error) {
3 years ago
log.NewInfo(req.OperationID, "GroupApplicationResponse args ", req.String())
3 years ago
3 years ago
groupRequest := db.GroupRequest{}
3 years ago
utils.CopyStructFields(&groupRequest, req)
3 years ago
groupRequest.UserID = req.FromUserID
groupRequest.HandleUserID = req.OpUserID
3 years ago
groupRequest.HandledTime = time.Now()
3 years ago
if !token_verify.IsManagerUserID(req.OpUserID) && !imdb.IsGroupOwnerAdmin(req.GroupID, req.OpUserID) {
log.NewError(req.OperationID, "IsManagerUserID IsGroupOwnerAdmin false ", req.GroupID, req.OpUserID)
3 years ago
return &pbGroup.GroupApplicationResponseResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
}
3 years ago
err := imdb.UpdateGroupRequest(groupRequest)
3 years ago
if err != nil {
3 years ago
//{openIM002 7836e478bc43ce1d3b8889cac983f59b 1 ok 0001-01-01 00:00:00 +0000 UTC openIM001 0001-01-01 00:00:00 +0000 UTC }
3 years ago
log.NewError(req.OperationID, "GroupApplicationResponse failed ", err.Error(), groupRequest)
3 years ago
return &pbGroup.GroupApplicationResponseResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
3 years ago
3 years ago
if req.HandleResult == constant.GroupResponseAgree {
user, err := imdb.GetUserByUserID(req.FromUserID)
if err != nil {
log.NewError(req.OperationID, "GroupApplicationResponse failed ", err.Error(), req.FromUserID)
return &pbGroup.GroupApplicationResponseResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
3 years ago
member := db.GroupMember{}
member.GroupID = req.GroupID
member.UserID = req.FromUserID
member.RoleLevel = constant.GroupOrdinaryUsers
member.OperatorUserID = req.OpUserID
3 years ago
member.FaceURL = user.FaceURL
3 years ago
member.Nickname = user.Nickname
err = imdb.InsertIntoGroupMember(member)
if err != nil {
log.NewError(req.OperationID, "GroupApplicationResponse failed ", err.Error(), member)
return &pbGroup.GroupApplicationResponseResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
var reqPb pbUser.SetConversationReq
reqPb.OperationID = req.OperationID
var c pbUser.Conversation
conversation, err := imdb.GetConversation(req.FromUserID, utils.GetConversationIDBySessionType(req.GroupID, constant.GroupChatType))
if err != nil {
c.OwnerUserID = req.FromUserID
c.ConversationID = utils.GetConversationIDBySessionType(req.GroupID, constant.GroupChatType)
c.ConversationType = constant.GroupChatType
c.GroupID = req.GroupID
c.IsNotInGroup = false
} else {
c.OwnerUserID = conversation.OwnerUserID
c.ConversationID = utils.GetConversationIDBySessionType(req.GroupID, constant.GroupChatType)
c.RecvMsgOpt = conversation.RecvMsgOpt
c.ConversationType = constant.GroupChatType
c.GroupID = req.GroupID
c.IsPinned = conversation.IsPinned
c.AttachedInfo = conversation.AttachedInfo
c.IsPrivateChat = conversation.IsPrivateChat
c.GroupAtType = conversation.GroupAtType
c.IsNotInGroup = false
c.Ex = conversation.Ex
}
reqPb.Conversation = &c
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
client := pbUser.NewUserClient(etcdConn)
respPb, err := client.SetConversation(context.Background(), &reqPb)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "SetConversation rpc failed, ", reqPb.String(), err.Error())
} else {
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "SetConversation success", respPb.String())
}
3 years ago
addGroupMemberToCacheReq := &pbCache.AddGroupMemberToCacheReq{OperationID: req.OperationID, GroupID: req.GroupID, UserIDList: []string{req.FromUserID}}
etcdCacheConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName)
cacheClient := pbCache.NewCacheClient(etcdCacheConn)
cacheResp, err := cacheClient.AddGroupMemberToCache(context.Background(), addGroupMemberToCacheReq)
if err != nil {
log.NewError(req.OperationID, "AddGroupMemberToCache rpc call failed ", err.Error())
return &pbGroup.GroupApplicationResponseResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
if cacheResp.CommonResp.ErrCode != 0 {
log.NewError(req.OperationID, "AddGroupMemberToCache rpc logic call failed ", cacheResp.String())
return &pbGroup.GroupApplicationResponseResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
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 {
log.Error(req.OperationID, "HandleResult failed ", req.HandleResult)
return &pbGroup.GroupApplicationResponseResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrArgs.ErrCode, ErrMsg: constant.ErrArgs.ErrMsg}}, nil
3 years ago
}
3 years ago
log.NewInfo(req.OperationID, "rpc GroupApplicationResponse return ", pbGroup.GroupApplicationResponseResp{CommonResp: &pbGroup.CommonResp{}})
3 years ago
return &pbGroup.GroupApplicationResponseResp{CommonResp: &pbGroup.CommonResp{}}, nil
3 years ago
}
3 years ago
func (s *groupServer) JoinGroup(ctx context.Context, req *pbGroup.JoinGroupReq) (*pbGroup.JoinGroupResp, error) {
3 years ago
log.NewInfo(req.OperationID, "JoinGroup args ", req.String())
3 years ago
_, err := imdb.GetUserByUserID(req.OpUserID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetUserByUserID failed ", err.Error(), req.OpUserID)
3 years ago
return &pbGroup.JoinGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
3 years ago
groupInfo, err := imdb.GetGroupInfoByGroupID(req.GroupID)
if err != nil {
log.NewError(req.OperationID, "GetGroupInfoByGroupID failed ", req.GroupID, err)
return &pbGroup.JoinGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
if groupInfo.Status == constant.GroupStatusDismissed {
errMsg := " group status is dismissed "
return &pbGroup.JoinGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrStatus.ErrCode, ErrMsg: errMsg}}, nil
}
3 years ago
var groupRequest db.GroupRequest
3 years ago
groupRequest.UserID = req.OpUserID
groupRequest.ReqMsg = req.ReqMessage
groupRequest.GroupID = req.GroupID
3 years ago
err = imdb.InsertIntoGroupRequest(groupRequest)
3 years ago
if err != nil {
log.NewError(req.OperationID, "UpdateGroupRequest ", err.Error(), groupRequest)
3 years ago
return &pbGroup.JoinGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
3 years ago
_, err = imdb.GetGroupMemberListByGroupIDAndRoleLevel(req.GroupID, constant.GroupOwner)
if err != nil {
log.NewError(req.OperationID, "GetGroupMemberListByGroupIDAndRoleLevel failed ", err.Error(), req.GroupID, constant.GroupOwner)
3 years ago
return &pbGroup.JoinGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""}}, nil
3 years ago
}
3 years ago
3 years ago
chat.JoinGroupApplicationNotification(req)
3 years ago
3 years ago
log.NewInfo(req.OperationID, "ReceiveJoinApplicationNotification rpc return ")
3 years ago
return &pbGroup.JoinGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""}}, nil
3 years ago
}
3 years ago
func (s *groupServer) QuitGroup(ctx context.Context, req *pbGroup.QuitGroupReq) (*pbGroup.QuitGroupResp, error) {
log.NewInfo(req.OperationID, "QuitGroup args ", req.String())
3 years ago
_, err := imdb.GetGroupMemberInfoByGroupIDAndUserID(req.GroupID, req.OpUserID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetGroupMemberInfoByGroupIDAndUserID failed ", err.Error(), req.GroupID, req.OpUserID)
3 years ago
return &pbGroup.QuitGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
3 years ago
err = imdb.DeleteGroupMemberByGroupIDAndUserID(req.GroupID, req.OpUserID)
3 years ago
if err != nil {
log.NewError(req.OperationID, "DeleteGroupMemberByGroupIdAndUserId failed ", err.Error(), req.GroupID, req.OpUserID)
3 years ago
return &pbGroup.QuitGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
err = db.DB.DelGroupMember(req.GroupID, req.OpUserID)
if err != nil {
log.NewError(req.OperationID, "DelGroupMember failed ", req.GroupID, req.OpUserID)
// return &pbGroup.CommonResp{ErrorCode: constant.ErrQuitGroup.ErrCode, ErrorMsg: constant.ErrQuitGroup.ErrMsg}, nil
}
//modify quitter conversation info
var reqPb pbUser.SetConversationReq
var c pbUser.Conversation
reqPb.OperationID = req.OperationID
c.OwnerUserID = req.OpUserID
c.ConversationID = utils.GetConversationIDBySessionType(req.GroupID, constant.GroupChatType)
c.ConversationType = constant.GroupChatType
c.GroupID = req.GroupID
c.IsNotInGroup = true
reqPb.Conversation = &c
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
client := pbUser.NewUserClient(etcdConn)
respPb, err := client.SetConversation(context.Background(), &reqPb)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "SetConversation rpc failed, ", reqPb.String(), err.Error())
} else {
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "SetConversation success", respPb.String())
}
3 years ago
reduceGroupMemberFromCacheReq := &pbCache.ReduceGroupMemberFromCacheReq{
UserIDList: []string{req.OpUserID},
GroupID: req.GroupID,
OperationID: req.OperationID,
}
etcdConnCache := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName)
cacheClient := pbCache.NewCacheClient(etcdConnCache)
cacheResp, err := cacheClient.ReduceGroupMemberFromCache(context.Background(), reduceGroupMemberFromCacheReq)
if err != nil {
log.NewError(req.OperationID, "ReduceGroupMemberFromCache rpc call failed ", err.Error())
return &pbGroup.QuitGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
if cacheResp.CommonResp.ErrCode != 0 {
log.NewError(req.OperationID, "ReduceGroupMemberFromCache rpc logic call failed ", cacheResp.String())
return &pbGroup.QuitGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
chat.MemberQuitNotification(req)
3 years ago
log.NewInfo(req.OperationID, "rpc QuitGroup return ", pbGroup.QuitGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""}})
3 years ago
return &pbGroup.QuitGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""}}, nil
3 years ago
}
func hasAccess(req *pbGroup.SetGroupInfoReq) bool {
if utils.IsContain(req.OpUserID, config.Config.Manager.AppManagerUid) {
return true
}
3 years ago
groupUserInfo, err := imdb.GetGroupMemberInfoByGroupIDAndUserID(req.GroupInfo.GroupID, req.OpUserID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetGroupMemberInfoByGroupIDAndUserID failed, ", err.Error(), req.GroupInfo.GroupID, req.OpUserID)
3 years ago
return false
}
3 years ago
if groupUserInfo.RoleLevel == constant.GroupOwner || groupUserInfo.RoleLevel == constant.GroupAdmin {
3 years ago
return true
}
3 years ago
return false
3 years ago
}
3 years ago
func (s *groupServer) SetGroupInfo(ctx context.Context, req *pbGroup.SetGroupInfoReq) (*pbGroup.SetGroupInfoResp, error) {
3 years ago
log.NewInfo(req.OperationID, "SetGroupInfo args ", req.String())
if !hasAccess(req) {
3 years ago
log.NewError(req.OperationID, "no access ", req)
3 years ago
return &pbGroup.SetGroupInfoResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
3 years ago
}
3 years ago
group, err := imdb.GetGroupInfoByGroupID(req.GroupInfo.GroupID)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "GetGroupInfoByGroupID failed ", err.Error(), req.GroupInfo.GroupID)
3 years ago
return &pbGroup.SetGroupInfoResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, http.WrapError(constant.ErrDB)
3 years ago
}
3 years ago
if group.Status == constant.GroupStatusDismissed {
errMsg := " group status is dismissed "
return &pbGroup.SetGroupInfoResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrStatus.ErrCode, ErrMsg: errMsg}}, nil
}
3 years ago
////bitwise operators: 0001:groupName; 0010:Notification 0100:Introduction; 1000:FaceUrl; 10000:owner
3 years ago
var changedType int32
3 years ago
if group.GroupName != req.GroupInfo.GroupName && req.GroupInfo.GroupName != "" {
3 years ago
changedType = 1
}
3 years ago
if group.Notification != req.GroupInfo.Notification && req.GroupInfo.Notification != "" {
3 years ago
changedType = changedType | (1 << 1)
}
3 years ago
if group.Introduction != req.GroupInfo.Introduction && req.GroupInfo.Introduction != "" {
3 years ago
changedType = changedType | (1 << 2)
}
3 years ago
if group.FaceURL != req.GroupInfo.FaceURL && req.GroupInfo.FaceURL != "" {
3 years ago
changedType = changedType | (1 << 3)
}
//only administrators can set group information
3 years ago
var groupInfo db.Group
3 years ago
utils.CopyStructFields(&groupInfo, req.GroupInfo)
err = imdb.SetGroupInfo(groupInfo)
if err != nil {
3 years ago
log.NewError(req.OperationID, "SetGroupInfo failed ", err.Error(), groupInfo)
3 years ago
return &pbGroup.SetGroupInfoResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, http.WrapError(constant.ErrDB)
3 years ago
}
3 years ago
log.NewInfo(req.OperationID, "SetGroupInfo rpc return ", pbGroup.SetGroupInfoResp{CommonResp: &pbGroup.CommonResp{}})
3 years ago
if changedType != 0 {
3 years ago
chat.GroupInfoSetNotification(req.OperationID, req.OpUserID, req.GroupInfo.GroupID)
3 years ago
}
3 years ago
return &pbGroup.SetGroupInfoResp{CommonResp: &pbGroup.CommonResp{}}, nil
3 years ago
}
3 years ago
func (s *groupServer) TransferGroupOwner(_ context.Context, req *pbGroup.TransferGroupOwnerReq) (*pbGroup.TransferGroupOwnerResp, error) {
3 years ago
log.NewInfo(req.OperationID, "TransferGroupOwner ", req.String())
3 years ago
3 years ago
groupInfo, err := imdb.GetGroupInfoByGroupID(req.GroupID)
if err != nil {
log.NewError(req.OperationID, "GetGroupInfoByGroupID failed ", req.GroupID, err)
return &pbGroup.TransferGroupOwnerResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
if groupInfo.Status == constant.GroupStatusDismissed {
errMsg := " group status is dismissed "
return &pbGroup.TransferGroupOwnerResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrStatus.ErrCode, ErrMsg: errMsg}}, nil
}
3 years ago
if req.OldOwnerUserID == req.NewOwnerUserID {
3 years ago
log.NewError(req.OperationID, "same owner ", req.OldOwnerUserID, req.NewOwnerUserID)
3 years ago
return &pbGroup.TransferGroupOwnerResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrArgs.ErrCode, ErrMsg: constant.ErrArgs.ErrMsg}}, nil
}
3 years ago
groupMemberInfo := db.GroupMember{GroupID: req.GroupID, UserID: req.OldOwnerUserID, RoleLevel: constant.GroupOrdinaryUsers}
err = imdb.UpdateGroupMemberInfo(groupMemberInfo)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "UpdateGroupMemberInfo failed ", groupMemberInfo)
3 years ago
return &pbGroup.TransferGroupOwnerResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
3 years ago
groupMemberInfo = db.GroupMember{GroupID: req.GroupID, UserID: req.NewOwnerUserID, RoleLevel: constant.GroupOwner}
3 years ago
err = imdb.UpdateGroupMemberInfo(groupMemberInfo)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, "UpdateGroupMemberInfo failed ", groupMemberInfo)
3 years ago
return &pbGroup.TransferGroupOwnerResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
3 years ago
}
3 years ago
chat.GroupOwnerTransferredNotification(req)
3 years ago
return &pbGroup.TransferGroupOwnerResp{CommonResp: &pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""}}, nil
3 years ago
}
3 years ago
3 years ago
func (s *groupServer) GetGroupById(_ context.Context, req *pbGroup.GetGroupByIdReq) (*pbGroup.GetGroupByIdResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
3 years ago
resp := &pbGroup.GetGroupByIdResp{CMSGroup: &pbGroup.CMSGroup{
GroupInfo: &open_im_sdk.GroupInfo{},
3 years ago
}}
3 years ago
group, err := imdb.GetGroupById(req.GroupId)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupById error", err.Error())
3 years ago
return resp, http.WrapError(constant.ErrDB)
3 years ago
}
3 years ago
resp.CMSGroup.GroupInfo = &open_im_sdk.GroupInfo{
3 years ago
GroupID: group.GroupID,
GroupName: group.GroupName,
3 years ago
FaceURL: group.FaceURL,
3 years ago
OwnerUserID: group.CreatorUserID,
3 years ago
MemberCount: 0,
Status: group.Status,
CreatorUserID: group.CreatorUserID,
3 years ago
GroupType: group.GroupType,
3 years ago
CreateTime: uint32(group.CreateTime.Unix()),
3 years ago
}
3 years ago
groupMember, err := imdb.GetGroupMaster(group.GroupID)
if err != nil {
3 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupMaster", err.Error())
return resp, http.WrapError(constant.ErrDB)
3 years ago
}
resp.CMSGroup.GroupMasterName = groupMember.Nickname
resp.CMSGroup.GroupMasterId = groupMember.UserID
resp.CMSGroup.GroupInfo.CreatorUserID = group.CreatorUserID
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
3 years ago
return resp, nil
}
3 years ago
func (s *groupServer) GetGroup(_ context.Context, req *pbGroup.GetGroupReq) (*pbGroup.GetGroupResp, error) {
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
3 years ago
resp := &pbGroup.GetGroupResp{
3 years ago
CMSGroups: []*pbGroup.CMSGroup{},
3 years ago
}
groups, err := imdb.GetGroupsByName(req.GroupName, req.Pagination.PageNumber, req.Pagination.ShowNumber)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupsByName error", req.String())
return resp, http.WrapError(constant.ErrDB)
3 years ago
}
nums, err := imdb.GetGroupsCountNum(db.Group{GroupName: req.GroupName})
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupsCountNum error", err.Error())
return resp, http.WrapError(constant.ErrDB)
3 years ago
}
resp.GroupNums = nums
3 years ago
resp.Pagination = &open_im_sdk.RequestPagination{
PageNumber: req.Pagination.PageNumber,
ShowNumber: req.Pagination.ShowNumber,
}
3 years ago
for _, v := range groups {
3 years ago
groupMember, err := imdb.GetGroupMaster(v.GroupID)
if err != nil {
3 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupMaster error", err.Error())
3 years ago
continue
3 years ago
}
resp.CMSGroups = append(resp.CMSGroups, &pbGroup.CMSGroup{
GroupInfo: &open_im_sdk.GroupInfo{
GroupID: v.GroupID,
GroupName: v.GroupName,
3 years ago
FaceURL: v.FaceURL,
3 years ago
OwnerUserID: v.CreatorUserID,
Status: v.Status,
CreatorUserID: v.CreatorUserID,
3 years ago
CreateTime: uint32(v.CreateTime.Unix()),
3 years ago
},
GroupMasterName: groupMember.Nickname,
GroupMasterId: groupMember.UserID,
3 years ago
})
3 years ago
}
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
3 years ago
return resp, nil
}
func (s *groupServer) GetGroups(_ context.Context, req *pbGroup.GetGroupsReq) (*pbGroup.GetGroupsResp, error) {
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "GetGroups ", req.String())
3 years ago
resp := &pbGroup.GetGroupsResp{
3 years ago
CMSGroups: []*pbGroup.CMSGroup{},
3 years ago
Pagination: &open_im_sdk.RequestPagination{},
}
3 years ago
groups, err := imdb.GetGroups(int(req.Pagination.PageNumber), int(req.Pagination.ShowNumber))
if err != nil {
3 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroups error", err.Error())
return resp, http.WrapError(constant.ErrDB)
3 years ago
}
3 years ago
groupsCountNum, err := imdb.GetGroupsCountNum(db.Group{})
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "groupsCountNum ", groupsCountNum)
3 years ago
if err != nil {
3 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupsCountNum", err.Error())
return resp, http.WrapError(constant.ErrDB)
3 years ago
}
3 years ago
resp.GroupNum = int32(groupsCountNum)
resp.Pagination.PageNumber = req.Pagination.PageNumber
resp.Pagination.ShowNumber = req.Pagination.ShowNumber
for _, v := range groups {
3 years ago
groupMember, err := imdb.GetGroupMaster(v.GroupID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
}
resp.CMSGroups = append(resp.CMSGroups, &pbGroup.CMSGroup{
GroupInfo: &open_im_sdk.GroupInfo{
GroupID: v.GroupID,
GroupName: v.GroupName,
3 years ago
FaceURL: v.FaceURL,
3 years ago
OwnerUserID: v.CreatorUserID,
Status: v.Status,
CreatorUserID: v.CreatorUserID,
3 years ago
CreateTime: uint32(v.CreateTime.Unix()),
3 years ago
},
GroupMasterId: groupMember.UserID,
3 years ago
GroupMasterName: groupMember.Nickname,
3 years ago
})
3 years ago
}
3 years ago
3 years ago
return resp, nil
}
3 years ago
func (s *groupServer) OperateGroupStatus(_ context.Context, req *pbGroup.OperateGroupStatusReq) (*pbGroup.OperateGroupStatusResp, error) {
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req.String())
3 years ago
resp := &pbGroup.OperateGroupStatusResp{}
if err := imdb.OperateGroupStatus(req.GroupId, req.Status); err != nil {
3 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "OperateGroupStatus", err.Error())
return resp, http.WrapError(constant.ErrDB)
3 years ago
}
return resp, nil
}
func (s *groupServer) DeleteGroup(_ context.Context, req *pbGroup.DeleteGroupReq) (*pbGroup.DeleteGroupResp, error) {
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req.String())
3 years ago
resp := &pbGroup.DeleteGroupResp{}
if err := imdb.DeleteGroup(req.GroupId); err != nil {
3 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "DeleteGroup error", err.Error())
return resp, http.WrapError(constant.ErrDB)
3 years ago
}
return resp, nil
}
3 years ago
func (s *groupServer) OperateUserRole(_ context.Context, req *pbGroup.OperateUserRoleReq) (*pbGroup.OperateUserRoleResp, error) {
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "args:", req.String())
3 years ago
resp := &pbGroup.OperateUserRoleResp{}
3 years ago
oldOwnerUserID, err := imdb.GetGroupMaster(req.GroupId)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupMaster failed", err.Error())
3 years ago
return resp, http.WrapError(constant.ErrDB)
3 years ago
}
3 years ago
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName)
client := pbGroup.NewGroupClient(etcdConn)
var reqPb pbGroup.TransferGroupOwnerReq
reqPb.OperationID = req.OperationID
reqPb.NewOwnerUserID = req.UserId
reqPb.GroupID = req.GroupId
reqPb.OpUserID = "cms admin"
reqPb.OldOwnerUserID = oldOwnerUserID.UserID
reply, err := client.TransferGroupOwner(context.Background(), &reqPb)
if reply.CommonResp.ErrCode != 0 || err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "TransferGroupOwner rpc failed")
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
}
}
3 years ago
return resp, nil
}
func (s *groupServer) GetGroupMembersCMS(_ context.Context, req *pbGroup.GetGroupMembersCMSReq) (*pbGroup.GetGroupMembersCMSResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "args:", req.String())
3 years ago
resp := &pbGroup.GetGroupMembersCMSResp{}
3 years ago
groupMembers, err := imdb.GetGroupMembersByGroupIdCMS(req.GroupId, req.UserName, req.Pagination.ShowNumber, req.Pagination.PageNumber)
3 years ago
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupMembersByGroupIdCMS Error", err.Error())
3 years ago
return resp, http.WrapError(constant.ErrDB)
3 years ago
}
groupMembersCount, err := imdb.GetGroupMembersCount(req.GroupId, req.UserName)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupMembersCMS Error", err.Error())
3 years ago
return resp, http.WrapError(constant.ErrDB)
3 years ago
}
3 years ago
log.NewInfo(req.OperationID, groupMembersCount)
resp.MemberNums = groupMembersCount
3 years ago
for _, groupMember := range groupMembers {
resp.Members = append(resp.Members, &open_im_sdk.GroupMemberFullInfo{
GroupID: req.GroupId,
UserID: groupMember.UserID,
RoleLevel: groupMember.RoleLevel,
3 years ago
JoinTime: int32(groupMember.JoinTime.Unix()),
Nickname: groupMember.Nickname,
FaceURL: groupMember.FaceURL,
JoinSource: groupMember.JoinSource,
3 years ago
})
}
3 years ago
resp.Pagination = &open_im_sdk.ResponsePagination{
CurrentPage: req.Pagination.PageNumber,
ShowNumber: req.Pagination.ShowNumber,
}
3 years ago
return resp, nil
}
func (s *groupServer) RemoveGroupMembersCMS(_ context.Context, req *pbGroup.RemoveGroupMembersCMSReq) (*pbGroup.RemoveGroupMembersCMSResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "args:", req.String())
resp := &pbGroup.RemoveGroupMembersCMSResp{}
for _, userId := range req.UserIds {
err := imdb.RemoveGroupMember(req.GroupId, userId)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
resp.Failed = append(resp.Failed, userId)
} else {
resp.Success = append(resp.Success, userId)
}
}
3 years ago
reqKick := &pbGroup.KickGroupMemberReq{
GroupID: req.GroupId,
KickedUserIDList: resp.Success,
Reason: "admin kick",
OperationID: req.OperationID,
OpUserID: req.OpUserId,
}
var reqPb pbUser.SetConversationReq
var c pbUser.Conversation
for _, v := range resp.Success {
reqPb.OperationID = req.OperationID
c.OwnerUserID = v
c.ConversationID = utils.GetConversationIDBySessionType(req.GroupId, constant.GroupChatType)
c.ConversationType = constant.GroupChatType
c.GroupID = req.GroupId
c.IsNotInGroup = true
reqPb.Conversation = &c
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
client := pbUser.NewUserClient(etcdConn)
respPb, err := client.SetConversation(context.Background(), &reqPb)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "SetConversation rpc failed, ", reqPb.String(), err.Error(), v)
} else {
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "SetConversation success", respPb.String(), v)
}
}
3 years ago
reduceGroupMemberFromCacheReq := &pbCache.ReduceGroupMemberFromCacheReq{
UserIDList: resp.Success,
GroupID: req.GroupId,
OperationID: req.OperationID,
}
etcdConnCache := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName)
cacheClient := pbCache.NewCacheClient(etcdConnCache)
cacheResp, err := cacheClient.ReduceGroupMemberFromCache(context.Background(), reduceGroupMemberFromCacheReq)
if err != nil {
log.NewError(req.OperationID, "ReduceGroupMemberFromCache rpc call failed ", err.Error())
return resp, http.WrapError(constant.ErrDB)
}
if cacheResp.CommonResp.ErrCode != 0 {
log.NewError(req.OperationID, "ReduceGroupMemberFromCache rpc logic call failed ", cacheResp.String())
return resp, http.WrapError(constant.ErrDB)
}
3 years ago
chat.MemberKickedNotification(reqKick, resp.Success)
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
return resp, nil
}
func (s *groupServer) AddGroupMembersCMS(_ context.Context, req *pbGroup.AddGroupMembersCMSReq) (*pbGroup.AddGroupMembersCMSResp, error) {
3 years ago
log.NewInfo(req.OperationId, utils.GetSelfFuncName(), "args:", req.String())
3 years ago
resp := &pbGroup.AddGroupMembersCMSResp{}
3 years ago
for _, userId := range req.UserIds {
if isExist := imdb.IsExistGroupMember(req.GroupId, userId); isExist {
log.NewError(req.OperationId, utils.GetSelfFuncName(), "user is exist in group", userId, req.GroupId)
resp.Failed = append(resp.Failed, userId)
continue
}
user, err := imdb.GetUserByUserID(userId)
if err != nil {
log.NewError(req.OperationId, utils.GetSelfFuncName(), "GetUserByUserID", err.Error())
resp.Failed = append(resp.Failed, userId)
continue
}
3 years ago
groupMember := db.GroupMember{
GroupID: req.GroupId,
3 years ago
UserID: userId,
Nickname: user.Nickname,
3 years ago
FaceURL: "",
3 years ago
RoleLevel: 1,
JoinTime: time.Time{},
JoinSource: constant.JoinByAdmin,
OperatorUserID: "CmsAdmin",
Ex: "",
}
if err := imdb.InsertIntoGroupMember(groupMember); err != nil {
3 years ago
log.NewError(req.OperationId, utils.GetSelfFuncName(), "InsertIntoGroupMember failed", req.String())
3 years ago
resp.Failed = append(resp.Failed, userId)
} else {
3 years ago
resp.Success = append(resp.Success, userId)
3 years ago
}
}
3 years ago
addGroupMemberToCacheReq := &pbCache.AddGroupMemberToCacheReq{
UserIDList: resp.Success,
GroupID: req.GroupId,
OperationID: req.OperationId,
}
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName)
cacheClient := pbCache.NewCacheClient(etcdConn)
cacheResp, err := cacheClient.AddGroupMemberToCache(context.Background(), addGroupMemberToCacheReq)
if err != nil {
3 years ago
log.NewError(req.OperationId, "AddGroupMemberToCache rpc call failed ", err.Error())
3 years ago
return resp, http.WrapError(constant.ErrDB)
}
if cacheResp.CommonResp.ErrCode != 0 {
3 years ago
log.NewError(req.OperationId, "AddGroupMemberToCache rpc logic call failed ", cacheResp.String())
3 years ago
return resp, http.WrapError(constant.ErrDB)
}
chat.MemberInvitedNotification(req.OperationId, req.GroupId, req.OpUserId, "admin add you to group", resp.Success)
3 years ago
return resp, nil
}
3 years ago
3 years ago
func (s *groupServer) GetUserReqApplicationList(_ context.Context, req *pbGroup.GetUserReqApplicationListReq) (*pbGroup.GetUserReqApplicationListResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
3 years ago
resp := &pbGroup.GetUserReqApplicationListResp{}
groupRequests, err := imdb.GetUserReqGroupByUserID(req.UserID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUserReqGroupByUserID failed ", err.Error())
resp.CommonResp = &pbGroup.CommonResp{
ErrCode: constant.ErrDB.ErrCode,
ErrMsg: constant.ErrDB.ErrMsg,
}
return resp, nil
}
for _, groupReq := range groupRequests {
node := open_im_sdk.GroupRequest{UserInfo: &open_im_sdk.PublicUserInfo{}, GroupInfo: &open_im_sdk.GroupInfo{}}
group, err := imdb.GetGroupInfoByGroupID(groupReq.GroupID)
if err != nil {
log.Error(req.OperationID, "GetGroupInfoByGroupID failed ", err.Error(), groupReq.GroupID)
continue
}
user, err := imdb.GetUserByUserID(groupReq.UserID)
if err != nil {
log.Error(req.OperationID, "GetUserByUserID failed ", err.Error(), groupReq.UserID)
continue
}
cp.GroupRequestDBCopyOpenIM(&node, &groupReq)
cp.UserDBCopyOpenIMPublicUser(node.UserInfo, user)
cp.GroupDBCopyOpenIM(node.GroupInfo, group)
resp.GroupRequestList = append(resp.GroupRequestList, &node)
}
resp.CommonResp = &pbGroup.CommonResp{
ErrCode: 0,
ErrMsg: "",
}
return resp, nil
3 years ago
}
3 years ago
func (s *groupServer) DismissGroup(ctx context.Context, req *pbGroup.DismissGroupReq) (*pbGroup.DismissGroupResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc args ", req.String())
3 years ago
if !token_verify.IsManagerUserID(req.OpUserID) && !imdb.IsGroupOwnerAdmin(req.GroupID, req.OpUserID) {
3 years ago
log.NewError(req.OperationID, "verify failed ", req.OpUserID, req.GroupID)
return &pbGroup.DismissGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
}
err := imdb.OperateGroupStatus(req.GroupID, constant.GroupStatusDismissed)
if err != nil {
log.NewError(req.OperationID, "OperateGroupStatus failed ", req.GroupID, constant.GroupStatusDismissed)
return &pbGroup.DismissGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
memberList, err := imdb.GetGroupMemberListByGroupID(req.GroupID)
if err != nil {
log.NewError(req.OperationID, "GetGroupMemberListByGroupID failed,", err.Error(), req.GroupID)
}
//modify quitter conversation info
var reqPb pbUser.SetConversationReq
var c pbUser.Conversation
for _, v := range memberList {
reqPb.OperationID = req.OperationID
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
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
client := pbUser.NewUserClient(etcdConn)
respPb, err := client.SetConversation(context.Background(), &reqPb)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "SetConversation rpc failed, ", reqPb.String(), err.Error(), v.UserID)
} else {
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "SetConversation success", respPb.String(), v.UserID)
}
}
chat.GroupDismissedNotification(req)
3 years ago
err = imdb.DeleteGroupMemberByGroupID(req.GroupID)
if err != nil {
log.NewError(req.OperationID, "DeleteGroupMemberByGroupID failed ", req.GroupID)
return &pbGroup.DismissGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc return ", pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""})
return &pbGroup.DismissGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""}}, nil
}
3 years ago
// rpc MuteGroupMember(MuteGroupMemberReq) returns(MuteGroupMemberResp);
// rpc CancelMuteGroupMember(CancelMuteGroupMemberReq) returns(CancelMuteGroupMemberResp);
// rpc MuteGroup(MuteGroupReq) returns(MuteGroupResp);
// rpc CancelMuteGroup(CancelMuteGroupReq) returns(CancelMuteGroupResp);
func (s *groupServer) MuteGroupMember(ctx context.Context, req *pbGroup.MuteGroupMemberReq) (*pbGroup.MuteGroupMemberResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc args ", req.String())
3 years ago
if !imdb.IsGroupOwnerAdmin(req.GroupID, req.OpUserID) && !token_verify.IsManagerUserID(req.OpUserID) {
3 years ago
log.Error(req.OperationID, "verify failed ", req.GroupID, req.UserID)
3 years ago
return &pbGroup.MuteGroupMemberResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
}
groupMemberInfo := db.GroupMember{GroupID: req.GroupID, UserID: req.UserID}
3 years ago
groupMemberInfo.MuteEndTime = time.Unix(int64(time.Now().Second())+int64(req.MutedSeconds), time.Now().UnixNano())
3 years ago
err := imdb.UpdateGroupMemberInfo(groupMemberInfo)
if err != nil {
log.Error(req.OperationID, "UpdateGroupMemberInfo failed ", err.Error(), groupMemberInfo)
return &pbGroup.MuteGroupMemberResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
3 years ago
chat.GroupMemberMutedNotification(req.OperationID, req.OpUserID, req.GroupID, req.UserID, req.MutedSeconds)
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc return ", pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""})
return &pbGroup.MuteGroupMemberResp{CommonResp: &pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""}}, nil
}
func (s *groupServer) CancelMuteGroupMember(ctx context.Context, req *pbGroup.CancelMuteGroupMemberReq) (*pbGroup.CancelMuteGroupMemberResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc args ", req.String())
3 years ago
if !imdb.IsGroupOwnerAdmin(req.GroupID, req.OpUserID) && !token_verify.IsManagerUserID(req.OpUserID) {
3 years ago
log.Error(req.OperationID, "verify failed ", req.OpUserID, req.GroupID)
return &pbGroup.CancelMuteGroupMemberResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
}
groupMemberInfo := db.GroupMember{GroupID: req.GroupID, UserID: req.UserID}
groupMemberInfo.MuteEndTime = time.Unix(0, 0)
err := imdb.UpdateGroupMemberInfo(groupMemberInfo)
if err != nil {
log.Error(req.OperationID, "UpdateGroupMemberInfo failed ", err.Error(), groupMemberInfo)
return &pbGroup.CancelMuteGroupMemberResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
3 years ago
chat.GroupMemberCancelMutedNotification(req.OperationID, req.OpUserID, req.GroupID, req.UserID)
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc return ", pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""})
return &pbGroup.CancelMuteGroupMemberResp{CommonResp: &pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""}}, nil
}
func (s *groupServer) MuteGroup(ctx context.Context, req *pbGroup.MuteGroupReq) (*pbGroup.MuteGroupResp, error) {
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc args ", req.String())
3 years ago
if !imdb.IsGroupOwnerAdmin(req.GroupID, req.OpUserID) && !token_verify.IsManagerUserID(req.OpUserID) {
3 years ago
log.Error(req.OperationID, "verify failed ", req.GroupID, req.GroupID)
return &pbGroup.MuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
}
err := imdb.OperateGroupStatus(req.GroupID, constant.GroupStatusMuted)
if err != nil {
log.Error(req.OperationID, "OperateGroupStatus failed ", err.Error(), req.GroupID, constant.GroupStatusMuted)
return &pbGroup.MuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
3 years ago
chat.GroupMutedNotification(req.OperationID, req.OpUserID, req.GroupID)
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc return ", pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""})
return &pbGroup.MuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""}}, nil
3 years ago
}
func (s *groupServer) CancelMuteGroup(ctx context.Context, req *pbGroup.CancelMuteGroupReq) (*pbGroup.CancelMuteGroupResp, error) {
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc args ", req.String())
3 years ago
if !imdb.IsGroupOwnerAdmin(req.GroupID, req.OpUserID) && !token_verify.IsManagerUserID(req.OpUserID) {
3 years ago
log.Error(req.OperationID, "verify failed ", req.OpUserID, req.GroupID)
return &pbGroup.CancelMuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
}
3 years ago
err := imdb.UpdateGroupInfoDefaultZero(req.GroupID, map[string]interface{}{"status": constant.GroupOk})
3 years ago
if err != nil {
3 years ago
log.Error(req.OperationID, "UpdateGroupInfoDefaultZero failed ", err.Error(), req.GroupID)
3 years ago
return &pbGroup.CancelMuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
3 years ago
chat.GroupCancelMutedNotification(req.OperationID, req.OpUserID, req.GroupID)
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc return ", pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""})
return &pbGroup.CancelMuteGroupResp{CommonResp: &pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""}}, nil
3 years ago
}
func (s *groupServer) SetGroupMemberNickname(ctx context.Context, req *pbGroup.SetGroupMemberNicknameReq) (*pbGroup.SetGroupMemberNicknameResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc args ", req.String())
if req.OpUserID != req.UserID && !token_verify.IsManagerUserID(req.OpUserID) {
errMsg := req.OperationID + " verify failed " + req.OpUserID + req.GroupID
log.Error(req.OperationID, errMsg)
return &pbGroup.SetGroupMemberNicknameResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil
}
groupMemberInfo := db.GroupMember{}
groupMemberInfo.UserID = req.UserID
groupMemberInfo.GroupID = req.GroupID
if req.Nickname == "" {
userNickname, err := imdb.GetUserNameByUserID(groupMemberInfo.UserID)
if err != nil {
errMsg := req.OperationID + " GetUserNameByUserID failed " + err.Error()
log.Error(req.OperationID, errMsg)
return &pbGroup.SetGroupMemberNicknameResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
groupMemberInfo.Nickname = userNickname
} else {
groupMemberInfo.Nickname = req.Nickname
}
err := imdb.UpdateGroupMemberInfo(groupMemberInfo)
if err != nil {
errMsg := req.OperationID + " UpdateGroupMemberInfo failed " + err.Error()
log.Error(req.OperationID, errMsg)
return &pbGroup.SetGroupMemberNicknameResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
}
chat.GroupMemberInfoSetNotification(req.OperationID, req.OpUserID, req.GroupID, req.UserID)
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc return ", pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""})
return &pbGroup.SetGroupMemberNicknameResp{CommonResp: &pbGroup.CommonResp{ErrCode: 0, ErrMsg: ""}}, nil
}