fix(shangwutong): 来源信息改为系统消息

This commit is contained in:
Rogee
2026-08-06 17:55:54 +08:00
parent d2aeea5f44
commit dc2a6577bf
7 changed files with 114 additions and 85 deletions
@@ -836,8 +836,7 @@ func (h *ConversationHandler) UpdateCustomAttributes(c *gin.Context) {
if !requireConnectorShangwutongConversation(c, h.conversationSvc.DB(), conversation) {
return
}
requestContext := service.WithShangwutongRequestMetadata(c.Request.Context(), middleware.IsConnectorService(c), currentUserID(c))
conversation, svcErr := h.conversationSvc.UpdateCustomAttributes(requestContext, accountID, conversation.ID, req.CustomAttributes)
conversation, svcErr := h.conversationSvc.UpdateCustomAttributes(c.Request.Context(), accountID, conversation.ID, req.CustomAttributes)
if svcErr != nil {
handleServiceError(c, svcErr)
return
@@ -1,38 +0,0 @@
package service
import (
"context"
"encoding/json"
"testing"
"github.com/gochat/gochat/internal/channel"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
"github.com/stretchr/testify/require"
"gorm.io/datatypes"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func TestUpdateCustomAttributesMergesConnectorPayload(t *testing.T) {
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
require.NoError(t, err)
require.NoError(t, db.AutoMigrate(&model.Conversation{}))
conversation := model.Conversation{
AccountID: 1,
CustomAttributes: datatypes.JSON([]byte(`{"swt_sid":"sid","swt_state":"chatting"}`)),
}
require.NoError(t, db.Create(&conversation).Error)
svc := NewConversationService(repository.NewConversationRepo(db), repository.NewMessageRepo(db), channel.NewDispatcher(), nil, nil, nil, nil)
ctx := WithShangwutongRequestMetadata(context.Background(), true, 0)
updated, err := svc.UpdateCustomAttributes(ctx, 1, conversation.ID, datatypes.JSON([]byte(`{"swt_source_channel":"百度搜索推广"}`)))
require.NoError(t, err)
attributes := map[string]any{}
require.NoError(t, json.Unmarshal(updated.CustomAttributes, &attributes))
require.Equal(t, "sid", attributes["swt_sid"])
require.Equal(t, "chatting", attributes["swt_state"])
require.Equal(t, "百度搜索推广", attributes["swt_source_channel"])
}
@@ -2,7 +2,6 @@ package service
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
@@ -1647,28 +1646,6 @@ func (s *ConversationService) UpdateCustomAttributes(ctx context.Context, accoun
return nil, err
}
metadata, _ := ctx.Value(shangwutongRequestMetadataKey{}).(shangwutongRequestMetadata)
if metadata.ConnectorOrigin {
merged := map[string]any{}
if len(conversation.CustomAttributes) > 0 {
if err := json.Unmarshal(conversation.CustomAttributes, &merged); err != nil {
return nil, fmt.Errorf("decode existing conversation custom attributes: %w", err)
}
}
incoming := map[string]any{}
if err := json.Unmarshal(attrs, &incoming); err != nil {
return nil, fmt.Errorf("decode incoming conversation custom attributes: %w", err)
}
for key, value := range incoming {
merged[key] = value
}
encoded, err := json.Marshal(merged)
if err != nil {
return nil, fmt.Errorf("encode merged conversation custom attributes: %w", err)
}
attrs = datatypes.JSON(encoded)
}
if err := s.repo.UpdateCustomAttributes(ctx, id, attrs); err != nil {
return nil, err
}
@@ -0,0 +1,3 @@
-- Restoring the old custom-attribute presentation is intentionally unsupported.
-- Migration 000075 remains the historical definition of that superseded UI.
SELECT 1;
@@ -0,0 +1,52 @@
DELETE FROM custom_attribute_definitions
WHERE attribute_model = 'conversation_attribute'
AND attribute_name IN (
'swt_source_url',
'swt_source_search_term',
'swt_source_purchase_term',
'swt_source_keyword_id',
'swt_source_channel',
'swt_source_realtime_location',
'swt_source_region',
'swt_source_ad_account_id',
'swt_source_wakeable',
'swt_baidu_conversation_type',
'swt_baidu_agent_name',
'swt_baidu_ssid'
);
UPDATE conversations
SET
custom_attributes = COALESCE(custom_attributes, '{}'::jsonb)
- 'swt_source_url'
- 'swt_source_description'
- 'swt_source_search_term'
- 'swt_source_purchase_term'
- 'swt_source_keyword_id'
- 'swt_source_channel'
- 'swt_source_realtime_location'
- 'swt_source_region'
- 'swt_source_ad_account_id'
- 'swt_source_wakeable'
- 'swt_baidu_conversation_type'
- 'swt_baidu_agent_name'
- 'swt_baidu_ssid',
updated_at = CURRENT_TIMESTAMP
WHERE COALESCE(custom_attributes, '{}'::jsonb) ?| ARRAY[
'swt_source_url',
'swt_source_description',
'swt_source_search_term',
'swt_source_purchase_term',
'swt_source_keyword_id',
'swt_source_channel',
'swt_source_realtime_location',
'swt_source_region',
'swt_source_ad_account_id',
'swt_source_wakeable',
'swt_baidu_conversation_type',
'swt_baidu_agent_name',
'swt_baidu_ssid'
];
-- Historical kind=8 system messages are backfilled from the Connector SQLite
-- store because PostgreSQL intentionally does not retain the raw encoded payload.