feat(message): enhance message rendering and payload handling with signed URLs

This commit is contained in:
mlogclub
2026-04-17 11:53:25 +08:00
parent 2464bbe428
commit 9d76d5d505
8 changed files with 372 additions and 76 deletions
+1 -11
View File
@@ -101,7 +101,7 @@ func BuildMessage(item *models.Message) response.MessageResponse {
}
func BuildMessageWithReadStates(item *models.Message, agentReadState, customerReadState *models.ConversationReadState, aiSenderNames, userSenderNames map[int64]string, agentProfiles map[int64]*models.AgentProfile) response.MessageResponse {
content, payload := buildMessageResponseContent(item)
content, payload := services.BuildRenderableMessage(item)
ret := response.MessageResponse{
ID: item.ID,
ConversationID: item.ConversationID,
@@ -162,16 +162,6 @@ func BuildMessageWithReadStates(item *models.Message, agentReadState, customerRe
return ret
}
func buildMessageResponseContent(item *models.Message) (content, payload string) {
if item == nil {
return "", ""
}
if item.RecalledAt != nil || item.SendStatus == int(enums.IMMessageStatusRecalled) {
return "该消息已撤回", ""
}
return item.Content, item.Payload
}
func collectAgentProfilesByMessages(list []models.Message) map[int64]*models.AgentProfile {
var agentUserIDs []int64
seen := make(map[int64]struct{})
+16 -16
View File
@@ -377,22 +377,22 @@ type ConversationReadState struct {
// Message 会话消息。
type Message struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
ConversationID int64 `gorm:"type:bigint;not null;index;uniqueIndex:uk_conversation_seq;uniqueIndex:uk_conversation_client_msg"`
ClientMsgID string `gorm:"type:varchar(128);not null;default:'';uniqueIndex:uk_conversation_client_msg"`
SenderType enums.IMSenderType `gorm:"type:varchar(30);not null;default:'';index"`
SenderID int64 `gorm:"type:bigint;not null;default:0;index"`
ReceiverType string `gorm:"type:varchar(30);not null;default:'';index"`
MessageType enums.IMMessageType `gorm:"type:varchar(30);not null;default:'';index"`
Content string `gorm:"type:text"`
Payload string `gorm:"type:text"`
SeqNo int64 `gorm:"type:bigint;not null;default:0;uniqueIndex:uk_conversation_seq"`
SendStatus int `gorm:"type:int;not null;default:2;index"`
SentAt *time.Time `gorm:"type:datetime;index"`
DeliveredAt *time.Time `gorm:"type:datetime"`
ReadAt *time.Time `gorm:"type:datetime"`
RecalledAt *time.Time `gorm:"type:datetime"`
QuotedMessageID int64 `gorm:"type:bigint;not null;default:0;index"`
ID int64 `gorm:"primaryKey;autoIncrement"`
ConversationID int64 `gorm:"type:bigint;not null;index;uniqueIndex:uk_conversation_seq;uniqueIndex:uk_conversation_client_msg"`
ClientMsgID string `gorm:"type:varchar(128);not null;default:'';uniqueIndex:uk_conversation_client_msg"`
SenderType enums.IMSenderType `gorm:"type:varchar(30);not null;default:'';index"`
SenderID int64 `gorm:"type:bigint;not null;default:0;index"`
ReceiverType string `gorm:"type:varchar(30);not null;default:'';index"`
MessageType enums.IMMessageType `gorm:"type:varchar(30);not null;default:'';index"`
Content string `gorm:"type:text"`
Payload string `gorm:"type:text"`
SeqNo int64 `gorm:"type:bigint;not null;default:0;uniqueIndex:uk_conversation_seq"`
SendStatus enums.IMMessageStatus `gorm:"type:int;not null;default:2;index"`
SentAt *time.Time `gorm:"type:datetime;index"`
DeliveredAt *time.Time `gorm:"type:datetime"`
ReadAt *time.Time `gorm:"type:datetime"`
RecalledAt *time.Time `gorm:"type:datetime"`
QuotedMessageID int64 `gorm:"type:bigint;not null;default:0;index"`
AuditFields
}
+21 -21
View File
@@ -3,25 +3,25 @@ package response
import "cs-agent/internal/pkg/enums"
type MessageResponse struct {
ID int64 `json:"id"`
ConversationID int64 `json:"conversationId"`
ClientMsgID string `json:"clientMsgId,omitempty"`
SenderType enums.IMSenderType `json:"senderType"`
SenderID int64 `json:"senderId"`
SenderName string `json:"senderName,omitempty"`
SenderAvatar string `json:"senderAvatar,omitempty"`
MessageType enums.IMMessageType `json:"messageType"`
Content string `json:"content"`
Payload string `json:"payload,omitempty"`
SeqNo int64 `json:"seqNo"`
SendStatus int `json:"sendStatus"`
SentAt string `json:"sentAt,omitempty"`
DeliveredAt string `json:"deliveredAt,omitempty"`
ReadAt string `json:"readAt,omitempty"`
CustomerRead bool `json:"customerRead"`
CustomerReadAt string `json:"customerReadAt,omitempty"`
AgentRead bool `json:"agentRead"`
AgentReadAt string `json:"agentReadAt,omitempty"`
RecalledAt string `json:"recalledAt,omitempty"`
QuotedMessageID int64 `json:"quotedMessageId,omitempty"`
ID int64 `json:"id"`
ConversationID int64 `json:"conversationId"`
ClientMsgID string `json:"clientMsgId,omitempty"`
SenderType enums.IMSenderType `json:"senderType"`
SenderID int64 `json:"senderId"`
SenderName string `json:"senderName,omitempty"`
SenderAvatar string `json:"senderAvatar,omitempty"`
MessageType enums.IMMessageType `json:"messageType"`
Content string `json:"content"`
Payload string `json:"payload,omitempty"`
SeqNo int64 `json:"seqNo"`
SendStatus enums.IMMessageStatus `json:"sendStatus"`
SentAt string `json:"sentAt,omitempty"`
DeliveredAt string `json:"deliveredAt,omitempty"`
ReadAt string `json:"readAt,omitempty"`
CustomerRead bool `json:"customerRead"`
CustomerReadAt string `json:"customerReadAt,omitempty"`
AgentRead bool `json:"agentRead"`
AgentReadAt string `json:"agentReadAt,omitempty"`
RecalledAt string `json:"recalledAt,omitempty"`
QuotedMessageID int64 `json:"quotedMessageId,omitempty"`
}
+65 -14
View File
@@ -10,11 +10,13 @@ import (
)
type imMessageAssetPayload struct {
AssetID string `json:"assetId"`
Filename string `json:"filename,omitempty"`
FileSize int64 `json:"fileSize,omitempty"`
MimeType string `json:"mimeType,omitempty"`
URL string `json:"url,omitempty"`
AssetID string `json:"assetId"`
Provider enums.AssetProvider `json:"provider,omitempty"`
StorageKey string `json:"storageKey,omitempty"`
Filename string `json:"filename,omitempty"`
FileSize int64 `json:"fileSize,omitempty"`
MimeType string `json:"mimeType,omitempty"`
URL string `json:"url,omitempty"`
}
func parseIMMessageAssetPayload(payload string) (*imMessageAssetPayload, error) {
@@ -27,6 +29,8 @@ func parseIMMessageAssetPayload(payload string) (*imMessageAssetPayload, error)
return nil, errorsx.InvalidParam("附件消息 payload 格式错误")
}
ret.AssetID = strings.TrimSpace(ret.AssetID)
ret.Provider = enums.AssetProvider(strings.TrimSpace(string(ret.Provider)))
ret.StorageKey = strings.TrimSpace(ret.StorageKey)
if ret.AssetID == "" {
return nil, errorsx.InvalidParam("附件消息缺少 assetId")
}
@@ -37,16 +41,13 @@ func buildIMMessageAssetPayload(asset *models.Asset) (string, error) {
if asset == nil {
return "", errorsx.InvalidParam("附件不存在")
}
provider, err := storage.NewProvider(asset.Provider)
if err != nil {
return "", err
}
payload, err := json.Marshal(imMessageAssetPayload{
AssetID: asset.AssetID,
Filename: asset.Filename,
FileSize: asset.FileSize,
MimeType: asset.MimeType,
URL: provider.GetURL(asset.StorageKey),
AssetID: asset.AssetID,
Provider: asset.Provider,
StorageKey: asset.StorageKey,
Filename: asset.Filename,
FileSize: asset.FileSize,
MimeType: asset.MimeType,
})
if err != nil {
return "", err
@@ -54,6 +55,56 @@ func buildIMMessageAssetPayload(asset *models.Asset) (string, error) {
return string(payload), nil
}
func buildIMMessageAssetPayloadForResponse(payload string) string {
assetPayload, err := parseIMMessageAssetPayload(payload)
if err != nil {
return strings.TrimSpace(payload)
}
assetPayload = hydrateIMMessageAssetPayload(assetPayload)
if assetPayload.Provider != "" && assetPayload.StorageKey != "" {
if provider, err := storage.NewProvider(assetPayload.Provider); err == nil {
assetPayload.URL = provider.GetSignedURL(assetPayload.StorageKey)
}
}
data, err := json.Marshal(assetPayload)
if err != nil {
return strings.TrimSpace(payload)
}
return string(data)
}
func hydrateIMMessageAssetPayload(payload *imMessageAssetPayload) *imMessageAssetPayload {
if payload == nil {
return nil
}
if payload.Provider != "" && payload.StorageKey != "" {
return payload
}
if payload.AssetID == "" {
return payload
}
asset := AssetService.GetByAssetID(payload.AssetID)
if asset == nil {
return payload
}
if payload.Provider == "" {
payload.Provider = asset.Provider
}
if payload.StorageKey == "" {
payload.StorageKey = strings.TrimSpace(asset.StorageKey)
}
if payload.Filename == "" {
payload.Filename = strings.TrimSpace(asset.Filename)
}
if payload.FileSize <= 0 {
payload.FileSize = asset.FileSize
}
if payload.MimeType == "" {
payload.MimeType = strings.TrimSpace(asset.MimeType)
}
return payload
}
func validateConversationAsset(asset *models.Asset, conversationID int64, messageType enums.IMMessageType) error {
if asset == nil {
return errorsx.InvalidParam("附件不存在")
+96
View File
@@ -0,0 +1,96 @@
package services
import (
"cs-agent/internal/models"
"cs-agent/internal/pkg/config"
"cs-agent/internal/pkg/enums"
"strings"
"testing"
)
func TestBuildIMMessageAssetPayloadForResponseAddsSignedURL(t *testing.T) {
config.SetCurrent(&config.Config{
Storage: config.StorageConfig{
Default: enums.AssetProviderLocal,
Local: config.LocalStorageConfig{
BaseURL: "https://files.example.com",
},
},
})
payload := `{"assetId":"asset_1","provider":"local","storageKey":"attachments/demo.png","filename":"demo.png"}`
got := buildIMMessageAssetPayloadForResponse(payload)
if !strings.Contains(got, `"provider":"local"`) {
t.Fatalf("expected provider in payload, got: %s", got)
}
if !strings.Contains(got, `"storageKey":"attachments/demo.png"`) {
t.Fatalf("expected storageKey in payload, got: %s", got)
}
if !strings.Contains(got, `"url":"https://files.example.com/attachments/demo.png"`) {
t.Fatalf("expected signed url in payload, got: %s", got)
}
}
func TestSanitizeMessageHTMLStripsStoredSrcForManagedImages(t *testing.T) {
html := `<p><img src="https://files.example.com/demo.png" data-provider="local" data-storage-key="attachments/demo.png" alt="demo"></p>`
got := sanitizeMessageHTML(html)
if strings.Contains(got, `src=`) {
t.Fatalf("expected src removed from stored html, got: %s", got)
}
if !strings.Contains(got, `data-provider="local"`) {
t.Fatalf("expected data-provider kept, got: %s", got)
}
if !strings.Contains(got, `data-storage-key="attachments/demo.png"`) {
t.Fatalf("expected data-storage-key kept, got: %s", got)
}
}
func TestBuildMessageHTMLForResponseAddsSignedURL(t *testing.T) {
config.SetCurrent(&config.Config{
Storage: config.StorageConfig{
Default: enums.AssetProviderLocal,
Local: config.LocalStorageConfig{
BaseURL: "https://files.example.com",
},
},
})
html := `<p><img data-provider="local" data-storage-key="attachments/demo.png" alt="demo"></p>`
got := buildMessageHTMLForResponse(html)
if !strings.Contains(got, `src="https://files.example.com/attachments/demo.png"`) {
t.Fatalf("expected signed src in response html, got: %s", got)
}
}
func TestBuildRenderableMessageTransformsPayloadAndHTML(t *testing.T) {
config.SetCurrent(&config.Config{
Storage: config.StorageConfig{
Default: enums.AssetProviderLocal,
Local: config.LocalStorageConfig{
BaseURL: "https://files.example.com",
},
},
})
image := &models.Message{
MessageType: enums.IMMessageTypeImage,
Payload: `{"assetId":"asset_1","provider":"local","storageKey":"attachments/demo.png","filename":"demo.png"}`,
}
_, imagePayload := BuildRenderableMessage(image)
if !strings.Contains(imagePayload, `"url":"https://files.example.com/attachments/demo.png"`) {
t.Fatalf("expected image payload signed url, got: %s", imagePayload)
}
htmlMsg := &models.Message{
MessageType: enums.IMMessageTypeHTML,
Content: `<p><img data-provider="local" data-storage-key="attachments/demo.png"></p>`,
}
htmlContent, _ := BuildRenderableMessage(htmlMsg)
if !strings.Contains(htmlContent, `src="https://files.example.com/attachments/demo.png"`) {
t.Fatalf("expected html content signed src, got: %s", htmlContent)
}
}
+163 -5
View File
@@ -1,12 +1,14 @@
package services
import (
"bytes"
"cs-agent/internal/models"
"cs-agent/internal/pkg/dto"
"cs-agent/internal/pkg/enums"
"cs-agent/internal/pkg/errorsx"
"cs-agent/internal/pkg/openidentity"
"cs-agent/internal/repositories"
"cs-agent/internal/services/storage"
"log/slog"
"slices"
"strings"
@@ -163,7 +165,7 @@ func (s *messageService) RecallAgentMessage(messageID int64, operator *dto.AuthP
if message.SenderID != operator.UserID {
return nil, errorsx.Forbidden("仅允许撤回自己发送的消息")
}
if message.RecalledAt != nil || message.SendStatus == int(enums.IMMessageStatusRecalled) {
if message.RecalledAt != nil || message.SendStatus == enums.IMMessageStatusRecalled {
return nil, errorsx.InvalidParam("消息已撤回")
}
@@ -185,7 +187,7 @@ func (s *messageService) RecallAgentMessage(messageID int64, operator *dto.AuthP
return err
}
message.SendStatus = int(enums.IMMessageStatusRecalled)
message.SendStatus = enums.IMMessageStatusRecalled
message.RecalledAt = &now
message.UpdatedAt = now
message.UpdateUserID = operator.UserID
@@ -307,7 +309,7 @@ func (s *messageService) sendMessage(conversationID int64, senderType enums.IMSe
Content: content,
Payload: payload,
SeqNo: nextSeq,
SendStatus: int(enums.IMMessageStatusSent),
SendStatus: enums.IMMessageStatusSent,
SentAt: &now,
AuditFields: models.AuditFields{
CreatedAt: now,
@@ -557,11 +559,11 @@ func readSeqNo(state *models.ConversationReadState) int64 {
func sanitizeMessageHTML(content string) string {
policy := bluemonday.UGCPolicy()
policy.AllowElements("img")
policy.AllowAttrs("src", "alt", "title").OnElements("img")
policy.AllowAttrs("src", "alt", "title", "data-provider", "data-storage-key").OnElements("img")
policy.AllowURLSchemes("http", "https")
policy.AllowStandardURLs()
policy.AllowElements("p", "br")
return strings.TrimSpace(policy.Sanitize(content))
return stripHTMLImageSrcIfBound(strings.TrimSpace(policy.Sanitize(content)))
}
func buildHTMLSummary(content string) string {
@@ -594,3 +596,159 @@ func buildHTMLSummary(content string) string {
walk(doc)
return strings.TrimSpace(strings.Join(parts, " "))
}
func BuildRenderableMessage(item *models.Message) (content, payload string) {
if item == nil {
return "", ""
}
if item.RecalledAt != nil {
return "该消息已撤回", ""
}
if item.SendStatus == enums.IMMessageStatusRecalled {
return "该消息已撤回", ""
}
content = item.Content
payload = item.Payload
switch item.MessageType {
case enums.IMMessageTypeImage, enums.IMMessageTypeAttachment:
payload = buildIMMessageAssetPayloadForResponse(item.Payload)
case enums.IMMessageTypeHTML:
content = buildMessageHTMLForResponse(item.Content)
}
return content, payload
}
func buildMessageHTMLForResponse(content string) string {
content = strings.TrimSpace(content)
if content == "" {
return ""
}
doc, err := html.Parse(strings.NewReader("<div>" + content + "</div>"))
if err != nil {
return content
}
var walk func(*html.Node)
walk = func(node *html.Node) {
if node == nil {
return
}
if node.Type == html.ElementNode && node.Data == "img" {
provider := enums.AssetProvider(strings.TrimSpace(findHTMLAttr(node, "data-provider")))
storageKey := strings.TrimSpace(findHTMLAttr(node, "data-storage-key"))
if provider != "" && storageKey != "" {
if storageProvider, err := storage.NewProvider(provider); err == nil {
setHTMLAttr(node, "src", storageProvider.GetSignedURL(storageKey))
}
}
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
walk(child)
}
}
walk(doc)
return renderHTMLFragment(doc)
}
func stripHTMLImageSrcIfBound(content string) string {
content = strings.TrimSpace(content)
if content == "" {
return ""
}
doc, err := html.Parse(strings.NewReader("<div>" + content + "</div>"))
if err != nil {
return content
}
var walk func(*html.Node)
walk = func(node *html.Node) {
if node == nil {
return
}
if node.Type == html.ElementNode && node.Data == "img" {
provider := strings.TrimSpace(findHTMLAttr(node, "data-provider"))
storageKey := strings.TrimSpace(findHTMLAttr(node, "data-storage-key"))
if provider != "" && storageKey != "" {
removeHTMLAttr(node, "src")
}
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
walk(child)
}
}
walk(doc)
return renderHTMLFragment(doc)
}
func renderHTMLFragment(doc *html.Node) string {
if doc == nil {
return ""
}
root := findHTMLRoot(doc)
if root == nil {
return ""
}
var buf bytes.Buffer
for child := root.FirstChild; child != nil; child = child.NextSibling {
if err := html.Render(&buf, child); err != nil {
return ""
}
}
return strings.TrimSpace(buf.String())
}
func findHTMLRoot(doc *html.Node) *html.Node {
var walk func(*html.Node) *html.Node
walk = func(node *html.Node) *html.Node {
if node == nil {
return nil
}
if node.Type == html.ElementNode && node.Data == "div" {
return node
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
if found := walk(child); found != nil {
return found
}
}
return nil
}
return walk(doc)
}
func findHTMLAttr(node *html.Node, key string) string {
if node == nil {
return ""
}
for _, attr := range node.Attr {
if attr.Key == key {
return attr.Val
}
}
return ""
}
func setHTMLAttr(node *html.Node, key, value string) {
if node == nil {
return
}
for i := range node.Attr {
if node.Attr[i].Key == key {
node.Attr[i].Val = value
return
}
}
node.Attr = append(node.Attr, html.Attribute{Key: key, Val: value})
}
func removeHTMLAttr(node *html.Node, key string) {
if node == nil {
return
}
dst := node.Attr[:0]
for _, attr := range node.Attr {
if attr.Key != key {
dst = append(dst, attr)
}
}
node.Attr = dst
}
+7 -7
View File
@@ -142,7 +142,7 @@ type RealtimeMessageCreatedPayload struct {
Content string `json:"content,omitempty"`
Payload string `json:"payload,omitempty"`
SeqNo int64 `json:"seqNo,omitempty"`
SendStatus int `json:"sendStatus,omitempty"`
SendStatus enums.IMMessageStatus `json:"sendStatus,omitempty"`
SentAt string `json:"sentAt,omitempty"`
}
@@ -161,12 +161,12 @@ func (e RealtimeMessageCreatedEvent) EventPayload() RealtimeEventPayload {
}
type RealtimeMessageRecalledPayload struct {
ConversationID int64 `json:"conversationId,omitempty"`
MessageID int64 `json:"messageId,omitempty"`
SenderType enums.IMSenderType `json:"senderType,omitempty"`
SenderID int64 `json:"senderId,omitempty"`
SendStatus int `json:"sendStatus,omitempty"`
RecalledAt string `json:"recalledAt,omitempty"`
ConversationID int64 `json:"conversationId,omitempty"`
MessageID int64 `json:"messageId,omitempty"`
SenderType enums.IMSenderType `json:"senderType,omitempty"`
SenderID int64 `json:"senderId,omitempty"`
SendStatus enums.IMMessageStatus `json:"sendStatus,omitempty"`
RecalledAt string `json:"recalledAt,omitempty"`
}
func (RealtimeMessageRecalledPayload) realtimeEventPayload() {}
+3 -2
View File
@@ -238,6 +238,7 @@ func (s *wsService) PublishMessageCreated(conversation *models.Conversation, mes
if conversation == nil || message == nil {
return
}
content, payload := BuildRenderableMessage(message)
event := s.newEvent(s.conversationTopic(conversation.ID), RealtimeMessageCreatedEvent{
Payload: RealtimeMessageCreatedPayload{
@@ -248,8 +249,8 @@ func (s *wsService) PublishMessageCreated(conversation *models.Conversation, mes
SenderType: message.SenderType,
SenderID: message.SenderID,
MessageType: message.MessageType,
Content: message.Content,
Payload: message.Payload,
Content: content,
Payload: payload,
SeqNo: message.SeqNo,
SendStatus: message.SendStatus,
SentAt: formatWsTime(message.SentAt),