mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-10 20:15:59 +08:00
Merge remote-tracking branch 'origin/errcode' into errcode
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
cbApi "Open_IM/pkg/call_back_struct"
|
||||
cbApi "Open_IM/pkg/callback_struct"
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/http"
|
||||
@@ -12,6 +12,17 @@ import (
|
||||
http2 "net/http"
|
||||
)
|
||||
|
||||
func callbackBeforeAddFriendV1(req *pbFriend.AddFriendReq) error {
|
||||
resp := callbackBeforeAddFriend(req)
|
||||
if resp.ErrCode != 0 {
|
||||
return (&constant.ErrInfo{
|
||||
ErrCode: resp.ErrCode,
|
||||
ErrMsg: resp.ErrMsg,
|
||||
}).Wrap()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func callbackBeforeAddFriend(req *pbFriend.AddFriendReq) cbApi.CommonCallbackResp {
|
||||
callbackResp := cbApi.CommonCallbackResp{OperationID: req.CommID.OperationID}
|
||||
if !config.Config.Callback.CallbackBeforeAddFriend.Enable {
|
||||
|
||||
+79
-108
@@ -7,17 +7,17 @@ import (
|
||||
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||
rocksCache "Open_IM/pkg/common/db/rocks_cache"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/common/middleware"
|
||||
promePkg "Open_IM/pkg/common/prometheus"
|
||||
"Open_IM/pkg/common/token_verify"
|
||||
"Open_IM/pkg/common/tools"
|
||||
cp "Open_IM/pkg/common/utils"
|
||||
"Open_IM/pkg/getcdv3"
|
||||
pbCache "Open_IM/pkg/proto/cache"
|
||||
pbFriend "Open_IM/pkg/proto/friend"
|
||||
sdkws "Open_IM/pkg/proto/sdk_ws"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/OpenIMSDK/getcdv3"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -29,19 +29,25 @@ import (
|
||||
)
|
||||
|
||||
type friendServer struct {
|
||||
rpcPort int
|
||||
rpcRegisterName string
|
||||
etcdSchema string
|
||||
etcdAddr []string
|
||||
rpcPort int
|
||||
rpcRegisterName string
|
||||
etcdSchema string
|
||||
etcdAddr []string
|
||||
friendModel *imdb.Friend
|
||||
friendRequestModel *imdb.FriendRequest
|
||||
blackModel *imdb.Black
|
||||
}
|
||||
|
||||
func NewFriendServer(port int) *friendServer {
|
||||
log.NewPrivateLog(constant.LogFileName)
|
||||
return &friendServer{
|
||||
rpcPort: port,
|
||||
rpcRegisterName: config.Config.RpcRegisterName.OpenImFriendName,
|
||||
etcdSchema: config.Config.Etcd.EtcdSchema,
|
||||
etcdAddr: config.Config.Etcd.EtcdAddr,
|
||||
rpcPort: port,
|
||||
rpcRegisterName: config.Config.RpcRegisterName.OpenImFriendName,
|
||||
etcdSchema: config.Config.Etcd.EtcdSchema,
|
||||
etcdAddr: config.Config.Etcd.EtcdAddr,
|
||||
friendModel: imdb.NewFriend(nil),
|
||||
friendRequestModel: imdb.NewFriendRequest(nil),
|
||||
blackModel: imdb.NewBlack(nil),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +71,7 @@ func (s *friendServer) Run() {
|
||||
defer listener.Close()
|
||||
//grpc server
|
||||
var grpcOpts []grpc.ServerOption
|
||||
grpcOpts = append(grpcOpts, grpc.UnaryInterceptor(middleware.RpcServerInterceptor))
|
||||
if config.Config.Prometheus.Enable {
|
||||
promePkg.NewGrpcRequestCounter()
|
||||
promePkg.NewGrpcRequestFailedCounter()
|
||||
@@ -100,67 +107,43 @@ func (s *friendServer) Run() {
|
||||
}
|
||||
|
||||
func (s *friendServer) AddBlacklist(ctx context.Context, req *pbFriend.AddBlacklistReq) (*pbFriend.AddBlacklistResp, error) {
|
||||
log.NewInfo(req.CommID.OperationID, "AddBlacklist args ", req.String())
|
||||
ok := token_verify.CheckAccess(ctx, req.CommID.OpUserID, req.CommID.FromUserID)
|
||||
if !ok {
|
||||
return &pbFriend.AddBlacklistResp{CommonResp: constant.Error2CommResp(ctx, constant.ErrNoPermission, "accress")}, nil
|
||||
resp := &pbFriend.AddBlacklistResp{}
|
||||
if err := token_verify.CheckAccessV3(ctx, req.CommID.FromUserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
black := imdb.Black{OwnerUserID: req.CommID.FromUserID, BlockUserID: req.CommID.ToUserID, OperatorUserID: req.CommID.OpUserID}
|
||||
err := imdb.InsertInToUserBlackList(ctx, black)
|
||||
if err := s.blackModel.Create(ctx, []*imdb.Black{&black}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
etcdConn, err := getcdv3.GetConn(ctx, config.Config.RpcRegisterName.OpenImCacheName)
|
||||
if err != nil {
|
||||
return &pbFriend.AddBlacklistResp{CommonResp: constant.Error2CommResp(ctx, constant.ErrDatabase, err.Error())}, nil
|
||||
return nil, err
|
||||
}
|
||||
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName, req.CommID.OperationID, config.Config.Etcd.UserName, config.Config.Etcd.Password)
|
||||
if etcdConn == nil {
|
||||
return &pbFriend.AddBlacklistResp{CommonResp: constant.Error2CommResp(ctx, constant.ErrInternalServer, "conn is nil")}, nil
|
||||
}
|
||||
cacheClient := pbCache.NewCacheClient(etcdConn)
|
||||
cacheResp, err := cacheClient.DelBlackIDListFromCache(ctx, &pbCache.DelBlackIDListFromCacheReq{UserID: req.CommID.FromUserID, OperationID: req.CommID.OperationID})
|
||||
_, err = pbCache.NewCacheClient(etcdConn).DelBlackIDListFromCache(ctx, &pbCache.DelBlackIDListFromCacheReq{UserID: req.CommID.FromUserID, OperationID: req.CommID.OperationID})
|
||||
if err != nil {
|
||||
return &pbFriend.AddBlacklistResp{CommonResp: constant.Error2CommResp(ctx, constant.ErrInternalServer, err.Error())}, nil
|
||||
}
|
||||
if cacheResp.CommonResp.ErrCode != 0 {
|
||||
err = errors.New(fmt.Sprintf("call DelBlackIDListFromCache rpc failed code is %d, err is %s, args is %s", cacheResp.CommonResp.ErrCode, cacheResp.CommonResp.ErrMsg, req.CommID.FromUserID))
|
||||
return &pbFriend.AddBlacklistResp{CommonResp: constant.Error2CommResp(ctx, constant.ErrInternalServer, err.Error())}, nil
|
||||
return nil, err
|
||||
}
|
||||
chat.BlackAddedNotification(req)
|
||||
return &pbFriend.AddBlacklistResp{CommonResp: constant.Error2CommResp(ctx, constant.ErrNone, "")}, nil
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq) (*pbFriend.AddFriendResp, error) {
|
||||
log.NewInfo(req.CommID.OperationID, "AddFriend args ", req.String())
|
||||
ok := token_verify.CheckAccess(ctx, req.CommID.OpUserID, req.CommID.FromUserID)
|
||||
if !ok {
|
||||
log.NewError(req.CommID.OperationID, "CheckAccess false ", req.CommID.OpUserID, req.CommID.FromUserID)
|
||||
return &pbFriend.AddFriendResp{CommonResp: &sdkws.CommonResp{ErrCode: constant.ErrNoPermission.ErrCode, ErrMsg: constant.ErrNoPermission.ErrMsg}}, nil
|
||||
resp := &pbFriend.AddFriendResp{}
|
||||
if err := token_verify.CheckAccessV3(ctx, req.CommID.FromUserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
callbackResp := callbackBeforeAddFriend(req)
|
||||
if callbackResp.ErrCode != 0 {
|
||||
log.NewError(req.CommID.OperationID, utils.GetSelfFuncName(), "callbackBeforeSendSingleMsg resp: ", callbackResp)
|
||||
if err := callbackBeforeAddFriendV1(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if callbackResp.ActionCode != constant.ActionAllow {
|
||||
if callbackResp.ErrCode == 0 {
|
||||
callbackResp.ErrCode = 201
|
||||
}
|
||||
log.NewDebug(req.CommID.OperationID, utils.GetSelfFuncName(), "callbackBeforeSendSingleMsg result", "end rpc and return", callbackResp)
|
||||
return &pbFriend.AddFriendResp{CommonResp: &sdkws.CommonResp{
|
||||
ErrCode: int32(callbackResp.ErrCode),
|
||||
ErrMsg: callbackResp.ErrMsg,
|
||||
}}, nil
|
||||
}
|
||||
var isSend = true
|
||||
userIDList, err := rocksCache.GetFriendIDListFromCache(ctx, req.CommID.ToUserID)
|
||||
if err != nil {
|
||||
log.NewError(req.CommID.OperationID, "GetFriendIDListFromCache failed ", err.Error(), req.CommID.ToUserID)
|
||||
return &pbFriend.AddFriendResp{CommonResp: &sdkws.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: err.Error()}}, nil
|
||||
return nil, err
|
||||
}
|
||||
userIDList2, err := rocksCache.GetFriendIDListFromCache(ctx, req.CommID.FromUserID)
|
||||
if err != nil {
|
||||
log.NewError(req.CommID.OperationID, "GetUserByUserID failed ", err.Error(), req.CommID.FromUserID)
|
||||
return &pbFriend.AddFriendResp{CommonResp: &sdkws.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: err.Error()}}, nil
|
||||
return nil, err
|
||||
}
|
||||
log.NewDebug(req.CommID.OperationID, "toUserID", userIDList, "fromUserID", userIDList2)
|
||||
var isSend = true
|
||||
for _, v := range userIDList {
|
||||
if v == req.CommID.FromUserID {
|
||||
for _, v2 := range userIDList2 {
|
||||
@@ -176,85 +159,73 @@ func (s *friendServer) AddFriend(ctx context.Context, req *pbFriend.AddFriendReq
|
||||
//Cannot add non-existent users
|
||||
|
||||
if isSend {
|
||||
if _, err := imdb.GetUserByUserID(req.CommID.ToUserID); err != nil {
|
||||
log.NewError(req.CommID.OperationID, "GetUserByUserID failed ", err.Error(), req.CommID.ToUserID)
|
||||
return &pbFriend.AddFriendResp{CommonResp: &sdkws.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
|
||||
if _, err := GetUserInfo(ctx, req.CommID.ToUserID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
friendRequest := imdb.FriendRequest{
|
||||
HandleResult: 0, ReqMsg: req.ReqMsg, CreateTime: time.Now()}
|
||||
utils.CopyStructFields(&friendRequest, req.CommID)
|
||||
// {openIM001 openIM002 0 test add friend 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC }]
|
||||
log.NewDebug(req.CommID.OperationID, "UpdateFriendApplication args ", friendRequest)
|
||||
//err := imdb.InsertFriendApplication(&friendRequest)
|
||||
err := imdb.InsertFriendApplication(&friendRequest,
|
||||
map[string]interface{}{"handle_result": 0, "req_msg": friendRequest.ReqMsg, "create_time": friendRequest.CreateTime,
|
||||
"handler_user_id": "", "handle_msg": "", "handle_time": utils.UnixSecondToTime(0), "ex": ""})
|
||||
if err != nil {
|
||||
log.NewError(req.CommID.OperationID, "UpdateFriendApplication failed ", err.Error(), friendRequest)
|
||||
return &pbFriend.AddFriendResp{CommonResp: &sdkws.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil
|
||||
FromUserID: req.CommID.FromUserID,
|
||||
ToUserID: req.CommID.ToUserID,
|
||||
HandleResult: 0,
|
||||
ReqMsg: req.ReqMsg,
|
||||
CreateTime: time.Now(),
|
||||
}
|
||||
if err := s.friendRequestModel.Create(ctx, []*imdb.FriendRequest{&friendRequest}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
chat.FriendApplicationNotification(req)
|
||||
}
|
||||
//Establish a latest relationship in the friend request table
|
||||
|
||||
return &pbFriend.AddFriendResp{CommonResp: &sdkws.CommonResp{}}, nil
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *friendServer) ImportFriend(ctx context.Context, req *pbFriend.ImportFriendReq) (*pbFriend.ImportFriendResp, error) {
|
||||
log.NewInfo(req.OperationID, "ImportFriend args ", req.String())
|
||||
resp := pbFriend.ImportFriendResp{CommonResp: &sdkws.CommonResp{}}
|
||||
var c sdkws.CommonResp
|
||||
|
||||
if !utils.IsContain(req.OpUserID, config.Config.Manager.AppManagerUid) {
|
||||
log.NewError(req.OperationID, "not authorized", req.OpUserID, config.Config.Manager.AppManagerUid)
|
||||
c.ErrCode = constant.ErrNoPermission.ErrCode
|
||||
c.ErrMsg = constant.ErrNoPermission.ErrMsg
|
||||
for _, v := range req.FriendUserIDList {
|
||||
resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: -1})
|
||||
}
|
||||
resp.CommonResp = &c
|
||||
return &resp, nil
|
||||
resp := &pbFriend.ImportFriendResp{}
|
||||
//var c sdkws.CommonResp
|
||||
if !utils.IsContain(tools.OpUserID(ctx), config.Config.Manager.AppManagerUid) {
|
||||
//log.NewError(req.OperationID, "not authorized", req.OpUserID, config.Config.Manager.AppManagerUid)
|
||||
//c.ErrCode = constant.ErrNoPermission.ErrCode
|
||||
//c.ErrMsg = constant.ErrNoPermission.ErrMsg
|
||||
//for _, userID := range req.FriendUserIDList {
|
||||
// resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: userID, Result: -1})
|
||||
//}
|
||||
return nil, constant.ErrNoPermission.Wrap()
|
||||
}
|
||||
if _, err := imdb.GetUserByUserID(req.FromUserID); err != nil {
|
||||
log.NewError(req.OperationID, "GetUserByUserID failed ", err.Error(), req.FromUserID)
|
||||
c.ErrCode = constant.ErrDB.ErrCode
|
||||
c.ErrMsg = "this user not exists,cant not add friend"
|
||||
for _, v := range req.FriendUserIDList {
|
||||
resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: -1})
|
||||
}
|
||||
resp.CommonResp = &c
|
||||
return &resp, nil
|
||||
if _, err := GetUserInfo(ctx, req.FromUserID); err != nil {
|
||||
//log.NewError(req.OperationID, "GetUserByUserID failed ", err.Error(), req.FromUserID)
|
||||
//c.ErrCode = constant.ErrDB.ErrCode
|
||||
//c.ErrMsg = "this user not exists,cant not add friend"
|
||||
//for _, userID := range req.FriendUserIDList {
|
||||
// resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: userID, Result: -1})
|
||||
//}
|
||||
//resp.CommonResp = &c
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, v := range req.FriendUserIDList {
|
||||
log.NewDebug(req.OperationID, "FriendUserIDList ", v)
|
||||
if _, fErr := imdb.GetUserByUserID(v); fErr != nil {
|
||||
log.NewError(req.OperationID, "GetUserByUserID failed ", fErr.Error(), v)
|
||||
resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: -1})
|
||||
for _, userID := range req.FriendUserIDList {
|
||||
if _, fErr := GetUserInfo(ctx, userID); fErr != nil {
|
||||
resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: userID, Result: -1})
|
||||
} else {
|
||||
if _, err := imdb.GetFriendRelationshipFromFriend(req.FromUserID, v); err != nil {
|
||||
if _, err := imdb.GetFriendRelationshipFromFriend(req.FromUserID, userID); err != nil {
|
||||
//Establish two single friendship
|
||||
toInsertFollow := imdb.Friend{OwnerUserID: req.FromUserID, FriendUserID: v}
|
||||
toInsertFollow := imdb.Friend{OwnerUserID: req.FromUserID, FriendUserID: userID}
|
||||
err1 := imdb.InsertToFriend(&toInsertFollow)
|
||||
if err1 != nil {
|
||||
log.NewError(req.OperationID, "InsertToFriend failed ", err1.Error(), toInsertFollow)
|
||||
resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: -1})
|
||||
resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: userID, Result: -1})
|
||||
continue
|
||||
}
|
||||
toInsertFollow = imdb.Friend{OwnerUserID: v, FriendUserID: req.FromUserID}
|
||||
toInsertFollow = imdb.Friend{OwnerUserID: userID, FriendUserID: req.FromUserID}
|
||||
err2 := imdb.InsertToFriend(&toInsertFollow)
|
||||
if err2 != nil {
|
||||
log.NewError(req.OperationID, "InsertToFriend failed ", err2.Error(), toInsertFollow)
|
||||
resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: -1})
|
||||
resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: userID, Result: -1})
|
||||
continue
|
||||
}
|
||||
resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: 0})
|
||||
resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: userID, Result: 0})
|
||||
log.NewDebug(req.OperationID, "UserIDResultList ", resp.UserIDResultList)
|
||||
chat.FriendAddedNotification(req.OperationID, req.OpUserID, req.FromUserID, v)
|
||||
chat.FriendAddedNotification(req.OperationID, req.OpUserID, req.FromUserID, userID)
|
||||
} else {
|
||||
log.NewWarn(req.OperationID, "GetFriendRelationshipFromFriend ok", req.FromUserID, v)
|
||||
resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: v, Result: 0})
|
||||
log.NewWarn(req.OperationID, "GetFriendRelationshipFromFriend ok", req.FromUserID, userID)
|
||||
resp.UserIDResultList = append(resp.UserIDResultList, &pbFriend.UserIDResult{UserID: userID, Result: 0})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
func GetUserInfo(ctx context.Context, userID string) (interface{}, error) {
|
||||
return nil, errors.New("TODO:GetUserInfo")
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package group
|
||||
|
||||
import (
|
||||
cbApi "Open_IM/pkg/call_back_struct"
|
||||
cbApi "Open_IM/pkg/callback_struct"
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||
@@ -35,7 +35,7 @@ func callbackBeforeCreateGroup(ctx context.Context, req *pbGroup.CreateGroupReq)
|
||||
//utils.CopyStructFields(req, msg.MsgData)
|
||||
defer log.NewDebug(req.OperationID, utils.GetSelfFuncName(), commonCallbackReq, *resp)
|
||||
err = http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeCreateGroupCommand, commonCallbackReq,
|
||||
resp, config.Config.Callback.CallbackBeforeCreateGroup.CallbackTimeOut, &config.Config.Callback.CallbackBeforeCreateGroup.CallbackFailedContinue)
|
||||
resp, config.Config.Callback.CallbackBeforeCreateGroup)
|
||||
if err == nil {
|
||||
if resp.GroupID != nil {
|
||||
req.GroupInfo.GroupID = *resp.GroupID
|
||||
@@ -98,7 +98,7 @@ func CallbackBeforeMemberJoinGroup(ctx context.Context, operationID string, grou
|
||||
CommonCallbackResp: &callbackResp,
|
||||
}
|
||||
err = http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeMemberJoinGroupCommand, callbackReq,
|
||||
resp, config.Config.Callback.CallbackBeforeMemberJoinGroup.CallbackTimeOut, &config.Config.Callback.CallbackBeforeMemberJoinGroup.CallbackFailedContinue)
|
||||
resp, config.Config.Callback.CallbackBeforeMemberJoinGroup)
|
||||
if err == nil {
|
||||
if resp.MuteEndTime != nil {
|
||||
groupMember.MuteEndTime = utils.UnixSecondToTime(*resp.MuteEndTime)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package msg
|
||||
|
||||
import (
|
||||
cbApi "Open_IM/pkg/call_back_struct"
|
||||
cbApi "Open_IM/pkg/callback_struct"
|
||||
"Open_IM/pkg/common/callback"
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package msg
|
||||
|
||||
import (
|
||||
cbApi "Open_IM/pkg/call_back_struct"
|
||||
cbApi "Open_IM/pkg/callback_struct"
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/http"
|
||||
|
||||
@@ -1,574 +0,0 @@
|
||||
package organization
|
||||
|
||||
import (
|
||||
chat "Open_IM/internal/rpc/msg"
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||
rocksCache "Open_IM/pkg/common/db/rocks_cache"
|
||||
"Open_IM/pkg/common/log"
|
||||
promePkg "Open_IM/pkg/common/prometheus"
|
||||
"Open_IM/pkg/common/token_verify"
|
||||
"Open_IM/pkg/getcdv3"
|
||||
pbAuth "Open_IM/pkg/proto/auth"
|
||||
groupRpc "Open_IM/pkg/proto/group"
|
||||
rpc "Open_IM/pkg/proto/organization"
|
||||
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type organizationServer struct {
|
||||
rpcPort int
|
||||
rpcRegisterName string
|
||||
etcdSchema string
|
||||
etcdAddr []string
|
||||
}
|
||||
|
||||
func NewServer(port int) *organizationServer {
|
||||
log.NewPrivateLog(constant.LogFileName)
|
||||
return &organizationServer{
|
||||
rpcPort: port,
|
||||
rpcRegisterName: config.Config.RpcRegisterName.OpenImOrganizationName,
|
||||
etcdSchema: config.Config.Etcd.EtcdSchema,
|
||||
etcdAddr: config.Config.Etcd.EtcdAddr,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *organizationServer) Run() {
|
||||
log.NewInfo("", "organization rpc start ")
|
||||
listenIP := ""
|
||||
if config.Config.ListenIP == "" {
|
||||
listenIP = "0.0.0.0"
|
||||
} else {
|
||||
listenIP = config.Config.ListenIP
|
||||
}
|
||||
address := listenIP + ":" + strconv.Itoa(s.rpcPort)
|
||||
//listener network
|
||||
listener, err := net.Listen("tcp", address)
|
||||
if err != nil {
|
||||
panic("listening err:" + err.Error() + s.rpcRegisterName)
|
||||
}
|
||||
log.NewInfo("", "listen network success, ", address, listener)
|
||||
defer listener.Close()
|
||||
//grpc server
|
||||
var grpcOpts []grpc.ServerOption
|
||||
if config.Config.Prometheus.Enable {
|
||||
promePkg.NewGrpcRequestCounter()
|
||||
promePkg.NewGrpcRequestFailedCounter()
|
||||
promePkg.NewGrpcRequestSuccessCounter()
|
||||
grpcOpts = append(grpcOpts, []grpc.ServerOption{
|
||||
// grpc.UnaryInterceptor(promePkg.UnaryServerInterceptorProme),
|
||||
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
|
||||
grpc.UnaryInterceptor(grpcPrometheus.UnaryServerInterceptor),
|
||||
}...)
|
||||
}
|
||||
srv := grpc.NewServer(grpcOpts...)
|
||||
defer srv.GracefulStop()
|
||||
//Service registers with etcd
|
||||
rpc.RegisterOrganizationServer(srv, s)
|
||||
rpcRegisterIP := config.Config.RpcRegisterIP
|
||||
if config.Config.RpcRegisterIP == "" {
|
||||
rpcRegisterIP, err = utils.GetLocalIP()
|
||||
if err != nil {
|
||||
log.Error("", "GetLocalIP failed ", err.Error())
|
||||
}
|
||||
}
|
||||
log.NewInfo("", "rpcRegisterIP", rpcRegisterIP)
|
||||
err = getcdv3.RegisterEtcd(s.etcdSchema, strings.Join(s.etcdAddr, ","), rpcRegisterIP, s.rpcPort, s.rpcRegisterName, 10)
|
||||
if err != nil {
|
||||
log.NewError("", "RegisterEtcd failed ", err.Error())
|
||||
panic(utils.Wrap(err, "register organization module rpc to etcd err"))
|
||||
}
|
||||
log.NewInfo("", "organization rpc RegisterEtcd success", rpcRegisterIP, s.rpcPort, s.rpcRegisterName, 10)
|
||||
err = srv.Serve(listener)
|
||||
if err != nil {
|
||||
log.NewError("", "Serve failed ", err.Error())
|
||||
return
|
||||
}
|
||||
log.NewInfo("", "organization rpc success")
|
||||
}
|
||||
|
||||
func (s *organizationServer) CreateDepartment(ctx context.Context, req *rpc.CreateDepartmentReq) (*rpc.CreateDepartmentResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
if !token_verify.IsManagerUserID(req.OpUserID) {
|
||||
errMsg := req.OperationID + " " + req.OpUserID + " is not app manager"
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.CreateDepartmentResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
|
||||
department := imdb.Department{}
|
||||
utils.CopyStructFields(&department, req.DepartmentInfo)
|
||||
if department.DepartmentID == "" {
|
||||
department.DepartmentID = utils.Md5(strconv.FormatInt(time.Now().UnixNano(), 10))
|
||||
}
|
||||
log.Debug(req.OperationID, "dst ", department, "src ", req.DepartmentInfo)
|
||||
if err := imdb.CreateDepartment(&department); err != nil {
|
||||
errMsg := req.OperationID + " " + "CreateDepartment failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.CreateDepartmentResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
createdDepartment, err := imdb.GetDepartment(department.DepartmentID)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + "GetDepartment failed " + err.Error() + department.DepartmentID
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.CreateDepartmentResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
log.Debug(req.OperationID, "GetDepartment ", department.DepartmentID, *createdDepartment)
|
||||
resp := &rpc.CreateDepartmentResp{DepartmentInfo: &open_im_sdk.Department{}}
|
||||
utils.CopyStructFields(resp.DepartmentInfo, createdDepartment)
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", *resp)
|
||||
if err := rocksCache.DelAllDepartmentsFromCache(); err != nil {
|
||||
errMsg := req.OperationID + " " + "UpdateDepartment failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.CreateDepartmentResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
chat.OrganizationNotificationToAll(req.OpUserID, req.OperationID)
|
||||
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, req.OperationID)
|
||||
if etcdConn == nil {
|
||||
errMsg := req.OperationID + "getcdv3.GetDefaultConn == nil"
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
resp.ErrCode = constant.ErrInternal.ErrCode
|
||||
resp.ErrMsg = errMsg
|
||||
return resp, nil
|
||||
}
|
||||
client := groupRpc.NewGroupClient(etcdConn)
|
||||
createGroupReq := &groupRpc.CreateGroupReq{
|
||||
InitMemberList: []*groupRpc.GroupAddMemberInfo{},
|
||||
GroupInfo: &open_im_sdk.GroupInfo{
|
||||
Introduction: req.DepartmentInfo.Name,
|
||||
GroupName: req.DepartmentInfo.Name,
|
||||
FaceURL: req.DepartmentInfo.FaceURL,
|
||||
CreateTime: uint32(time.Now().Unix()),
|
||||
CreatorUserID: req.OpUserID,
|
||||
GroupType: constant.NormalGroup,
|
||||
OwnerUserID: req.OpUserID,
|
||||
},
|
||||
OperationID: req.OperationID,
|
||||
OpUserID: req.OpUserID,
|
||||
OwnerUserID: req.OpUserID,
|
||||
}
|
||||
createGroupResp, err := client.CreateGroup(context.Background(), createGroupReq)
|
||||
if err != nil {
|
||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), "CreateGroup rpc failed", createGroupReq, err.Error())
|
||||
resp.ErrCode = constant.ErrDB.ErrCode
|
||||
resp.ErrMsg = constant.ErrDB.ErrMsg + " createGroup failed " + err.Error()
|
||||
return resp, nil
|
||||
}
|
||||
if createGroupResp.ErrCode != 0 {
|
||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), resp)
|
||||
resp.ErrCode = constant.ErrDB.ErrCode
|
||||
resp.ErrMsg = constant.ErrDB.ErrMsg + " createGroup failed " + createGroupResp.ErrMsg
|
||||
return resp, nil
|
||||
}
|
||||
if err := imdb.SetDepartmentRelatedGroupID(createGroupResp.GroupInfo.GroupID, department.DepartmentID); err != nil {
|
||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), "SetDepartmentRelatedGroupID failed", err.Error())
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) UpdateDepartment(ctx context.Context, req *rpc.UpdateDepartmentReq) (*rpc.UpdateDepartmentResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
if !token_verify.IsManagerUserID(req.OpUserID) {
|
||||
errMsg := req.OperationID + " " + req.OpUserID + " is not app manager"
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.UpdateDepartmentResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
|
||||
department := imdb.Department{}
|
||||
utils.CopyStructFields(&department, req.DepartmentInfo)
|
||||
log.Debug(req.OperationID, "dst ", department, "src ", req.DepartmentInfo)
|
||||
if err := imdb.UpdateDepartment(&department, nil); err != nil {
|
||||
errMsg := req.OperationID + " " + "UpdateDepartment failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg, department)
|
||||
return &rpc.UpdateDepartmentResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
if err := rocksCache.DelAllDepartmentsFromCache(); err != nil {
|
||||
errMsg := req.OperationID + " " + "UpdateDepartment failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.UpdateDepartmentResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
resp := &rpc.UpdateDepartmentResp{}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", *resp)
|
||||
chat.OrganizationNotificationToAll(req.OpUserID, req.OperationID)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) GetSubDepartment(ctx context.Context, req *rpc.GetSubDepartmentReq) (*rpc.GetSubDepartmentResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
var departmentList []imdb.Department
|
||||
var err error
|
||||
if req.DepartmentID == "-1" {
|
||||
departmentList, err = rocksCache.GetAllDepartmentsFromCache()
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + "GetDepartment failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.GetSubDepartmentResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
} else {
|
||||
departmentList, err = imdb.GetSubDepartmentList(req.DepartmentID)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + "GetDepartment failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.GetSubDepartmentResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug(req.OperationID, "GetSubDepartmentList ", req.DepartmentID, departmentList)
|
||||
resp := &rpc.GetSubDepartmentResp{}
|
||||
for _, v := range departmentList {
|
||||
v1 := open_im_sdk.Department{}
|
||||
utils.CopyStructFields(&v1, v)
|
||||
log.Debug(req.OperationID, "src ", v, "dst ", v1)
|
||||
err, v1.MemberNum = imdb.GetDepartmentMemberNum(v1.DepartmentID)
|
||||
if err != nil {
|
||||
log.Error(req.OperationID, "GetDepartmentMemberNum failed ", err.Error(), v1.DepartmentID)
|
||||
continue
|
||||
}
|
||||
err, v1.SubDepartmentNum = imdb.GetSubDepartmentNum(v1.DepartmentID)
|
||||
if err != nil {
|
||||
log.Error(req.OperationID, "GetSubDepartmentNum failed ", err.Error(), v1.DepartmentID)
|
||||
continue
|
||||
}
|
||||
resp.DepartmentList = append(resp.DepartmentList, &v1)
|
||||
}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", *resp)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) DeleteDepartment(ctx context.Context, req *rpc.DeleteDepartmentReq) (*rpc.DeleteDepartmentResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
if !token_verify.IsManagerUserID(req.OpUserID) {
|
||||
errMsg := req.OperationID + " " + req.OpUserID + " is not app manager"
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.DeleteDepartmentResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
err := imdb.DeleteDepartment(req.DepartmentID)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + "DeleteDepartment failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg, req.DepartmentID)
|
||||
return &rpc.DeleteDepartmentResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
log.Debug(req.OperationID, "DeleteDepartment ", req.DepartmentID)
|
||||
|
||||
if err := rocksCache.DelAllDepartmentsFromCache(); err != nil {
|
||||
errMsg := req.OperationID + " " + "UpdateDepartment failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.DeleteDepartmentResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
resp := &rpc.DeleteDepartmentResp{}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", resp)
|
||||
chat.OrganizationNotificationToAll(req.OpUserID, req.OperationID)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) CreateOrganizationUser(ctx context.Context, req *rpc.CreateOrganizationUserReq) (*rpc.CreateOrganizationUserResp, error) {
|
||||
authReq := &pbAuth.UserRegisterReq{UserInfo: &open_im_sdk.UserInfo{}}
|
||||
utils.CopyStructFields(authReq.UserInfo, req.OrganizationUser)
|
||||
authReq.OperationID = req.OperationID
|
||||
if req.IsRegister {
|
||||
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImAuthName, req.OperationID)
|
||||
if etcdConn == nil {
|
||||
errMsg := req.OperationID + "getcdv3.GetDefaultConn == nil"
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
return &rpc.CreateOrganizationUserResp{ErrCode: constant.ErrInternal.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
client := pbAuth.NewAuthClient(etcdConn)
|
||||
reply, err := client.UserRegister(context.Background(), authReq)
|
||||
if err != nil {
|
||||
errMsg := "UserRegister failed " + err.Error()
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
return &rpc.CreateOrganizationUserResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
if reply.CommonResp.ErrCode != 0 {
|
||||
errMsg := "UserRegister failed " + reply.CommonResp.ErrMsg
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
return &rpc.CreateOrganizationUserResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
if !token_verify.IsManagerUserID(req.OpUserID) {
|
||||
errMsg := req.OperationID + " " + req.OpUserID + " is not app manager"
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.CreateOrganizationUserResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
organizationUser := imdb.OrganizationUser{}
|
||||
utils.CopyStructFields(&organizationUser, req.OrganizationUser)
|
||||
organizationUser.Birth = utils.UnixSecondToTime(int64(req.OrganizationUser.Birth))
|
||||
log.Debug(req.OperationID, "src ", *req.OrganizationUser, "dst ", organizationUser)
|
||||
err := imdb.CreateOrganizationUser(&organizationUser)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + "CreateOrganizationUser failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg, organizationUser)
|
||||
return &rpc.CreateOrganizationUserResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
log.Debug(req.OperationID, "CreateOrganizationUser ", organizationUser)
|
||||
resp := &rpc.CreateOrganizationUserResp{}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", *resp)
|
||||
//chat.OrganizationNotificationToAll(req.OpUserID, req.OperationID)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) UpdateOrganizationUser(ctx context.Context, req *rpc.UpdateOrganizationUserReq) (*rpc.UpdateOrganizationUserResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
if !token_verify.IsManagerUserID(req.OpUserID) && req.OpUserID != req.OrganizationUser.UserID {
|
||||
errMsg := req.OperationID + " " + req.OpUserID + " is not app manager"
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.UpdateOrganizationUserResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
organizationUser := imdb.OrganizationUser{}
|
||||
utils.CopyStructFields(&organizationUser, req.OrganizationUser)
|
||||
if req.OrganizationUser.Birth != 0 {
|
||||
organizationUser.Birth = utils.UnixSecondToTime(int64(req.OrganizationUser.Birth))
|
||||
log.Debug(req.OperationID, "time: ", organizationUser.Birth, req.OrganizationUser.Birth)
|
||||
}
|
||||
|
||||
log.Debug(req.OperationID, "src ", *req.OrganizationUser, "dst ", organizationUser)
|
||||
err := imdb.UpdateOrganizationUser(&organizationUser, nil)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + "CreateOrganizationUser failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg, organizationUser)
|
||||
return &rpc.UpdateOrganizationUserResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
log.Debug(req.OperationID, "UpdateOrganizationUser ", organizationUser)
|
||||
resp := &rpc.UpdateOrganizationUserResp{}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", resp)
|
||||
chat.OrganizationNotificationToAll(req.OpUserID, req.OperationID)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) CreateDepartmentMember(ctx context.Context, req *rpc.CreateDepartmentMemberReq) (*rpc.CreateDepartmentMemberResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
if !token_verify.IsManagerUserID(req.OpUserID) {
|
||||
errMsg := req.OperationID + " " + req.OpUserID + " is not app manager"
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.CreateDepartmentMemberResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
|
||||
err, _ := imdb.GetOrganizationUser(req.DepartmentMember.UserID)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + req.DepartmentMember.UserID + " is not exist"
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.CreateDepartmentMemberResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
|
||||
departmentMember := imdb.DepartmentMember{}
|
||||
utils.CopyStructFields(&departmentMember, req.DepartmentMember)
|
||||
log.Debug(req.OperationID, "src ", *req.DepartmentMember, "dst ", departmentMember)
|
||||
err = imdb.CreateDepartmentMember(&departmentMember)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + "CreateDepartmentMember failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg, departmentMember)
|
||||
return &rpc.CreateDepartmentMemberResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
log.Debug(req.OperationID, "UpdateOrganizationUser ", departmentMember)
|
||||
if err := rocksCache.DelAllDepartmentMembersFromCache(); err != nil {
|
||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
|
||||
}
|
||||
resp := &rpc.CreateDepartmentMemberResp{}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", *resp)
|
||||
chat.OrganizationNotificationToAll(req.OpUserID, req.OperationID)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) GetDepartmentParentIDList(_ context.Context, req *rpc.GetDepartmentParentIDListReq) (resp *rpc.GetDepartmentParentIDListResp, err error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req.String())
|
||||
resp = &rpc.GetDepartmentParentIDListResp{}
|
||||
parentIDList, err := imdb.GetDepartmentParentIDList(req.DepartmentID)
|
||||
if err != nil {
|
||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetDepartmentParentIDList failed", err.Error())
|
||||
resp.ErrMsg = constant.ErrDB.ErrMsg + ": " + err.Error()
|
||||
resp.ErrCode = constant.ErrDB.ErrCode
|
||||
return resp, nil
|
||||
}
|
||||
resp.ParentIDList = parentIDList
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp:", resp.String())
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) GetUserInDepartmentByUserID(userID string, operationID string) (*open_im_sdk.UserInDepartment, error) {
|
||||
err, organizationUser := imdb.GetOrganizationUser(userID)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "GetOrganizationUser failed")
|
||||
}
|
||||
err, departmentMemberList := imdb.GetUserInDepartment(userID)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "GetUserInDepartment failed")
|
||||
}
|
||||
log.Debug(operationID, "GetUserInDepartment ", departmentMemberList)
|
||||
resp := &open_im_sdk.UserInDepartment{OrganizationUser: &open_im_sdk.OrganizationUser{}}
|
||||
utils.CopyStructFields(resp.OrganizationUser, organizationUser)
|
||||
for _, v := range departmentMemberList {
|
||||
v1 := open_im_sdk.DepartmentMember{}
|
||||
utils.CopyStructFields(&v1, v)
|
||||
log.Debug(operationID, "DepartmentMember src ", v, "dst ", v1)
|
||||
resp.DepartmentMemberList = append(resp.DepartmentMemberList, &v1)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) GetUserInDepartment(ctx context.Context, req *rpc.GetUserInDepartmentReq) (*rpc.GetUserInDepartmentResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
r, err := s.GetUserInDepartmentByUserID(req.UserID, req.OperationID)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + "GetUserInDepartmentByUserID failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg, req.UserID)
|
||||
return &rpc.GetUserInDepartmentResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
log.Debug(req.OperationID, "GetUserInDepartmentByUserID success ", req.UserID, r)
|
||||
resp := rpc.GetUserInDepartmentResp{UserInDepartment: &open_im_sdk.UserInDepartment{OrganizationUser: &open_im_sdk.OrganizationUser{}}}
|
||||
resp.UserInDepartment.DepartmentMemberList = r.DepartmentMemberList
|
||||
resp.UserInDepartment.OrganizationUser = r.OrganizationUser
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", resp)
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) UpdateUserInDepartment(ctx context.Context, req *rpc.UpdateUserInDepartmentReq) (*rpc.UpdateUserInDepartmentResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
if !token_verify.IsManagerUserID(req.OpUserID) {
|
||||
errMsg := req.OperationID + " " + req.OpUserID + " is not app manager"
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.UpdateUserInDepartmentResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
departmentMember := &imdb.DepartmentMember{}
|
||||
utils.CopyStructFields(departmentMember, req.DepartmentMember)
|
||||
log.Debug(req.OperationID, "dst ", departmentMember, "src ", req.DepartmentMember)
|
||||
err := imdb.UpdateUserInDepartment(departmentMember, nil)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + "UpdateUserInDepartment failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg, *departmentMember)
|
||||
return &rpc.UpdateUserInDepartmentResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
resp := &rpc.UpdateUserInDepartmentResp{}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", *resp)
|
||||
chat.OrganizationNotificationToAll(req.OpUserID, req.OperationID)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) DeleteUserInDepartment(ctx context.Context, req *rpc.DeleteUserInDepartmentReq) (*rpc.DeleteUserInDepartmentResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
if !token_verify.IsManagerUserID(req.OpUserID) {
|
||||
errMsg := req.OperationID + " " + req.OpUserID + " is not app manager"
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.DeleteUserInDepartmentResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
|
||||
err := imdb.DeleteUserInDepartment(req.DepartmentID, req.UserID)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + "DeleteUserInDepartment failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg, req.DepartmentID, req.UserID)
|
||||
return &rpc.DeleteUserInDepartmentResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
log.Debug(req.OperationID, "DeleteUserInDepartment success ", req.DepartmentID, req.UserID)
|
||||
resp := &rpc.DeleteUserInDepartmentResp{}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", *resp)
|
||||
chat.OrganizationNotificationToAll(req.OpUserID, req.OperationID)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) DeleteOrganizationUser(ctx context.Context, req *rpc.DeleteOrganizationUserReq) (*rpc.DeleteOrganizationUserResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
if !token_verify.IsManagerUserID(req.OpUserID) {
|
||||
errMsg := req.OperationID + " " + req.OpUserID + " is not app manager"
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.DeleteOrganizationUserResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
err := imdb.DeleteOrganizationUser(req.UserID)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + "DeleteOrganizationUser failed " + err.Error()
|
||||
log.Error(req.OperationID, errMsg, req.UserID)
|
||||
return &rpc.DeleteOrganizationUserResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
log.Debug(req.OperationID, "DeleteOrganizationUser success ", req.UserID)
|
||||
resp := &rpc.DeleteOrganizationUserResp{}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", *resp)
|
||||
chat.OrganizationNotificationToAll(req.OpUserID, req.OperationID)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) GetDepartmentMember(ctx context.Context, req *rpc.GetDepartmentMemberReq) (*rpc.GetDepartmentMemberResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
var departmentMemberList []imdb.DepartmentMember
|
||||
var err error
|
||||
if req.DepartmentID == "-1" {
|
||||
departmentMemberList, err = rocksCache.GetAllDepartmentMembersFromCache()
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + req.OpUserID + " is not app manager"
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.GetDepartmentMemberResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
} else {
|
||||
departmentMemberList, err = imdb.GetDepartmentMemberList(req.DepartmentID)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + req.OpUserID + " is not app manager"
|
||||
log.Error(req.OperationID, errMsg)
|
||||
return &rpc.GetDepartmentMemberResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: errMsg}, nil
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug(req.OperationID, "GetDepartmentMemberList ", departmentMemberList)
|
||||
resp := rpc.GetDepartmentMemberResp{}
|
||||
for _, v := range departmentMemberList {
|
||||
err, organizationUser := imdb.GetOrganizationUser(v.UserID)
|
||||
if err != nil {
|
||||
log.Error(req.OperationID, "GetOrganizationUser failed ", err.Error())
|
||||
continue
|
||||
}
|
||||
respOrganizationUser := &open_im_sdk.OrganizationUser{}
|
||||
respDepartmentMember := &open_im_sdk.DepartmentMember{}
|
||||
|
||||
utils.CopyStructFields(respOrganizationUser, organizationUser)
|
||||
utils.CopyStructFields(respDepartmentMember, &v)
|
||||
userDepartmentMember := open_im_sdk.UserDepartmentMember{OrganizationUser: respOrganizationUser, DepartmentMember: respDepartmentMember}
|
||||
|
||||
log.Debug(req.OperationID, "GetUserInDepartmentByUserID success ", userDepartmentMember)
|
||||
resp.UserDepartmentMemberList = append(resp.UserDepartmentMemberList, &userDepartmentMember)
|
||||
}
|
||||
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", resp)
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) GetDepartmentRelatedGroupIDList(ctx context.Context, req *rpc.GetDepartmentRelatedGroupIDListReq) (resp *rpc.GetDepartmentRelatedGroupIDListResp, err error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
||||
resp = &rpc.GetDepartmentRelatedGroupIDListResp{}
|
||||
groupIDList, err := imdb.GetDepartmentRelatedGroupIDList(req.DepartmentIDList)
|
||||
if err != nil {
|
||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
|
||||
resp.ErrMsg = constant.ErrDB.ErrMsg + " GetDepartMentRelatedGroupIDList failed " + err.Error()
|
||||
resp.ErrCode = constant.ErrDB.ErrCode
|
||||
return resp, nil
|
||||
}
|
||||
resp.GroupIDList = groupIDList
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *organizationServer) GetUserInOrganization(_ context.Context, req *rpc.GetUserInOrganizationReq) (resp *rpc.GetUserInOrganizationResp, err error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
|
||||
resp = &rpc.GetUserInOrganizationResp{}
|
||||
organizationUserList, err := imdb.GetOrganizationUsers(req.UserIDList)
|
||||
if err != nil {
|
||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserIDList)
|
||||
resp.ErrCode = constant.ErrDB.ErrCode
|
||||
resp.ErrMsg = err.Error()
|
||||
return resp, nil
|
||||
}
|
||||
for _, v := range organizationUserList {
|
||||
organizationUser := &open_im_sdk.OrganizationUser{}
|
||||
utils.CopyStructFields(organizationUser, v)
|
||||
organizationUser.CreateTime = uint32(v.CreateTime.Unix())
|
||||
organizationUser.Birth = uint32(v.CreateTime.Unix())
|
||||
resp.OrganizationUsers = append(resp.OrganizationUsers, organizationUser)
|
||||
}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
|
||||
return resp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user