修复:实时同步官方客户端出站消息
This commit is contained in:
1 parent
37e494ae76
commit
29d550473e
8 files changed
+248
-30
No files matched your search
@@ -301,6 +301,69 @@ func extractChatMessage(decrypted map[string]any, accountID, cookieStr string) *
|
||||
}
|
||||
}
|
||||
|
||||
// extractOwnWebSocketEcho 识别官方客户端发出后回显到当前账号 WebSocket 的普通消息。
|
||||
// decrypted 是已解密平台帧;accountID 和 cookieStr 用于归属与自身身份判断;返回值仅供出站持久化观察,绝不进入自动回复链。
|
||||
func extractOwnWebSocketEcho(decrypted map[string]any, accountID, cookieStr string) *OutgoingChatMessage {
|
||||
// m1 保存平台聊天信封主体;ok 表示帧是否具备普通聊天的顶层结构。
|
||||
m1, ok := decrypted["1"].(map[string]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
// m10 保存消息展示扩展;缺失时无法安全取得文本和发送者身份。
|
||||
m10, _ := m1["10"].(map[string]any)
|
||||
if m10 == nil {
|
||||
return nil
|
||||
}
|
||||
// text 保存平台通知层给出的消息摘要;空摘要不创建无内容出站记录。
|
||||
text, _ := m10["reminderContent"].(string)
|
||||
if text == "" || isNonUserChatNotice(m1, m10, text) {
|
||||
return nil
|
||||
}
|
||||
// senderUserID 保存归一化后的发送者身份,必须与当前账号身份一致才是自身回显。
|
||||
senderUserID := extractChatSenderUserIDFromMaps(m1, m10)
|
||||
// selfUserID 保存当前账号的非敏感平台身份,仅用于区分自身回显与买家入站消息。
|
||||
selfUserID := protocol.TransCookies(cookieStr)["unb"]
|
||||
if !isSelfUserID(senderUserID, selfUserID) {
|
||||
return nil
|
||||
}
|
||||
// chatID 保存去除协议后缀后的会话标识,供出站消息与既有会话关联。
|
||||
chatID := toString(m1["2"])
|
||||
// suffixIndex 保存会话协议后缀的起始下标,存在时只保留平台原始会话 ID。
|
||||
if suffixIndex := strings.Index(chatID, "@"); suffixIndex >= 0 {
|
||||
chatID = chatID[:suffixIndex]
|
||||
}
|
||||
if strings.TrimSpace(chatID) == "" {
|
||||
return nil
|
||||
}
|
||||
// reminderURL 保存通知深链,用于从 peerUserId 恢复会话对端身份。
|
||||
reminderURL, _ := m10["reminderUrl"].(string)
|
||||
// buyerID 保存深链中的对端用户标识;它不能等于当前账号,缺失时由既有会话保留原身份。
|
||||
buyerID := extractChatPeerUserID(reminderURL, selfUserID)
|
||||
return &OutgoingChatMessage{
|
||||
AccountID: accountID,
|
||||
ChatID: chatID,
|
||||
BuyerID: buyerID,
|
||||
Text: text,
|
||||
MessageKey: extractMessageID(decrypted),
|
||||
}
|
||||
}
|
||||
|
||||
// extractChatPeerUserID 从闲鱼聊天深链读取对端用户标识,并拒绝误填为当前账号的值。
|
||||
// reminderURL 是平台通知深链;selfUserID 是当前账号身份;返回空字符串表示深链缺失或身份不可信。
|
||||
func extractChatPeerUserID(reminderURL, selfUserID string) string {
|
||||
// parsed 保存深链解析结果;parseErr 表示平台提供了无法按 URL 解释的兼容字符串。
|
||||
parsed, parseErr := url.Parse(strings.TrimSpace(reminderURL))
|
||||
if parseErr != nil {
|
||||
return ""
|
||||
}
|
||||
// peerUserID 保存深链查询参数中的会话对端身份,并移除 IM 协议后缀。
|
||||
peerUserID := strings.TrimSuffix(strings.TrimSpace(parsed.Query().Get("peerUserId")), "@goofish")
|
||||
if peerUserID == "" || isSelfUserID(peerUserID, selfUserID) {
|
||||
return ""
|
||||
}
|
||||
return peerUserID
|
||||
}
|
||||
|
||||
// extractChatSenderUserIDFromMaps 优先读取展示扩展,并兼容紧凑信封中的发送者账号 ID。
|
||||
func extractChatSenderUserIDFromMaps(m1, m10 map[string]any) string {
|
||||
if m10 != nil {
|
||||
|
||||
@@ -146,22 +146,36 @@ func TestHandleMessage_DedupSkipsRepeat(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleMessage_IgnoresOwnWebSocketEcho 验证当前账号的 WS 回显不会触发自动回复。
|
||||
func TestHandleMessage_IgnoresOwnWebSocketEcho(t *testing.T) {
|
||||
// TestHandleMessage_RecordsOwnWebSocketEchoWithoutAutoReply 验证官方客户端回显会走出站观察持久化,但绝不触发自动回复。
|
||||
func TestHandleMessage_RecordsOwnWebSocketEchoWithoutAutoReply(t *testing.T) {
|
||||
// acc 是待测账号;h 记录业务处理调用;cleanup 释放测试数据库和运行时资源。
|
||||
acc, h, _, cleanup := newAccountForTest(t)
|
||||
defer cleanup()
|
||||
defer acc.Stop()
|
||||
|
||||
// newAccountForTest 使用 unb=123;模拟同一账号在闲鱼官方客户端发出的
|
||||
// 消息回显,不能进入自动回复或业务消息处理链。
|
||||
acc.handleMessage(plainChatMessage(t, "官方客户端发送", "123", "chat-own"))
|
||||
// decrypted 模拟官方客户端发送的文本回显,深链提供真实买家身份,PNM 键用于后续持久化幂等。
|
||||
decrypted := plainChatMessage(t, "官方客户端发送", "123", "chat-own")
|
||||
// envelope 保存可变测试消息信封,用于补齐官方回显的 PNM 键和对端用户标识。
|
||||
envelope := decrypted["1"].(map[string]any)
|
||||
envelope["3"] = "own-message.PNM"
|
||||
// extension 保存消息展示扩展;深链中的 peerUserId 必须指向买家而不是当前账号。
|
||||
extension := envelope["10"].(map[string]any)
|
||||
extension["reminderUrl"] = "fleamarket://message_chat?itemId=item-chat-own&peerUserId=buyer-own"
|
||||
acc.handleMessage(decrypted)
|
||||
time.Sleep(MessageDebounceDelay + 200*time.Millisecond)
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if len(h.chats) != 0 {
|
||||
t.Fatalf("账号自身消息回显不应触发自动回复,got %d", len(h.chats))
|
||||
}
|
||||
if len(h.outgoing) != 1 {
|
||||
t.Fatalf("账号自身消息回显应交给出站观察器,got %d", len(h.outgoing))
|
||||
}
|
||||
// echo 保存观察器收到的出站回显,用于断言会话、买家和平台幂等键未丢失。
|
||||
echo := h.outgoing[0]
|
||||
if echo.AccountID != "cid" || echo.ChatID != "chat-own" || echo.BuyerID != "buyer-own" || echo.MessageKey != "own-message.PNM" || echo.Text != "官方客户端发送" {
|
||||
t.Fatalf("官方客户端出站回显字段错误: %+v", echo)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleMessage_NoReminderNoOp 无 reminderContent 的消息不进入回复链也不报错。
|
||||
|
||||
@@ -15,8 +15,13 @@ import (
|
||||
|
||||
// recordingHandler 记录收到的聊天消息,用于断言防抖与去重行为。
|
||||
type recordingHandler struct {
|
||||
mu sync.Mutex
|
||||
chats []ChatMessage
|
||||
// mu 保护聊天回调、出站回显和续期计数,测试中的异步防抖回调可与断言并发执行。
|
||||
mu sync.Mutex
|
||||
// chats 保存进入自动回复旁路的买家入站消息。
|
||||
chats []ChatMessage
|
||||
// outgoing 保存官方客户端或本程序发送后观察到的出站消息回显。
|
||||
outgoing []OutgoingChatMessage
|
||||
// refresh 保存密码登录续期回调次数。
|
||||
refresh int
|
||||
}
|
||||
|
||||
@@ -47,6 +52,14 @@ func (h *recordingHandler) OnPasswordLoginRefresh(_ context.Context, _ string) b
|
||||
// OnAccountAlert 封装On账号Alert业务协调。
|
||||
func (h *recordingHandler) OnAccountAlert(_ context.Context, _, _, _, _ string) {}
|
||||
|
||||
// HandleOutgoingChatMessage 记录出站观察消息,供测试确认官方客户端回显不进入自动回复链但仍可被持久化。
|
||||
func (h *recordingHandler) HandleOutgoingChatMessage(_ context.Context, message OutgoingChatMessage) error {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.outgoing = append(h.outgoing, message)
|
||||
return nil
|
||||
}
|
||||
|
||||
// newAccountForTest 封装new账号ForTest业务协调。
|
||||
func newAccountForTest(t *testing.T) (*Account, *recordingHandler, *db.Store, func()) {
|
||||
t.Helper()
|
||||
|
||||
@@ -184,6 +184,20 @@ func (d *messageDispatcher) handleMessageContext(ctx context.Context, decrypted
|
||||
}
|
||||
return
|
||||
}
|
||||
// ownEcho 保存当前账号从官方客户端发出后回显的消息;它必须实时落库,但绝不能进入自动回复防抖链。
|
||||
if ownEcho := extractOwnWebSocketEcho(decrypted, d.cookieID, d.currentCookie()); ownEcho != nil {
|
||||
// handler 保存当前可选业务处理器;旧集成未实现出站观察能力时保持原有仅过滤语义。
|
||||
if handler := d.currentHandler(); handler != nil {
|
||||
// observer、supported 保存出站观察接口及其实现判断,避免扩大基础 Handler 的必选职责。
|
||||
if observer, supported := handler.(outgoingChatHandler); supported {
|
||||
// err 保存自身回显持久化或实时发布失败;失败不应影响 WebSocket 接收循环或误触发自动回复。
|
||||
if err := observer.HandleOutgoingChatMessage(ctx, *ownEcho); err != nil {
|
||||
d.logger.Warn("处理官方客户端出站消息回显失败", "err", err, "chat_id", ownEcho.ChatID)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// chat 用于本次流程后续判断的聊天
|
||||
chat := extractChatMessage(decrypted, d.cookieID, d.currentCookie())
|
||||
|
||||
Reference in new issue
Block a user