add management interface and callback args optimization

pull/103/head
Gordon 3 years ago
parent 3329068bcc
commit 09fd45346e

@ -143,4 +143,6 @@ tokenpolicy:
messagecallback: messagecallback:
callbackSwitch: false callbackSwitch: false
callbackUrl: "http://www.xxx.com/msg/judge" callbackUrl: "http://www.xxx.com/msg/judge"
#TimeOut use second as unit
callbackTimeOut: 10

@ -24,15 +24,17 @@ import (
var validate *validator.Validate var validate *validator.Validate
type paramsManagementSendMsg struct { type paramsManagementSendMsg struct {
OperationID string `json:"operationID" binding:"required"` OperationID string `json:"operationID" binding:"required"`
SendID string `json:"sendID" binding:"required"` SendID string `json:"sendID" binding:"required"`
RecvID string `json:"recvID" binding:"required"` RecvID string `json:"recvID" binding:"required"`
SenderNickName string `json:"senderNickName" ` SenderNickName string `json:"senderNickName" `
SenderFaceURL string `json:"senderFaceURL" ` SenderFaceURL string `json:"senderFaceURL" `
ForceList []string `json:"forceList" ` SenderPlatformID int32 `json:"senderPlatformID"`
Content map[string]interface{} `json:"content" binding:"required"` ForceList []string `json:"forceList" `
ContentType int32 `json:"contentType" binding:"required"` Content map[string]interface{} `json:"content" binding:"required"`
SessionType int32 `json:"sessionType" binding:"required"` ContentType int32 `json:"contentType" binding:"required"`
SessionType int32 `json:"sessionType" binding:"required"`
IsOnlineOnly bool `json:"isOnlineOnly"`
} }
func newUserSendMsgReq(params *paramsManagementSendMsg) *pbChat.UserSendMsgReq { func newUserSendMsgReq(params *paramsManagementSendMsg) *pbChat.UserSendMsgReq {
@ -49,7 +51,11 @@ func newUserSendMsgReq(params *paramsManagementSendMsg) *pbChat.UserSendMsgReq {
case constant.File: case constant.File:
newContent = utils.StructToJsonString(params.Content) newContent = utils.StructToJsonString(params.Content)
default: default:
}
options := make(map[string]int32, 2)
if params.IsOnlineOnly {
options["history"] = 0
options["persistent"] = 0
} }
pbData := pbChat.UserSendMsgReq{ pbData := pbChat.UserSendMsgReq{
ReqIdentifier: constant.WSSendMsg, ReqIdentifier: constant.WSSendMsg,
@ -57,7 +63,7 @@ func newUserSendMsgReq(params *paramsManagementSendMsg) *pbChat.UserSendMsgReq {
SenderNickName: params.SenderNickName, SenderNickName: params.SenderNickName,
SenderFaceURL: params.SenderFaceURL, SenderFaceURL: params.SenderFaceURL,
OperationID: params.OperationID, OperationID: params.OperationID,
PlatformID: 0, PlatformID: params.SenderPlatformID,
SessionType: params.SessionType, SessionType: params.SessionType,
MsgFrom: constant.UserMsgType, MsgFrom: constant.UserMsgType,
ContentType: params.ContentType, ContentType: params.ContentType,
@ -65,6 +71,7 @@ func newUserSendMsgReq(params *paramsManagementSendMsg) *pbChat.UserSendMsgReq {
ForceList: params.ForceList, ForceList: params.ForceList,
Content: newContent, Content: newContent,
ClientMsgID: utils.GetMsgID(params.SendID), ClientMsgID: utils.GetMsgID(params.SendID),
Options: utils.MapIntToJsonString(options),
} }
return &pbData return &pbData
} }
@ -84,8 +91,24 @@ func ManagementSendMsg(c *gin.Context) {
data = TextElem{} data = TextElem{}
case constant.Picture: case constant.Picture:
data = PictureElem{} data = PictureElem{}
case constant.Voice:
data = SoundElem{}
case constant.Video:
data = VideoElem{}
case constant.File:
data = FileElem{}
//case constant.AtText:
// data = AtElem{}
//case constant.Merger:
// data =
//case constant.Card:
//case constant.Location:
case constant.Custom: case constant.Custom:
data = CustomElem{} data = CustomElem{}
//case constant.Revoke:
//case constant.HasReadReceipt:
//case constant.Typing:
//case constant.Quote:
default: default:
c.JSON(http.StatusBadRequest, gin.H{"errCode": 404, "errMsg": "contentType err"}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 404, "errMsg": "contentType err"})
log.ErrorByKv("contentType err", c.PostForm("operationID"), "content", c.PostForm("content")) log.ErrorByKv("contentType err", c.PostForm("operationID"), "content", c.PostForm("content"))

@ -21,14 +21,16 @@ import (
) )
type MsgCallBackReq struct { type MsgCallBackReq struct {
SendID string `json:"sendID"` SendID string `json:"sendID"`
RecvID string `json:"recvID"` RecvID string `json:"recvID"`
Content string `json:"content"` Content string `json:"content"`
SendTime int64 `json:"sendTime"` SendTime int64 `json:"sendTime"`
MsgFrom int32 `json:"msgFrom"` MsgFrom int32 `json:"msgFrom"`
ContentType int32 `json:"contentType"` ContentType int32 `json:"contentType"`
SessionType int32 `json:"sessionType"` SessionType int32 `json:"sessionType"`
PlatformID int32 `json:"senderPlatformID"` PlatformID int32 `json:"senderPlatformID"`
MsgID string `json:"msgID"`
IsOnlineOnly bool `json:"isOnlineOnly"`
} }
type MsgCallBackResp struct { type MsgCallBackResp struct {
ErrCode int32 `json:"errCode"` ErrCode int32 `json:"errCode"`
@ -68,29 +70,36 @@ func (rpc *rpcChat) UserSendMsg(_ context.Context, pb *pbChat.UserSendMsgReq) (*
} else { } else {
pbData.SendTime = pb.SendTime pbData.SendTime = pb.SendTime
} }
m := MsgCallBackResp{} Options := utils.JsonStringToMap(pbData.Options)
isHistory := utils.GetSwitchFromOptions(Options, "history")
mReq := MsgCallBackReq{
SendID: pb.SendID,
RecvID: pb.RecvID,
Content: pb.Content,
SendTime: pbData.SendTime,
MsgFrom: pbData.MsgFrom,
ContentType: pb.ContentType,
SessionType: pb.SessionType,
PlatformID: pb.PlatformID,
MsgID: pb.ClientMsgID,
}
if !isHistory {
mReq.IsOnlineOnly = true
}
mResp := MsgCallBackResp{}
if config.Config.MessageCallBack.CallbackSwitch { if config.Config.MessageCallBack.CallbackSwitch {
bMsg, err := http2.Post(config.Config.MessageCallBack.CallbackUrl, MsgCallBackReq{ bMsg, err := http2.Post(config.Config.MessageCallBack.CallbackUrl, mReq, config.Config.MessageCallBack.CallBackTimeOut)
SendID: pb.SendID,
RecvID: pb.RecvID,
Content: pb.Content,
SendTime: pbData.SendTime,
MsgFrom: pbData.MsgFrom,
ContentType: pb.ContentType,
SessionType: pb.SessionType,
PlatformID: pb.PlatformID,
}, "application/json; charset=utf-8")
if err != nil { if err != nil {
log.ErrorByKv("callback to Business server err", pb.OperationID, "args", pb.String(), "err", err.Error()) log.ErrorByKv("callback to Business server err", pb.OperationID, "args", pb.String(), "err", err.Error())
return returnMsg(&replay, pb, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError), "", 0) return returnMsg(&replay, pb, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError), "", 0)
} else if err = json.Unmarshal(bMsg, &m); err != nil { } else if err = json.Unmarshal(bMsg, &mResp); err != nil {
log.ErrorByKv("ws json Unmarshal err", pb.OperationID, "args", pb.String(), "err", err.Error()) log.ErrorByKv("ws json Unmarshal err", pb.OperationID, "args", pb.String(), "err", err.Error())
return returnMsg(&replay, pb, 200, err.Error(), "", 0) return returnMsg(&replay, pb, 200, err.Error(), "", 0)
} else { } else {
if m.ErrCode != 0 { if mResp.ErrCode != 0 {
return returnMsg(&replay, pb, m.ResponseErrCode, m.ErrMsg, "", 0) return returnMsg(&replay, pb, mResp.ResponseErrCode, mResp.ErrMsg, "", 0)
} else { } else {
pbData.Content = m.ResponseResult.ModifiedMsg pbData.Content = mResp.ResponseResult.ModifiedMsg
} }
} }
} }

