diff --git a/packages/core/src/bridge/apis/forward.ts b/packages/core/src/bridge/apis/forward.ts index 698729b8..af071639 100644 --- a/packages/core/src/bridge/apis/forward.ts +++ b/packages/core/src/bridge/apis/forward.ts @@ -163,7 +163,9 @@ async function buildForwardPushBody( subType: 4, msgId: random, sequence: seq, - timestamp: now, + // Honour a custom per-node display time (#209); default to now. `now` + // still drives the c2c-file expireTime above, which must stay real. + timestamp: node.time && node.time > 0 ? node.time : now, c2cCmd: 0, }, body: { diff --git a/packages/core/tests/actions/forward.test.ts b/packages/core/tests/actions/forward.test.ts index 999d0c40..0eec208c 100644 --- a/packages/core/tests/actions/forward.test.ts +++ b/packages/core/tests/actions/forward.test.ts @@ -14,13 +14,26 @@ vi.mock('@snowluma/protocol/element-builder', () => ({ buildSendElems: vi.fn(async () => []), })); -import { gzipSync } from 'zlib'; -import { protobuf_encode } from '@snowluma/proton'; -import type { SendLongMsgResp, LongMsgResult, RecvLongMsgResp } from '@snowluma/proto-defs/longmsg'; +import { gunzipSync, gzipSync } from 'zlib'; +import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; +import type { SendLongMsgReq, SendLongMsgResp, LongMsgResult, RecvLongMsgResp } from '@snowluma/proto-defs/longmsg'; import type { PushMsgBody } from '@snowluma/proto-defs/message'; import { ForwardApi } from '../../src/bridge/apis/forward'; import { mockBridge } from './_helpers'; +/** Decode the SsoSendLongMsg packet body back into the per-node contentHead + * timestamps of the MultiMsg action — how #209's custom time is verified on + * the wire. */ +function sentNodeTimestamps(body: Uint8Array): number[] { + const req = protobuf_decode(body); + const payload = req.info?.payload; + if (!payload) return []; + const longMsg = protobuf_decode(gunzipSync(Buffer.from(payload))); + const main = longMsg.action?.find((a) => a?.actionCommand === 'MultiMsg'); + const bodies = Array.isArray(main?.actionData?.msgBody) ? main!.actionData!.msgBody : []; + return bodies.map((b) => Number(b.contentHead?.timestamp ?? 0)); +} + function uploadResponseWithResId(resId: string) { const encoded = protobuf_encode({ result: { resId } }); return { @@ -65,6 +78,25 @@ describe('actions/forward', () => { expect((body as Uint8Array).length).toBeGreaterThan(0); }); + it('honours a custom per-node time on the wire, and defaults to now when absent (#209)', async () => { + const bridge = mockBridge({ + sendRawPacket: vi.fn(async () => uploadResponseWithResId('res-time')) as any, + }); + + const customTime = 1600000000; // 2020-09-13, clearly not "now" + await new ForwardApi(bridge as any).upload([ + { userUin: 10001, nickname: 'alice', elements: [], time: customTime }, + { userUin: 10002, nickname: 'bob', elements: [] }, // no time → fallback + ]); + + const [, body] = bridge.sendRawPacket.mock.calls[0]!; + const [tsAlice, tsBob] = sentNodeTimestamps(body as Uint8Array); + + expect(tsAlice).toBe(customTime); + // Bob falls back to the current time (seconds). + expect(Math.abs(tsBob - Math.floor(Date.now() / 1000))).toBeLessThan(5); + }); + it('uploadForwardNodes throws when sendRawPacket reports failure', async () => { const bridge = mockBridge({ sendRawPacket: vi.fn(async () => ({ diff --git a/packages/onebot/src/modules/message-actions.ts b/packages/onebot/src/modules/message-actions.ts index 3e91130c..1b4d5122 100644 --- a/packages/onebot/src/modules/message-actions.ts +++ b/packages/onebot/src/modules/message-actions.ts @@ -1240,6 +1240,12 @@ async function parseForwardNodes( } const node: ForwardNodePayload = { userUin, nickname, elements }; + // Honour an explicit per-node display time (OneBot `data.time`, unix + // seconds) so a custom forward can set/back-date each node's timestamp + // (#209). The wire field is uint32, so reject a millisecond value or any + // out-of-range input (it would overflow/throw) and fall back to now. + const nodeTime = toPositiveInt(nodeData.time); + if (nodeTime > 0 && nodeTime < 0xffffffff) node.time = nodeTime; if (innerForward) node.innerForward = innerForward; nodes.push(node); } diff --git a/packages/onebot/tests/forward-nested.test.ts b/packages/onebot/tests/forward-nested.test.ts index fa285ed7..21623b3b 100644 --- a/packages/onebot/tests/forward-nested.test.ts +++ b/packages/onebot/tests/forward-nested.test.ts @@ -97,6 +97,31 @@ describe('forward — nested {type:"node"} content', () => { expect(sendGroupMessage).toHaveBeenCalledOnce(); }); + it('threads an explicit data.time onto the node payload, leaving absent ones for the default (#209)', async () => { + const uploadForwardNodes = vi.fn(async (_nodes: any[], _groupId?: number, _userId?: number) => 'RESID'); + const sendGroupMessage = vi.fn(async () => ({ + messageId: 1, sequence: 100, clientSequence: 0, random: 1, timestamp: 1700000000, + })); + + const bridge = fakeBridge({ apis: { message: { sendGroup: sendGroupMessage }, forward: { upload: uploadForwardNodes } } } as any); + const ctx = makeCtx(bridge); + + const messages = [ + { type: 'node', data: { user_id: 111, nickname: 'a', time: 1600000000, content: [{ type: 'text', data: { text: 'hi' } }] } }, + { type: 'node', data: { user_id: 222, nickname: 'b', content: [{ type: 'text', data: { text: 'yo' } }] } }, + { type: 'node', data: { user_id: 333, nickname: 'c', time: 1700000000000, content: [{ type: 'text', data: { text: 'ms' } }] } }, // ms → out of uint32 range + { type: 'node', data: { user_id: 444, nickname: 'd', time: -5, content: [{ type: 'text', data: { text: 'neg' } }] } }, + ]; + + await sendGroupForwardMessage(ctx, 12345, messages as any); + + const [nodes] = uploadForwardNodes.mock.calls[0]!; + expect((nodes as any[])[0].time).toBe(1600000000); // explicit seconds carried through + expect((nodes as any[])[1].time).toBeUndefined(); // absent → default to now + expect((nodes as any[])[2].time).toBeUndefined(); // millisecond input rejected (uint32 overflow guard) + expect((nodes as any[])[3].time).toBeUndefined(); // negative rejected → default to now + }); + it('private: nested forward threads userId into uploadForwardNodes', async () => { // Same shape but routed via sendPrivateForwardMessage. The c2c path // passes `userId` instead of `groupId` so the upload pipeline can