From 2ffb6192ddc7df0167ad4c1f3138f1c6017e067a Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Tue, 16 Jun 2026 01:24:31 +0800 Subject: [PATCH] =?UTF-8?q?fix(msg-push):=20drop=20blank=20"[=E7=A9=BA?= =?UTF-8?q?=E6=B6=88=E6=81=AF]"=20pushes=20in=20c2c/group=20history=20too?= =?UTF-8?q?=20(#102)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #102 fix dropped content-less control pushes only in parseMsgPush (live). History fetches (fetchC2cMessageRange / fetchGroupMessageRange) decoded straight into the result, so the same phantom resurfaced as "[空消息]" on backfill. RE of wrapper.linux.node confirms QQ NT filters blanks on BOTH paths — live/roam C2C via c2c_roam_msg_mgr.cc::FilterBlankMsgAndRetryFetch and group roam via group_roam_msg_worker.cc::FilterBlankSeqsMsg. Extracts the blank test into blank-filter.ts (bodyHasDecodableContent + isBlankMessage) and applies it in all three message-producing paths. A message that decoded to 0 elements but whose body carried content is a missing decoder, not a blank — isBlankMessage returns false there so it's kept (live path still warns). --- .../protocol/src/msg-push/blank-filter.ts | 42 +++++++++++++++++++ .../src/msg-push/fetch-c2c-history.ts | 7 +++- .../src/msg-push/fetch-group-history.ts | 7 +++- packages/protocol/src/msg-push/index.ts | 17 +------- .../tests/msg-push/fetch-c2c-history.test.ts | 25 ++++++++++- .../msg-push/fetch-group-history.test.ts | 27 +++++++++++- 6 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 packages/protocol/src/msg-push/blank-filter.ts diff --git a/packages/protocol/src/msg-push/blank-filter.ts b/packages/protocol/src/msg-push/blank-filter.ts new file mode 100644 index 00000000..9e02a404 --- /dev/null +++ b/packages/protocol/src/msg-push/blank-filter.ts @@ -0,0 +1,42 @@ +import type { PushMsgBody } from './context'; + +/** + * Whether a push body carries anything {@link decodeRichBody} could turn into an + * element: source `richText.elems`, a voice (`ptt`), a c2c file + * (`notOnlineFile`), or serialised `msgContent` file metadata. Mirrors exactly + * what the rich-body decoder reads. A body with none of these is a genuinely + * content-less control push, not an element type we merely fail to decode. + */ +export function bodyHasDecodableContent(body: PushMsgBody | undefined): boolean { + const rt = body?.richText; + if (rt) { + if (rt.elems && rt.elems.length > 0) return true; + if (rt.ptt || rt.notOnlineFile) return true; + } + return !!(body?.msgContent && body.msgContent.length > 0); +} + +/** + * A message-kind event is "blank" — the "[空消息]" phantom from #102 — when it + * decoded to zero elements AND its body carried nothing decodable. These are + * QQ's content-less C2C control/system pushes (msgType 166/141/167 carrying a + * `c2c_cmd`, routed by the official client through `OnRecvSysMsg` rather than + * shown as a bubble). + * + * QQ NT drops such records before the UI ever sees them, in BOTH the live push + * path and the history (roam) fetch path — RE of `wrapper.linux.node`: + * - live/roam C2C: `c2c_roam_msg_mgr.cc::FilterBlankMsgAndRetryFetch` + * - group roam: `group_roam_msg_worker.cc::FilterBlankSeqsMsg` + * So we mirror it everywhere a message event is produced — live (`parseMsgPush`) + * and history (`fetchC2cMessageRange` / `fetchGroupMessageRange`). + * + * NOTE: a body that DID carry content but still decoded to zero elements is a + * missing decoder, not a blank message — this returns false for that case so + * the caller can keep (and warn about) it rather than silently dropping content. + */ +export function isBlankMessage( + elements: readonly unknown[], + body: PushMsgBody | undefined, +): boolean { + return elements.length === 0 && !bodyHasDecodableContent(body); +} diff --git a/packages/protocol/src/msg-push/fetch-c2c-history.ts b/packages/protocol/src/msg-push/fetch-c2c-history.ts index 9b08986c..b6c537b3 100644 --- a/packages/protocol/src/msg-push/fetch-c2c-history.ts +++ b/packages/protocol/src/msg-push/fetch-c2c-history.ts @@ -3,6 +3,7 @@ import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; import type { SsoGetC2cMsg, SsoGetC2cMsgResponse } from '@snowluma/proto-defs/get-c2c-msg'; import type { QQEventVariant } from '../events'; import type { IdentityService } from '../identity-service'; +import { isBlankMessage } from './blank-filter'; import { buildContextFromMessage } from './context'; import { decodeFriendMessage } from './decoders/friend-message'; @@ -48,7 +49,11 @@ export async function fetchC2cMessageRange( const ctx = buildContextFromMessage(msg, selfUin, identity); if (!ctx) continue; for (const ev of decodeFriendMessage(ctx)) { - if (ev.kind === 'friend_message' && ev.msgSeq > 0) out.push(ev); + if (ev.kind !== 'friend_message' || ev.msgSeq <= 0) continue; + // Drop content-less control pushes (the "[空消息]" phantom, #102) just as + // QQ NT does on its roam/history fetch — keep history parity with live. + if (isBlankMessage(ev.elements, ctx.body)) continue; + out.push(ev); } } out.sort((a, b) => a.msgSeq - b.msgSeq); diff --git a/packages/protocol/src/msg-push/fetch-group-history.ts b/packages/protocol/src/msg-push/fetch-group-history.ts index df201cec..c9cb87bd 100644 --- a/packages/protocol/src/msg-push/fetch-group-history.ts +++ b/packages/protocol/src/msg-push/fetch-group-history.ts @@ -3,6 +3,7 @@ import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; import type { SsoGetGroupMsg, SsoGetGroupMsgResponse } from '@snowluma/proto-defs/get-group-msg'; import type { QQEventVariant } from '../events'; import type { IdentityService } from '../identity-service'; +import { isBlankMessage } from './blank-filter'; import { buildContextFromMessage } from './context'; import { decodeGroupMessage } from './decoders/group-message'; @@ -48,7 +49,11 @@ export async function fetchGroupMessageRange( const ctx = buildContextFromMessage(msg, selfUin, identity); if (!ctx) continue; for (const ev of decodeGroupMessage(ctx)) { - if (ev.kind === 'group_message' && ev.msgSeq > 0) out.push(ev); + if (ev.kind !== 'group_message' || ev.msgSeq <= 0) continue; + // Drop content-less control pushes (the "[空消息]" phantom, #102) just as + // QQ NT does on its group roam/history fetch — keep history parity with live. + if (isBlankMessage(ev.elements, ctx.body)) continue; + out.push(ev); } } out.sort((a, b) => a.msgSeq - b.msgSeq); diff --git a/packages/protocol/src/msg-push/index.ts b/packages/protocol/src/msg-push/index.ts index 695b20eb..62cd5fc8 100644 --- a/packages/protocol/src/msg-push/index.ts +++ b/packages/protocol/src/msg-push/index.ts @@ -4,6 +4,7 @@ import type { PacketInfo } from '@snowluma/common/protocol-types'; import type { Elem } from '@snowluma/proto-defs/element'; import type { QQEventVariant } from '../events'; import type { IdentityService } from '../identity-service'; +import { bodyHasDecodableContent } from './blank-filter'; import { buildContext, type PushMsgBody } from './context'; import { decodeEvent0x210 } from './decoders/event-0x210'; import { decodeEvent0x2DC } from './decoders/event-0x2dc'; @@ -53,22 +54,6 @@ const MESSAGE_KINDS = new Set([ 'friend_message', 'group_message', 'temp_message', ]); -/** - * Whether a push body carries anything the rich-body decoder could turn into an - * element: source `richText.elems`, a voice (`ptt`), a c2c file - * (`notOnlineFile`), or serialised `msgContent` file metadata. Mirrors exactly - * what {@link decodeRichBody} reads. A body that has none of these is a - * genuinely content-less control push, not an element type we fail to decode. - */ -function bodyHasDecodableContent(body: PushMsgBody | undefined): boolean { - const rt = body?.richText; - if (rt) { - if (rt.elems && rt.elems.length > 0) return true; - if (rt.ptt || rt.notOnlineFile) return true; - } - return !!(body?.msgContent && body.msgContent.length > 0); -} - /** * Summarise a body that decoded to zero elements despite carrying content — * each element's field names, every `commonElem`'s serviceType/businessType + diff --git a/packages/protocol/tests/msg-push/fetch-c2c-history.test.ts b/packages/protocol/tests/msg-push/fetch-c2c-history.test.ts index 8c438b06..da4d09d5 100644 --- a/packages/protocol/tests/msg-push/fetch-c2c-history.test.ts +++ b/packages/protocol/tests/msg-push/fetch-c2c-history.test.ts @@ -41,12 +41,12 @@ describe('fetchC2cMessageRange / SsoGetC2cMsg', () => { { responseHead: { fromUin: 222, forward: { friendName: 'Bob' } }, contentHead: { msgType: 166, sequence: 120, timestamp: 1700000120, msgId: 7120 }, - body: { richText: { elems: [] } }, + body: { richText: { elems: [{ text: { str: 'hi' } }] } }, }, { responseHead: { fromUin: 111, forward: { friendName: 'Alice' } }, contentHead: { msgType: 166, sequence: 110, timestamp: 1700000110, msgId: 7110 }, - body: { richText: { elems: [] } }, + body: { richText: { elems: [{ text: { str: 'hi' } }] } }, }, ], }); @@ -61,6 +61,27 @@ describe('fetchC2cMessageRange / SsoGetC2cMsg', () => { expect(out[1]).toMatchObject({ msgSeq: 120, senderUin: 222, senderNick: 'Bob' }); }); + it('drops content-less blank messages, keeps real ones (#102 parity)', async () => { + const resp = protobuf_encode({ + friendUid: 'u_friend', + messages: [ + { // genuinely-blank control push (the invite phantom) → dropped + responseHead: { fromUin: 111, forward: { friendName: 'Alice' } }, + contentHead: { msgType: 166, sequence: 110, timestamp: 1700000110, msgId: 7110 }, + body: { richText: { elems: [] } }, + }, + { // real message → kept + responseHead: { fromUin: 222, forward: { friendName: 'Bob' } }, + contentHead: { msgType: 166, sequence: 120, timestamp: 1700000120, msgId: 7120 }, + body: { richText: { elems: [{ text: { str: 'hi' } }] } }, + }, + ], + }); + const out = await fetchC2cMessageRange({ sendRawPacket: async () => okResult(resp) }, identity, 10001, 'u_friend', 100, 120); + expect(out.map((m) => m.msgSeq)).toEqual([120]); + expect(out[0]).toMatchObject({ senderUin: 222, elements: [{ type: 'text', text: 'hi' }] }); + }); + it('returns [] on a failed packet, empty uid, or out-of-range request', async () => { const failSender = { sendRawPacket: async () => ({ success: false, gotResponse: false } as SendPacketResult) }; expect(await fetchC2cMessageRange(failSender, identity, 10001, 'u_x', 100, 120)).toEqual([]); diff --git a/packages/protocol/tests/msg-push/fetch-group-history.test.ts b/packages/protocol/tests/msg-push/fetch-group-history.test.ts index 422a1e91..72b8e2d2 100644 --- a/packages/protocol/tests/msg-push/fetch-group-history.test.ts +++ b/packages/protocol/tests/msg-push/fetch-group-history.test.ts @@ -45,12 +45,12 @@ describe('fetchGroupMessageRange / SsoGetGroupMsg', () => { { responseHead: { fromUin: 222, grp: { groupUin: 9999, memberName: 'Bob' } }, contentHead: { msgType: 82, sequence: 120, timestamp: 1700000120, msgId: 5120 }, - body: { richText: { elems: [] } }, + body: { richText: { elems: [{ text: { str: 'hi' } }] } }, }, { responseHead: { fromUin: 111, grp: { groupUin: 9999, memberName: 'Alice' } }, contentHead: { msgType: 82, sequence: 110, timestamp: 1700000110, msgId: 5110 }, - body: { richText: { elems: [] } }, + body: { richText: { elems: [{ text: { str: 'hi' } }] } }, }, ], }, @@ -67,6 +67,29 @@ describe('fetchGroupMessageRange / SsoGetGroupMsg', () => { expect(out[1]).toMatchObject({ msgSeq: 120, senderUin: 222, senderNick: 'Bob' }); }); + it('drops content-less blank messages, keeps real ones (#102 parity)', async () => { + const resp = protobuf_encode({ + body: { + groupUin: 9999, + messages: [ + { // genuinely-blank control push → dropped + responseHead: { fromUin: 111, grp: { groupUin: 9999, memberName: 'Alice' } }, + contentHead: { msgType: 82, sequence: 110, timestamp: 1700000110, msgId: 5110 }, + body: { richText: { elems: [] } }, + }, + { // real message → kept + responseHead: { fromUin: 222, grp: { groupUin: 9999, memberName: 'Bob' } }, + contentHead: { msgType: 82, sequence: 120, timestamp: 1700000120, msgId: 5120 }, + body: { richText: { elems: [{ text: { str: 'hi' } }] } }, + }, + ], + }, + }); + const out = await fetchGroupMessageRange({ sendRawPacket: async () => okResult(resp) }, identity, 10001, 9999, 100, 120); + expect(out.map((m) => m.msgSeq)).toEqual([120]); + expect(out[0]).toMatchObject({ senderUin: 222, elements: [{ type: 'text', text: 'hi' }] }); + }); + it('returns [] on a failed packet or out-of-range request', async () => { const failSender = { sendRawPacket: async () => ({ success: false, gotResponse: false } as SendPacketResult) }; expect(await fetchGroupMessageRange(failSender, identity, 10001, 9999, 100, 120)).toEqual([]);