diff --git a/src/client/channel/attachments.ts b/src/client/channel/attachments.ts index 5a2c62ce5..70f51fd58 100644 --- a/src/client/channel/attachments.ts +++ b/src/client/channel/attachments.ts @@ -565,12 +565,8 @@ export class ChannelAttachments { if (this.messageRemoved(key)) throw new Error("attachment_unavailable"); if (this.sessionId && !this.rooms.some(room => room.id === key.roomId && room.authorized)) throw new Error("room_unavailable"); if (inlineImage?.original) return inlineImageFile(inlineImage, attachment.name); - const source = this.sources.get(fileKeyString(key)); - if (source) { - if (!sameContent(source.attachment, attachment)) { this.withdraw(source, true); throw new Error("attachment_unavailable"); } - return source.file; - } - const id = fileKeyString(key); + const id = fileKeyString(key), source = this.sources.get(id); + if (source && sameContent(source.attachment, attachment)) return source.file; // Runtime initialization is shared; cancellation only retires this wait. let cancel!: () => void; const cancelled = new Promise((_, reject) => { @@ -585,6 +581,7 @@ export class ChannelAttachments { const room = this.rooms.find(room => room.id === key.roomId); if (!this.current(runtime) || !room?.authorized) throw new Error("room_unavailable"); if (!this.matchesPlan({ key, attachment })) throw new Error("attachment_unavailable"); + if (source && room.available) this.withdraw(source, true); let entry = this.sinks.get(id); if (entry && !sameContent(entry.attachment, attachment)) { void this.stopSink(entry, new Error("attachment_unavailable")); diff --git a/tests/channel-attachment-retention.test.ts b/tests/channel-attachment-retention.test.ts index 8c6209ab6..fca944a59 100644 --- a/tests/channel-attachment-retention.test.ts +++ b/tests/channel-attachment-retention.test.ts @@ -612,7 +612,6 @@ test.each(["in progress", "completed without retention"])("restore replaces a re await expect(old).resolves.toBe("AAA"); } update("restored"); - if (phase === "completed without retention") await expect(read(b)).rejects.toThrow("attachment_unavailable"); const next = read(b), download = files.download(key, b); await vi.waitFor(() => expect(getFileHandle).toHaveBeenCalledTimes(2)); if (phase === "in progress") await expect(old).rejects.toThrow("attachment_unavailable");