mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-08 11:05:59 +08:00
feat: Sending messages supports returning fields modified by webhook
This commit is contained in:
+80
-4
@@ -17,10 +17,12 @@ package api
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"sync"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/apistruct"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/authverify"
|
||||
@@ -41,6 +43,39 @@ import (
|
||||
"github.com/openimsdk/tools/utils/timeutil"
|
||||
)
|
||||
|
||||
var (
|
||||
msgDataDescriptor []protoreflect.FieldDescriptor
|
||||
msgDataDescriptorOnce sync.Once
|
||||
)
|
||||
|
||||
func getMsgDataDescriptor() []protoreflect.FieldDescriptor {
|
||||
msgDataDescriptorOnce.Do(func() {
|
||||
skip := make(map[string]struct{})
|
||||
respFields := new(msg.SendMsgResp).ProtoReflect().Descriptor().Fields()
|
||||
for i := 0; i < respFields.Len(); i++ {
|
||||
field := respFields.Get(i)
|
||||
if !field.HasJSONName() {
|
||||
continue
|
||||
}
|
||||
skip[field.JSONName()] = struct{}{}
|
||||
}
|
||||
fields := new(sdkws.MsgData).ProtoReflect().Descriptor().Fields()
|
||||
num := fields.Len()
|
||||
msgDataDescriptor = make([]protoreflect.FieldDescriptor, 0, num)
|
||||
for i := 0; i < num; i++ {
|
||||
field := fields.Get(i)
|
||||
if !field.HasJSONName() {
|
||||
continue
|
||||
}
|
||||
if _, ok := skip[field.JSONName()]; ok {
|
||||
continue
|
||||
}
|
||||
msgDataDescriptor = append(msgDataDescriptor, fields.Get(i))
|
||||
}
|
||||
})
|
||||
return msgDataDescriptor
|
||||
}
|
||||
|
||||
type MessageApi struct {
|
||||
Client msg.MsgClient
|
||||
userClient *rpcli.UserClient
|
||||
@@ -197,6 +232,42 @@ func (m *MessageApi) getSendMsgReq(c *gin.Context, req apistruct.SendMsg) (sendM
|
||||
return m.newUserSendMsgReq(c, &req), nil
|
||||
}
|
||||
|
||||
func (m *MessageApi) getModifyFields(req, respModify *sdkws.MsgData) map[string]any {
|
||||
if req == nil || respModify == nil {
|
||||
return nil
|
||||
}
|
||||
fields := make(map[string]any)
|
||||
reqProtoReflect := req.ProtoReflect()
|
||||
respProtoReflect := respModify.ProtoReflect()
|
||||
for _, descriptor := range getMsgDataDescriptor() {
|
||||
reqValue := reqProtoReflect.Get(descriptor)
|
||||
respValue := respProtoReflect.Get(descriptor)
|
||||
if !reqValue.Equal(respValue) {
|
||||
val := respValue.Interface()
|
||||
name := descriptor.JSONName()
|
||||
if name == "content" {
|
||||
if bs, ok := val.([]byte); ok {
|
||||
val = string(bs)
|
||||
}
|
||||
}
|
||||
fields[name] = val
|
||||
}
|
||||
}
|
||||
if len(fields) == 0 {
|
||||
fields = nil
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
func (m *MessageApi) ginRespSendMsg(c *gin.Context, req *msg.SendMsgReq, resp *msg.SendMsgResp) {
|
||||
res := m.getModifyFields(req.MsgData, resp.Modify)
|
||||
resp.Modify = nil
|
||||
apiresp.GinSuccess(c, &apistruct.SendMsgResp{
|
||||
SendMsgResp: resp,
|
||||
Modify: res,
|
||||
})
|
||||
}
|
||||
|
||||
// SendMessage handles the sending of a message. It's an HTTP handler function to be used with Gin framework.
|
||||
func (m *MessageApi) SendMessage(c *gin.Context) {
|
||||
// Initialize a request struct for sending a message.
|
||||
@@ -250,7 +321,7 @@ func (m *MessageApi) SendMessage(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Respond with a success message and the response payload.
|
||||
apiresp.GinSuccess(c, respPb)
|
||||
m.ginRespSendMsg(c, sendMsgReq, respPb)
|
||||
}
|
||||
|
||||
func (m *MessageApi) SendBusinessNotification(c *gin.Context) {
|
||||
@@ -316,7 +387,7 @@ func (m *MessageApi) SendBusinessNotification(c *gin.Context) {
|
||||
apiresp.GinError(c, err)
|
||||
return
|
||||
}
|
||||
apiresp.GinSuccess(c, respPb)
|
||||
m.ginRespSendMsg(c, &sendMsgReq, respPb)
|
||||
}
|
||||
|
||||
func (m *MessageApi) BatchSendMsg(c *gin.Context) {
|
||||
@@ -370,6 +441,7 @@ func (m *MessageApi) BatchSendMsg(c *gin.Context) {
|
||||
ClientMsgID: rpcResp.ClientMsgID,
|
||||
SendTime: rpcResp.SendTime,
|
||||
RecvID: recvID,
|
||||
Modify: m.getModifyFields(sendMsgReq.MsgData, rpcResp.Modify),
|
||||
})
|
||||
}
|
||||
apiresp.GinSuccess(c, resp)
|
||||
@@ -432,7 +504,11 @@ func (m *MessageApi) SendSimpleMessage(c *gin.Context) {
|
||||
Ex: req.Ex,
|
||||
}
|
||||
|
||||
respPb, err := m.Client.SendMsg(c, &msg.SendMsgReq{MsgData: msgData})
|
||||
sendReq := &msg.SendMsgReq{
|
||||
MsgData: msgData,
|
||||
}
|
||||
|
||||
respPb, err := m.Client.SendMsg(c, sendReq)
|
||||
if err != nil {
|
||||
apiresp.GinError(c, err)
|
||||
return
|
||||
@@ -449,7 +525,7 @@ func (m *MessageApi) SendSimpleMessage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
apiresp.GinSuccess(c, respPb)
|
||||
m.ginRespSendMsg(c, sendReq, respPb)
|
||||
}
|
||||
|
||||
func (m *MessageApi) CheckMsgIsSendSuccess(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user