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
This commit is contained in:
DrSmoothl committed 2026-10-04 14:00:03 +08:00
1 parent 3068d3ef60
commit d86e0fc7b5
6 files changed
+51 -5

No files matched your search

@@ -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];
@@ -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];
@@ -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;
}
+8 -1
View File
@@ -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 */ }
@@ -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();
}
@@ -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<EncodableMediaMsgInfo>(bytes);
expect(sendRawPacket).toHaveBeenCalledTimes(1);