diff --git a/web/packages/base/http.ts b/web/packages/base/http.ts index d926037445..31006064ad 100644 --- a/web/packages/base/http.ts +++ b/web/packages/base/http.ts @@ -35,9 +35,24 @@ export const publicRequestHeaders = () => ({ */ export interface PublicAlbumsCredentials { /** - * An access token that does the same job as the "X-Auth-Token" for usual - * authenticated API requests, except it will be passed as the - * ""X-Auth-Access-Token" header. + * [Note: Public album access token] + * + * The public album access is a token that serves a similar purpose as the + * "X-Auth-Token" for usual authenticated API requests that happen for a + * logged in user, except: + * + * - It will be passed as the "X-Auth-Access-Token" header, and + * - It also tells remote about the public album under consideration. + * + * This access token is variously referred to as the album token, or the + * auth token, when the context is clear. The client obtains this from the + * "t" query parameter of a public album URL, and then uses it both to: + * + * 1. Identify and authenticate itself with remote (this header). + * + * 2. Scope local storage per public album by using this access token as a + * part of the local storage key. In this context it is sometimes also + * referred to as a "collectionUID" by old code. */ accessToken: string; /** diff --git a/web/packages/new/albums/services/public-albums-fdb.ts b/web/packages/new/albums/services/public-albums-fdb.ts index 6e031fc815..8980a65932 100644 --- a/web/packages/new/albums/services/public-albums-fdb.ts +++ b/web/packages/new/albums/services/public-albums-fdb.ts @@ -2,9 +2,15 @@ * @file Public albums app specific files DB. See: [Note: Files DB]. */ -import { LocalCollections } from "ente-gallery/services/files-db"; +import { + LocalCollections, + LocalEnteFiles, + transformFilesIfNeeded, +} from "ente-gallery/services/files-db"; import { type Collection } from "ente-media/collection"; +import type { EnteFile } from "ente-media/file"; import localForage from "ente-shared/storage/localForage"; +import { z } from "zod/v4"; /** * Return all public collections present in our local database. @@ -29,3 +35,35 @@ export const savedPublicCollections = async (): Promise => */ export const savePublicCollections = (collections: Collection[]) => localForage.setItem("public-collections", collections); + +const LocalSavedPublicCollectionFilesEntry = z.object({ + /** + * The collection, identified by its access token. + * + * See: [Note: Public album access token] + */ + collectionUID: z.string(), + files: LocalEnteFiles, +}); + +type LocalSavedPublicCollectionFilesEntry = z.infer< + typeof LocalSavedPublicCollectionFilesEntry +>; + +/** + * Return all files for a public collection present in our local database. + * + * Use {@link savePublicCollectionFiles} to update the database. + * + * @param accessToken The access token of the public album whose files we want. + */ +export const savedPublicCollectionFiles = async ( + accessToken: string, +): Promise => { + type ES = LocalSavedPublicCollectionFilesEntry[]; + // See: [Note: Avoiding Zod parsing for large DB arrays] for why we use an + // (implied) cast here instead of parsing using the Zod schema. + const entries = await localForage.getItem("public-collection-files"); + const entry = (entries ?? []).find((e) => e.collectionUID == accessToken); + return transformFilesIfNeeded(entry ? entry.files : []); +}; diff --git a/web/packages/new/photos/services/photos-fdb.ts b/web/packages/new/photos/services/photos-fdb.ts index 836ca4159b..8c549c2a2c 100644 --- a/web/packages/new/photos/services/photos-fdb.ts +++ b/web/packages/new/photos/services/photos-fdb.ts @@ -154,6 +154,9 @@ export const savedCollectionFiles = async (): Promise => { // As an optimization, we skip the runtime check here and cast. This might // not be the most optimal choice in the future, so (a) use it sparingly, // and (b) mark all such cases with the title of this note. + // + // Note that the cast is happening inside the local forage code since we're + // passing a type parameter. let files = (await localForage.getItem("files")) ?? []; // Previously hidden files were stored separately. If that key is present,