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 });
|
||||
|
||||
Reference in new issue
Block a user