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/msggateway/message_handler.go

181 lines
4.9 KiB

2 years ago
package msggateway
import (
"context"
2 years ago
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2 years ago
"github.com/go-playground/validator/v10"
"github.com/golang/protobuf/proto"
)
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"`
}
2 years ago
func (r *Req) String() string {
return utils.StructToJsonString(r)
}
2 years ago
type Resp struct {
ReqIdentifier int32 `json:"reqIdentifier"`
MsgIncr string `json:"msgIncr"`
OperationID string `json:"operationID"`
2 years ago
ErrCode int `json:"errCode"`
2 years ago
ErrMsg string `json:"errMsg"`
Data []byte `json:"data"`
}
2 years ago
func (r *Resp) String() string {
return utils.StructToJsonString(r)
}
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)
2 years ago
SetUserDeviceBackground(context context.Context, data Req) ([]byte, bool, error)
}
var _ MessageHandler = (*GrpcHandler)(nil)
type GrpcHandler struct {
2 years ago
msgRpcClient *rpcclient.MsgClient
2 years ago
validate *validator.Validate
}
2 years ago
func NewGrpcHandler(validate *validator.Validate, msgRpcClient *rpcclient.MsgClient) *GrpcHandler {
return &GrpcHandler{msgRpcClient: msgRpcClient, validate: validate}
}
func (g GrpcHandler) GetSeq(context context.Context, data Req) ([]byte, error) {
2 years ago
req := sdkws.GetMaxAndMinSeqReq{}
if err := proto.Unmarshal(data.Data, &req); err != nil {
return nil, err
}
2 years ago
if err := g.validate.Struct(&req); err != nil {
2 years ago
return nil, err
}
2 years ago
resp, err := g.msgRpcClient.GetMaxAndMinSeq(context, &req)
2 years ago
if err != nil {
return nil, err
}
c, err := proto.Marshal(resp)
if err != nil {
return nil, err
}
return c, nil
}
func (g GrpcHandler) SendMessage(context context.Context, data Req) ([]byte, error) {
2 years ago
msgData := sdkws.MsgData{}
if err := proto.Unmarshal(data.Data, &msgData); err != nil {
return nil, err
}
2 years ago
if err := g.validate.Struct(&msgData); err != nil {
2 years ago
return nil, err
}
req := msg.SendMsgReq{MsgData: &msgData}
2 years ago
resp, err := g.msgRpcClient.SendMsg(context, &req)
2 years ago
if err != nil {
return nil, err
}
c, err := proto.Marshal(resp)
if err != nil {
return nil, err
}
return c, nil
}
func (g GrpcHandler) SendSignalMessage(context context.Context, data Req) ([]byte, error) {
2 years ago
signalReq := sdkws.SignalReq{}
2 years ago
if err := proto.Unmarshal(data.Data, &signalReq); err != nil {
return nil, err
}
2 years ago
if err := g.validate.Struct(&signalReq); err != nil {
2 years ago
return nil, err
}
//req := pbRtc.SignalMessageAssembleReq{SignalReq: &signalReq, OperationID: "111"}
//todo rtc rpc call
2 years ago
resp, err := g.msgRpcClient.SendMsg(context, nil)
2 years ago
if err != nil {
return nil, err
}
c, err := proto.Marshal(resp)
if err != nil {
return nil, err
}
return c, nil
}
func (g GrpcHandler) PullMessageBySeqList(context context.Context, data Req) ([]byte, error) {
2 years ago
req := sdkws.PullMessageBySeqsReq{}
2 years ago
if err := proto.Unmarshal(data.Data, &req); err != nil {
return nil, err
}
if err := g.validate.Struct(data); err != nil {
return nil, err
}
2 years ago
resp, err := g.msgRpcClient.PullMessageBySeqList(context, &req)
2 years ago
if err != nil {
return nil, err
}
c, err := proto.Marshal(resp)
if err != nil {
return nil, err
}
return c, nil
}
func (g GrpcHandler) UserLogout(context context.Context, data Req) ([]byte, error) {
2 years ago
//todo
2 years ago
resp, err := g.msgRpcClient.PullMessageBySeqList(context, nil)
2 years ago
if err != nil {
return nil, err
}
c, err := proto.Marshal(resp)
if err != nil {
return nil, err
}
return c, nil
}
2 years ago
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
}
2 years ago
//func (g GrpcHandler) call[T any](ctx context.Context, data Req, m proto.Message, rpc func(ctx context.Context, req proto.Message)) ([]byte, error) {
// if err := proto.Unmarshal(data.Data, m); err != nil {
// return nil, err
// }
// if err := g.validate.Struct(m); err != nil {
// return nil, err
// }
// rpc(ctx, m)
// 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
//}