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:
5 files changed
+105
-4
No files matched your search
@@ -550,7 +550,7 @@ export class ForwardApi {
|
||||
forwardResCache.set(resId, uploadReadyNodes.map(node => ({
|
||||
userUin: node.userUin,
|
||||
nickname: node.nickname,
|
||||
elements: [...node.elements],
|
||||
elements: node.elements.map(cacheableForwardElement),
|
||||
time: node.time,
|
||||
msgId: node.msgId,
|
||||
msgSeq: node.msgSeq,
|
||||
@@ -854,6 +854,14 @@ export class ForwardApi {
|
||||
}
|
||||
}
|
||||
|
||||
function cacheableForwardElement(element: MessageElement): MessageElement {
|
||||
if (element.type !== 'image') return element;
|
||||
if (element.imageUrl || element.fileId || element.md5Hex) return element;
|
||||
const source = (element.url ?? '').trim();
|
||||
if (!source) return element;
|
||||
return { ...element, imageUrl: source };
|
||||
}
|
||||
|
||||
function cloneNodes(nodes: ForwardNodePayload[]): ForwardNodePayload[] {
|
||||
return nodes.map(node => ({
|
||||
userUin: node.userUin,
|
||||
|
||||
@@ -121,6 +121,29 @@ describe('actions/forward', () => {
|
||||
])).rejects.toThrow(/missing res_id/);
|
||||
});
|
||||
|
||||
it('cached fetch after upload keeps a usable image source (#441)', async () => {
|
||||
const sendRawPacket = vi.fn(async () => uploadResponseWithResId('res-img-cache')) as any;
|
||||
const bridge = mockBridge({ sendRawPacket });
|
||||
const source = 'https://cdn.example/bot-built.png';
|
||||
|
||||
const resId = await new ForwardApi(bridge as any).upload([
|
||||
{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'image', url: source }],
|
||||
},
|
||||
]);
|
||||
const fetched = await new ForwardApi(bridge as any).fetch(resId);
|
||||
|
||||
expect(fetched).toHaveLength(1);
|
||||
expect(fetched[0]!.elements[0]).toMatchObject({
|
||||
type: 'image',
|
||||
url: source,
|
||||
imageUrl: source,
|
||||
});
|
||||
expect(sendRawPacket).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('fetchForwardNodes serves from cache after a successful upload (no second sendRawPacket)', async () => {
|
||||
const sendRawPacket = vi.fn(async () => uploadResponseWithResId('res-cache')) as any;
|
||||
const bridge = mockBridge({ sendRawPacket });
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
Reference in new issue
Block a user