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,62 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { expect, test } from "vitest";
|
||||
|
||||
import { AlbumContactSheet, PhotoContactSheet } from "./catalog-content";
|
||||
|
||||
test("renders granted albums as labelled contact-sheet links", () => {
|
||||
render(
|
||||
<AlbumContactSheet
|
||||
albums={[
|
||||
{
|
||||
id: "heimspiel",
|
||||
albumName: "Heimspiel gegen Nord",
|
||||
description: null,
|
||||
albumThumbnailAssetId: "cover-1",
|
||||
assetCount: 24,
|
||||
},
|
||||
]}
|
||||
/>,
|
||||
);
|
||||
|
||||
const album = screen.getByRole("link", { name: /heimspiel gegen nord/i });
|
||||
expect(album.getAttribute("href")).toBe("/redaktion/alben/heimspiel");
|
||||
expect(screen.getByText("24 Fotos")).not.toBeNull();
|
||||
expect(screen.getByAltText("Titelbild: Heimspiel gegen Nord").getAttribute("src")).toBe(
|
||||
"/api/assets/cover-1/thumbnail",
|
||||
);
|
||||
});
|
||||
|
||||
test("renders a labelled photo search and descriptive contact-sheet metadata", () => {
|
||||
render(
|
||||
<PhotoContactSheet
|
||||
albumName="Heimspiel gegen Nord"
|
||||
page={{
|
||||
items: [
|
||||
{
|
||||
id: "asset-1",
|
||||
type: "IMAGE",
|
||||
originalFileName: "tor-des-sieges.jpg",
|
||||
originalMimeType: "image/jpeg",
|
||||
localDateTime: "2026-08-08T18:42:00.000Z",
|
||||
fileCreatedAt: null,
|
||||
albums: [{ id: "heimspiel" }],
|
||||
},
|
||||
],
|
||||
total: 1,
|
||||
nextPage: null,
|
||||
previousPage: null,
|
||||
}}
|
||||
query="Tor"
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByRole("search", { name: /fotos in heimspiel gegen nord durchsuchen/i })).not.toBeNull();
|
||||
expect((screen.getByRole("searchbox", { name: /suchbegriff/i }) as HTMLInputElement).value).toBe("Tor");
|
||||
expect(screen.getByAltText("tor-des-sieges.jpg").getAttribute("src")).toBe(
|
||||
"/api/assets/asset-1/thumbnail",
|
||||
);
|
||||
expect(screen.getByText("tor-des-sieges.jpg")).not.toBeNull();
|
||||
expect(screen.getByText(/1 foto gefunden/i)).not.toBeNull();
|
||||
});
|
||||
@@ -0,0 +1,136 @@
|
||||
/* eslint-disable @next/next/no-img-element */
|
||||
|
||||
import Link from "next/link";
|
||||
|
||||
import type { ImmichSearchPage } from "@/modules/immich/types";
|
||||
|
||||
import type { VisibleAlbum } from "./catalog";
|
||||
|
||||
export function AlbumContactSheet({ albums }: Readonly<{ albums: VisibleAlbum[] }>) {
|
||||
return (
|
||||
<ul className="album-contact-sheet" aria-label="Freigegebene Fotoalben">
|
||||
{albums.map((album) => (
|
||||
<li key={album.id}>
|
||||
<Link className="contact-sheet-link" href={`/redaktion/alben/${encodeURIComponent(album.id)}`}>
|
||||
<article className="album-contact-frame">
|
||||
{album.albumThumbnailAssetId ? (
|
||||
<img
|
||||
alt={`Titelbild: ${album.albumName}`}
|
||||
className="contact-sheet-image"
|
||||
height="480"
|
||||
src={thumbnailUrl(album.albumThumbnailAssetId)}
|
||||
width="720"
|
||||
/>
|
||||
) : (
|
||||
<div aria-label={`Kein Titelbild für ${album.albumName}`} className="missing-contact-image" role="img">
|
||||
Kein Titelbild
|
||||
</div>
|
||||
)}
|
||||
<footer className="contact-strip">
|
||||
<span className="contact-strip-name">{album.albumName}</span>
|
||||
<span>{formatCount(album.assetCount, "Foto")}</span>
|
||||
</footer>
|
||||
</article>
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
|
||||
export function PhotoContactSheet({
|
||||
albumId,
|
||||
albumName,
|
||||
page,
|
||||
query,
|
||||
}: Readonly<{
|
||||
albumId?: string;
|
||||
albumName: string;
|
||||
page: ImmichSearchPage;
|
||||
query: string;
|
||||
}>) {
|
||||
return (
|
||||
<>
|
||||
<form aria-label={`Fotos in ${albumName} durchsuchen`} className="photo-search" role="search">
|
||||
<label htmlFor="photo-query">Suchbegriff</label>
|
||||
<div className="photo-search-controls">
|
||||
<input defaultValue={query} id="photo-query" name="suche" type="search" />
|
||||
<button type="submit">Fotos suchen</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<p aria-live="polite" className="result-summary" role="status">
|
||||
{formatCount(page.total, "Foto")} gefunden
|
||||
</p>
|
||||
|
||||
{page.items.length === 0 ? (
|
||||
<p className="catalog-empty">Keine Fotos passen zu dieser Suche. Ändern Sie den Suchbegriff oder wählen Sie ein anderes Album.</p>
|
||||
) : (
|
||||
<ul className="photo-contact-sheet" aria-label={`Fotos aus ${albumName}`}>
|
||||
{page.items.map((asset) => (
|
||||
<li key={asset.id}>
|
||||
<figure className="photo-contact-frame">
|
||||
<img
|
||||
alt={asset.originalFileName}
|
||||
className="contact-sheet-image"
|
||||
height="600"
|
||||
src={thumbnailUrl(asset.id)}
|
||||
width="900"
|
||||
/>
|
||||
<figcaption className="contact-strip">
|
||||
<span className="contact-strip-name">{asset.originalFileName}</span>
|
||||
<span>{formatDate(asset.localDateTime ?? asset.fileCreatedAt)}</span>
|
||||
</figcaption>
|
||||
</figure>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
|
||||
{albumId ? <PhotoPagination albumId={albumId} page={page} query={query} /> : null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function PhotoPagination({ albumId, page, query }: Readonly<{ albumId: string; page: ImmichSearchPage; query: string }>) {
|
||||
if (page.previousPage === null && page.nextPage === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<nav aria-label="Suchergebnisseiten" className="photo-pagination">
|
||||
{page.previousPage === null ? null : (
|
||||
<Link href={pageHref(albumId, page.previousPage, query)}>Vorherige Seite</Link>
|
||||
)}
|
||||
{page.nextPage === null ? null : <Link href={pageHref(albumId, page.nextPage, query)}>Nächste Seite</Link>}
|
||||
</nav>
|
||||
);
|
||||
}
|
||||
|
||||
function thumbnailUrl(assetId: string): string {
|
||||
return `/api/assets/${encodeURIComponent(assetId)}/thumbnail`;
|
||||
}
|
||||
|
||||
function pageHref(albumId: string, page: number, query: string): string {
|
||||
const parameters = new URLSearchParams({ page: String(page) });
|
||||
if (query) {
|
||||
parameters.set("suche", query);
|
||||
}
|
||||
return `/redaktion/alben/${encodeURIComponent(albumId)}?${parameters.toString()}`;
|
||||
}
|
||||
|
||||
function formatCount(count: number, noun: string): string {
|
||||
return `${count} ${noun}${count === 1 ? "" : "s"}`;
|
||||
}
|
||||
|
||||
function formatDate(value: string | null): string {
|
||||
if (!value) {
|
||||
return "Ohne Zeitangabe";
|
||||
}
|
||||
|
||||
const date = new Date(value);
|
||||
if (Number.isNaN(date.valueOf())) {
|
||||
return "Ohne Zeitangabe";
|
||||
}
|
||||
return new Intl.DateTimeFormat("de-DE", { dateStyle: "medium" }).format(date);
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
import { expect, test, vi } from "vitest";
|
||||
|
||||
import {
|
||||
AlbumCatalogError,
|
||||
createAlbumCatalog,
|
||||
} from "./catalog";
|
||||
import type { ImmichClient } from "@/modules/immich/types";
|
||||
|
||||
const allowedAlbum = {
|
||||
id: "allowed",
|
||||
albumName: "Heimspiel gegen Nord",
|
||||
description: null,
|
||||
albumThumbnailAssetId: "cover-1",
|
||||
assetCount: 12,
|
||||
};
|
||||
|
||||
function createCatalog(options?: {
|
||||
grantedAlbumIds?: string[];
|
||||
searchError?: Error;
|
||||
}) {
|
||||
const grants = {
|
||||
getGrantedAlbumIds: vi.fn().mockResolvedValue(options?.grantedAlbumIds ?? ["allowed"]),
|
||||
};
|
||||
const immich = {
|
||||
listAlbums: vi.fn().mockResolvedValue([allowedAlbum, { ...allowedAlbum, id: "other" }]),
|
||||
searchPhotos: vi.fn().mockImplementation(async () => {
|
||||
if (options?.searchError) throw options.searchError;
|
||||
return { items: [], total: 0, nextPage: null, previousPage: null };
|
||||
}),
|
||||
};
|
||||
|
||||
return {
|
||||
catalog: createAlbumCatalog({ grants, immich }),
|
||||
grants,
|
||||
immich,
|
||||
};
|
||||
}
|
||||
|
||||
test("ignores a caller-supplied album outside the editorial grants", async () => {
|
||||
const { catalog, immich } = createCatalog({ grantedAlbumIds: ["allowed"] });
|
||||
|
||||
await catalog.searchVisiblePhotos("editorial-1", {
|
||||
requestedAlbumId: "forbidden",
|
||||
description: "tor",
|
||||
page: 1,
|
||||
});
|
||||
|
||||
expect(immich.searchPhotos).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ albumIds: ["allowed"], description: "tor", type: "IMAGE" }),
|
||||
);
|
||||
});
|
||||
|
||||
test("uses a requested album only when it is in the current editorial grants", async () => {
|
||||
const { catalog, immich } = createCatalog({ grantedAlbumIds: ["allowed", "also-allowed"] });
|
||||
|
||||
await catalog.searchVisiblePhotos("editorial-1", { requestedAlbumId: "also-allowed", page: 1 });
|
||||
|
||||
expect(immich.searchPhotos).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ albumIds: ["also-allowed"], type: "IMAGE" }),
|
||||
);
|
||||
});
|
||||
|
||||
test("does not call Immich when an editorial has no current album grants", async () => {
|
||||
const { catalog, immich } = createCatalog({ grantedAlbumIds: [] });
|
||||
|
||||
await expect(catalog.searchVisiblePhotos("editorial-1", { page: 1 })).resolves.toEqual({
|
||||
items: [],
|
||||
total: 0,
|
||||
nextPage: null,
|
||||
previousPage: null,
|
||||
});
|
||||
expect(immich.searchPhotos).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("reads grants for every search so a revoked grant is immediately excluded", async () => {
|
||||
const { catalog, grants, immich } = createCatalog({ grantedAlbumIds: ["allowed"] });
|
||||
grants.getGrantedAlbumIds.mockResolvedValueOnce(["allowed"]).mockResolvedValueOnce([]);
|
||||
|
||||
await catalog.searchVisiblePhotos("editorial-1", { page: 1 });
|
||||
await catalog.searchVisiblePhotos("editorial-1", { page: 1 });
|
||||
|
||||
expect(immich.searchPhotos).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test("normalizes descriptions and bounds invalid pagination before sending an upstream request", async () => {
|
||||
const { catalog, immich } = createCatalog();
|
||||
|
||||
await catalog.searchVisiblePhotos("editorial-1", {
|
||||
description: " Tor\n des\tSieges ",
|
||||
page: Number.POSITIVE_INFINITY,
|
||||
});
|
||||
|
||||
expect(immich.searchPhotos).toHaveBeenCalledWith({
|
||||
albumIds: ["allowed"],
|
||||
description: "Tor des Sieges",
|
||||
type: "IMAGE",
|
||||
page: 1,
|
||||
size: 60,
|
||||
});
|
||||
|
||||
await catalog.searchVisiblePhotos("editorial-1", { page: 100_000 });
|
||||
expect(immich.searchPhotos).toHaveBeenLastCalledWith(
|
||||
expect.objectContaining({ page: 1_000 }),
|
||||
);
|
||||
});
|
||||
|
||||
test("only returns the current editorial's albums from the Immich album list", async () => {
|
||||
const { catalog } = createCatalog({ grantedAlbumIds: ["allowed"] });
|
||||
|
||||
await expect(catalog.getVisibleAlbums("editorial-1")).resolves.toEqual([allowedAlbum]);
|
||||
});
|
||||
|
||||
test("maps an Immich search outage to the catalog's stable unavailable state", async () => {
|
||||
const { catalog } = createCatalog({ searchError: new Error("upstream down") });
|
||||
|
||||
await expect(catalog.searchVisiblePhotos("editorial-1", { page: 1 })).rejects.toEqual(
|
||||
new AlbumCatalogError("IMMICH_UNAVAILABLE"),
|
||||
);
|
||||
});
|
||||
|
||||
test("does not expose a caller-controlled asset type in the catalog contract", async () => {
|
||||
const { catalog, immich } = createCatalog();
|
||||
|
||||
await catalog.searchVisiblePhotos("editorial-1", { page: 2, type: "VIDEO" } as never);
|
||||
|
||||
expect(immich.searchPhotos).toHaveBeenCalledWith(expect.objectContaining({ type: "IMAGE" }));
|
||||
});
|
||||
@@ -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,
|
||||
});
|
||||
@@ -38,7 +38,7 @@ export interface ImmichClient {
|
||||
listAlbums(signal?: AbortSignal): Promise<ImmichAlbumSummary[]>;
|
||||
getAlbum(albumId: string, signal?: AbortSignal): Promise<ImmichAlbum>;
|
||||
searchPhotos(
|
||||
input: { albumIds: string[]; description?: string; page: number; size: number },
|
||||
input: { albumIds: string[]; description?: string; page: number; size: number; type?: "IMAGE" },
|
||||
signal?: AbortSignal,
|
||||
): Promise<ImmichSearchPage>;
|
||||
getThumbnail(assetId: string, signal?: AbortSignal): Promise<ImmichBinary>;
|
||||
|
||||
Reference in new issue
Block a user