Files
open-im-server/internal/api/msg.go
T

225 lines
6.8 KiB
Go
Raw Normal View History

2023-03-02 14:41:59 +08:00
package api
import (
"context"
2023-04-28 18:33:33 +08:00
2023-03-17 11:27:34 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r"
"github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp"
2023-03-16 10:46:06 +08:00
"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/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"
2023-03-02 14:41:59 +08:00
"github.com/gin-gonic/gin"
2023-03-03 19:44:34 +08:00
"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
2023-04-28 18:33:33 +08:00
"google.golang.org/protobuf/proto"
2023-03-02 14:41:59 +08:00
)
2023-03-20 19:34:50 +08:00
func NewMsg(c discoveryregistry.SvcDiscoveryRegistry) *Message {
return &Message{c: c, validate: validator.New()}
2023-03-02 14:41:59 +08:00
}
2023-03-20 19:34:50 +08:00
type Message struct {
2023-03-08 17:29:03 +08:00
c discoveryregistry.SvcDiscoveryRegistry
2023-03-06 10:17:55 +08:00
validate *validator.Validate
2023-03-02 14:41:59 +08:00
}
2023-03-20 19:34:50 +08:00
func (Message) SetOptions(options map[string]bool, value bool) {
2023-03-03 19:44:34 +08:00
utils.SetSwitchFromOptions(options, constant.IsHistory, value)
utils.SetSwitchFromOptions(options, constant.IsPersistent, value)
utils.SetSwitchFromOptions(options, constant.IsSenderSync, value)
utils.SetSwitchFromOptions(options, constant.IsConversationUpdate, value)
}
2023-03-20 19:34:50 +08:00
func (m Message) newUserSendMsgReq(c *gin.Context, params *apistruct.ManagementSendMsgReq) *msg.SendMsgReq {
2023-03-03 19:44:34 +08:00
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 {
2023-03-09 20:05:18 +08:00
m.SetOptions(options, false)
2023-03-03 19:44:34 +08:00
}
if params.NotOfflinePush {
utils.SetSwitchFromOptions(options, constant.IsOfflinePush, false)
}
if params.ContentType == constant.CustomOnlineOnly {
2023-03-09 20:05:18 +08:00
m.SetOptions(options, false)
2023-03-03 19:44:34 +08:00
} 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 {
2023-05-22 10:08:04 +08:00
log.ZError(c, "Marshal failed ", err, "tips", tips.String())
2023-03-03 19:44:34 +08:00
}
}
return &pbData
}
2023-05-08 12:39:45 +08:00
func (m *Message) client(ctx context.Context) (msg.MsgClient, error) {
conn, err := m.c.GetConn(ctx, config.Config.RpcRegisterName.OpenImMsgName)
2023-03-02 14:41:59 +08:00
if err != nil {
return nil, err
}
return msg.NewMsgClient(conn), nil
}
2023-03-20 19:34:50 +08:00
func (m *Message) GetSeq(c *gin.Context) {
2023-05-05 12:19:04 +08:00
a2r.Call(msg.MsgClient.GetMaxSeq, m.client, c)
2023-03-02 14:41:59 +08:00
}
2023-03-20 19:34:50 +08:00
func (m *Message) PullMsgBySeqs(c *gin.Context) {
2023-03-09 20:05:18 +08:00
a2r.Call(msg.MsgClient.PullMessageBySeqs, m.client, c)
2023-03-02 14:41:59 +08:00
}
2023-05-22 16:04:12 +08:00
func (m *Message) RevokeMsg(c *gin.Context) {
a2r.Call(msg.MsgClient.RevokeMsg, m.client, c)
}
2023-03-20 19:34:50 +08:00
func (m *Message) SetMessageReactionExtensions(c *gin.Context) {
2023-03-09 20:05:18 +08:00
a2r.Call(msg.MsgClient.SetMessageReactionExtensions, m.client, c)
2023-03-02 14:41:59 +08:00
}
2023-03-20 19:34:50 +08:00
func (m *Message) GetMessageListReactionExtensions(c *gin.Context) {
2023-03-09 20:05:18 +08:00
a2r.Call(msg.MsgClient.GetMessagesReactionExtensions, m.client, c)
2023-03-02 14:41:59 +08:00
}
2023-03-20 19:34:50 +08:00
func (m *Message) AddMessageReactionExtensions(c *gin.Context) {
2023-03-09 20:05:18 +08:00
a2r.Call(msg.MsgClient.AddMessageReactionExtensions, m.client, c)
2023-03-02 14:41:59 +08:00
}
2023-03-20 19:34:50 +08:00
func (m *Message) DeleteMessageReactionExtensions(c *gin.Context) {
2023-03-09 20:05:18 +08:00
a2r.Call(msg.MsgClient.DeleteMessageReactionExtensions, m.client, c)
2023-03-02 14:41:59 +08:00
}
2023-03-20 20:52:52 +08:00
2023-03-20 19:34:50 +08:00
func (m *Message) SendMessage(c *gin.Context) {
2023-03-03 19:44:34 +08:00
params := apistruct.ManagementSendMsgReq{}
if err := c.BindJSON(&params); err != nil {
2023-03-20 19:34:50 +08:00
apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap())
2023-03-03 19:44:34 +08:00
return
}
2023-05-10 10:23:55 +08:00
// todo
//if !tokenverify.IsAppManagerUid(c) {
// apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message"))
// return
//}
2023-03-21 10:18:53 +08:00
2023-03-09 20:05:18 +08:00
var data interface{}
2023-03-03 19:44:34 +08:00
switch params.ContentType {
case constant.Text:
2023-03-09 20:05:18 +08:00
data = apistruct.TextElem{}
2023-03-03 19:44:34 +08:00
case constant.Picture:
2023-03-09 20:05:18 +08:00
data = apistruct.PictureElem{}
2023-03-03 19:44:34 +08:00
case constant.Voice:
2023-03-09 20:05:18 +08:00
data = apistruct.SoundElem{}
2023-03-03 19:44:34 +08:00
case constant.Video:
2023-03-09 20:05:18 +08:00
data = apistruct.VideoElem{}
2023-03-03 19:44:34 +08:00
case constant.File:
2023-03-09 20:05:18 +08:00
data = apistruct.FileElem{}
2023-03-03 19:44:34 +08:00
case constant.Custom:
2023-03-09 20:05:18 +08:00
data = apistruct.CustomElem{}
2023-03-03 19:44:34 +08:00
case constant.Revoke:
2023-03-09 20:05:18 +08:00
data = apistruct.RevokeElem{}
2023-03-03 19:44:34 +08:00
case constant.AdvancedRevoke:
2023-03-09 20:05:18 +08:00
data = apistruct.MessageRevoked{}
2023-03-03 19:44:34 +08:00
case constant.OANotification:
2023-03-09 20:05:18 +08:00
data = apistruct.OANotificationElem{}
2023-03-03 19:44:34 +08:00
params.SessionType = constant.NotificationChatType
case constant.CustomNotTriggerConversation:
2023-03-09 20:05:18 +08:00
data = apistruct.CustomElem{}
2023-03-03 19:44:34 +08:00
case constant.CustomOnlineOnly:
2023-03-09 20:05:18 +08:00
data = apistruct.CustomElem{}
2023-03-03 19:44:34 +08:00
default:
2023-03-20 19:34:50 +08:00
apiresp.GinError(c, errs.ErrArgs.WithDetail("not support err contentType").Wrap())
2023-03-03 19:44:34 +08:00
return
}
if err := mapstructure.WeakDecode(params.Content, &data); err != nil {
2023-03-20 19:34:50 +08:00
apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error()))
2023-03-03 19:44:34 +08:00
return
2023-03-09 20:05:18 +08:00
} else if err := m.validate.Struct(data); err != nil {
2023-03-20 19:34:50 +08:00
apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error()))
2023-03-03 19:44:34 +08:00
return
}
2023-03-09 20:05:18 +08:00
pbReq := m.newUserSendMsgReq(c, &params)
2023-05-08 12:39:45 +08:00
conn, err := m.c.GetConn(c, config.Config.RpcRegisterName.OpenImMsgName)
2023-03-03 19:44:34 +08:00
if err != nil {
2023-03-07 12:19:30 +08:00
apiresp.GinError(c, errs.ErrInternalServer)
2023-03-03 19:44:34 +08:00
return
}
client := msg.NewMsgClient(conn)
2023-03-09 20:05:18 +08:00
var status int
respPb, err := client.SendMsg(c, pbReq)
2023-03-03 19:44:34 +08:00
if err != nil {
2023-03-09 20:05:18 +08:00
status = constant.MsgSendFailed
2023-03-03 19:44:34 +08:00
apiresp.GinError(c, err)
return
}
2023-03-09 20:05:18 +08:00
status = constant.MsgSendSuccessed
_, err = client.SetSendMsgStatus(c, &msg.SetSendMsgStatusReq{
Status: int32(status),
})
if err != nil {
2023-05-06 11:04:28 +08:00
log.ZError(c, "SetSendMsgStatus failed", err)
2023-03-09 20:05:18 +08:00
}
2023-03-21 11:45:48 +08:00
apiresp.GinSuccess(c, respPb)
2023-03-02 14:41:59 +08:00
}
2023-03-20 19:34:50 +08:00
func (m *Message) ManagementBatchSendMsg(c *gin.Context) {
2023-03-09 20:05:18 +08:00
a2r.Call(msg.MsgClient.SendMsg, m.client, c)
2023-03-03 19:44:34 +08:00
}
2023-03-20 19:34:50 +08:00
func (m *Message) CheckMsgIsSendSuccess(c *gin.Context) {
2023-03-09 20:05:18 +08:00
a2r.Call(msg.MsgClient.GetSendMsgStatus, m.client, c)
2023-03-03 19:44:34 +08:00
}
2023-03-20 19:34:50 +08:00
func (m *Message) GetUsersOnlineStatus(c *gin.Context) {
2023-03-09 20:05:18 +08:00
a2r.Call(msg.MsgClient.GetSendMsgStatus, m.client, c)
2023-03-03 19:44:34 +08:00
}