add management interface and callback args optimization

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

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

@ -21,14 +21,16 @@ import (
)
type MsgCallBackReq struct {
SendID string `json:"sendID"`
RecvID string `json:"recvID"`
Content string `json:"content"`
SendTime int64 `json:"sendTime"`
MsgFrom int32 `json:"msgFrom"`
ContentType int32 `json:"contentType"`
SessionType int32 `json:"sessionType"`
PlatformID int32 `json:"senderPlatformID"`
SendID string `json:"sendID"`
RecvID string `json:"recvID"`
Content string `json:"content"`
SendTime int64 `json:"sendTime"`
MsgFrom int32 `json:"msgFrom"`
ContentType int32 `json:"contentType"`
SessionType int32 `json:"sessionType"`
PlatformID int32 `json:"senderPlatformID"`
MsgID string `json:"msgID"`
IsOnlineOnly bool `json:"isOnlineOnly"`
}
type MsgCallBackResp struct {
ErrCode int32 `json:"errCode"`
@ -68,29 +70,36 @@ func (rpc *rpcChat) UserSendMsg(_ context.Context, pb *pbChat.UserSendMsgReq) (*
} else {
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 {
bMsg, err := http2.Post(config.Config.MessageCallBack.CallbackUrl, 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,
}, "application/json; charset=utf-8")
bMsg, err := http2.Post(config.Config.MessageCallBack.CallbackUrl, mReq, config.Config.MessageCallBack.CallBackTimeOut)
if err != nil {
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)
} 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())
return returnMsg(&replay, pb, 200, err.Error(), "", 0)
} else {
if m.ErrCode != 0 {
return returnMsg(&replay, pb, m.ResponseErrCode, m.ErrMsg, "", 0)
if mResp.ErrCode != 0 {
return returnMsg(&replay, pb, mResp.ResponseErrCode, mResp.ErrMsg, "", 0)
} else {
pbData.Content = m.ResponseResult.ModifiedMsg
pbData.Content = mResp.ResponseResult.ModifiedMsg
}
}
}

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

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

@ -29,22 +29,24 @@ func Get(url string) (response []byte, err error) {
}
//application/json; charset=utf-8
func Post(url string, data interface{}, contentType string) (content []byte, err error) {
jsonStr, _ := json.Marshal(data)
func Post(url string, data interface{}, timeOutSecond int) (content []byte, err error) {
jsonStr, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
return nil, err
}
req.Header.Add("content-type", contentType)
defer req.Body.Close()
req.Close = true
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)
if err != nil {
return nil, err
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err

Loading…
Cancel
Save