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.5 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"
"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"
"google.golang.org/grpc"
2 years ago
"google.golang.org/protobuf/proto"
2 years ago
)
1 year ago
func NewMsg(discov discoveryregistry.SvcDiscoveryRegistry) *Message {
1 year ago
// conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImMsgName)
// if err != nil {
// panic(err)
// }
return &Message{validate: validator.New(), discov: discov}
2 years ago
}
2 years ago
type Message struct {
conn *grpc.ClientConn
2 years ago
validate *validator.Validate
1 year ago
discov discoveryregistry.SvcDiscoveryRegistry
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.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.ZError(c, "Marshal failed ", err, "tips", tips.String())
2 years ago
}
}
return &pbData
}
2 years ago
func (m *Message) client(ctx context.Context) (msg.MsgClient, error) {
1 year ago
c, err := m.discov.GetConn(ctx, config.Config.RpcRegisterName.OpenImMsgName)
if err != nil {
return nil, err
}
return msg.NewMsgClient(c), nil
2 years ago
}
2 years ago
func (m *Message) GetSeq(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.GetMaxSeq, 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) RevokeMsg(c *gin.Context) {
a2r.Call(msg.MsgClient.RevokeMsg, m.client, c)
}
1 year ago
func (m *Message) MarkMsgsAsRead(c *gin.Context) {
a2r.Call(msg.MsgClient.MarkMsgsAsRead, m.client, c)
}
1 year ago
func (m *Message) GetConversationsHasReadAndMaxSeq(c *gin.Context) {
a2r.Call(msg.MsgClient.GetConversationsHasReadAndMaxSeq, m.client, c)
}
2 years ago
func (m *Message) ClearConversationsMsg(c *gin.Context) {
a2r.Call(msg.MsgClient.ClearConversationsMsg, m.client, c)
}
func (m *Message) UserClearAllMsg(c *gin.Context) {
a2r.Call(msg.MsgClient.UserClearAllMsg, m.client, c)
}
func (m *Message) DeleteMsgs(c *gin.Context) {
a2r.Call(msg.MsgClient.DeleteMsgs, m.client, c)
}
func (m *Message) DeleteMsgPhysicalBySeq(c *gin.Context) {
a2r.Call(msg.MsgClient.DeleteMsgPhysicalBySeq, m.client, c)
}
func (m *Message) DeleteMsgPhysical(c *gin.Context) {
a2r.Call(msg.MsgClient.DeleteMsgPhysical, m.client, c)
}
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
}
2 years ago
// todo
//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.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)
client := msg.NewMsgClient(m.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.ZError(c, "SetSendMsgStatus failed", err)
2 years ago
}
2 years ago
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
}