fix(onebot): keep image sources when reading bot-built forwards

get_forward_msg treated pictures inside a just-sent merge as empty.
Keep the original media source so file or url comes back.

Fixes #441
This commit is contained in:
DrSmoothl committed 2026-09-13 21:14:41 +08:00
1 parent 26fe4e5fb5
commit 48ce95f53e
5 files changed
+105 -4

No files matched your search

@@ -256,11 +256,21 @@ export const ELEMENT_CODECS = {
image: {
async toSegment(element, ctx) {
const url = ctx.imageUrlResolver ? await ctx.imageUrlResolver(element, ctx.isGroup) : (element.imageUrl ?? '');
// Bot-built forwards cache the upload source on `url`. Incoming
// pictures use `imageUrl`. get_forward_msg must accept either —
// otherwise a just-sent merge comes back with empty file/url (#441).
const source = (element.imageUrl || element.url || '').trim();
const resolved = source && !element.imageUrl
? { ...element, imageUrl: source }
: element;
const url = ctx.imageUrlResolver
? await ctx.imageUrlResolver(resolved, ctx.isGroup)
: source;
const data: JsonObject = {
url,
file: element.fileId
|| (element.md5Hex ? `${element.md5Hex.toLowerCase()}.png` : ''),
|| (element.md5Hex ? `${element.md5Hex.toLowerCase()}.png` : '')
|| source,
sub_type: element.subType ?? 0,
summary: element.summary ?? '',
};
@@ -499,7 +509,7 @@ export const ELEMENT_CODECS = {
async fromSegment(data) {
return {
type: 'forward',
resId: String(data.id ?? ''),
resId: String(data.id ?? data.res_id ?? data.forward_id ?? ''),
};
},
},
@@ -422,6 +422,37 @@ describe('convertEvent — message elements', () => {
expect((seg.data as Record<string, unknown>).url).toBe('http://x');
});
it('image: falls back to element.url when imageUrl is empty (#441)', async () => {
const source = 'https://cdn.example/bot-built.png';
const seg = await segment({ type: 'image', url: source });
expect(seg).toEqual({
type: 'image',
data: {
url: source,
file: source,
sub_type: 0,
summary: '',
},
});
});
it('image: resolver sees the upload source when only url is present (#441)', async () => {
const source = 'https://cdn.example/bot-built.png';
const seen: string[] = [];
const seg = await segment(
{ type: 'image', url: source },
{
imageUrlResolver: (element) => {
seen.push(element.type === 'image' ? (element.imageUrl ?? '') : '');
return `${element.type === 'image' ? element.imageUrl : ''}&signed=1`;
},
},
);
expect(seen).toEqual([source]);
expect((seg.data as Record<string, unknown>).url).toBe(`${source}&signed=1`);
expect((seg.data as Record<string, unknown>).file).toBe(source);
});
it('image: forwards optional sub_type and summary for custom emoji/stickers', async () => {
const seg = await segment({
type: 'image',
@@ -1283,6 +1283,35 @@ describe('buildApiContext send and forward dispatch', () => {
}]);
expect(fetch).toHaveBeenCalledWith('res-abc');
});
it('returns file/url for a cached bot-built image node (#441)', async () => {
const source = 'https://cdn.example/bot-built.png';
const fetch = vi.fn(async () => [{
messageType: 'group',
userUin: PEER_ID,
nickname: 'peer',
time: 1_725_000_100,
msgId: 11,
msgSeq: 12,
groupId: GROUP_ID,
elements: [{ type: 'image', url: source }],
}]);
const { api } = makeRef({
apis: { forward: { upload: vi.fn(), fetch } },
});
const messages = await api.getForwardMsg('res-img');
expect(messages).toHaveLength(1);
expect(messages[0]!.message).toEqual([{
type: 'image',
data: {
url: source,
file: source,
sub_type: 0,
summary: '',
},
}]);
});
});
describe('buildApiContext history', () => {