@ -155,8 +155,9 @@ type config struct {
AccessExpire int64 `yaml:"accessExpire"` AccessExpire int64 `yaml:"accessExpire"`
} }
MessageCallBack struct { MessageCallBack struct {
CallbackSwitch bool `yaml:"callbackSwitch"` CallbackSwitch bool `yaml:"callbackSwitch"`
CallbackUrl string `yaml:"callbackUrl"` CallbackUrl string `yaml:"callbackUrl"`
CallBackTimeOut int `yaml:"callbackTimeOut"`
} }
} }

@ -32,9 +32,13 @@ const (
Video = 104 Video = 104
File = 105 File = 105
AtText = 106 AtText = 106
Merger = 107
Card = 108
Location = 109
Custom = 110 Custom = 110
HasReadReceipt = 112 HasReadReceipt = 112
Typing = 113 Typing = 113
Quote = 114
Common = 200 Common = 200
GroupMsg = 201 GroupMsg = 201

@ -29,22 +29,24 @@ func Get(url string) (response []byte, err error) {
} }
//application/json; charset=utf-8 //application/json; charset=utf-8
func Post(url string, data interface{}, contentType string) (content []byte, err error) { func Post(url string, data interface{}, timeOutSecond int) (content []byte, err error) {
jsonStr, _ := json.Marshal(data) jsonStr, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.Header.Add("content-type", contentType) req.Close = true
defer req.Body.Close() req.Header.Add("content-type", "application/json; charset=utf-8")
client := &http.Client{Timeout: 5 * time.Second} client := &http.Client{Timeout: time.Duration(timeOutSecond) * time.Second}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body) result, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err

Loading…
Cancel
Save