feat: add account automation and isolated chat
- add account-level auto rating and daily polish tasks\n- add cached historical chat, media, emoji, and per-account realtime sync\n- add SQLite, MySQL, and PostgreSQL migrations and regression coverage\n- align account, sidebar, and chat interfaces with the new workflows
This commit is contained in:
1 parent
d7abbfb2b6
commit
67d7214930
59 files changed
+5202
-211
No files matched your search
@@ -125,6 +125,29 @@ type ChatMessage struct {
|
||||
Raw map[string]any // 解密后的完整消息
|
||||
}
|
||||
|
||||
// OutgoingChatMessage is emitted after the existing account WebSocket has
|
||||
// accepted a text message. It is an observation hook only; persistence errors
|
||||
// never change the delivery result.
|
||||
type OutgoingChatMessage struct {
|
||||
AccountID string
|
||||
ChatID string
|
||||
BuyerID string
|
||||
Text string
|
||||
MessageKey string
|
||||
}
|
||||
|
||||
type outgoingChatHandler interface {
|
||||
HandleOutgoingChatMessage(ctx context.Context, message OutgoingChatMessage) error
|
||||
}
|
||||
|
||||
type outgoingMessageKeyContextKey struct{}
|
||||
|
||||
// WithOutgoingMessageKey correlates a UI-created pending message with the
|
||||
// post-send observer so the same text is not inserted twice.
|
||||
func WithOutgoingMessageKey(ctx context.Context, key string) context.Context {
|
||||
return context.WithValue(ctx, outgoingMessageKeyContextKey{}, strings.TrimSpace(key))
|
||||
}
|
||||
|
||||
// RuntimeStatus 是账号引擎的实时连接状态,不写入数据库。
|
||||
type RuntimeStatus struct {
|
||||
State string `json:"state"`
|
||||
@@ -1752,7 +1775,18 @@ func (a *Account) SendText(ctx context.Context, chatID, toUserID, text string) e
|
||||
}
|
||||
sendCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
return conn.SendText(sendCtx, myID, chatID, toUserID, text)
|
||||
if err := conn.SendText(sendCtx, myID, chatID, toUserID, text); err != nil {
|
||||
return err
|
||||
}
|
||||
if observer, ok := a.handler.(outgoingChatHandler); ok {
|
||||
key, _ := ctx.Value(outgoingMessageKeyContextKey{}).(string)
|
||||
if err := observer.HandleOutgoingChatMessage(ctx, OutgoingChatMessage{
|
||||
AccountID: a.CookieID, ChatID: chatID, BuyerID: toUserID, Text: text, MessageKey: key,
|
||||
}); err != nil {
|
||||
a.logger.Warn("保存出站聊天旁路失败", "account", a.CookieID, "chat_id", chatID, "err", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendImage 通过当前 WebSocket 给买家发送图片消息。当前仅支持可直接访问的 CDN/公网 URL。
|
||||
@@ -1789,6 +1823,39 @@ func (a *Account) currentSenderState() (WSConn, string, error) {
|
||||
return a.conn, myID, nil
|
||||
}
|
||||
|
||||
// FetchChatHistory reuses the account's registered IM connection. Keeping this
|
||||
// optional capability outside WSConn avoids forcing non-chat test transports to
|
||||
// implement history retrieval.
|
||||
func (a *Account) FetchChatHistory(ctx context.Context, chatID string, cursor int64, limit int) (map[string]any, string, error) {
|
||||
conn, myID, err := a.currentSenderState()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
history, ok := conn.(interface {
|
||||
ListUserMessages(context.Context, string, int64, int) (map[string]any, error)
|
||||
})
|
||||
if !ok {
|
||||
return nil, "", errors.New("当前 WebSocket 连接不支持聊天历史")
|
||||
}
|
||||
body, err := history.ListUserMessages(ctx, chatID, cursor, limit)
|
||||
return body, myID, err
|
||||
}
|
||||
|
||||
func (a *Account) FetchChatConversations(ctx context.Context, cursor int64, limit int) (map[string]any, string, error) {
|
||||
conn, myID, err := a.currentSenderState()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
fetcher, ok := conn.(interface {
|
||||
ListConversations(context.Context, int64, int) (map[string]any, error)
|
||||
})
|
||||
if !ok {
|
||||
return nil, "", errors.New("当前 WebSocket 连接不支持历史会话")
|
||||
}
|
||||
body, err := fetcher.ListConversations(ctx, cursor, limit)
|
||||
return body, myID, err
|
||||
}
|
||||
|
||||
// AutomationReady 报告自动化消息是否可以立即使用当前 WS 连接发送。
|
||||
func (a *Account) AutomationReady() bool {
|
||||
a.mu.Lock()
|
||||
|
||||
@@ -243,6 +243,9 @@ func extractChatMessage(decrypted map[string]any, accountID, cookieStr string) *
|
||||
}
|
||||
senderUserID, _ := m10["senderUserId"].(string)
|
||||
senderName, _ := m10["senderNick"].(string)
|
||||
if strings.TrimSpace(senderName) == "" {
|
||||
senderName, _ = m10["reminderTitle"].(string)
|
||||
}
|
||||
reminderURL, _ := m10["reminderUrl"].(string)
|
||||
itemID := extractItemID(reminderURL)
|
||||
return &ChatMessage{
|
||||
@@ -263,6 +266,9 @@ func extractChatMessage(decrypted map[string]any, accountID, cookieStr string) *
|
||||
// - contentType=26:交易卡片,如“我已拍下,待付款”“我发起了退款申请”
|
||||
// 付款待发货卡片已经在 handleMessage 前半段进入 automation.Center,这里不能再进入聊天回复链。
|
||||
func isNonUserChatNotice(m1, m10 map[string]any, reminder string) bool {
|
||||
if sessionType := strings.TrimSpace(toString(m10["sessionType"])); sessionType != "" && sessionType != "1" {
|
||||
return true
|
||||
}
|
||||
contentType := messageContentType(m1, m10)
|
||||
switch contentType {
|
||||
case "14":
|
||||
|
||||
@@ -91,6 +91,17 @@ func TestExtractChatMessage_FiltersContentType14Notice(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractChatMessageUsesReminderTitleAsNickname(t *testing.T) {
|
||||
decrypted := map[string]any{"1": map[string]any{
|
||||
"2": "chat-1@goofish",
|
||||
"10": map[string]any{"reminderContent": "你好", "reminderTitle": "真实昵称", "senderUserId": "buyer-1", "sessionType": "1"},
|
||||
}}
|
||||
chat := extractChatMessage(decrypted, "account-1", "cookie")
|
||||
if chat == nil || chat.SenderName != "真实昵称" {
|
||||
t.Fatalf("chat=%+v", chat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractChatMessage_FiltersRefundTradeCard(t *testing.T) {
|
||||
decrypted := mustRefundTradeCard(t)
|
||||
if chat := extractChatMessage(decrypted, "cid", "cookie"); chat != nil {
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"xianyu-go/internal/automation"
|
||||
)
|
||||
|
||||
type outgoingObserverHandler struct {
|
||||
messages []OutgoingChatMessage
|
||||
}
|
||||
|
||||
func (h *outgoingObserverHandler) HandleChatMessage(context.Context, ChatMessage) error { return nil }
|
||||
func (h *outgoingObserverHandler) HandleSystemEvent(context.Context, automation.Task) error {
|
||||
return nil
|
||||
}
|
||||
func (h *outgoingObserverHandler) OnPasswordLoginRefresh(context.Context, string) bool { return false }
|
||||
func (h *outgoingObserverHandler) OnAccountAlert(context.Context, string, string, string, string) {}
|
||||
func (h *outgoingObserverHandler) HandleOutgoingChatMessage(_ context.Context, message OutgoingChatMessage) error {
|
||||
h.messages = append(h.messages, message)
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestSendTextEmitsCorrelatedOutgoingObservation(t *testing.T) {
|
||||
handler := &outgoingObserverHandler{}
|
||||
account := New(Config{CookieID: "account-1", CookieStr: "unb=me", Handler: handler})
|
||||
conn := &fakeWSConn{}
|
||||
account.mu.Lock()
|
||||
account.conn = conn
|
||||
account.mu.Unlock()
|
||||
ctx := WithOutgoingMessageKey(context.Background(), "local-1")
|
||||
if err := account.SendText(ctx, "chat-1", "buyer-1", "您好"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(handler.messages) != 1 {
|
||||
t.Fatalf("messages=%+v", handler.messages)
|
||||
}
|
||||
got := handler.messages[0]
|
||||
if got.AccountID != "account-1" || got.ChatID != "chat-1" || got.BuyerID != "buyer-1" || got.Text != "您好" || got.MessageKey != "local-1" {
|
||||
t.Fatalf("observation=%+v", got)
|
||||
}
|
||||
}
|
||||
@@ -282,6 +282,9 @@ func TestIsNonUserChatNotice(t *testing.T) {
|
||||
if isNonUserChatNotice(map[string]any{}, map[string]any{}, "[买家说你好]") {
|
||||
t.Error("普通消息不应判为系统提示")
|
||||
}
|
||||
if !isNonUserChatNotice(map[string]any{}, map[string]any{"sessionType": "24"}, "售后问卷") {
|
||||
t.Error("非真人会话不应进入买家聊天列表")
|
||||
}
|
||||
}
|
||||
|
||||
// TestToStringAndTrimFloatInt 数字/字符串安全转换。
|
||||
|
||||
Reference in new issue
Block a user