From d86e0fc7b51c249c18a7ef059ff6b0a80dba1686 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sun, 4 Oct 2026 14:00:03 +0800 Subject: [PATCH] fix(protocol): preserve sent media for message history Keep media sent by the bot available for later history and forwarding, including messages without a server echo. Fixes #462 --- packages/core/tests/highway/ptt-upload.test.ts | 9 +++++++++ packages/core/tests/highway/video-upload.test.ts | 9 +++++++++ packages/protocol/src/highway/image-upload.ts | 9 ++++++++- packages/protocol/src/highway/ptt-upload.ts | 9 ++++++++- packages/protocol/src/highway/video-upload.ts | 9 ++++++++- packages/protocol/tests/highway/image-sticker.test.ts | 11 +++++++++-- 6 files changed, 51 insertions(+), 5 deletions(-) diff --git a/packages/core/tests/highway/ptt-upload.test.ts b/packages/core/tests/highway/ptt-upload.test.ts index d902b6f9..b6610338 100644 --- a/packages/core/tests/highway/ptt-upload.test.ts +++ b/packages/core/tests/highway/ptt-upload.test.ts @@ -30,6 +30,15 @@ describe('ptt-upload', () => { vi.mocked(pipeline.finalizeMediaMsgInfo).mockClear(); }); + it('retains successful upload metadata for self-sent history', async () => { + const node = { fileUuid: 'uploaded-resource', storeId: 1, info: { fileSize: 1234 } }; + vi.mocked(pipeline.runNtv2Upload).mockResolvedValueOnce({ msgInfo: { msgInfoBody: [{ index: node }] } } as any); + const element = { ...FINGERPRINT }; + await uploadPttMsgInfo({} as any, true, 12345, element); + expect(element).toMatchObject({ fileId: 'uploaded-resource', mediaNode: node, md5Hex: 'aa', sha1Hex: 'bb' }); + expect(element.fileName).toBeTruthy(); + }); + it('group: 0x126E_100 + GROUP_PTT_CMD_ID + group-flavored bytesGeneralFlags', async () => { await uploadPttMsgInfo({} as any, true, 12345, FINGERPRINT); const args = vi.mocked(pipeline.runNtv2Upload).mock.calls[0]![0]; diff --git a/packages/core/tests/highway/video-upload.test.ts b/packages/core/tests/highway/video-upload.test.ts index 7a367a7a..94ba83bf 100644 --- a/packages/core/tests/highway/video-upload.test.ts +++ b/packages/core/tests/highway/video-upload.test.ts @@ -49,6 +49,15 @@ describe('video-upload', () => { vi.mocked(pipeline.finalizeMediaMsgInfo).mockClear(); }); + it('retains successful upload metadata for self-sent history', async () => { + const node = { fileUuid: 'uploaded-resource', storeId: 1, info: { fileSize: 1234 } }; + vi.mocked(pipeline.runNtv2Upload).mockResolvedValueOnce({ msgInfo: { msgInfoBody: [{ index: node }] } } as any); + const element = { ...FINGERPRINT }; + await uploadVideoMsgInfo({} as any, true, 12345, element); + expect(element).toMatchObject({ fileId: 'uploaded-resource', mediaNode: node, md5Hex: 'aa', sha1Hex: 'bb' }); + expect(element.fileName).toBeTruthy(); + }); + it('group: 0x11EA_100 + GROUP_VIDEO_CMD_ID for main, GROUP_VIDEO_THUMB_CMD_ID for thumb', async () => { await uploadVideoMsgInfo({} as any, true, 12345, FINGERPRINT); const args = vi.mocked(pipeline.runNtv2Upload).mock.calls[0]![0]; diff --git a/packages/protocol/src/highway/image-upload.ts b/packages/protocol/src/highway/image-upload.ts index 077a6b93..802d6773 100644 --- a/packages/protocol/src/highway/image-upload.ts +++ b/packages/protocol/src/highway/image-upload.ts @@ -182,5 +182,12 @@ export async function uploadImageMsgInfo( label: 'image', }); - return finalizeMediaMsgInfo(upload, pic); + const encoded = finalizeMediaMsgInfo(upload, pic); + // Retain successful upload metadata for the caller's self-sent history. + Object.assign(element, { + md5Hex: image.md5Hex, sha1Hex: image.sha1Hex, fileSize: image.fileSize, + fileName: image.fileName, width: image.width, height: image.height, + picFormat: image.picFormat, subType: image.subType, summary: image.summary, + }); + return encoded; } diff --git a/packages/protocol/src/highway/ptt-upload.ts b/packages/protocol/src/highway/ptt-upload.ts index 187ea396..dd5544a5 100644 --- a/packages/protocol/src/highway/ptt-upload.ts +++ b/packages/protocol/src/highway/ptt-upload.ts @@ -240,7 +240,14 @@ export async function uploadPttMsgInfo( }); mergePttWaveform(upload, ptt.waveform); - return finalizeMediaMsgInfo(upload); + const encoded = finalizeMediaMsgInfo(upload); + const mediaNode = upload.msgInfo?.msgInfoBody?.[0]?.index; + Object.assign(element, { + md5Hex: ptt.md5Hex, sha1Hex: ptt.sha1Hex, fileSize: ptt.fileSize, + fileName: ptt.fileName, duration: ptt.duration, voiceFormat: ptt.voiceFormat, + fileId: mediaNode?.fileUuid, mediaNode, + }); + return encoded; } finally { for (const fn of ptt.cleanups) { try { fn(); } catch { /* best-effort cleanup */ } diff --git a/packages/protocol/src/highway/video-upload.ts b/packages/protocol/src/highway/video-upload.ts index c7bff801..539273a7 100644 --- a/packages/protocol/src/highway/video-upload.ts +++ b/packages/protocol/src/highway/video-upload.ts @@ -437,7 +437,14 @@ export async function uploadVideoMsgInfo( }); log.debug('video upload completed: md5=%s scene=%s', video.md5Hex, isGroup ? 'group' : 'c2c'); - return finalizeMediaMsgInfo(upload); + const encoded = finalizeMediaMsgInfo(upload); + const mediaNode = upload.msgInfo?.msgInfoBody?.[0]?.index; + Object.assign(element, { + md5Hex: video.md5Hex, sha1Hex: video.sha1Hex, fileSize: video.fileSize, + fileName: 'nya.mp4', duration: video.duration, width: video.width, height: video.height, videoFormat: 0, + fileId: mediaNode?.fileUuid, mediaNode, + }); + return encoded; } finally { await video.cleanup(); } diff --git a/packages/protocol/tests/highway/image-sticker.test.ts b/packages/protocol/tests/highway/image-sticker.test.ts index f502a3f6..9e31fb8f 100644 --- a/packages/protocol/tests/highway/image-sticker.test.ts +++ b/packages/protocol/tests/highway/image-sticker.test.ts @@ -1,3 +1,5 @@ +import type { MessageElement } from '../../src/events'; +import { createHash } from 'node:crypto'; import { describe, expect, it, vi } from 'vitest'; import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; import type { OidbBase } from '@snowluma/proto-defs/oidb'; @@ -24,8 +26,13 @@ describe('image sticker presentation (#468)', () => { }; }); const bridge = { identity: { uin: '10001' }, sendRawPacket } as unknown as BridgeContext; - const bytes = await uploadImageMsgInfo(bridge, isGroup, isGroup ? 123 : 'u_peer', { - type: 'image', url: png, subType: 1, summary: '[收藏表情]', + const element: MessageElement = { type: 'image', url: png, subType: 1, summary: '[收藏表情]' }; + const bytes = await uploadImageMsgInfo(bridge, isGroup, isGroup ? 123 : 'u_peer', element); + const imageBytes = Buffer.from(png.slice('base64://'.length), 'base64'); + expect(element).toMatchObject({ + md5Hex: createHash('md5').update(imageBytes).digest('hex'), + sha1Hex: createHash('sha1').update(imageBytes).digest('hex'), + fileSize: imageBytes.length, width: 1, height: 1, picFormat: 1001, }); const sent = protobuf_decode(bytes); expect(sendRawPacket).toHaveBeenCalledTimes(1);