fix: harden production flows and user messaging
This commit is contained in:
1 parent
52dce168dd
commit
86dfe8053d
43 files changed
+3520
-358
No files matched your search
@@ -3,6 +3,12 @@ import "server-only";
|
||||
import { albumGrantService } from "@/modules/albums/grants";
|
||||
import { immichClient } from "@/modules/immich/client";
|
||||
import type { ImmichAlbumSummary, ImmichClient, ImmichSearchPage } from "@/modules/immich/types";
|
||||
import { consumeRateLimit, searchPolicy } from "@/modules/security/rate-limit";
|
||||
import {
|
||||
enforceRateLimit,
|
||||
RateLimitExceededError,
|
||||
type ConsumeRateLimit,
|
||||
} from "@/modules/security/rate-limit-guard";
|
||||
|
||||
export const PHOTO_PAGE_SIZE = 60;
|
||||
export const MAX_PHOTO_PAGE = 1_000;
|
||||
@@ -20,17 +26,21 @@ export type SearchVisiblePhotosInput = {
|
||||
export type AlbumCatalogDependencies = {
|
||||
grants: Pick<typeof albumGrantService, "getGrantedAlbumIds">;
|
||||
immich: Pick<ImmichClient, "listAlbums" | "searchPhotos">;
|
||||
consumeRateLimit: ConsumeRateLimit;
|
||||
};
|
||||
|
||||
export class AlbumCatalogError extends Error {
|
||||
readonly name = "AlbumCatalogError";
|
||||
|
||||
constructor(readonly code: "IMMICH_UNAVAILABLE") {
|
||||
constructor(
|
||||
readonly code: "IMMICH_UNAVAILABLE" | "RATE_LIMITED",
|
||||
readonly retryAfterSeconds?: number,
|
||||
) {
|
||||
super(code);
|
||||
}
|
||||
}
|
||||
|
||||
export function createAlbumCatalog({ grants, immich }: AlbumCatalogDependencies) {
|
||||
export function createAlbumCatalog({ grants, immich, consumeRateLimit: consume }: AlbumCatalogDependencies) {
|
||||
return {
|
||||
async getVisibleAlbums(editorialId: string): Promise<VisibleAlbum[]> {
|
||||
const [grantedAlbumIds, albums] = await Promise.all([
|
||||
@@ -45,12 +55,22 @@ export function createAlbumCatalog({ grants, immich }: AlbumCatalogDependencies)
|
||||
editorialId: string,
|
||||
input: SearchVisiblePhotosInput,
|
||||
): Promise<ImmichSearchPage> {
|
||||
const description = normalizeDescription(input.description);
|
||||
if (description) {
|
||||
try {
|
||||
await enforceRateLimit(consume, `search:${editorialId}`, searchPolicy);
|
||||
} catch (error) {
|
||||
if (error instanceof RateLimitExceededError) {
|
||||
throw new AlbumCatalogError("RATE_LIMITED", error.retryAfterSeconds);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
const grantedAlbumIds = await currentGrantIds(grants, editorialId);
|
||||
if (grantedAlbumIds.length === 0) {
|
||||
return emptySearchPage();
|
||||
}
|
||||
|
||||
const description = normalizeDescription(input.description);
|
||||
const page = normalizePage(input.page);
|
||||
const size = input.size ?? PHOTO_PAGE_SIZE;
|
||||
|
||||
@@ -169,4 +189,5 @@ async function loadAlbums(
|
||||
export const albumCatalog = createAlbumCatalog({
|
||||
grants: albumGrantService,
|
||||
immich: immichClient,
|
||||
consumeRateLimit,
|
||||
});
|
||||
Reference in new issue
Block a user