feat: 完善聊天识别与规则页面响应式布局

- 按闲鱼协议字段识别官方系统消息、交易卡片和闲小蜜,清理空会话壳并补齐历史消息校正\n- 同步 README、项目展示页和 GitHub Wiki,补充在线聊天、账号自动评价与每日擦亮说明\n- 优化自动化规则页面的中等宽度布局,补充前端类型、响应式测试和嵌入资源
This commit is contained in:
Christ committed 2026-08-08 23:48:42 +08:00
1 parent 4d1f58c5ec
commit fe3f715cdc
27 files changed
+336 -32

No files matched your search

+82 -1
View File
@@ -65,6 +65,12 @@ func (s *Service) RecordConversationPage(ctx context.Context, accountID, myID st
}
lastWrap, _ := conv["lastMessage"].(map[string]any)
last, _ := lastWrap["message"].(map[string]any)
// The conversation endpoint can return empty shells for notification
// recipients. The official web client does not render these as contacts;
// importing them creates fake users such as numeric IDs with “暂无消息”.
if len(last) == 0 {
continue
}
// reminderTitle belongs to the message presentation layer. Depending on
// the message type it may contain a nickname, an order-state prompt, or
// another card title. The official web client resolves conversation
@@ -72,10 +78,17 @@ func (s *Service) RecordConversationPage(ctx context.Context, accountID, myID st
// as the session nickname.
peerName := ""
custom := map[string]any{}
extension := mapValue(last["extension"])
if content, ok := last["content"].(map[string]any); ok {
custom, _ = content["custom"].(map[string]any)
}
summary := cleanNilString(custom["summary"])
if summary == "" {
summary = cleanNilString(extension["reminderContent"])
}
if summary == "" {
summary = cleanNilString(extension["detailNotice"])
}
if summary == "" {
if encoded := cleanNilString(custom["data"]); encoded != "" {
var decoded map[string]any
@@ -240,6 +253,14 @@ func (s *Service) RecordIncoming(ctx context.Context, in Incoming) (*db.ChatMess
BuyerName: in.BuyerName, BuyerAvatar: extractString(in.Raw, "avatar", "avatarUrl", "senderAvatar"),
ItemID: in.ItemID, ItemTitle: extractString(in.Raw, "itemTitle", "title")}
messageType, content := extractMessageContent(in.Raw, in.Text)
if isOfficialSystemMessage(in.Raw, in.BuyerID, in.Text) {
messageType = "system"
if strings.TrimSuffix(strings.TrimSpace(in.BuyerID), "@goofish") == "1400" {
in.BuyerName = "闲小蜜"
session.BuyerName = "闲小蜜"
session.BuyerAvatar = xianxiaomiAvatar
}
}
message := db.ChatMessage{MessageKey: key, Direction: "incoming", SenderID: in.BuyerID,
SenderName: in.BuyerName, MessageType: messageType, Content: content, Status: "received", SentAt: sentAt}
stored, inserted, err := s.store.Chats.SaveMessage(ctx, session, message, true)
@@ -260,7 +281,7 @@ func (s *Service) RecordHistoryPage(ctx context.Context, accountID, chatID, myID
continue
}
session.CookieID, session.ChatID = accountID, chatID
if message.Direction == "incoming" {
if message.Direction == "incoming" && message.MessageType != "system" {
if session.BuyerID == "" {
session.BuyerID = message.SenderID
}
@@ -269,6 +290,12 @@ func (s *Service) RecordHistoryPage(ctx context.Context, accountID, chatID, myID
if err != nil {
return page, err
}
if message.MessageType == "system" {
if err := s.store.Chats.UpdateMessageType(ctx, accountID, message.MessageKey, "system"); err != nil {
return page, err
}
stored.MessageType = "system"
}
page.Messages = append(page.Messages, *stored)
}
return page, nil
@@ -307,6 +334,12 @@ func parseHistoryMessage(accountID, chatID, myID string, model map[string]any) (
}
}
messageType, content := extractMessageContent(rawContent, fallback)
if isOfficialSystemMessage(rawContent, senderID, fallback) {
messageType = "system"
if senderID == "1400" {
senderName = "闲小蜜"
}
}
if content == "" {
content = "[系统消息]"
}
@@ -406,6 +439,54 @@ func extractMessageContent(raw map[string]any, fallback string) (string, string)
return "text", strings.TrimSpace(fallback)
}
// isOfficialSystemMessage recognizes platform-generated IM content using the
// protocol metadata, rather than matching a growing list of Chinese prompts.
// contentType=14 is a platform notice and contentType=26 is an official trade
// card. User 1400 is 闲小蜜, whose messages are also not peer chat.
func isOfficialSystemMessage(raw map[string]any, senderID, fallback string) bool {
if strings.TrimSuffix(strings.TrimSpace(senderID), "@goofish") == "1400" {
return true
}
if contentType := findOfficialContentType(raw); contentType == "14" || contentType == "26" {
return true
}
return strings.TrimSpace(fallback) == "发来一条新消息"
}
// findOfficialContentType walks decoded history content as well as live WS
// envelopes. History stores the inner content JSON under custom.data, while
// live messages expose it under 1.6.3 or 1.10.extJson.
func findOfficialContentType(value any) string {
var found string
var walk func(any)
walk = func(current any) {
if found != "" {
return
}
switch typed := current.(type) {
case string:
var nested any
if json.Unmarshal([]byte(typed), &nested) == nil {
walk(nested)
}
case map[string]any:
if candidate := strings.TrimSpace(fmt.Sprint(typed["contentType"])); candidate == "14" || candidate == "26" {
found = candidate
return
}
for _, child := range typed {
walk(child)
}
case []any:
for _, child := range typed {
walk(child)
}
}
}
walk(value)
return found
}
func (s *Service) CreateOutgoing(ctx context.Context, session db.ChatSession, text string) (*db.ChatMessage, error) {
return s.CreateOutgoingMedia(ctx, session, "text", strings.TrimSpace(text))
}
+76
View File
@@ -58,6 +58,54 @@ func TestRecordHistoryPageParsesDirectionMediaAndDeduplicates(t *testing.T) {
}
}
func TestRecordHistoryPageClassifiesOfficialCardsAsSystem(t *testing.T) {
store, cleanup := chatTestStore(t)
defer cleanup()
service := New(store)
encoded := base64.StdEncoding.EncodeToString([]byte(`{"contentType":26,"dxCard":{"item":{"main":{"exContent":{"title":"我已拍下,待付款"}}}}}`))
body := map[string]any{"userMessageModels": []any{
map[string]any{"message": map[string]any{
"messageId": "official-card", "createAt": float64(3000),
"extension": map[string]any{"senderUserId": "peer@goofish", "reminderTitle": "买家已拍下,待付款"},
"content": map[string]any{"custom": map[string]any{"data": encoded, "summary": "[我已拍下,待付款]"}},
}},
}}
session := db.ChatSession{CookieID: "account-1", ChatID: "official", BuyerID: "peer", BuyerName: "真实昵称"}
if _, _, err := store.Chats.SaveMessage(context.Background(), session, db.ChatMessage{
MessageKey: "official-card", Direction: "incoming", SenderID: "peer", SenderName: "真实昵称",
MessageType: "text", Content: "[我已拍下,待付款]", Status: "received", SentAt: 3000,
}, false); err != nil {
t.Fatal(err)
}
page, err := service.RecordHistoryPage(context.Background(), "account-1", "official", "self", session, body)
if err != nil {
t.Fatal(err)
}
if len(page.Messages) != 1 || page.Messages[0].MessageType != "system" || page.Messages[0].Direction != "incoming" {
t.Fatalf("official card was not classified as system: %+v", page.Messages)
}
if page.Messages[0].SenderName != "真实昵称" {
t.Fatalf("history sender metadata unexpectedly changed: %+v", page.Messages[0])
}
}
func TestRecordIncomingClassifiesXianxiaomiAndPlaceholder(t *testing.T) {
store, cleanup := chatTestStore(t)
defer cleanup()
service := New(store)
message, inserted, err := service.RecordIncoming(context.Background(), Incoming{
AccountID: "account-1", ChatID: "xiaomi", BuyerID: "1400@goofish",
BuyerName: "闲小蜜发来一条新消息", Text: "邀您填写售后问卷",
Raw: map[string]any{"messageId": "xiaomi-1"},
})
if err != nil || !inserted {
t.Fatalf("record xianxiaomi message: message=%+v inserted=%v err=%v", message, inserted, err)
}
if message.MessageType != "system" || message.SenderName != "闲小蜜" {
t.Fatalf("xianxiaomi message was not classified: %+v", message)
}
}
func TestRecordConversationPageImportsHistoricalContacts(t *testing.T) {
store, cleanup := chatTestStore(t)
defer cleanup()
@@ -124,6 +172,34 @@ func TestRecordConversationPageHandlesXianxiaomiAndRemovesInvisibleSessions(t *t
}
}
func TestRecordConversationPageSkipsEmptyConversationShells(t *testing.T) {
store, cleanup := chatTestStore(t)
defer cleanup()
service := New(store)
body := map[string]any{"userConvs": []any{
map[string]any{"singleChatUserConversation": map[string]any{
"singleChatConversation": map[string]any{"cid": "empty@goofish", "pairFirst": "self@goofish", "pairSecond": "69@goofish"},
}},
map[string]any{"singleChatUserConversation": map[string]any{
"singleChatConversation": map[string]any{"cid": "system@goofish", "pairFirst": "self@goofish", "pairSecond": "1400@goofish"},
"lastMessage": map[string]any{"message": map[string]any{
"createAt": float64(100), "reminderContent": "邀您填写售后问卷",
}},
}},
}}
if _, err := service.RecordConversationPage(context.Background(), "account-1", "self", body); err != nil {
t.Fatal(err)
}
owner, _ := store.Users.GetByUsername(context.Background(), "owner")
rows, err := store.Chats.ListSessions(context.Background(), owner.ID, "account-1", 20)
if err != nil {
t.Fatal(err)
}
if len(rows) != 1 || rows[0].ChatID != "system" {
t.Fatalf("empty conversation shell was imported: %+v", rows)
}
}
func TestDeleteEmptySessionsRemovesGhostsButKeepsRealConversation(t *testing.T) {
store, cleanup := chatTestStore(t)
defer cleanup()