You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/api/msg.go

250 lines
7.4 KiB

2 years ago
package api
import (
"context"
2 years ago
"errors"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/internal/api/a2r"
"github.com/OpenIMSDK/Open-IM-Server/internal/apiresp"
"github.com/OpenIMSDK/Open-IM-Server/pkg/apistruct"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tracelog"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2 years ago
"github.com/gin-gonic/gin"
2 years ago
"github.com/go-playground/validator/v10"
"github.com/golang/protobuf/proto"
"github.com/mitchellh/mapstructure"
2 years ago
)
var _ context.Context // 解决goland编辑器bug
2 years ago
func NewMsg(c discoveryregistry.SvcDiscoveryRegistry) *Msg {
return &Msg{c: c, validate: validator.New()}
2 years ago
}
type Msg struct {
2 years ago
c discoveryregistry.SvcDiscoveryRegistry
2 years ago
validate *validator.Validate
2 years ago
}
2 years ago
func (Msg) SetOptions(options map[string]bool, value bool) {
2 years ago
utils.SetSwitchFromOptions(options, constant.IsHistory, value)
utils.SetSwitchFromOptions(options, constant.IsPersistent, value)
utils.SetSwitchFromOptions(options, constant.IsSenderSync, value)
utils.SetSwitchFromOptions(options, constant.IsConversationUpdate, value)
}
2 years ago
func (m Msg) newUserSendMsgReq(c *gin.Context, params *apistruct.ManagementSendMsgReq) *msg.SendMsgReq {
2 years ago
var newContent string
var err error
switch params.ContentType {
case constant.Text:
newContent = params.Content["text"].(string)
case constant.Picture:
fallthrough
case constant.Custom:
fallthrough
case constant.Voice:
fallthrough
case constant.Video:
fallthrough
case constant.File:
fallthrough
case constant.CustomNotTriggerConversation:
fallthrough
case constant.CustomOnlineOnly:
fallthrough
case constant.AdvancedRevoke:
newContent = utils.StructToJsonString(params.Content)
case constant.Revoke:
newContent = params.Content["revokeMsgClientID"].(string)
default:
}
options := make(map[string]bool, 5)
if params.IsOnlineOnly {
2 years ago
m.SetOptions(options, false)
2 years ago
}
if params.NotOfflinePush {
utils.SetSwitchFromOptions(options, constant.IsOfflinePush, false)
}
if params.ContentType == constant.CustomOnlineOnly {
2 years ago
m.SetOptions(options, false)
2 years ago
} else if params.ContentType == constant.CustomNotTriggerConversation {
utils.SetSwitchFromOptions(options, constant.IsConversationUpdate, false)
}
pbData := msg.SendMsgReq{
MsgData: &sdkws.MsgData{
SendID: params.SendID,
GroupID: params.GroupID,
ClientMsgID: utils.GetMsgID(params.SendID),
SenderPlatformID: params.SenderPlatformID,
SenderNickname: params.SenderNickname,
SenderFaceURL: params.SenderFaceURL,
SessionType: params.SessionType,
MsgFrom: constant.SysMsgType,
ContentType: params.ContentType,
Content: []byte(newContent),
RecvID: params.RecvID,
CreateTime: utils.GetCurrentTimestampByMill(),
Options: options,
OfflinePushInfo: params.OfflinePushInfo,
},
}
if params.ContentType == constant.OANotification {
var tips sdkws.TipsComm
tips.JsonDetail = utils.StructToJsonString(params.Content)
pbData.MsgData.Content, err = proto.Marshal(&tips)
if err != nil {
2 years ago
log.Error(tracelog.GetOperationID(c), "Marshal failed ", err.Error(), tips.String())
2 years ago
}
}
return &pbData
}
2 years ago
func (m *Msg) client() (msg.MsgClient, error) {
conn, err := m.c.GetConn(config.Config.RpcRegisterName.OpenImMsgName)
2 years ago
if err != nil {
return nil, err
}
return msg.NewMsgClient(conn), nil
}
2 years ago
func (m *Msg) GetSeq(c *gin.Context) {
a2r.Call(msg.MsgClient.GetMaxAndMinSeq, m.client, c)
2 years ago
}
2 years ago
func (m *Msg) PullMsgBySeqs(c *gin.Context) {
a2r.Call(msg.MsgClient.PullMessageBySeqs, m.client, c)
2 years ago
}
2 years ago
func (m *Msg) DelMsg(c *gin.Context) {
a2r.Call(msg.MsgClient.DelMsgs, m.client, c)
2 years ago
}
2 years ago
func (m *Msg) DelSuperGroupMsg(c *gin.Context) {
a2r.Call(msg.MsgClient.DelSuperGroupMsg, m.client, c)
2 years ago
}
2 years ago
func (m *Msg) ClearMsg(c *gin.Context) {
a2r.Call(msg.MsgClient.ClearMsg, m.client, c)
2 years ago
}
2 years ago
func (m *Msg) SetMessageReactionExtensions(c *gin.Context) {
a2r.Call(msg.MsgClient.SetMessageReactionExtensions, m.client, c)
2 years ago
}
2 years ago
func (m *Msg) GetMessageListReactionExtensions(c *gin.Context) {
a2r.Call(msg.MsgClient.GetMessagesReactionExtensions, m.client, c)
2 years ago
}
2 years ago
func (m *Msg) AddMessageReactionExtensions(c *gin.Context) {
a2r.Call(msg.MsgClient.AddMessageReactionExtensions, m.client, c)
2 years ago
}
2 years ago
func (m *Msg) DeleteMessageReactionExtensions(c *gin.Context) {
a2r.Call(msg.MsgClient.DeleteMessageReactionExtensions, m.client, c)
2 years ago
}
2 years ago
func (m *Msg) SendMsg(c *gin.Context) {
2 years ago
params := apistruct.ManagementSendMsgReq{}
if err := c.BindJSON(&params); err != nil {
apiresp.GinError(c, err)
return
}
2 years ago
var data interface{}
2 years ago
switch params.ContentType {
case constant.Text:
2 years ago
data = apistruct.TextElem{}
2 years ago
case constant.Picture:
2 years ago
data = apistruct.PictureElem{}
2 years ago
case constant.Voice:
2 years ago
data = apistruct.SoundElem{}
2 years ago
case constant.Video:
2 years ago
data = apistruct.VideoElem{}
2 years ago
case constant.File:
2 years ago
data = apistruct.FileElem{}
2 years ago
case constant.Custom:
2 years ago
data = apistruct.CustomElem{}
2 years ago
case constant.Revoke:
2 years ago
data = apistruct.RevokeElem{}
2 years ago
case constant.AdvancedRevoke:
2 years ago
data = apistruct.MessageRevoked{}
2 years ago
case constant.OANotification:
2 years ago
data = apistruct.OANotificationElem{}
2 years ago
params.SessionType = constant.NotificationChatType
case constant.CustomNotTriggerConversation:
2 years ago
data = apistruct.CustomElem{}
2 years ago
case constant.CustomOnlineOnly:
2 years ago
data = apistruct.CustomElem{}
2 years ago
//case constant.HasReadReceipt:
//case constant.Typing:
//case constant.Quote:
default:
apiresp.GinError(c, errors.New("wrong contentType"))
return
}
if err := mapstructure.WeakDecode(params.Content, &data); err != nil {
2 years ago
apiresp.GinError(c, errs.ErrData)
2 years ago
return
2 years ago
} else if err := m.validate.Struct(data); err != nil {
2 years ago
apiresp.GinError(c, errs.ErrData)
2 years ago
return
}
switch params.SessionType {
case constant.SingleChatType:
if len(params.RecvID) == 0 {
2 years ago
apiresp.GinError(c, errs.ErrData)
2 years ago
return
}
case constant.GroupChatType, constant.SuperGroupChatType:
if len(params.GroupID) == 0 {
2 years ago
apiresp.GinError(c, errs.ErrData)
2 years ago
return
}
}
2 years ago
pbReq := m.newUserSendMsgReq(c, &params)
conn, err := m.c.GetConn(config.Config.RpcRegisterName.OpenImMsgName)
2 years ago
if err != nil {
2 years ago
apiresp.GinError(c, errs.ErrInternalServer)
2 years ago
return
}
client := msg.NewMsgClient(conn)
2 years ago
var status int
respPb, err := client.SendMsg(c, pbReq)
2 years ago
if err != nil {
2 years ago
status = constant.MsgSendFailed
2 years ago
apiresp.GinError(c, err)
return
}
2 years ago
status = constant.MsgSendSuccessed
_, err = client.SetSendMsgStatus(c, &msg.SetSendMsgStatusReq{
Status: int32(status),
})
if err != nil {
log.NewError(tracelog.GetOperationID(c), "SetSendMsgStatus failed")
}
resp := apistruct.ManagementSendMsgResp{ResultList: sdkws.UserSendMsgResp{ServerMsgID: respPb.ServerMsgID, ClientMsgID: respPb.ClientMsgID, SendTime: respPb.SendTime}}
2 years ago
apiresp.GinSuccess(c, resp)
2 years ago
}
2 years ago
func (m *Msg) ManagementBatchSendMsg(c *gin.Context) {
a2r.Call(msg.MsgClient.SendMsg, m.client, c)
2 years ago
}
2 years ago
func (m *Msg) CheckMsgIsSendSuccess(c *gin.Context) {
a2r.Call(msg.MsgClient.GetSendMsgStatus, m.client, c)
2 years ago
}
2 years ago
func (m *Msg) GetUsersOnlineStatus(c *gin.Context) {
a2r.Call(msg.MsgClient.GetSendMsgStatus, m.client, c)
2 years ago
}
2 years ago
func (m *Msg) AccountCheck(c *gin.Context) {
a2r.Call(msg.MsgClient.GetSendMsgStatus, m.client, c)
2 years ago
}