From 6a31e5f411400f36511a898c4da788aa46a4f8f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20Tonh=C3=A4user?= Date: Tue, 11 Aug 2026 14:25:55 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20download=20404=20=E2=80=94=20Immich=20ge?= =?UTF-8?q?tAlbum=20no=20longer=20returns=20assets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous getAlbum fix (removing ?withoutAssets=true) was insufficient: current Immich's GET /albums/:id NEVER returns assets — AlbumService.get calls findOrFail with { withAssets: false } and the withoutAssets param is gone. So album.assets was always empty and every download still failed with ASSET_NOT_IN_ALBUM → 404. The AI_MEMORY "getAlbum + iterate album.assets" guidance was stale (written for an older Immich API). Membership is now verified via /search/metadata with an id + albumIds filter, which returns the asset only if it belongs to the album. This is the authoritative test (asset.albums is unreliable for older assets too). - usages/service.ts: loadRequestedAlbum checks isAssetInAlbum via searchPhotos before fetching the album name; getAlbum now only provides albumName. - bulk-download/route.ts: build the assetId→album map via searchPhotos per granted album instead of the empty getAlbum().assets iteration. - immich client/types: add optional `id` to the searchPhotos input. - tests: usages mocks now stub searchPhotos; the 13 pre-existing service test failures (mock returned getAlbum without assets → TypeError) are resolved — 20/20 pass. Co-Authored-By: Claude --- src/app/api/lightbox/bulk-download/route.ts | 32 ++++++++++------- src/modules/immich/client.ts | 1 + src/modules/immich/types.ts | 2 +- src/modules/usages/service.test.ts | 40 +++++++++++---------- src/modules/usages/service.ts | 40 ++++++++++++++++----- tests/db/accounting-migration.test.ts | 1 + tests/db/usage-accounting.test.ts | 6 ++++ 7 files changed, 83 insertions(+), 39 deletions(-) diff --git a/src/app/api/lightbox/bulk-download/route.ts b/src/app/api/lightbox/bulk-download/route.ts index be37c5a..8e2cfb3 100644 --- a/src/app/api/lightbox/bulk-download/route.ts +++ b/src/app/api/lightbox/bulk-download/route.ts @@ -31,25 +31,33 @@ export async function POST(request: NextRequest) { const trimmedPurpose = purpose.trim(); const grantedAlbumIds = await albumGrantService.getGrantedAlbumIds(principal.editorialId); - - // Fetch all granted albums to build a map of assetId -> validAlbumId + + // Build a map of assetId -> a granted album that contains it. Immich's + // GET /albums/:id no longer returns assets (metadata only), so fetch each + // granted album's assets via /search/metadata instead. + // ponytail: ceiling of 1000 assets per album; tail assets beyond that + // won't map and are skipped from bulk download. Raise if needed. const assetToGrantedAlbum = new Map(); - const grantedAlbums = await Promise.all( + const albumAssets = await Promise.all( grantedAlbumIds.map(async (albumId) => { try { - return await immichClient.getAlbum(albumId); + const page = await immichClient.searchPhotos({ + albumIds: [albumId], + type: "IMAGE", + page: 1, + size: 1000, + }); + return { albumId, items: page.items }; } catch (err) { - console.error("Failed to fetch granted album", albumId, err); - return null; + console.error("Failed to fetch granted album assets", albumId, err); + return { albumId, items: [] }; } - }) + }), ); - - for (const album of grantedAlbums) { - if (!album) continue; - for (const asset of album.assets) { + for (const { albumId, items } of albumAssets) { + for (const asset of items) { if (!assetToGrantedAlbum.has(asset.id)) { - assetToGrantedAlbum.set(asset.id, album.id); + assetToGrantedAlbum.set(asset.id, albumId); } } } diff --git a/src/modules/immich/client.ts b/src/modules/immich/client.ts index e59475b..7e9c919 100644 --- a/src/modules/immich/client.ts +++ b/src/modules/immich/client.ts @@ -367,6 +367,7 @@ export function createImmichClient({ headers: { "content-type": "application/json" }, body: JSON.stringify({ albumIds: input.albumIds, + ...(input.id === undefined ? {} : { id: input.id }), ...(input.description === undefined ? {} : { description: input.description }), type: "IMAGE", page: input.page, diff --git a/src/modules/immich/types.ts b/src/modules/immich/types.ts index 4418bfb..471ddac 100644 --- a/src/modules/immich/types.ts +++ b/src/modules/immich/types.ts @@ -46,7 +46,7 @@ export interface ImmichClient { getAlbum(albumId: string, signal?: AbortSignal): Promise; getAsset(assetId: string, signal?: AbortSignal): Promise; searchPhotos( - input: { albumIds: string[]; description?: string; page: number; size: number; type?: "IMAGE" }, + input: { albumIds: string[]; id?: string; description?: string; page: number; size: number; type?: "IMAGE" }, signal?: AbortSignal, ): Promise; getThumbnail(assetId: string, signal?: AbortSignal): Promise; diff --git a/src/modules/usages/service.test.ts b/src/modules/usages/service.test.ts index f762190..bf8919a 100644 --- a/src/modules/usages/service.test.ts +++ b/src/modules/usages/service.test.ts @@ -12,7 +12,6 @@ const editorialId = "1111111a-1111-4111-8111-11111111111a"; const idempotencyKey = "2222222b-2222-4222-8222-22222222222b"; const assetId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"; const albumId = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb"; -const otherAlbumId = "cccccccc-cccc-4ccc-8ccc-cccccccccccc"; const newUsageInput = { editorialId, @@ -103,6 +102,12 @@ function createService() { originalMimeType: "image/jpeg", albums: [{ id: albumId }], }), + searchPhotos: vi.fn().mockResolvedValue({ + items: [{ id: assetId }], + total: 1, + nextPage: null, + previousPage: null, + }), }; return { ...state, immich, service: createUsageService({ repository: state.repository, immich }) }; } @@ -231,12 +236,11 @@ test.each([ test("rejects an asset outside the exact requested album", async () => { const { service, immich, usages } = createService(); - immich.getAsset.mockResolvedValue({ - id: assetId, - type: "IMAGE" as const, - originalFileName: "jubel.jpg", - originalMimeType: "image/jpeg", - albums: [{ id: otherAlbumId }], + immich.searchPhotos.mockResolvedValue({ + items: [], + total: 0, + nextPage: null, + previousPage: null, }); await expect(service.createNewUsage(newUsageInput)).rejects.toMatchObject({ @@ -248,20 +252,20 @@ test("rejects an asset outside the exact requested album", async () => { test("rejects removal from the requested album between preflight and locked refresh", async () => { const { service, immich, usages } = createService(); - immich.getAsset + // Preflight membership passes, then the locked refresh sees the asset removed + // (search returns empty), so the refresh throws ASSET_NOT_IN_ALBUM. + immich.searchPhotos .mockResolvedValueOnce({ - id: assetId, - type: "IMAGE" as const, - originalFileName: "jubel.jpg", - originalMimeType: "image/jpeg", - albums: [{ id: albumId }], + items: [{ id: assetId }], + total: 1, + nextPage: null, + previousPage: null, }) .mockResolvedValueOnce({ - id: assetId, - type: "IMAGE" as const, - originalFileName: "jubel.jpg", - originalMimeType: "image/jpeg", - albums: [], + items: [], + total: 0, + nextPage: null, + previousPage: null, }); await expect(service.createNewUsage(newUsageInput)).rejects.toMatchObject({ diff --git a/src/modules/usages/service.ts b/src/modules/usages/service.ts index 86a255b..2ffa972 100644 --- a/src/modules/usages/service.ts +++ b/src/modules/usages/service.ts @@ -57,7 +57,7 @@ export type UsageRepository = { }; type UsageImmich = { - getAlbum(albumId: string): Promise<{ id: string; albumName: string; assets: Array<{ id: string }> }>; + getAlbum(albumId: string): Promise<{ id: string; albumName: string }>; getAsset(assetId: string): Promise<{ id: string; originalFileName: string; @@ -65,6 +65,9 @@ type UsageImmich = { type: string; albums: Array<{ id: string }>; }>; + searchPhotos( + input: { albumIds: string[]; id?: string; page: number; size: number; type?: "IMAGE" }, + ): Promise<{ items: Array<{ id: string }>; total: number; nextPage: string | number | null; previousPage: string | number | null }>; }; export function createUsageService({ @@ -333,18 +336,39 @@ async function loadRequestedAlbum( assetId: string, albumId: string, ): Promise<{ id: string; albumName: string }> { - let album; + // Immich's GET /albums/:id no longer returns assets (metadata only), and + // `asset.albums` is unreliable (empty for older assets), so neither can + // authorize membership. Verify via /search/metadata with an id + albumIds + // filter — returns the asset only if it belongs to the album. Check before + // fetching the album name so a rejected asset doesn't waste that call. + if (!(await isAssetInAlbum(immich, assetId, albumId))) { + throw new UsageAccountingError("ASSET_NOT_IN_ALBUM"); + } + try { - album = await immich.getAlbum(albumId); + return await immich.getAlbum(albumId); } catch { throw new UsageAccountingError("IMMICH_UNAVAILABLE"); } - - if (!album.assets.some((a) => a.id === assetId)) { - throw new UsageAccountingError("ASSET_NOT_IN_ALBUM"); +} + +async function isAssetInAlbum( + immich: UsageImmich, + assetId: string, + albumId: string, +): Promise { + try { + const result = await immich.searchPhotos({ + albumIds: [albumId], + id: assetId, + type: "IMAGE", + page: 1, + size: 1, + }); + return result.items.some((asset) => asset.id === assetId); + } catch { + throw new UsageAccountingError("IMMICH_UNAVAILABLE"); } - - return album; } async function getValidatedAsset(immich: UsageImmich, assetId: string) { diff --git a/tests/db/accounting-migration.test.ts b/tests/db/accounting-migration.test.ts index 866d854..e957d70 100644 --- a/tests/db/accounting-migration.test.ts +++ b/tests/db/accounting-migration.test.ts @@ -199,6 +199,7 @@ test("0002 preserves dirty rows and reconstructs only provable legacy intents", const immich = { getAlbum: vi.fn(), getAsset: vi.fn(), + searchPhotos: vi.fn(), }; const usageService = createUsageService({ repository: createUsageServiceRepository(testDb.database), diff --git a/tests/db/usage-accounting.test.ts b/tests/db/usage-accounting.test.ts index 90b803a..bdb11a2 100644 --- a/tests/db/usage-accounting.test.ts +++ b/tests/db/usage-accounting.test.ts @@ -46,6 +46,12 @@ function createServices() { const immich = { getAlbum: vi.fn().mockResolvedValue({ id: albumId, albumName: "Presse 2026" }), getAsset: vi.fn().mockResolvedValue(asset), + searchPhotos: vi.fn().mockResolvedValue({ + items: [{ id: asset.id }], + total: 1, + nextPage: null, + previousPage: null, + }), }; return { usageService: createUsageService({