From ecf0efc5e21e2176baf438d1480bef72aefa23ff Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sun, 16 Aug 2026 23:22:28 +0800 Subject: [PATCH] refactor(protocol): keep hasDecodableContent next to decodeRichBody Move the body-shape predicate into RichBody so admission no longer mirrors decode slots. C2C control stays a header check. --- .../protocol/src/msg-push/blank-filter.ts | 19 ++------------- packages/protocol/src/msg-push/index.ts | 5 ++-- .../src/msg-push/rich-body-decoder.ts | 13 +++++++++++ .../tests/msg-push/rich-body-decoder.test.ts | 23 ++++++++++++++++++- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/packages/protocol/src/msg-push/blank-filter.ts b/packages/protocol/src/msg-push/blank-filter.ts index d91a267d..069c22d0 100644 --- a/packages/protocol/src/msg-push/blank-filter.ts +++ b/packages/protocol/src/msg-push/blank-filter.ts @@ -1,4 +1,5 @@ import type { MsgPushHead, PushMsgBody } from './context'; +import { hasDecodableContent } from './rich-body-decoder'; // C2C-family message types (private 166, temp 141, and 167) whose pushes QQ NT // classifies by `c2c_cmd`. RE: `long_cnn_msg_mgr.cc::OnRecvSysMsg` gates on @@ -24,22 +25,6 @@ export function isC2cControlPush(head: Pick): return C2C_CONTROL_TYPES.has(head.msgType) && C2C_CONTROL_CMDS.has(head.c2cCmd); } -/** - * 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 @@ -62,5 +47,5 @@ export function isBlankMessage( elements: readonly unknown[], body: PushMsgBody | undefined, ): boolean { - return elements.length === 0 && !bodyHasDecodableContent(body); + return elements.length === 0 && !hasDecodableContent(body); } diff --git a/packages/protocol/src/msg-push/index.ts b/packages/protocol/src/msg-push/index.ts index c26cb26a..b1e41b6c 100644 --- a/packages/protocol/src/msg-push/index.ts +++ b/packages/protocol/src/msg-push/index.ts @@ -4,7 +4,8 @@ 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, isC2cControlPush } from './blank-filter'; +import { isC2cControlPush } from './blank-filter'; +import { hasDecodableContent } from './rich-body-decoder'; import { buildContext, type PushMsgBody } from './context'; import { decodeEvent0x210 } from './decoders/event-0x210'; import { decodeEvent0x2DC } from './decoders/event-0x2dc'; @@ -217,7 +218,7 @@ function parseMsgPushInternal( // If instead the body *had* content we just couldn't decode, keep the // (still-empty) event but warn so the missing decoder gets noticed rather // than silently swallowed. - if (bodyHasDecodableContent(ctx.body)) { + if (hasDecodableContent(ctx.body)) { log.warn('message had content but decoded to 0 elements — missing decoder? (kind=%s seq=%d from=%d msgType=%d/%d): %s', ev.kind, ctx.head.sequence, ctx.fromUin, ctx.head.msgType, ctx.head.subType, describeUndecodedBody(ctx.body)); diff --git a/packages/protocol/src/msg-push/rich-body-decoder.ts b/packages/protocol/src/msg-push/rich-body-decoder.ts index a4d061e2..aa0a200b 100644 --- a/packages/protocol/src/msg-push/rich-body-decoder.ts +++ b/packages/protocol/src/msg-push/rich-body-decoder.ts @@ -457,6 +457,19 @@ function buildMediaNode(idx: IndexNode, fi: FileInfo): MessageElement['mediaNode }; } +/** Whether this body carries any slot {@link decodeRichBody} would try: + * `richText.elems`, a voice (`ptt`), a c2c file (`notOnlineFile`), or + * serialized `msgContent`. Presence, not successful decode — a body that + * has elems we do not yet understand is still decodable content. */ +export function hasDecodableContent(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); +} + export function decodeRichBody(body: PushMsgBody | undefined, isGroup: boolean): MessageElement[] { const elements: MessageElement[] = []; logUnknownWireFields(body, 'body'); diff --git a/packages/protocol/tests/msg-push/rich-body-decoder.test.ts b/packages/protocol/tests/msg-push/rich-body-decoder.test.ts index 6d13aafa..5ae7bb7e 100644 --- a/packages/protocol/tests/msg-push/rich-body-decoder.test.ts +++ b/packages/protocol/tests/msg-push/rich-body-decoder.test.ts @@ -12,7 +12,7 @@ import { describe, expect, it } from 'vitest'; import { deflateSync } from 'zlib'; import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; import { getLogLevel, setLogLevel, subscribeLogs } from '@snowluma/common/logger'; -import { decodeRichBody } from '../../src/msg-push/rich-body-decoder'; +import { decodeRichBody, hasDecodableContent } from '../../src/msg-push/rich-body-decoder'; import { MAX_RICH_CARD_OUTPUT_BYTES } from '../../src/msg-push/helpers'; import { buildSendElems } from '../../src/element-builder'; import { assertValidMessageElement } from '../../src/element-manifest'; @@ -982,3 +982,24 @@ describe('decodeRichBody / unknown wire element observability', () => { expect(exhausted).toHaveLength(2); }); }); + +describe('hasDecodableContent', () => { + it('is false for a missing or empty body', () => { + expect(hasDecodableContent(undefined)).toBe(false); + expect(hasDecodableContent({})).toBe(false); + expect(hasDecodableContent({ richText: { elems: [] } })).toBe(false); + }); + + it('is true for elems, ptt, notOnlineFile, or msgContent', () => { + expect(hasDecodableContent({ richText: { elems: [{ text: { str: 'hi' } }] } })).toBe(true); + expect(hasDecodableContent({ richText: { ptt: { fileName: 'a.amr' } } })).toBe(true); + expect(hasDecodableContent({ richText: { notOnlineFile: { fileName: 'a.bin' } } })).toBe(true); + expect(hasDecodableContent({ msgContent: new Uint8Array([1]) })).toBe(true); + }); + + it('is true when elems exist even if decodeRichBody would emit nothing', () => { + const body: MessageBody = { richText: { elems: [{ commonElem: { serviceType: 999, businessType: 0 } }] } }; + expect(hasDecodableContent(body)).toBe(true); + expect(decodeRichBody(body, false)).toEqual([]); + }); +});