mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
add management interface and callback args optimization
This commit is contained in:
parent
3329068bcc
commit
09fd45346e
@ -144,3 +144,5 @@ 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
|
@ -29,10 +29,12 @@ type paramsManagementSendMsg struct {
|
|||||||
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" `
|
||||||
|
SenderPlatformID int32 `json:"senderPlatformID"`
|
||||||
ForceList []string `json:"forceList" `
|
ForceList []string `json:"forceList" `
|
||||||
Content map[string]interface{} `json:"content" binding:"required"`
|
Content map[string]interface{} `json:"content" binding:"required"`
|
||||||
ContentType int32 `json:"contentType" binding:"required"`
|
ContentType int32 `json:"contentType" binding:"required"`
|
||||||
SessionType int32 `json:"sessionType" 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"))
|
||||||
|
@ -29,6 +29,8 @@ type MsgCallBackReq struct {
|
|||||||
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,9 +70,9 @@ 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)
|
||||||
if config.Config.MessageCallBack.CallbackSwitch {
|
isHistory := utils.GetSwitchFromOptions(Options, "history")
|
||||||
bMsg, err := http2.Post(config.Config.MessageCallBack.CallbackUrl, MsgCallBackReq{
|
mReq := MsgCallBackReq{
|
||||||
SendID: pb.SendID,
|
SendID: pb.SendID,
|
||||||
RecvID: pb.RecvID,
|
RecvID: pb.RecvID,
|
||||||
Content: pb.Content,
|
Content: pb.Content,
|
||||||
@ -79,18 +81,25 @@ func (rpc *rpcChat) UserSendMsg(_ context.Context, pb *pbChat.UserSendMsgReq) (*
|
|||||||
ContentType: pb.ContentType,
|
ContentType: pb.ContentType,
|
||||||
SessionType: pb.SessionType,
|
SessionType: pb.SessionType,
|
||||||
PlatformID: pb.PlatformID,
|
PlatformID: pb.PlatformID,
|
||||||
}, "application/json; charset=utf-8")
|
MsgID: pb.ClientMsgID,
|
||||||
|
}
|
||||||
|
if !isHistory {
|
||||||
|
mReq.IsOnlineOnly = true
|
||||||
|
}
|
||||||
|
mResp := MsgCallBackResp{}
|
||||||
|
if config.Config.MessageCallBack.CallbackSwitch {
|
||||||
|
bMsg, err := http2.Post(config.Config.MessageCallBack.CallbackUrl, mReq, config.Config.MessageCallBack.CallBackTimeOut)
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -157,6 +157,7 @@ type config struct {
|
|||||||
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…
x
Reference in New Issue
Block a user