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.
This commit is contained in:
DrSmoothl
2026-08-16 23:22:28 +08:00
parent 206727f823
commit ecf0efc5e2
4 changed files with 40 additions and 20 deletions
+2 -17
View File
@@ -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<MsgPushHead, 'msgType' | 'c2cCmd'>):
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);
}
+3 -2
View File
@@ -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));
@@ -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');
@@ -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([]);
});
});