diff --git a/packages/core/tests/actions/forward-nested-fetch.test.ts b/packages/core/tests/actions/forward-nested-fetch.test.ts
new file mode 100644
index 00000000..50d6766d
--- /dev/null
+++ b/packages/core/tests/actions/forward-nested-fetch.test.ts
@@ -0,0 +1,216 @@
+import { describe, expect, it, vi } from 'vitest';
+import { deflateSync, gzipSync } from 'zlib';
+import { buildApiContext, type OneBotInstanceContext } from '@snowluma/onebot/instance-context';
+import type { Elem } from '@snowluma/proto-defs/element';
+import type { LongMsgResult, RecvLongMsgReq, RecvLongMsgResp } from '@snowluma/proto-defs/longmsg';
+import type { PushMsgBody } from '@snowluma/proto-defs/message';
+import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
+import { ForwardApi } from '../../src/bridge/apis/forward';
+import type { BridgeContext } from '../../src/bridge/bridge-context';
+import { mockBridge } from './_helpers';
+
+function forwardCard(format: 'xml' | 'lightApp', resId: string, key?: string): Elem {
+ const content =
+ format === 'xml'
+ ? ``
+ : JSON.stringify({
+ app: 'com.tencent.multimsg',
+ meta: { detail: { resid: resId, uniseq: key } },
+ });
+ const data = Buffer.concat([Buffer.from([1]), deflateSync(Buffer.from(content))]);
+ return format === 'xml'
+ ? { richMsg: { serviceId: 35, template1: data } }
+ : { lightApp: { data } };
+}
+
+function message(...elems: Elem[]): PushMsgBody {
+ return {
+ responseHead: {
+ fromUin: 20002,
+ fromUid: 'sender-uid',
+ grp: { groupUin: 12345, memberCard: 'alice' },
+ },
+ contentHead: { msgType: 82, sequence: 10, timestamp: 1700000000 },
+ body: { richText: { elems } },
+ };
+}
+
+function setup(downloads: Map) {
+ const requested: string[] = [];
+ const bridge = mockBridge({
+ sendRawPacket: vi.fn(async (command: string, bytes: Uint8Array) => {
+ expect(command).toBe('trpc.group.long_msg_interface.MsgService.SsoRecvLongMsg');
+ const resId = protobuf_decode(bytes).info!.resId!;
+ requested.push(resId);
+ const data = downloads.get(resId);
+ return {
+ success: true,
+ gotResponse: true,
+ errorCode: 0,
+ errorMessage: '',
+ responseData: Buffer.from(
+ protobuf_encode({
+ result: {
+ resId,
+ payload: data
+ ? gzipSync(protobuf_encode(data))
+ : undefined,
+ },
+ }),
+ ),
+ };
+ }),
+ });
+ const forward = new ForwardApi(bridge as unknown as BridgeContext);
+ const ctx = {
+ selfId: 10001,
+ bridge: { apis: { forward } },
+ converterCtx: {
+ selfId: 10001,
+ imageUrlResolver: null,
+ mediaUrlResolver: null,
+ messageIdResolver: null,
+ mediaSegmentSink: null,
+ },
+ } as unknown as OneBotInstanceContext;
+ return { api: buildApiContext(ctx), requested };
+}
+
+describe('get_forward_msg / nested downloads (#529)', () => {
+ it.each(['xml', 'lightApp'] as const)(
+ 'resolves siblings and three levels of %s forwards from one outer payload',
+ async (format) => {
+ const outer = `nested-fetch-${format}-outer`;
+ const inner = `nested-fetch-${format}-inner`;
+ const sibling = `nested-fetch-${format}-sibling`;
+ const leaf = `nested-fetch-${format}-leaf`;
+ const { api, requested } = setup(
+ new Map([
+ [
+ outer,
+ {
+ action: [
+ {
+ actionCommand: 'MultiMsg',
+ actionData: {
+ msgBody: [
+ message(forwardCard(format, inner, 'inner-key')),
+ message(forwardCard(format, sibling, 'sibling-key')),
+ ],
+ },
+ },
+ {
+ actionCommand: 'leaf-key',
+ actionData: {
+ msgBody: [
+ message(
+ { text: { str: 'deepest content' } },
+ {
+ customFace: {
+ md5: Buffer.alloc(16, 0xab),
+ origUrl: 'https://example.com/forward.png',
+ width: 32,
+ height: 24,
+ },
+ },
+ ),
+ ],
+ },
+ },
+ {
+ actionCommand: 'sibling-key',
+ actionData: {
+ msgBody: [message({ text: { str: 'sibling content' } })],
+ },
+ },
+ {
+ actionCommand: 'inner-key',
+ actionData: {
+ msgBody: [message(forwardCard(format, leaf, 'leaf-key'))],
+ },
+ },
+ ],
+ },
+ ],
+ ]),
+ );
+
+ const parent = await api.getForwardMsg(outer);
+ expect(parent.map((node) => node.message)).toEqual([
+ [{ type: 'forward', data: { id: inner } }],
+ [{ type: 'forward', data: { id: sibling } }],
+ ]);
+ const middle = await api.getForwardMsg(inner);
+ expect(middle[0]!.message).toEqual([{ type: 'forward', data: { id: leaf } }]);
+ expect((await api.getForwardMsg(leaf))[0]).toMatchObject({
+ sender: { user_id: 20002, nickname: 'alice' },
+ time: 1700000000,
+ message: [
+ { type: 'text', data: { text: 'deepest content' } },
+ { type: 'image', data: { url: 'https://example.com/forward.png' } },
+ ],
+ });
+ expect((await api.getForwardMsg(sibling))[0]!.message).toEqual([
+ { type: 'text', data: { text: 'sibling content' } },
+ ]);
+ expect(requested).toEqual([outer]);
+ },
+ );
+
+ it.each([undefined, 'unmatched-key'])(
+ 'downloads by resId when the XML filename is %s and preserves empty-payload errors',
+ async (key) => {
+ const outer = `nested-fetch-fallback-${key}-outer`;
+ const inner = `nested-fetch-fallback-${key}-inner`;
+ const empty = `nested-fetch-fallback-${key}-empty`;
+ const { api, requested } = setup(
+ new Map([
+ [
+ outer,
+ {
+ action: [
+ {
+ actionCommand: 'MultiMsg',
+ actionData: {
+ msgBody: [
+ message(forwardCard('xml', inner, key)),
+ message(forwardCard('xml', empty, key)),
+ ],
+ },
+ },
+ {
+ actionCommand: 'unrelated-key',
+ actionData: {
+ msgBody: [message({ text: { str: 'unrelated content' } })],
+ },
+ },
+ ],
+ },
+ ],
+ [
+ inner,
+ {
+ action: [
+ {
+ actionCommand: 'MultiMsg',
+ actionData: {
+ msgBody: [message({ text: { str: 'downloaded content' } })],
+ },
+ },
+ ],
+ },
+ ],
+ ]),
+ );
+
+ await api.getForwardMsg(outer);
+ expect((await api.getForwardMsg(inner))[0]!.message).toEqual([
+ { type: 'text', data: { text: 'downloaded content' } },
+ ]);
+ await expect(api.getForwardMsg(empty)).rejects.toThrow(
+ 'download forward message payload is empty',
+ );
+ expect(requested).toEqual([outer, inner, empty]);
+ },
+ );
+});
diff --git a/packages/protocol/src/msg-push/rich-body-decoder.ts b/packages/protocol/src/msg-push/rich-body-decoder.ts
index 0684eae2..26e9793e 100644
--- a/packages/protocol/src/msg-push/rich-body-decoder.ts
+++ b/packages/protocol/src/msg-push/rich-body-decoder.ts
@@ -137,9 +137,12 @@ function decodeRichMsgCard(
if (svcId === 35) {
const match = /\bm_resid="([^"]+)"/.exec(content);
+ // XML forwards link bundled inner messages by m_fileName, just as
+ // LightApp forwards use uniseq. Keep it for the outer payload lookup.
+ const fileName = /\bm_fileName="([^"]+)"/.exec(content)?.[1];
return {
element: match
- ? { type: 'forward', resId: match[1] }
+ ? { type: 'forward', resId: match[1], forwardUuid: fileName }
: { type: 'xml', text: content, subType: svcId },
error: null,
inputBytes: data.length,
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 71abc7ae..355eb448 100644
--- a/packages/protocol/tests/msg-push/rich-body-decoder.test.ts
+++ b/packages/protocol/tests/msg-push/rich-body-decoder.test.ts
@@ -218,6 +218,34 @@ describe('decodeRichBody / forward LightApp', () => {
expect(out[0]!.type).toBe('json');
});
+ it.each([false, true])(
+ 'preserves the XML forward filename for nested lookup (compressed=%s, #529)',
+ (compressed) => {
+ const xml = Buffer.from(
+ '',
+ );
+ const body: MessageBody = {
+ richText: {
+ elems: [
+ {
+ richMsg: {
+ serviceId: 35,
+ template1: Buffer.concat([
+ Buffer.from([compressed ? 1 : 0]),
+ compressed ? deflateSync(xml) : xml,
+ ]),
+ },
+ },
+ ],
+ },
+ };
+
+ expect(decodeRichBody(body, true)).toEqual([
+ { type: 'forward', resId: 'inner-xml-res', forwardUuid: 'inner-xml-key' },
+ ]);
+ },
+ );
+
it('still decodes the legacy richMsg serviceID=35 path (backward compat with mobile QQ)', () => {
// Older clients (and some bridges) still emit the m_resid XML
// shape. The decoder must keep treating it as a forward element