fix(onebot): preserve images in message history

Refs #659
This commit is contained in:
DrSmoothl committed 2026-11-07 13:31:14 +08:00
1 parent 87f7bd8c54
commit f2ade5603f
15 files changed
+1284 -58

No files matched your search

@@ -11,6 +11,7 @@ import {
type MediaSubFileUpload,
} from './pipeline';
import { computeHashes, detectImageFormat, loadBinarySource } from './utils';
import { ntImageUrlFromMsgInfo } from '../msg-push/helpers';
const moduleLog = createLogger('Highway.Image');
@@ -208,8 +209,16 @@ export async function uploadImageMsgInfo(
});
const encoded = finalizeMediaMsgInfo(upload, pic);
const imageUrl = ntImageUrlFromMsgInfo(upload.msgInfo?.msgInfoBody?.[0], isGroup);
if (!imageUrl) {
throw new Error('image upload response is missing a download reference');
}
// Retain successful upload metadata for the caller's self-sent history.
Object.assign(element, {
imageUrl,
fileId: image.fileName,
md5Hex: image.md5Hex,
sha1Hex: image.sha1Hex,
fileSize: image.fileSize,
+11
View File
@@ -3,6 +3,7 @@ import { hexPreview } from '@snowluma/common/hex';
import { inflateSync } from 'zlib';
import type { IdentityService } from '../identity-service';
import type { OperatorInfo } from '@snowluma/proto-defs/notify';
import type { MsgInfoBody } from '@snowluma/proto-defs/element';
export const MAX_RICH_CARD_OUTPUT_BYTES = 4 * 1024 * 1024;
export const MAX_RICH_CARD_MESSAGE_OUTPUT_BYTES = 8 * 1024 * 1024;
@@ -51,6 +52,16 @@ export function ntImageUrlFromFileId(fileId: string, isGroup: boolean): string {
return `https://multimedia.nt.qq.com.cn/download?appid=${appid}&fileid=${encodeURIComponent(fileId)}`;
}
/** Upload receipts and received messages carry the same NT image reference. */
export function ntImageUrlFromMsgInfo(body: MsgInfoBody | undefined, isGroup: boolean): string {
if (body?.picture?.urlPath) {
const { domain, urlPath, ext } = body.picture;
return `https://${domain ?? 'multimedia.nt.qq.com.cn'}${urlPath}${ext?.originalParameter ?? ''}`;
}
return ntImageUrlFromFileId(body?.index?.fileUuid ?? '', isGroup);
}
export function decompressData(
data: Uint8Array,
maxOutputBytes = MAX_RICH_CARD_OUTPUT_BYTES,
@@ -33,7 +33,7 @@ import {
makeImageUrl,
MAX_RICH_CARD_MESSAGE_OUTPUT_BYTES,
MAX_RICH_CARD_OUTPUT_BYTES,
ntImageUrlFromFileId,
ntImageUrlFromMsgInfo,
} from './helpers';
import { readXmlMsgAttributes } from './xml-card';
@@ -1096,27 +1096,9 @@ function convertElements(elems: ElemDecoded[], isGroup: boolean): MessageElement
if (bizType === 10 || bizType === 20) {
// Image
let url = '';
if (body.picture) {
const domain = body.picture.domain ?? 'multimedia.nt.qq.com.cn';
const path = body.picture.urlPath ?? '';
if (path) {
url = 'https://' + domain + path;
if (body.picture.ext?.originalParameter) {
url += body.picture.ext.originalParameter;
}
}
}
let url = ntImageUrlFromMsgInfo(body, isGroup);
let fileId = fi.fileName || idx.fileUuid || '';
if (!url && idx.fileUuid) {
url = ntImageUrlFromFileId(idx.fileUuid, isGroup);
}
const me: MessageElement = {
type: 'image',
fileId,
@@ -64,7 +64,9 @@ describe('image sticker presentation (#468)', () => {
width: 1,
height: 1,
picFormat: 1001,
imageUrl: `https://multimedia.nt.qq.com.cn/download?appid=${isGroup ? 1407 : 1406}&fileid=cached-image`,
});
expect(element.fileId).toBe(element.fileName);
const sent = protobuf_decode<EncodableMediaMsgInfo>(bytes);
expect(sendRawPacket).toHaveBeenCalledTimes(1);
expect(sent.msgInfoBody?.[0]?.index?.fileUuid).toBe('cached-image');
@@ -0,0 +1,93 @@
import { describe, expect, it, vi } from 'vitest';
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
import type { OidbBase } from '@snowluma/proto-defs/oidb';
import type {
EncodableMediaMsgInfo,
HighwayMsgInfoBody,
NTV2UploadRichMediaResp,
} from '@snowluma/proto-defs/highway';
import type { BridgeContext } from '../../src/bridge-context';
import type { MessageElement } from '../../src/events';
import { uploadImageMsgInfo } from '../../src/highway/image-upload';
const webp = 'base64://UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA';
function bridge(body: HighwayMsgInfoBody): BridgeContext {
return {
identity: { uin: '10001' },
sendRawPacket: vi.fn(async () => ({
success: true,
gotResponse: true,
errorCode: 0,
responseData: protobuf_encode<OidbBase<NTV2UploadRichMediaResp>>({
body: { upload: { msgInfo: { msgInfoBody: [body] } } },
}),
})),
} as unknown as BridgeContext;
}
describe('uploaded image download references (#659)', () => {
it.each([false, true])(
'retains the returned host, original parameters and WebP filename (group=%s)',
async (isGroup) => {
const picture = {
domain: 'image.example',
urlPath: '/download?fileid=server-resource',
ext: { originalParameter: '&spec=0', thumbParameter: '&spec=small' },
};
const element: MessageElement = { type: 'image', url: webp };
const encoded = await uploadImageMsgInfo(
bridge({ index: { fileUuid: 'resource' }, picture }),
isGroup,
isGroup ? 20002 : 'u_peer',
element,
);
expect(element).toMatchObject({
imageUrl: 'https://image.example/download?fileid=server-resource&spec=0',
picFormat: 1002,
width: 1,
height: 1,
});
expect(element.fileName).toBe(`${element.md5Hex}.webp`);
expect(element.fileId).toBe(element.fileName);
expect(
protobuf_decode<EncodableMediaMsgInfo>(encoded).msgInfoBody?.[0]?.picture,
).toMatchObject(picture);
},
);
it.each([false, true])(
'uses an encoded upload resource id when the server omits the URL (group=%s)',
async (isGroup) => {
const element: MessageElement = { type: 'image', url: webp };
await uploadImageMsgInfo(
bridge({ index: { fileUuid: 'resource/+=' } }),
isGroup,
isGroup ? 20002 : 'u_peer',
element,
);
expect(element.imageUrl).toBe(
`https://multimedia.nt.qq.com.cn/download?appid=${isGroup ? 1407 : 1406}&fileid=resource%2F%2B%3D`,
);
},
);
it('uses the standard image host when a wire response omits the domain', async () => {
const element: MessageElement = { type: 'image', url: webp };
await uploadImageMsgInfo(
bridge({ picture: { urlPath: '/download?fileid=resource' } }),
false,
'u_peer',
element,
);
expect(element.imageUrl).toBe('https://multimedia.nt.qq.com.cn/download?fileid=resource');
});
it('rejects an unusable upload receipt before publishing incomplete send metadata', async () => {
const element: MessageElement = { type: 'image', url: webp };
await expect(uploadImageMsgInfo(bridge({}), false, 'u_peer', element)).rejects.toThrow(
'missing a download reference',
);
expect(element).toEqual({ type: 'image', url: webp });
});
});