mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-27 12:08:52 +08:00
groupCallback
This commit is contained in:
parent
635c703535
commit
99fc84aaea
@ -7,17 +7,19 @@ import (
|
|||||||
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||||
"Open_IM/pkg/common/http"
|
"Open_IM/pkg/common/http"
|
||||||
"Open_IM/pkg/common/log"
|
"Open_IM/pkg/common/log"
|
||||||
|
"Open_IM/pkg/common/trace_log"
|
||||||
pbGroup "Open_IM/pkg/proto/group"
|
pbGroup "Open_IM/pkg/proto/group"
|
||||||
"Open_IM/pkg/utils"
|
"Open_IM/pkg/utils"
|
||||||
http2 "net/http"
|
"context"
|
||||||
|
|
||||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func callbackBeforeCreateGroup(req *pbGroup.CreateGroupReq) cbApi.CommonCallbackResp {
|
func callbackBeforeCreateGroup(ctx context.Context, req *pbGroup.CreateGroupReq) (isPass bool, err error) {
|
||||||
callbackResp := cbApi.CommonCallbackResp{OperationID: req.OperationID}
|
defer func() {
|
||||||
|
trace_log.SetContextInfo(ctx, utils.GetSelfFuncName(), err, "req", req, "isPass", isPass, "err", err)
|
||||||
|
}()
|
||||||
if !config.Config.Callback.CallbackBeforeCreateGroup.Enable {
|
if !config.Config.Callback.CallbackBeforeCreateGroup.Enable {
|
||||||
return callbackResp
|
return true, nil
|
||||||
}
|
}
|
||||||
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), req.String())
|
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), req.String())
|
||||||
commonCallbackReq := &cbApi.CallbackBeforeCreateGroupReq{
|
commonCallbackReq := &cbApi.CallbackBeforeCreateGroupReq{
|
||||||
@ -26,23 +28,15 @@ func callbackBeforeCreateGroup(req *pbGroup.CreateGroupReq) cbApi.CommonCallback
|
|||||||
GroupInfo: *req.GroupInfo,
|
GroupInfo: *req.GroupInfo,
|
||||||
InitMemberList: req.InitMemberList,
|
InitMemberList: req.InitMemberList,
|
||||||
}
|
}
|
||||||
|
callbackResp := cbApi.CommonCallbackResp{OperationID: req.OperationID}
|
||||||
resp := &cbApi.CallbackBeforeCreateGroupResp{
|
resp := &cbApi.CallbackBeforeCreateGroupResp{
|
||||||
CommonCallbackResp: &callbackResp,
|
CommonCallbackResp: &callbackResp,
|
||||||
}
|
}
|
||||||
//utils.CopyStructFields(req, msg.MsgData)
|
//utils.CopyStructFields(req, msg.MsgData)
|
||||||
defer log.NewDebug(req.OperationID, utils.GetSelfFuncName(), commonCallbackReq, *resp)
|
defer log.NewDebug(req.OperationID, utils.GetSelfFuncName(), commonCallbackReq, *resp)
|
||||||
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeCreateGroupCommand, commonCallbackReq, resp, config.Config.Callback.CallbackBeforeCreateGroup.CallbackTimeOut); err != nil {
|
isPass, err = http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeCreateGroupCommand, commonCallbackReq,
|
||||||
callbackResp.ErrCode = http2.StatusInternalServerError
|
resp, config.Config.Callback.CallbackBeforeCreateGroup.CallbackTimeOut, &config.Config.Callback.CallbackBeforeCreateGroup.CallbackFailedContinue)
|
||||||
callbackResp.ErrMsg = err.Error()
|
if isPass && err == nil {
|
||||||
if !config.Config.Callback.CallbackBeforeCreateGroup.CallbackFailedContinue {
|
|
||||||
callbackResp.ActionCode = constant.ActionForbidden
|
|
||||||
return callbackResp
|
|
||||||
} else {
|
|
||||||
callbackResp.ActionCode = constant.ActionAllow
|
|
||||||
return callbackResp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if resp.ErrCode == constant.CallbackHandleSuccess && resp.ActionCode == constant.ActionAllow {
|
|
||||||
if resp.GroupID != nil {
|
if resp.GroupID != nil {
|
||||||
req.GroupInfo.GroupID = *resp.GroupID
|
req.GroupInfo.GroupID = *resp.GroupID
|
||||||
}
|
}
|
||||||
@ -80,13 +74,16 @@ func callbackBeforeCreateGroup(req *pbGroup.CreateGroupReq) cbApi.CommonCallback
|
|||||||
req.GroupInfo.LookMemberInfo = *resp.LookMemberInfo
|
req.GroupInfo.LookMemberInfo = *resp.LookMemberInfo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return callbackResp
|
return isPass, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func CallbackBeforeMemberJoinGroup(operationID string, groupMember *im_mysql_model.GroupMember, groupEx string) cbApi.CommonCallbackResp {
|
func CallbackBeforeMemberJoinGroup(ctx context.Context, operationID string, groupMember *im_mysql_model.GroupMember, groupEx string) (isPass bool, err error) {
|
||||||
|
defer func() {
|
||||||
|
trace_log.SetContextInfo(ctx, "CallbackBeforeMemberJoinGroup", err, "groupMember", *groupMember, "groupEx", groupEx, "isPass", isPass)
|
||||||
|
}()
|
||||||
callbackResp := cbApi.CommonCallbackResp{OperationID: operationID}
|
callbackResp := cbApi.CommonCallbackResp{OperationID: operationID}
|
||||||
if !config.Config.Callback.CallbackBeforeMemberJoinGroup.Enable {
|
if !config.Config.Callback.CallbackBeforeMemberJoinGroup.Enable {
|
||||||
return callbackResp
|
return true, nil
|
||||||
}
|
}
|
||||||
log.NewDebug(operationID, "args: ", *groupMember)
|
log.NewDebug(operationID, "args: ", *groupMember)
|
||||||
callbackReq := cbApi.CallbackBeforeMemberJoinGroupReq{
|
callbackReq := cbApi.CallbackBeforeMemberJoinGroupReq{
|
||||||
@ -100,39 +97,35 @@ func CallbackBeforeMemberJoinGroup(operationID string, groupMember *im_mysql_mod
|
|||||||
resp := &cbApi.CallbackBeforeMemberJoinGroupResp{
|
resp := &cbApi.CallbackBeforeMemberJoinGroupResp{
|
||||||
CommonCallbackResp: &callbackResp,
|
CommonCallbackResp: &callbackResp,
|
||||||
}
|
}
|
||||||
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeMemberJoinGroupCommand, callbackReq, resp, config.Config.Callback.CallbackBeforeMemberJoinGroup.CallbackTimeOut); err != nil {
|
isPass, err = http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeMemberJoinGroupCommand, callbackReq,
|
||||||
callbackResp.ErrCode = http2.StatusInternalServerError
|
resp, config.Config.Callback.CallbackBeforeMemberJoinGroup.CallbackTimeOut, &config.Config.Callback.CallbackBeforeMemberJoinGroup.CallbackFailedContinue)
|
||||||
callbackResp.ErrMsg = err.Error()
|
if isPass && err == nil {
|
||||||
if !config.Config.Callback.CallbackBeforeMemberJoinGroup.CallbackFailedContinue {
|
if resp.MuteEndTime != nil {
|
||||||
callbackResp.ActionCode = constant.ActionForbidden
|
groupMember.MuteEndTime = utils.UnixSecondToTime(*resp.MuteEndTime)
|
||||||
return callbackResp
|
}
|
||||||
} else {
|
if resp.FaceURL != nil {
|
||||||
callbackResp.ActionCode = constant.ActionAllow
|
groupMember.FaceURL = *resp.FaceURL
|
||||||
return callbackResp
|
}
|
||||||
|
if resp.Ex != nil {
|
||||||
|
groupMember.Ex = *resp.Ex
|
||||||
|
}
|
||||||
|
if resp.NickName != nil {
|
||||||
|
groupMember.Nickname = *resp.NickName
|
||||||
|
}
|
||||||
|
if resp.RoleLevel != nil {
|
||||||
|
groupMember.RoleLevel = *resp.RoleLevel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if resp.MuteEndTime != nil {
|
return isPass, err
|
||||||
groupMember.MuteEndTime = utils.UnixSecondToTime(*resp.MuteEndTime)
|
|
||||||
}
|
|
||||||
if resp.FaceURL != nil {
|
|
||||||
groupMember.FaceURL = *resp.FaceURL
|
|
||||||
}
|
|
||||||
if resp.Ex != nil {
|
|
||||||
groupMember.Ex = *resp.Ex
|
|
||||||
}
|
|
||||||
if resp.NickName != nil {
|
|
||||||
groupMember.Nickname = *resp.NickName
|
|
||||||
}
|
|
||||||
if resp.RoleLevel != nil {
|
|
||||||
groupMember.RoleLevel = *resp.RoleLevel
|
|
||||||
}
|
|
||||||
return callbackResp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CallbackBeforeSetGroupMemberInfo(req *pbGroup.SetGroupMemberInfoReq) cbApi.CommonCallbackResp {
|
func CallbackBeforeSetGroupMemberInfo(ctx context.Context, req *pbGroup.SetGroupMemberInfoReq) (isPass bool, err error) {
|
||||||
|
defer func() {
|
||||||
|
trace_log.SetContextInfo(ctx, "CallbackBeforeSetGroupMemberInfo", err, "req", *req)
|
||||||
|
}()
|
||||||
callbackResp := cbApi.CommonCallbackResp{OperationID: req.OperationID}
|
callbackResp := cbApi.CommonCallbackResp{OperationID: req.OperationID}
|
||||||
if !config.Config.Callback.CallbackBeforeSetGroupMemberInfo.Enable {
|
if !config.Config.Callback.CallbackBeforeSetGroupMemberInfo.Enable {
|
||||||
return callbackResp
|
return true, nil
|
||||||
}
|
}
|
||||||
callbackReq := cbApi.CallbackBeforeSetGroupMemberInfoReq{
|
callbackReq := cbApi.CallbackBeforeSetGroupMemberInfoReq{
|
||||||
CallbackCommand: constant.CallbackBeforeSetGroupMemberInfoCommand,
|
CallbackCommand: constant.CallbackBeforeSetGroupMemberInfoCommand,
|
||||||
@ -155,29 +148,21 @@ func CallbackBeforeSetGroupMemberInfo(req *pbGroup.SetGroupMemberInfoReq) cbApi.
|
|||||||
resp := &cbApi.CallbackBeforeSetGroupMemberInfoResp{
|
resp := &cbApi.CallbackBeforeSetGroupMemberInfoResp{
|
||||||
CommonCallbackResp: &callbackResp,
|
CommonCallbackResp: &callbackResp,
|
||||||
}
|
}
|
||||||
|
isPass, err = http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeSetGroupMemberInfoCommand, callbackReq,
|
||||||
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeSetGroupMemberInfoCommand, callbackReq, resp, config.Config.Callback.CallbackBeforeSetGroupMemberInfo.CallbackTimeOut); err != nil {
|
resp, config.Config.Callback.CallbackBeforeSetGroupMemberInfo.CallbackTimeOut, &config.Config.Callback.CallbackBeforeSetGroupMemberInfo.CallbackFailedContinue)
|
||||||
callbackResp.ErrCode = http2.StatusInternalServerError
|
if isPass && err == nil {
|
||||||
callbackResp.ErrMsg = err.Error()
|
if resp.FaceURL != nil {
|
||||||
if !config.Config.Callback.CallbackBeforeSetGroupMemberInfo.CallbackFailedContinue {
|
req.FaceURL = &wrapperspb.StringValue{Value: *resp.FaceURL}
|
||||||
callbackResp.ActionCode = constant.ActionForbidden
|
}
|
||||||
return callbackResp
|
if resp.Nickname != nil {
|
||||||
} else {
|
req.Nickname = &wrapperspb.StringValue{Value: *resp.Nickname}
|
||||||
callbackResp.ActionCode = constant.ActionAllow
|
}
|
||||||
return callbackResp
|
if resp.RoleLevel != nil {
|
||||||
|
req.RoleLevel = &wrapperspb.Int32Value{Value: *resp.RoleLevel}
|
||||||
|
}
|
||||||
|
if resp.Ex != nil {
|
||||||
|
req.Ex = &wrapperspb.StringValue{Value: *resp.Ex}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if resp.FaceURL != nil {
|
return isPass, err
|
||||||
req.FaceURL = &wrapperspb.StringValue{Value: *resp.FaceURL}
|
|
||||||
}
|
|
||||||
if resp.Nickname != nil {
|
|
||||||
req.Nickname = &wrapperspb.StringValue{Value: *resp.Nickname}
|
|
||||||
}
|
|
||||||
if resp.RoleLevel != nil {
|
|
||||||
req.RoleLevel = &wrapperspb.Int32Value{Value: *resp.RoleLevel}
|
|
||||||
}
|
|
||||||
if resp.Ex != nil {
|
|
||||||
req.Ex = &wrapperspb.StringValue{Value: *resp.Ex}
|
|
||||||
}
|
|
||||||
return callbackResp
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package call_back_struct
|
package call_back_struct
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
type CommonCallbackReq struct {
|
type CommonCallbackReq struct {
|
||||||
SendID string `json:"sendID"`
|
SendID string `json:"sendID"`
|
||||||
CallbackCommand string `json:"callbackCommand"`
|
CallbackCommand string `json:"callbackCommand"`
|
||||||
@ -20,6 +25,10 @@ type CommonCallbackReq struct {
|
|||||||
Ex string `json:"ex"`
|
Ex string `json:"ex"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CallbackResp interface {
|
||||||
|
Parse() (IsPass bool, err error)
|
||||||
|
}
|
||||||
|
|
||||||
type CommonCallbackResp struct {
|
type CommonCallbackResp struct {
|
||||||
ActionCode int `json:"actionCode"`
|
ActionCode int `json:"actionCode"`
|
||||||
ErrCode int `json:"errCode"`
|
ErrCode int `json:"errCode"`
|
||||||
@ -27,6 +36,18 @@ type CommonCallbackResp struct {
|
|||||||
OperationID string `json:"operationID"`
|
OperationID string `json:"operationID"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CommonCallbackResp) Parse() (isPass bool, err error) {
|
||||||
|
if c.ActionCode != 0 {
|
||||||
|
err = errors.New(fmt.Sprintf("callback response error actionCode is %d, errCode is %d, errMsg is %s", c.ActionCode, c.ErrCode, c.ErrMsg))
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if c.ErrCode != 0 {
|
||||||
|
err = errors.New(fmt.Sprintf("callback response error actionCode is %d, errCode is %d, errMsg is %s", c.ActionCode, c.ErrCode, c.ErrMsg))
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
type UserStatusBaseCallback struct {
|
type UserStatusBaseCallback struct {
|
||||||
CallbackCommand string `json:"callbackCommand"`
|
CallbackCommand string `json:"callbackCommand"`
|
||||||
OperationID string `json:"operationID"`
|
OperationID string `json:"operationID"`
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
cbApi "Open_IM/pkg/call_back_struct"
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -56,16 +57,22 @@ func Post(url string, data interface{}, timeOutSecond int) (content []byte, err
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CallBackPostReturn(url, callbackCommand string, input, output interface{}, timeOut int) error {
|
func CallBackPostReturn(url, callbackCommand string, input interface{}, output cbApi.CallbackResp, timeOut int, failedContinue *bool) (bool, error) {
|
||||||
v := urlLib.Values{}
|
v := urlLib.Values{}
|
||||||
v.Set("callbackCommand", callbackCommand)
|
v.Set("callbackCommand", callbackCommand)
|
||||||
url = url + "?" + v.Encode()
|
url = url + "?" + v.Encode()
|
||||||
b, err := Post(url, input, timeOut)
|
b, err := Post(url, input, timeOut)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
if failedContinue != nil {
|
||||||
|
return *failedContinue, err
|
||||||
|
}
|
||||||
|
return true, err
|
||||||
}
|
}
|
||||||
if err = json.Unmarshal(b, output); err != nil {
|
if err = json.Unmarshal(b, output); err != nil {
|
||||||
return err
|
if failedContinue != nil {
|
||||||
|
return *failedContinue, err
|
||||||
|
}
|
||||||
|
return true, err
|
||||||
}
|
}
|
||||||
return nil
|
return output.Parse()
|
||||||
}
|
}
|
||||||
|
@ -81,9 +81,9 @@ func SetContextInfo(ctx context.Context, funcName string, err error, args ...int
|
|||||||
funcInfo.Err = err
|
funcInfo.Err = err
|
||||||
*t.Funcs = append(*t.Funcs, funcInfo)
|
*t.Funcs = append(*t.Funcs, funcInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.NewError(t.OperationID, funcName, "error: ", err, funcInfo.Args)
|
log.NewError(t.OperationID, funcName, "error", err, "args", funcInfo.Args)
|
||||||
} else {
|
} else {
|
||||||
log.NewInfo(t.OperationID, funcName, "args: ", funcInfo.Args)
|
log.NewInfo(t.OperationID, funcName, "args", funcInfo.Args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user