diff --git a/config/config.yaml b/config/config.yaml index c4b0e49fc..d61763524 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -143,4 +143,6 @@ tokenpolicy: messagecallback: callbackSwitch: false - callbackUrl: "http://www.xxx.com/msg/judge" \ No newline at end of file + callbackUrl: "http://www.xxx.com/msg/judge" + #TimeOut use second as unit + callbackTimeOut: 10 \ No newline at end of file diff --git a/internal/api/manage/management_chat.go b/internal/api/manage/management_chat.go index a398dce10..3d4487aa5 100644 --- a/internal/api/manage/management_chat.go +++ b/internal/api/manage/management_chat.go @@ -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")) diff --git a/internal/rpc/chat/send_msg.go b/internal/rpc/chat/send_msg.go index 5cfc4ee71..298f3ab65 100644 --- a/internal/rpc/chat/send_msg.go +++ b/internal/rpc/chat/send_msg.go @@ -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 } } } diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index e5d6996a2..9bfdaa6e1 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -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"` } } diff --git a/pkg/common/constant/constant.go b/pkg/common/constant/constant.go index 8a1aa4029..ca2e70919 100644 --- a/pkg/common/constant/constant.go +++ b/pkg/common/constant/constant.go @@ -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 diff --git a/pkg/common/http/http_client.go b/pkg/common/http/http_client.go index 85f2c9f79..4d480770a 100644 --- a/pkg/common/http/http_client.go +++ b/pkg/common/http/http_client.go @@ -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