refactor(core): simplify message gateway handler initialization

This commit is contained in:
dsx137 2026-11-04 17:20:00 +08:00
parent a714add2de
commit 31df12dba4
3 changed files with 29 additions and 48 deletions

View File

@ -22,7 +22,6 @@ import (
"github.com/go-playground/validator/v10"
"google.golang.org/protobuf/proto"
"github.com/openimsdk/open-im-server/v3/pkg/authverify"
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
"github.com/openimsdk/open-im-server/v3/pkg/rpcli"
"github.com/openimsdk/protocol/constant"
@ -117,18 +116,16 @@ type MessageHandler interface {
var _ MessageHandler = (*GrpcHandler)(nil)
type GrpcHandler struct {
imAdminUserID []string
validate *validator.Validate
msgClient *rpcli.MsgClient
pushClient *rpcli.PushMsgServiceClient
validate *validator.Validate
msgClient *rpcli.MsgClient
pushClient *rpcli.PushMsgServiceClient
}
func NewGrpcHandler(validate *validator.Validate, msgClient *rpcli.MsgClient, pushClient *rpcli.PushMsgServiceClient, imAdminUserID []string) *GrpcHandler {
func NewGrpcHandler(validate *validator.Validate, msgClient *rpcli.MsgClient, pushClient *rpcli.PushMsgServiceClient) *GrpcHandler {
return &GrpcHandler{
imAdminUserID: imAdminUserID,
validate: validate,
msgClient: msgClient,
pushClient: pushClient,
validate: validate,
msgClient: msgClient,
pushClient: pushClient,
}
}
@ -158,16 +155,11 @@ func (g *GrpcHandler) SendMessage(ctx context.Context, data *Req) ([]byte, error
if err := proto.Unmarshal(data.Data, &msgData); err != nil {
return nil, errs.WrapMsg(err, "SendMessage: error unmarshaling message data", "action", "unmarshal", "dataType", "MsgData")
}
if err := authverify.CheckAccessV3(ctx, msgData.SendID, g.imAdminUserID); err != nil {
return nil, err
if msgData.ContentType >= constant.NotificationBegin && msgData.ContentType <= constant.NotificationEnd {
return nil, errs.ErrNoPermission.WrapMsg("SendMessage: clients cannot send notification content types")
}
if !authverify.IsAppManagerUid(ctx, g.imAdminUserID) {
if msgData.ContentType >= constant.NotificationBegin && msgData.ContentType <= constant.NotificationEnd {
return nil, errs.ErrNoPermission.WrapMsg("SendMessage: clients cannot send notification content types")
}
if msgData.SessionType == constant.NotificationChatType || msgprocessor.IsNotificationByMsg(&msgData) {
return nil, errs.ErrNoPermission.WrapMsg("SendMessage: clients cannot send to notification sessions")
}
if msgData.SessionType == constant.NotificationChatType || msgprocessor.IsNotificationByMsg(&msgData) {
return nil, errs.ErrNoPermission.WrapMsg("SendMessage: clients cannot send to notification sessions")
}
if err := g.validate.Struct(&msgData); err != nil {
@ -205,9 +197,6 @@ func (g *GrpcHandler) PullMessageBySeqList(ctx context.Context, data *Req) ([]by
if err := proto.Unmarshal(data.Data, &req); err != nil {
return nil, errs.WrapMsg(err, "err proto unmarshal", "action", "unmarshal", "dataType", "PullMessageBySeqsReq")
}
if err := authverify.CheckAccessV3(ctx, req.UserID, g.imAdminUserID); err != nil {
return nil, err
}
if err := g.validate.Struct(data); err != nil {
return nil, errs.WrapMsg(err, "validation failed", "action", "validate", "dataType", "PullMessageBySeqsReq")
}
@ -227,9 +216,6 @@ func (g *GrpcHandler) GetConversationsHasReadAndMaxSeq(ctx context.Context, data
if err := proto.Unmarshal(data.Data, &req); err != nil {
return nil, errs.WrapMsg(err, "err proto unmarshal", "action", "unmarshal", "dataType", "GetConversationsHasReadAndMaxSeq")
}
if err := authverify.CheckAccessV3(ctx, req.UserID, g.imAdminUserID); err != nil {
return nil, err
}
if err := g.validate.Struct(data); err != nil {
return nil, errs.WrapMsg(err, "validation failed", "action", "validate", "dataType", "GetConversationsHasReadAndMaxSeq")
}
@ -249,9 +235,6 @@ func (g *GrpcHandler) GetSeqMessage(ctx context.Context, data *Req) ([]byte, err
if err := proto.Unmarshal(data.Data, &req); err != nil {
return nil, errs.WrapMsg(err, "error unmarshaling request", "action", "unmarshal", "dataType", "GetSeqMessage")
}
if err := authverify.CheckAccessV3(ctx, req.UserID, g.imAdminUserID); err != nil {
return nil, err
}
if err := g.validate.Struct(data); err != nil {
return nil, errs.WrapMsg(err, "validation failed", "action", "validate", "dataType", "GetSeqMessage")
}
@ -271,9 +254,6 @@ func (g *GrpcHandler) UserLogout(ctx context.Context, data *Req) ([]byte, error)
if err := proto.Unmarshal(data.Data, &req); err != nil {
return nil, errs.WrapMsg(err, "error unmarshaling request", "action", "unmarshal", "dataType", "DelUserPushTokenReq")
}
if err := authverify.CheckAccessV3(ctx, req.UserID, g.imAdminUserID); err != nil {
return nil, err
}
resp, err := g.pushClient.PushMsgServiceClient.DelUserPushToken(ctx, &req)
if err != nil {
return nil, err
@ -301,9 +281,6 @@ func (g *GrpcHandler) GetLastMessage(ctx context.Context, data *Req) ([]byte, er
if err := proto.Unmarshal(data.Data, &req); err != nil {
return nil, err
}
if err := authverify.CheckAccessV3(ctx, req.UserID, g.imAdminUserID); err != nil {
return nil, err
}
resp, err := g.msgClient.GetLastMessage(ctx, &req)
if err != nil {
return nil, err

View File

@ -98,7 +98,7 @@ func (ws *WsServer) SetDiscoveryRegistry(ctx context.Context, disCov discovery.S
}
ws.userClient = rpcli.NewUserClient(userConn)
ws.authClient = rpcli.NewAuthClient(authConn)
ws.MessageHandler = NewGrpcHandler(ws.validate, rpcli.NewMsgClient(msgConn), rpcli.NewPushMsgServiceClient(pushConn), config.Share.IMAdminUserID)
ws.MessageHandler = NewGrpcHandler(ws.validate, rpcli.NewMsgClient(msgConn), rpcli.NewPushMsgServiceClient(pushConn))
ws.disCov = disCov
return nil
}

View File

@ -17,6 +17,7 @@ package msg
import (
"context"
"github.com/openimsdk/open-im-server/v3/pkg/authverify"
"github.com/openimsdk/open-im-server/v3/pkg/common/prommetrics"
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
"github.com/openimsdk/open-im-server/v3/pkg/util/conversationutil"
@ -32,20 +33,23 @@ import (
)
func (m *msgServer) SendMsg(ctx context.Context, req *pbmsg.SendMsgReq) (*pbmsg.SendMsgResp, error) {
if req.MsgData != nil {
m.encapsulateMsgData(req.MsgData)
switch req.MsgData.SessionType {
case constant.SingleChatType:
return m.sendMsgSingleChat(ctx, req)
case constant.NotificationChatType:
return m.sendMsgNotification(ctx, req)
case constant.ReadGroupChatType:
return m.sendMsgGroupChat(ctx, req)
default:
return nil, errs.ErrArgs.WrapMsg("unknown sessionType")
}
if req.MsgData == nil {
return nil, errs.ErrArgs.WrapMsg("msgData is nil")
}
if err := authverify.CheckAccessV3(ctx, req.MsgData.SendID, m.config.Share.IMAdminUserID); err != nil {
return nil, err
}
m.encapsulateMsgData(req.MsgData)
switch req.MsgData.SessionType {
case constant.SingleChatType:
return m.sendMsgSingleChat(ctx, req)
case constant.NotificationChatType:
return m.sendMsgNotification(ctx, req)
case constant.ReadGroupChatType:
return m.sendMsgGroupChat(ctx, req)
default:
return nil, errs.ErrArgs.WrapMsg("unknown sessionType")
}
return nil, errs.ErrArgs.WrapMsg("msgData is nil")
}
func (m *msgServer) sendMsgGroupChat(ctx context.Context, req *pbmsg.SendMsgReq) (resp *pbmsg.SendMsgResp, err error) {