Files
open-im-server/internal/msggateway/new/message_handler.go
T

150 lines
4.1 KiB
Go
Raw Normal View History

2023-02-14 21:08:36 +08:00
package new
2023-02-16 16:32:31 +08:00
import (
2023-02-23 19:15:30 +08:00
"OpenIM/internal/common/notification"
"OpenIM/pkg/proto/msg"
pbRtc "OpenIM/pkg/proto/rtc"
"OpenIM/pkg/proto/sdkws"
2023-02-16 16:32:31 +08:00
"context"
2023-02-22 21:06:55 +08:00
"github.com/go-playground/validator/v10"
"github.com/golang/protobuf/proto"
2023-02-16 16:32:31 +08:00
)
2023-02-14 21:08:36 +08:00
type Req struct {
ReqIdentifier int32 `json:"reqIdentifier" validate:"required"`
Token string `json:"token" `
SendID string `json:"sendID" validate:"required"`
OperationID string `json:"operationID" validate:"required"`
MsgIncr string `json:"msgIncr" validate:"required"`
Data []byte `json:"data"`
}
2023-02-15 19:57:16 +08:00
type Resp struct {
ReqIdentifier int32 `json:"reqIdentifier"`
MsgIncr string `json:"msgIncr"`
OperationID string `json:"operationID"`
ErrCode int32 `json:"errCode"`
ErrMsg string `json:"errMsg"`
Data []byte `json:"data"`
}
2023-02-14 21:08:36 +08:00
type MessageHandler interface {
GetSeq(context context.Context, data Req) ([]byte, error)
SendMessage(context context.Context, data Req) ([]byte, error)
SendSignalMessage(context context.Context, data Req) ([]byte, error)
PullMessageBySeqList(context context.Context, data Req) ([]byte, error)
UserLogout(context context.Context, data Req) ([]byte, error)
2023-02-22 21:06:55 +08:00
SetUserDeviceBackground(context context.Context, data Req) ([]byte, bool, error)
2023-02-14 21:08:36 +08:00
}
var _ MessageHandler = (*GrpcHandler)(nil)
type GrpcHandler struct {
2023-02-22 21:06:55 +08:00
notification *notification.Check
validate *validator.Validate
2023-02-16 16:32:31 +08:00
}
2023-02-22 21:06:55 +08:00
func NewGrpcHandler(validate *validator.Validate, notification *notification.Check) *GrpcHandler {
return &GrpcHandler{notification: notification, validate: validate}
2023-02-14 21:08:36 +08:00
}
func (g GrpcHandler) GetSeq(context context.Context, data Req) ([]byte, error) {
2023-02-22 21:06:55 +08:00
req := sdkws.GetMaxAndMinSeqReq{}
if err := proto.Unmarshal(data.Data, &req); err != nil {
return nil, err
}
if err := g.validate.Struct(req); err != nil {
return nil, err
}
resp, err := g.notification.Msg.GetMaxAndMinSeq(context, &req)
if err != nil {
return nil, err
}
c, err := proto.Marshal(resp)
if err != nil {
return nil, err
}
return c, nil
2023-02-14 21:08:36 +08:00
}
func (g GrpcHandler) SendMessage(context context.Context, data Req) ([]byte, error) {
2023-02-22 21:06:55 +08:00
msgData := sdkws.MsgData{}
if err := proto.Unmarshal(data.Data, &msgData); err != nil {
return nil, err
}
if err := g.validate.Struct(msgData); err != nil {
return nil, err
}
req := msg.SendMsgReq{MsgData: &msgData}
resp, err := g.notification.Msg.SendMsg(context, &req)
if err != nil {
return nil, err
}
c, err := proto.Marshal(resp)
if err != nil {
return nil, err
}
return c, nil
2023-02-14 21:08:36 +08:00
}
func (g GrpcHandler) SendSignalMessage(context context.Context, data Req) ([]byte, error) {
2023-02-22 21:06:55 +08:00
signalReq := pbRtc.SignalReq{}
if err := proto.Unmarshal(data.Data, &signalReq); err != nil {
return nil, err
}
if err := g.validate.Struct(signalReq); err != nil {
return nil, err
}
//req := pbRtc.SignalMessageAssembleReq{SignalReq: &signalReq, OperationID: "111"}
//todo rtc rpc call
resp, err := g.notification.Msg.SendMsg(context, nil)
if err != nil {
return nil, err
}
c, err := proto.Marshal(resp)
if err != nil {
return nil, err
}
return c, nil
2023-02-14 21:08:36 +08:00
}
func (g GrpcHandler) PullMessageBySeqList(context context.Context, data Req) ([]byte, error) {
2023-02-22 21:06:55 +08:00
req := sdkws.PullMessageBySeqListReq{}
if err := proto.Unmarshal(data.Data, &req); err != nil {
return nil, err
}
if err := g.validate.Struct(data); err != nil {
return nil, err
}
resp, err := g.notification.Msg.PullMessageBySeqList(context, &req)
if err != nil {
return nil, err
}
c, err := proto.Marshal(resp)
if err != nil {
return nil, err
}
return c, nil
2023-02-14 21:08:36 +08:00
}
func (g GrpcHandler) UserLogout(context context.Context, data Req) ([]byte, error) {
2023-02-22 21:06:55 +08:00
//todo
resp, err := g.notification.Msg.PullMessageBySeqList(context, nil)
if err != nil {
return nil, err
}
c, err := proto.Marshal(resp)
if err != nil {
return nil, err
}
return c, nil
2023-02-14 21:08:36 +08:00
}
2023-02-22 21:06:55 +08:00
func (g GrpcHandler) SetUserDeviceBackground(_ context.Context, data Req) ([]byte, bool, error) {
req := sdkws.SetAppBackgroundStatusReq{}
if err := proto.Unmarshal(data.Data, &req); err != nil {
return nil, false, err
}
if err := g.validate.Struct(data); err != nil {
return nil, false, err
}
return nil, req.IsBackground, nil
2023-02-14 21:08:36 +08:00
}