fix(forward): piggyback every nested level onto the outermost long-msg upload (NapCat model)

Cross-referenced `dev/NapCatQQ/.../SendMsg.uploadForwardedNodesPacket`
(208-347): every layer of a nested forward attaches its own msgBody to
the *outer* long-msg upload under a uuid `actionCommand` slot. Receiver
walks the whole tree from one server fetch instead of resolving each
layer's res_id separately.

`dev/Lagrange.Core` (V2 branch on `linwenxuan04/v2`,
`MultiMsgUploadService.cs:23-31`) only writes a single MultiMsg action
— that's why NapCat-style receivers couldn't follow our previous
nested-forward sends past the first level.

Refactor:

1. `ForwardNodePayload` gets an optional `innerForward?:
   ForwardNodePayload[]` field. `parseForwardNodes` no longer eagerly
   uploads inner chains — it just attaches them here.

2. `uploadForwardNodes` now drives the whole recursion. The new
   `uploadForwardNodesRecursive`:
   - walks each node, recursively uploads any `innerForward` first
   - replaces that node's `elements` with a real `{type:'forward'}`
     preview pointing at the inner res_id
   - piggybacks the inner level's `{uuid, msgBody}` + every deeper
     level it surfaced into this level's `myInnerActions`
   - encodes the long-msg payload as
     `[{MultiMsg, ownMsgBody}, ...innerActions.map(uuid+msgBody)]`
   - returns `{resId, msgBody, uuid, innerActions}` so an outer call
     can keep climbing

   Top-level public signature unchanged: `Promise<string>`.

3. New `deriveInnerSource` / `previewLinesFromNodes` /
   `previewFromElements` helpers synthesise the ARK preview metadata
   the inner bubble shows before the user taps to expand. They handle
   text/image/record/video/file/forward/face element previews.

4. `parseForwardNodes`: when `content` is an all-node array, attach
   the parsed inner chain to `node.innerForward` and let the upload
   pipeline take over. `elements = []` placeholder is replaced by
   `uploadForwardNodes` once it has the real res_id.

Tests:

- `actions/forward.test.ts`:
  - `uploadForwardNodes recursively uploads nested innerForward chains
    (NapCat piggyback model)` — asserts two SsoSendLongMsg roundtrips
    (inner first, then outer) and the outer res_id propagates up
  - `flat sends still take a single roundtrip` — backwards-compat

- `onebot/forward-nested.test.ts`: rewritten — mock now sees exactly
  one `uploadForwardNodes` call with `innerForward` attached, instead
  of two sequential calls; depth-limit + mixed-content tests
  unchanged.
This commit is contained in:
DrSmoothl committed 2026-05-19 23:29:59 +08:00
1 parent 0b8dc1f7af
commit cec38b4c1b
5 files changed
+289 -86

No files matched your search

@@ -635,34 +635,42 @@ async function parseForwardNodes(
const content = (nodeData.content ?? nodeData.message ?? '') as JsonValue;
let elements: MessageElement[];
let innerForward: ForwardNodePayload[] | undefined;
if (isNestedNodeArray(content)) {
// Nested forward chain — `content` is itself a list of `{type:'node'}`
// segments. Recursively parse them into ForwardNodePayloads, upload
// that inner chain as its own forward, then embed an ARK preview
// card pointing at the inner res_id. The receiving QQ client renders
// it as a tap-to-expand nested forward bubble. Matches NapCat's
// `SendMsg.uploadForwardedNodesPacket` recursion contract.
// segments. We recursively parse them into a sibling
// `ForwardNodePayload[]` and attach it to this node as `innerForward`.
// `uploadForwardNodes` then drives the recursive upload + ARK-preview
// generation + msgBody piggyback in one pass over the whole tree.
//
// Without this, the inner `{type:'node'}` entries fell through to
// `parseMessage` which produced useless `MessageElement{type:'node'}`
// entries that `element-builder` silently drops — leaving the outer
// forward with an empty body that QQ refuses with "message is empty"
// (single-node case) or a node whose content is missing the inner
// forward (mixed case).
const innerNodes = await parseForwardNodes(ref, content, {
// Why hand it off instead of uploading here: NapCat (`dev/NapCatQQ/
// .../SendMsg.uploadForwardedNodesPacket`) carries every inner level's
// packetMsg up to the outermost long-msg upload as extra
// `actionCommand` slots, so the receiver gets the whole tree from one
// server fetch. That cross-layer piggyback needs the upload pipeline
// to own the recursion — doing it in `parseForwardNodes` would force
// each level to do its own independent long-msg upload, which is what
// the previous implementation did and what a NapCat-compatible
// receiver couldn't walk.
innerForward = await parseForwardNodes(ref, content, {
groupId: options.groupId,
userId: options.userId,
depth: depth + 1,
});
const innerResId = await ref.bridge.uploadForwardNodes(innerNodes, options.groupId, options.userId);
const isGroup = options.groupId !== undefined;
elements = [buildForwardPreviewElement(innerResId, innerNodes, isGroup, undefined)];
// Placeholder — `uploadForwardNodes` replaces these with a real
// forward-preview MessageElement once it has the inner res_id +
// uuid in hand.
elements = [];
} else {
elements = await parseMessage(content, false);
}
if (elements.length === 0) throw new Error(`forward node content is empty: ${userUin}`);
if (!innerForward && elements.length === 0) {
throw new Error(`forward node content is empty: ${userUin}`);
}
nodes.push({ userUin, nickname, elements });
const node: ForwardNodePayload = { userUin, nickname, elements };
if (innerForward) node.innerForward = innerForward;
nodes.push(node);
}
if (nodes.length === 0) {