diff --git a/packages/core/tests/element-builder.test.ts b/packages/core/tests/element-builder.test.ts index 5142f602..15b02efb 100644 --- a/packages/core/tests/element-builder.test.ts +++ b/packages/core/tests/element-builder.test.ts @@ -347,26 +347,25 @@ describe('element-builder / file element is no longer carried in elems[]', () => }); }); -describe('element-builder / forward preview (com.tencent.multimsg LightApp)', () => { - // The forward preview is the bubble the recipient renders in chat - // before tapping to expand. It MUST be the modern LightApp / - // `com.tencent.multimsg` JSON (not the older `richMsg serviceID=35` - // XML) because nested forwards rely on `meta.detail.uniseq` to link - // each inner preview to the matching `actionCommand` piggyback on - // the outer's LongMsgResult — without uniseq the QQ-NT client has - // no way to walk the tree and has to re-fetch each inner resId. +describe('element-builder / forward preview', () => { + // Flat outer forwards use the serviceID=35 RichMsg shape accepted by NTQQ. + // Nested previews keep the modern LightApp JSON because their + // meta.detail.uniseq must match an actionCommand piggyback in the outer + // LongMsgResult. function decodeLightApp(elem: any): unknown { return JSON.parse(inflatePrefixedPayload(elem.lightApp.data)); } - it('emits a lightApp.data blob with prefix=0x01 (deflated JSON, not XML/serviceID=35)', async () => { + it('emits a serviceID=35 richMsg XML for a flat outer forward', async () => { const [elem] = await buildSendElems( [{ type: 'forward', resId: 'res-XYZ' } as any], { bridge: fakeBridge, groupId: 12345 }, ); - expect(elem.lightApp).toBeDefined(); - expect((elem as any).richMsg).toBeUndefined(); - expect(elem.lightApp!.data![0]).toBe(0x01); + expect(elem.richMsg?.serviceId).toBe(35); + expect(elem.lightApp).toBeUndefined(); + const xml = inflatePrefixedPayload(elem.richMsg!.template1!); + expect(xml).toContain('action="viewMultiMsg"'); + expect(xml).toContain('m_resid="res-XYZ"'); }); it('places resid + uniseq inside meta.detail (and uniseq matches extra.filename)', async () => { @@ -374,6 +373,8 @@ describe('element-builder / forward preview (com.tencent.multimsg LightApp)', () [{ type: 'forward', resId: 'res-XYZ', forwardUuid: 'fixed-uuid-1234' } as any], { bridge: fakeBridge, groupId: 12345 }, ); + expect(elem.lightApp).toBeDefined(); + expect(elem.richMsg).toBeUndefined(); const json = decodeLightApp(elem) as any; expect(json.app).toBe('com.tencent.multimsg'); expect(json.meta.detail.resid).toBe('res-XYZ'); @@ -384,17 +385,40 @@ describe('element-builder / forward preview (com.tencent.multimsg LightApp)', () expect(extra.filename).toBe('fixed-uuid-1234'); }); - it('autogenerates a uniseq when the element omits forwardUuid (flat forward — cosmetic)', async () => { + it('autogenerates m_fileName for a flat outer forward', async () => { const [elem] = await buildSendElems( [{ type: 'forward', resId: 'res-flat' } as any], { bridge: fakeBridge, groupId: 12345 }, ); - const json = decodeLightApp(elem) as any; - expect(json.meta.detail.resid).toBe('res-flat'); - // Auto-generated UUID — non-empty, non-trivial. - expect(json.meta.detail.uniseq).toMatch(/^[0-9a-f-]{36}$/i); - const extra = JSON.parse(json.extra); - expect(extra.filename).toBe(json.meta.detail.uniseq); + const xml = inflatePrefixedPayload(elem.richMsg!.template1!); + expect(xml).toContain('m_resid="res-flat"'); + expect(xml).toMatch(/m_fileName="[0-9a-f-]{36}"/i); + }); + + it('escapes dynamic XML fields in a flat outer forward', async () => { + const [elem] = await buildSendElems( + [{ + type: 'forward', + resId: 'res&<>"\'', + forwardSource: 'Alice & ', + forwardSummary: 'summary "quoted" & more', + forwardPrompt: '[A & B]', + forwardNews: [ + { text: 'Alice: ' }, + { text: 'Bob: it\'s "ok"' }, + ], + forwardTSum: 2, + } as any], + { bridge: fakeBridge, groupId: 12345 }, + ); + const xml = inflatePrefixedPayload(elem.richMsg!.template1!); + expect(xml).toContain('m_resid="res&<>"'"'); + expect(xml).toContain('brief="[A & B]"'); + expect(xml).toContain('Alice & <Bob>'); + expect(xml).toContain('summary "quoted" & more'); + expect(xml).toContain('Alice: <hello>'); + expect(xml).toContain('Bob: it's "ok"'); + expect(xml).toContain('tSum="2"'); }); it('threads forwardSource / forwardSummary / forwardPrompt / forwardNews / forwardTSum verbatim', async () => { diff --git a/packages/protocol/src/element-builder.ts b/packages/protocol/src/element-builder.ts index 830aea1f..25a9e4f8 100644 --- a/packages/protocol/src/element-builder.ts +++ b/packages/protocol/src/element-builder.ts @@ -265,16 +265,24 @@ function makeMarketFaceElem(element: MessageElement): ProtoElem { }; } +function escapeXml(value: string): string { + return value + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); +} + function makeForwardElem(element: MessageElement): ProtoElem { const resId = (element.resId ?? '').trim(); if (!resId) { throw new Error('forward resId is required'); } - // `uniseq` MUST round-trip between the preview JSON and the outer - // upload's piggyback `actionCommand` for nested forwards to resolve - // without an extra server hit. Generate one fresh if absent (flat - // forwards don't piggyback anyway, so the value is cosmetic there). + // Nested forwards must round-trip this value between the LightApp preview + // and the outer upload's piggyback `actionCommand`. Flat forwards have no + // piggyback entry and use the generated value as the RichMsg `m_fileName`. const uniseq = (element.forwardUuid ?? '').trim() || randomUUID(); const source = element.forwardSource && element.forwardSource.length > 0 @@ -291,14 +299,32 @@ function makeForwardElem(element: MessageElement): ProtoElem { ? element.forwardTSum : Math.max(news.length, 1); - // LightApp / `com.tencent.multimsg` is the modern wire shape both - // QQ-NT, Lagrange.Core, and NapCat emit and decode. The older - // `richMsg serviceID=35 m_resid=…` XML still renders on mobile QQ - // but it doesn't carry `uniseq`, so nested forwards lose the link - // between the inner preview and the piggybacked actions on the - // outer's LongMsgResult — cross-checked against - // `dev/Lagrange.Core/.../Message/Entity/MultiMsgEntity.cs:43-115` - // and `dev/NapCatQQ/.../helper/forward-msg-builder.ts:52-122`. + // Flat outer forwards use serviceID=35 RichMsg. Nested previews keep + // LightApp because their forwardUuid must match a piggyback actionCommand + // in the outer long-message body. + if (!element.forwardUuid) { + const titles = news.map((item) => + '' + escapeXml(item.text ?? '') + '', + ).join(''); + const xml = " " + + '' + + ' ' + + '' + escapeXml(source) + '' + + titles + + '
' + escapeXml(summary) + '' + + '
'; + return { + richMsg: { + template1: makeDeflatedPayload(xml), + serviceId: 35, + }, + }; + } + const lightApp = { app: 'com.tencent.multimsg', config: {