From 075a27cc46ce18fcfc34bc08f73ea177166f78df Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sat, 26 Sep 2026 16:09:31 +0800 Subject: [PATCH] fix(protocol): classify private file subcommand receipts Drop download acknowledgements carried by the offline-file subcommand in both push and history paths, while retaining incoming file transfers. Fixes #477 --- .../src/msg-push/rich-body-decoder.ts | 16 +++++++------- .../tests/msg-push/blank-filter.test.ts | 13 ++++++++++++ .../tests/msg-push/empty-message-drop.test.ts | 21 +++++++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/packages/protocol/src/msg-push/rich-body-decoder.ts b/packages/protocol/src/msg-push/rich-body-decoder.ts index 5d3e91cc..cc2ae6b8 100644 --- a/packages/protocol/src/msg-push/rich-body-decoder.ts +++ b/packages/protocol/src/msg-push/rich-body-decoder.ts @@ -16,7 +16,7 @@ import type { TextElem, } from '@snowluma/proto-defs/element'; import type { MarkdownData } from '@snowluma/proto-defs/action'; -import type { FileExtra, MessageBody, PushMsgBody as PushMsgBodyFull, RichText } from '@snowluma/proto-defs/message'; +import type { FileExtra, MessageBody, NotOnlineFile, PushMsgBody as PushMsgBodyFull, RichText } from '@snowluma/proto-defs/message'; import { decompressData, imageUrlFromMd5, @@ -1200,7 +1200,7 @@ function extractRichtextExtras( } // NotOnlineFile (C2C file) - if (rt.notOnlineFile && !isOfflineFileReceiptFlag(rt.notOnlineFile.downloadFlag)) { + if (rt.notOnlineFile && !isOfflineFileReceiptMetadata(rt.notOnlineFile)) { const f = rt.notOnlineFile; elements.push({ type: 'file', fileId: f.fileUuid ?? '', @@ -1228,7 +1228,7 @@ function extractMsgContent(msgContent: Uint8Array, elements: MessageElement[]): ); if (!extra?.file) return; const f = extra.file; - if (isOfflineFileReceiptFlag(f.downloadFlag)) return; + if (isOfflineFileReceiptMetadata(f)) return; if (!f.fileUuid) return; elements.push({ type: 'file', @@ -1239,19 +1239,19 @@ function extractMsgContent(msgContent: Uint8Array, elements: MessageElement[]): }); } -/** QQ treats downloadFlag=2 as an offline-file receipt gray tip, not a chat bubble. */ -function isOfflineFileReceiptFlag(flag: number | undefined): boolean { - return flag === 2; +/** Download acknowledgements can use either the receipt flag or the file subcommand. */ +function isOfflineFileReceiptMetadata(file: NotOnlineFile | null | undefined): boolean { + return file?.downloadFlag === 2 || (file?.fileType === 1 && file.subcmd === 2); } /** True when this body is an offline-file download receipt, not a new file message. */ export function isOfflineFileReceipt(body: PushMsgBody | undefined): boolean { - if (isOfflineFileReceiptFlag(body?.richText?.notOnlineFile?.downloadFlag)) return true; + if (isOfflineFileReceiptMetadata(body?.richText?.notOnlineFile)) return true; if (!body?.msgContent || body.msgContent.length === 0) return false; const extra = decodeProtobufPayload( 'messageBody.msgContent', body.msgContent, () => protobuf_decode(body.msgContent!), ); - return isOfflineFileReceiptFlag(extra?.file?.downloadFlag); + return isOfflineFileReceiptMetadata(extra?.file); } diff --git a/packages/protocol/tests/msg-push/blank-filter.test.ts b/packages/protocol/tests/msg-push/blank-filter.test.ts index 2ae66b22..27ca2cd3 100644 --- a/packages/protocol/tests/msg-push/blank-filter.test.ts +++ b/packages/protocol/tests/msg-push/blank-filter.test.ts @@ -51,4 +51,17 @@ describe('classifyMessageSurvival', () => { }; expect(classifyMessageSurvival(privateHead, [{ type: 'file' }], body)).toBe('drop-receipt'); }); + + it('classifies subcommand receipts consistently for history and push consumers (#477)', () => { + const file = { fileType: 1, subcmd: 2, fileUuid: 'already-sent', fileName: 'test.json', fileSize: 42n }; + for (const body of [ + { msgContent: protobuf_encode({ file }) }, + { richText: { notOnlineFile: file } }, + ]) { + expect(classifyMessageSurvival(privateHead, [{ type: 'file' }], body)).toBe('drop-receipt'); + } + expect(classifyMessageSurvival(privateHead, [{ type: 'file' }], { + richText: { notOnlineFile: { ...file, fileType: 3 } }, + })).toBe('keep'); + }); }); diff --git a/packages/protocol/tests/msg-push/empty-message-drop.test.ts b/packages/protocol/tests/msg-push/empty-message-drop.test.ts index 79ca4cd9..87f6dce0 100644 --- a/packages/protocol/tests/msg-push/empty-message-drop.test.ts +++ b/packages/protocol/tests/msg-push/empty-message-drop.test.ts @@ -123,6 +123,7 @@ describe('parseMsgPush — empty message drop (#102)', () => { it('keeps a real incoming c2c file (#442)', () => { const extra = protobuf_encode({ file: { + fileType: 1, fileUuid: 'fid-from-user', fileName: 'doc.bin', fileSize: 10n, @@ -141,6 +142,26 @@ describe('parseMsgPush — empty message drop (#102)', () => { }); }); + it.each(['msgContent', 'richText'] as const)('drops a subcommand receipt in %s without a download flag (#477)', (location) => { + const entries: LogEntry[] = []; + setLogLevel('trace'); + const unsubscribe = subscribeLogs((entry) => entries.push(entry)); + const file = { fileType: 1, subcmd: 2, fileUuid: 'already-sent', fileName: 'test.json', fileSize: 42n }; + try { + const out = parseMsgPush(pushPacket({ + responseHead: { fromUin: 10001, fromUid: 'u_x' }, + contentHead: { msgType: 529, subType: 4, c2cCmd: 4, sequence: 10, timestamp: 1, msgId: 1 }, + body: location === 'msgContent' + ? { msgContent: protobuf_encode({ file }) } + : { richText: { notOnlineFile: file } }, + }), identity); + expect(out).toEqual([]); + expect(traceMessages(entries).some((message) => message.includes('branch=offline_file_receipt'))).toBe(true); + } finally { + unsubscribe(); + } + }); + it('records the precise branch for a C2C control push with content', () => { // 166 + c2c_cmd ∈ {1,73,75,129,131,133,135,192} is a control/system signal // QQ NT routes via OnRecvSysMsg, never a bubble — drop regardless of body.