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

344 lines
11 KiB

2 years ago
package api
import (
"OpenIM/internal/api/a2r"
2 years ago
"OpenIM/internal/apiresp"
"OpenIM/pkg/apistruct"
2 years ago
"OpenIM/pkg/common/config"
2 years ago
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/log"
2 years ago
"OpenIM/pkg/discoveryregistry"
2 years ago
"OpenIM/pkg/errs"
2 years ago
"OpenIM/pkg/proto/msg"
2 years ago
"OpenIM/pkg/proto/sdkws"
"OpenIM/pkg/utils"
2 years ago
"context"
2 years ago
"errors"
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 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)
}
func newUserSendMsgReq(params *apistruct.ManagementSendMsgReq) *msg.SendMsgReq {
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 {
SetOptions(options, false)
}
if params.NotOfflinePush {
utils.SetSwitchFromOptions(options, constant.IsOfflinePush, false)
}
if params.ContentType == constant.CustomOnlineOnly {
SetOptions(options, false)
} 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 {
log.Error(params.OperationID, "Marshal failed ", err.Error(), tips.String())
}
}
return &pbData
}
2 years ago
func (o *Msg) client() (msg.MsgClient, error) {
2 years ago
conn, err := o.c.GetConn(config.Config.RpcRegisterName.OpenImMsgName)
2 years ago
if err != nil {
return nil, err
}
return msg.NewMsgClient(conn), nil
}
func (o *Msg) GetSeq(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.GetMaxAndMinSeq, o.client, c)
2 years ago
}
func (o *Msg) SendMsg(c *gin.Context) {
a2r.Call(msg.MsgClient.SendMsg, o.client, c)
}
2 years ago
func (o *Msg) PullMsgBySeqs(c *gin.Context) {
a2r.Call(msg.MsgClient.PullMessageBySeqs, o.client, c)
2 years ago
}
func (o *Msg) DelMsg(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.DelMsgs, o.client, c)
2 years ago
}
func (o *Msg) DelSuperGroupMsg(c *gin.Context) {
a2r.Call(msg.MsgClient.DelSuperGroupMsg, o.client, c)
}
func (o *Msg) ClearMsg(c *gin.Context) {
a2r.Call(msg.MsgClient.ClearMsg, o.client, c)
}
func (o *Msg) SetMessageReactionExtensions(c *gin.Context) {
a2r.Call(msg.MsgClient.SetMessageReactionExtensions, o.client, c)
}
func (o *Msg) GetMessageListReactionExtensions(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.GetMessagesReactionExtensions, o.client, c)
2 years ago
}
func (o *Msg) AddMessageReactionExtensions(c *gin.Context) {
a2r.Call(msg.MsgClient.AddMessageReactionExtensions, o.client, c)
}
func (o *Msg) DeleteMessageReactionExtensions(c *gin.Context) {
a2r.Call(msg.MsgClient.DeleteMessageReactionExtensions, o.client, c)
}
func (o *Msg) ManagementSendMsg(c *gin.Context) {
2 years ago
var data interface{}
params := apistruct.ManagementSendMsgReq{}
if err := c.BindJSON(&params); err != nil {
apiresp.GinError(c, err)
return
}
switch params.ContentType {
case constant.Text:
data = TextElem{}
case constant.Picture:
data = PictureElem{}
case constant.Voice:
data = SoundElem{}
case constant.Video:
data = VideoElem{}
case constant.File:
data = FileElem{}
case constant.Custom:
data = CustomElem{}
case constant.Revoke:
data = RevokeElem{}
case constant.AdvancedRevoke:
data = MessageRevoked{}
case constant.OANotification:
data = OANotificationElem{}
params.SessionType = constant.NotificationChatType
case constant.CustomNotTriggerConversation:
data = CustomElem{}
case constant.CustomOnlineOnly:
data = CustomElem{}
//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 := o.validate.Struct(data); err != nil {
2 years ago
apiresp.GinError(c, errs.ErrData)
2 years ago
return
}
log.NewInfo(params.OperationID, data, params)
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
}
}
log.NewInfo(params.OperationID, "Ws call success to ManagementSendMsgReq", params)
pbData := newUserSendMsgReq(&params)
2 years ago
conn, err := o.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)
log.Info(params.OperationID, "", "api ManagementSendMsg call, api call rpc...")
//var status int32
RpcResp, err := client.SendMsg(context.Background(), pbData)
if err != nil {
//status = constant.MsgSendFailed
apiresp.GinError(c, err)
return
}
//status = constant.MsgSendSuccessed
//_, err2 := client.SetSendMsgStatus(context.Background(), &msg.SetSendMsgStatusReq{OperationID: params.OperationID, Status: status})
//if err2 != nil {
// log.NewError(params.OperationID, utils.GetSelfFuncName(), err2.Error())
//}
log.Info(params.OperationID, "", "api ManagementSendMsg call end..., [data: %s] [reply: %s]", pbData.String(), RpcResp.String())
resp := apistruct.ManagementSendMsgResp{ResultList: sdkws.UserSendMsgResp{ServerMsgID: RpcResp.ServerMsgID, ClientMsgID: RpcResp.ClientMsgID, SendTime: RpcResp.SendTime}}
log.Info(params.OperationID, "ManagementSendMsg return", resp)
2 years ago
apiresp.GinSuccess(c, resp)
2 years ago
}
func (o *Msg) ManagementBatchSendMsg(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.SendMsg, o.client, c)
2 years ago
}
func (o *Msg) CheckMsgIsSendSuccess(c *gin.Context) {
2 years ago
a2r.Call(msg.MsgClient.GetSendMsgStatus, o.client, c)
}
func (o *Msg) GetUsersOnlineStatus(c *gin.Context) {
a2r.Call(msg.MsgClient.GetSendMsgStatus, o.client, c)
}
func (o *Msg) AccountCheck(c *gin.Context) {
a2r.Call(msg.MsgClient.GetSendMsgStatus, o.client, c)
}
type PictureBaseInfo struct {
UUID string `mapstructure:"uuid"`
Type string `mapstructure:"type" `
Size int64 `mapstructure:"size" `
Width int32 `mapstructure:"width" `
Height int32 `mapstructure:"height"`
Url string `mapstructure:"url" `
}
type PictureElem struct {
SourcePath string `mapstructure:"sourcePath"`
SourcePicture PictureBaseInfo `mapstructure:"sourcePicture"`
BigPicture PictureBaseInfo `mapstructure:"bigPicture" `
SnapshotPicture PictureBaseInfo `mapstructure:"snapshotPicture"`
}
type SoundElem struct {
UUID string `mapstructure:"uuid"`
SoundPath string `mapstructure:"soundPath"`
SourceURL string `mapstructure:"sourceUrl"`
DataSize int64 `mapstructure:"dataSize"`
Duration int64 `mapstructure:"duration"`
}
type VideoElem struct {
VideoPath string `mapstructure:"videoPath"`
VideoUUID string `mapstructure:"videoUUID"`
VideoURL string `mapstructure:"videoUrl"`
VideoType string `mapstructure:"videoType"`
VideoSize int64 `mapstructure:"videoSize"`
Duration int64 `mapstructure:"duration"`
SnapshotPath string `mapstructure:"snapshotPath"`
SnapshotUUID string `mapstructure:"snapshotUUID"`
SnapshotSize int64 `mapstructure:"snapshotSize"`
SnapshotURL string `mapstructure:"snapshotUrl"`
SnapshotWidth int32 `mapstructure:"snapshotWidth"`
SnapshotHeight int32 `mapstructure:"snapshotHeight"`
}
type FileElem struct {
FilePath string `mapstructure:"filePath"`
UUID string `mapstructure:"uuid"`
SourceURL string `mapstructure:"sourceUrl"`
FileName string `mapstructure:"fileName"`
FileSize int64 `mapstructure:"fileSize"`
}
type AtElem struct {
Text string `mapstructure:"text"`
AtUserList []string `mapstructure:"atUserList"`
IsAtSelf bool `mapstructure:"isAtSelf"`
}
type LocationElem struct {
Description string `mapstructure:"description"`
Longitude float64 `mapstructure:"longitude"`
Latitude float64 `mapstructure:"latitude"`
}
type CustomElem struct {
Data string `mapstructure:"data" validate:"required"`
Description string `mapstructure:"description"`
Extension string `mapstructure:"extension"`
}
type TextElem struct {
Text string `mapstructure:"text" validate:"required"`
}
type RevokeElem struct {
RevokeMsgClientID string `mapstructure:"revokeMsgClientID" validate:"required"`
}
type OANotificationElem struct {
NotificationName string `mapstructure:"notificationName" json:"notificationName" validate:"required"`
NotificationFaceURL string `mapstructure:"notificationFaceURL" json:"notificationFaceURL"`
NotificationType int32 `mapstructure:"notificationType" json:"notificationType" validate:"required"`
Text string `mapstructure:"text" json:"text" validate:"required"`
Url string `mapstructure:"url" json:"url"`
MixType int32 `mapstructure:"mixType" json:"mixType"`
PictureElem PictureElem `mapstructure:"pictureElem" json:"pictureElem"`
SoundElem SoundElem `mapstructure:"soundElem" json:"soundElem"`
VideoElem VideoElem `mapstructure:"videoElem" json:"videoElem"`
FileElem FileElem `mapstructure:"fileElem" json:"fileElem"`
Ex string `mapstructure:"ex" json:"ex"`
}
type MessageRevoked struct {
RevokerID string `mapstructure:"revokerID" json:"revokerID" validate:"required"`
RevokerRole int32 `mapstructure:"revokerRole" json:"revokerRole" validate:"required"`
ClientMsgID string `mapstructure:"clientMsgID" json:"clientMsgID" validate:"required"`
RevokerNickname string `mapstructure:"revokerNickname" json:"revokerNickname"`
SessionType int32 `mapstructure:"sessionType" json:"sessionType" validate:"required"`
Seq uint32 `mapstructure:"seq" json:"seq" validate:"required"`
2 years ago
}