Files
open-im-server/internal/api/chat/send_msg.go
T

100 lines
3.5 KiB
Go
Raw Normal View History

2021-05-26 19:15:25 +08:00
package apiChat
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/log"
2021-10-11 22:42:10 +08:00
pbChat "Open_IM/pkg/proto/chat"
"Open_IM/pkg/utils"
2021-05-26 19:15:25 +08:00
"context"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
2021-05-26 19:15:25 +08:00
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
type paramsUserSendMsg struct {
2021-07-02 14:24:33 +08:00
ReqIdentifier int32 `json:"reqIdentifier" binding:"required"`
PlatformID int32 `json:"platformID" binding:"required"`
SendID string `json:"sendID" binding:"required"`
2021-07-02 18:13:31 +08:00
SenderNickName string `json:"senderNickName"`
2021-07-02 14:24:33 +08:00
SenderFaceURL string `json:"senderFaceUrl"`
OperationID string `json:"operationID" binding:"required"`
Data struct {
2021-05-26 19:15:25 +08:00
SessionType int32 `json:"sessionType" binding:"required"`
MsgFrom int32 `json:"msgFrom" binding:"required"`
ContentType int32 `json:"contentType" binding:"required"`
RecvID string `json:"recvID" binding:"required"`
2021-06-28 15:26:31 +08:00
ForceList []string `json:"forceList"`
2021-05-26 19:15:25 +08:00
Content string `json:"content" binding:"required"`
2021-12-08 18:15:14 +08:00
Options map[string]int32 `json:"options" `
2021-05-26 19:15:25 +08:00
ClientMsgID string `json:"clientMsgID" binding:"required"`
2021-06-28 15:26:31 +08:00
OffLineInfo map[string]interface{} `json:"offlineInfo" `
2021-05-26 19:15:25 +08:00
Ex map[string]interface{} `json:"ext"`
}
}
2021-12-23 17:34:32 +08:00
func newUserSendMsgReq(token string, params *paramsUserSendMsg) *pbChat.SendMsgReq {
pbData := pbChat.SendMsgReq{
2021-07-02 14:24:33 +08:00
ReqIdentifier: params.ReqIdentifier,
Token: token,
SendID: params.SendID,
SenderNickName: params.SenderNickName,
SenderFaceURL: params.SenderFaceURL,
OperationID: params.OperationID,
PlatformID: params.PlatformID,
SessionType: params.Data.SessionType,
MsgFrom: params.Data.MsgFrom,
ContentType: params.Data.ContentType,
RecvID: params.Data.RecvID,
ForceList: params.Data.ForceList,
Content: params.Data.Content,
2021-12-10 10:49:49 +08:00
Options: utils.MapIntToJsonString(params.Data.Options),
2021-07-02 14:24:33 +08:00
ClientMsgID: params.Data.ClientMsgID,
OffLineInfo: utils.MapToJsonString(params.Data.OffLineInfo),
Ex: utils.MapToJsonString(params.Data.Ex),
2021-05-26 19:15:25 +08:00
}
return &pbData
}
func UserSendMsg(c *gin.Context) {
params := paramsUserSendMsg{}
if err := c.BindJSON(&params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
log.ErrorByKv("json unmarshal err", "", "err", err.Error(), "data", c.PostForm("data"))
return
}
token := c.Request.Header.Get("token")
log.InfoByKv("api call success to sendMsgReq", params.OperationID, "Parameters", params)
2021-05-26 19:15:25 +08:00
pbData := newUserSendMsgReq(token, &params)
2021-12-23 17:34:32 +08:00
log.Info("", "", "api SendMsg call start..., [data: %s]", pbData.String())
2021-05-26 19:15:25 +08:00
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImOfflineMessageName)
client := pbChat.NewChatClient(etcdConn)
2021-12-23 17:34:32 +08:00
log.Info("", "", "api SendMsg call, api call rpc...")
2021-05-26 19:15:25 +08:00
2021-12-23 17:34:32 +08:00
reply, err := client.SendMsg(context.Background(), pbData)
2021-12-08 17:44:16 +08:00
if err != nil {
2021-12-23 17:34:32 +08:00
log.NewError(params.OperationID, "SendMsg rpc failed, ", params, err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 401, "errMsg": "SendMsg rpc failed, " + err.Error()})
2021-12-08 17:44:16 +08:00
return
}
2021-12-23 17:34:32 +08:00
log.Info("", "", "api SendMsg call end..., [data: %s] [reply: %s]", pbData.String(), reply.String())
2021-05-26 19:15:25 +08:00
c.JSON(http.StatusOK, gin.H{
2021-05-31 10:03:57 +08:00
"errCode": reply.ErrCode,
"errMsg": reply.ErrMsg,
2021-05-26 19:15:25 +08:00
"reqIdentifier": reply.ReqIdentifier,
"data": gin.H{
"clientMsgID": reply.ClientMsgID,
"serverMsgID": reply.ServerMsgID,
2021-06-28 15:26:31 +08:00
"sendTime": reply.SendTime,
2021-05-26 19:15:25 +08:00
},
})
}