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

237 lines
7.3 KiB

2 years ago
package api
import (
"context"
2 years ago
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r"
"github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp"
2 years ago
"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"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify"
2 years ago
"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/mitchellh/mapstructure"
2 years ago
"google.golang.org/protobuf/proto"
2 years ago
)
var _ context.Context // 解决goland编辑器bug
2 years ago
func NewMsg(c discoveryregistry.SvcDiscoveryRegistry) *Message {
return &Message{c: c, validate: validator.New()}
2 years ago
}
2 years ago
type Message struct {
2 years ago
c discoveryregistry.SvcDiscoveryRegistry
2 years ago
validate *validator.Validate
2 years ago
}
2 years ago
func (Message) 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 Message) 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(mcontext.GetOperationID(c), "Marshal failed ", err.Error(), tips.String())
2 years ago
}
}
return &pbData
}
2 years ago
func (m *Message) client() (msg.MsgClient, error) {
2 years ago
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 *Message) GetSeq(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.GetMaxAndMinSeq, m.client, c)
2 years ago
}
2 years ago
func (m *Message) PullMsgBySeqs(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.PullMessageBySeqs, m.client, c)
2 years ago
}
2 years ago
func (m *Message) DelMsg(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.DelMsgs, m.client, c)
2 years ago
}
2 years ago
func (m *Message) DelSuperGroupMsg(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.DelSuperGroupMsg, m.client, c)
2 years ago
}
2 years ago
func (m *Message) ClearMsg(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.ClearMsg, m.client, c)
2 years ago
}
2 years ago
func (m *Message) SetMessageReactionExtensions(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.SetMessageReactionExtensions, m.client, c)
2 years ago
}
2 years ago
func (m *Message) GetMessageListReactionExtensions(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.GetMessagesReactionExtensions, m.client, c)
2 years ago
}
2 years ago
func (m *Message) AddMessageReactionExtensions(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.AddMessageReactionExtensions, m.client, c)
2 years ago
}
2 years ago
func (m *Message) DeleteMessageReactionExtensions(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.DeleteMessageReactionExtensions, m.client, c)
2 years ago
}
2 years ago
2 years ago
func (m *Message) SendMessage(c *gin.Context) {
2 years ago
params := apistruct.ManagementSendMsgReq{}
if err := c.BindJSON(&params); err != nil {
2 years ago
apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap())
2 years ago
return
}
if !tokenverify.IsAppManagerUid(c) {
apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message"))
return
}
2 years ago
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
default:
2 years ago
apiresp.GinError(c, errs.ErrArgs.WithDetail("not support err contentType").Wrap())
2 years ago
return
}
if err := mapstructure.WeakDecode(params.Content, &data); err != nil {
2 years ago
apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error()))
2 years ago
return
2 years ago
} else if err := m.validate.Struct(data); err != nil {
2 years ago
apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error()))
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 {
2 years ago
log.NewError(mcontext.GetOperationID(c), "SetSendMsgStatus failed")
2 years ago
}
2 years ago
//resp := apistruct.ManagementSendMsgResp{ResultList: sdkws.UserSendMsgResp{ServerMsgID: respPb.ServerMsgID, ClientMsgID: respPb.ClientMsgID, SendTime: respPb.SendTime}}
apiresp.GinSuccess(c, respPb)
2 years ago
}
2 years ago
func (m *Message) ManagementBatchSendMsg(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.SendMsg, m.client, c)
2 years ago
}
2 years ago
func (m *Message) CheckMsgIsSendSuccess(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.GetSendMsgStatus, m.client, c)
2 years ago
}
2 years ago
func (m *Message) GetUsersOnlineStatus(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.GetSendMsgStatus, m.client, c)
2 years ago
}