From b24960449d5e0a69c8aebcb5978b6163ad20ac05 Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Mon, 20 Mar 2023 20:42:02 +0800 Subject: [PATCH] user update --- internal/api/msg.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/internal/api/msg.go b/internal/api/msg.go index e12c9ce45..c4eaca706 100644 --- a/internal/api/msg.go +++ b/internal/api/msg.go @@ -15,6 +15,7 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" "github.com/go-playground/validator/v10" "github.com/golang/protobuf/proto" "github.com/mitchellh/mapstructure" @@ -148,13 +149,31 @@ func (m *Message) AddMessageReactionExtensions(c *gin.Context) { func (m *Message) DeleteMessageReactionExtensions(c *gin.Context) { a2r.Call(msg.MsgClient.DeleteMessageReactionExtensions, m.client, c) } - +func RequiredIf(fl validator.FieldLevel) bool { + sessionType := fl.Parent().FieldByName("SessionType").Int() + switch sessionType { + case 1, 4: + if fl.StructFieldName() == "RecvID" { + return fl.Field().String() != "" + } + case 2, 3: + if fl.StructFieldName() == "GroupID" { + return fl.Field().String() != "" + } + default: + return true + } + return false +} func (m *Message) SendMessage(c *gin.Context) { params := apistruct.ManagementSendMsgReq{} if err := c.BindJSON(¶ms); err != nil { apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) return } + if v, ok := binding.Validator.Engine().(*validator.Validate); ok { + _ = v.RegisterValidation("required_if", RequiredIf) + } var data interface{} switch params.ContentType { case constant.Text: