feat: add branded editorial photo catalog
This commit is contained in:
1 parent
f6e6018cc1
commit
66f0599653
20 files changed
+1299
-36
No files matched your search
@@ -0,0 +1,115 @@
|
||||
import "server-only";
|
||||
|
||||
import { albumGrantService } from "@/modules/albums/grants";
|
||||
import { immichClient } from "@/modules/immich/client";
|
||||
import type { ImmichAlbumSummary, ImmichClient, ImmichSearchPage } from "@/modules/immich/types";
|
||||
|
||||
export const PHOTO_PAGE_SIZE = 60;
|
||||
export const MAX_PHOTO_PAGE = 1_000;
|
||||
const MAX_DESCRIPTION_LENGTH = 200;
|
||||
|
||||
export type VisibleAlbum = ImmichAlbumSummary;
|
||||
|
||||
export type SearchVisiblePhotosInput = {
|
||||
requestedAlbumId?: string;
|
||||
description?: unknown;
|
||||
page?: unknown;
|
||||
};
|
||||
|
||||
export type AlbumCatalogDependencies = {
|
||||
grants: Pick<typeof albumGrantService, "getGrantedAlbumIds">;
|
||||
immich: Pick<ImmichClient, "listAlbums" | "searchPhotos">;
|
||||
};
|
||||
|
||||
export class AlbumCatalogError extends Error {
|
||||
readonly name = "AlbumCatalogError";
|
||||
|
||||
constructor(readonly code: "IMMICH_UNAVAILABLE") {
|
||||
super(code);
|
||||
}
|
||||
}
|
||||
|
||||
export function createAlbumCatalog({ grants, immich }: AlbumCatalogDependencies) {
|
||||
return {
|
||||
async getVisibleAlbums(editorialId: string): Promise<VisibleAlbum[]> {
|
||||
const [grantedAlbumIds, albums] = await Promise.all([
|
||||
currentGrantIds(grants, editorialId),
|
||||
loadAlbums(immich),
|
||||
]);
|
||||
const granted = new Set(grantedAlbumIds);
|
||||
return albums.filter((album) => granted.has(album.id));
|
||||
},
|
||||
|
||||
async searchVisiblePhotos(
|
||||
editorialId: string,
|
||||
input: SearchVisiblePhotosInput,
|
||||
): Promise<ImmichSearchPage> {
|
||||
const grantedAlbumIds = await currentGrantIds(grants, editorialId);
|
||||
const albumIds = authorizedSearchAlbums(grantedAlbumIds, input.requestedAlbumId);
|
||||
if (albumIds.length === 0) {
|
||||
return emptySearchPage();
|
||||
}
|
||||
|
||||
const description = normalizeDescription(input.description);
|
||||
try {
|
||||
return await immich.searchPhotos({
|
||||
albumIds,
|
||||
...(description === undefined ? {} : { description }),
|
||||
type: "IMAGE",
|
||||
page: normalizePage(input.page),
|
||||
size: PHOTO_PAGE_SIZE,
|
||||
});
|
||||
} catch {
|
||||
throw new AlbumCatalogError("IMMICH_UNAVAILABLE");
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function emptySearchPage(): ImmichSearchPage {
|
||||
return { items: [], total: 0, nextPage: null, previousPage: null };
|
||||
}
|
||||
|
||||
async function currentGrantIds(
|
||||
grants: Pick<typeof albumGrantService, "getGrantedAlbumIds">,
|
||||
editorialId: string,
|
||||
): Promise<string[]> {
|
||||
return [...new Set(await grants.getGrantedAlbumIds(editorialId))];
|
||||
}
|
||||
|
||||
function authorizedSearchAlbums(grantedAlbumIds: string[], requestedAlbumId: string | undefined): string[] {
|
||||
if (requestedAlbumId && grantedAlbumIds.includes(requestedAlbumId)) {
|
||||
return [requestedAlbumId];
|
||||
}
|
||||
return grantedAlbumIds;
|
||||
}
|
||||
|
||||
function normalizeDescription(description: unknown): string | undefined {
|
||||
if (typeof description !== "string") {
|
||||
return undefined;
|
||||
}
|
||||
const normalized = description.replace(/\s+/g, " ").trim().slice(0, MAX_DESCRIPTION_LENGTH);
|
||||
return normalized || undefined;
|
||||
}
|
||||
|
||||
function normalizePage(page: unknown): number {
|
||||
if (typeof page !== "number" || !Number.isFinite(page) || !Number.isInteger(page)) {
|
||||
return 1;
|
||||
}
|
||||
return Math.min(MAX_PHOTO_PAGE, Math.max(1, page));
|
||||
}
|
||||
|
||||
async function loadAlbums(
|
||||
immich: Pick<ImmichClient, "listAlbums">,
|
||||
): Promise<ImmichAlbumSummary[]> {
|
||||
try {
|
||||
return await immich.listAlbums();
|
||||
} catch {
|
||||
throw new AlbumCatalogError("IMMICH_UNAVAILABLE");
|
||||
}
|
||||
}
|
||||
|
||||
export const albumCatalog = createAlbumCatalog({
|
||||
grants: albumGrantService,
|
||||
immich: immichClient,
|
||||
});
|
||||
Reference in new issue
Block a user