Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Split

Manav Rathi80f142017024
diff --git a/web/apps/photos/src/services/publicCollectionService.ts b/web/apps/photos/src/services/publicCollectionService.ts
index 5327bbebd6..2764c994b6 100644
--- a/web/apps/photos/src/services/publicCollectionService.ts
+++ b/web/apps/photos/src/services/publicCollectionService.ts
@@ -11,6 +11,7 @@ import type {
MagicMetadataCore,
} from "ente-media/file";
import { decryptFile, mergeMetadata } from "ente-media/file";
+import { savedPublicCollections } from "ente-new/albums/services/public-albums-fdb";
import { sortFiles } from "ente-new/photos/services/files";
import { CustomError, parseSharingErrorCodes } from "ente-shared/error";
import HTTPService from "ente-shared/network/HTTPService";
@@ -106,9 +107,7 @@ export const savePublicCollectionPassword = async (
};
export const getLocalPublicCollection = async (collectionKey: string) => {
- const localCollections =
- (await localForage.getItem<Collection[]>(PUBLIC_COLLECTIONS_TABLE)) ||
- [];
+ const localCollections = await savedPublicCollections();
const publicCollection =
localCollections.find(
(localSavedPublicCollection) =>
@@ -118,9 +117,7 @@ export const getLocalPublicCollection = async (collectionKey: string) => {
};
export const savePublicCollection = async (collection: Collection) => {
- const publicCollections =
- (await localForage.getItem<Collection[]>(PUBLIC_COLLECTIONS_TABLE)) ??
- [];
+ const publicCollections = await savedPublicCollections();
await localForage.setItem(
PUBLIC_COLLECTIONS_TABLE,
dedupeCollections([collection, ...publicCollections]),
@@ -373,9 +370,7 @@ export const removePublicCollectionWithFiles = async (
collectionUID: string,
collectionKey: string,
) => {
- const publicCollections =
- (await localForage.getItem<Collection[]>(PUBLIC_COLLECTIONS_TABLE)) ||
- [];
+ const publicCollections = await savedPublicCollections();
await localForage.setItem(
PUBLIC_COLLECTIONS_TABLE,
publicCollections.filter(
diff --git a/web/packages/new/photos/services/files-db.ts b/web/packages/gallery/services/files-db.ts
similarity index 72%
rename from web/packages/new/photos/services/files-db.ts
rename to web/packages/gallery/services/files-db.ts
index 07ab7fb773..475473f27f 100644
--- a/web/packages/new/photos/services/files-db.ts
+++ b/web/packages/gallery/services/files-db.ts
@@ -18,6 +18,20 @@
* So this table is not legacy or deprecated, and there is currently no strong
* reason to migrate this data to another IndexedDB table (it works fine as it
* is, really). However we do want to avoid adding more items here.
+ *
+ * ---
+ *
+ * This file contains the common code and types. Application layer code should
+ * usually be accessing the app specific files DB:
+ *
+ * - Photos app: `photos-fdb.ts`
+ * - Public albums app: `public-albums-fdb.ts`
+ *
+ * Note that even though both of them refer to the same conceptual "files DB",
+ * the actual storage is distinct since both the apps run on separate domains
+ * and so have their separate IndexedDB storage.
+ *
+ * Still, the key names are (generally) distinct to reduce chances of confusion.
*/
import {
@@ -26,16 +40,14 @@ import {
CollectionShareeMagicMetadataData,
RemoteCollectionUser,
RemotePublicURL,
- type Collection,
} from "ente-media/collection";
-import localForage from "ente-shared/storage/localForage";
import { nullishToEmpty, nullToUndefined } from "ente-utils/transform";
import { z } from "zod/v4";
/**
* Zod schema for a {@link Collection} saved in our local persistence.
*
- * This is similar to {@link RemoteCollection}, but has both significant
+ * This is similar to {@link RemoteCollection}, but also has both significant
* differences in that it contains the decrypted fields, and some minor tweaks.
*/
const LocalCollection = z.looseObject({
@@ -73,24 +85,4 @@ const LocalCollection = z.looseObject({
.transform(nullToUndefined),
});
-const LocalCollections = z.array(LocalCollection);
-
-/**
- * Return all collections present in our local database.
- *
- * This includes both normal (non-hidden) and hidden collections.
- *
- * Use {@link saveCollections} to update the database.
- */
-export const savedCollections = async (): Promise<Collection[]> =>
- LocalCollections.parse(await localForage.getItem("collections"));
-
-/**
- * Replace the list of collections stored in our local database.
- *
- * This updates the underlying storage of both normal (non-hidden) and hidden
- * collections (the split between normal and hidden is not at the database level
- * but is a filter when they are accessed).
- */
-export const saveCollections = (collections: Collection[]) =>
- localForage.setItem("collections", collections);
+export const LocalCollections = z.array(LocalCollection);
diff --git a/web/packages/media/collection.ts b/web/packages/media/collection.ts
index ab6b1af294..51333eb59b 100644
--- a/web/packages/media/collection.ts
+++ b/web/packages/media/collection.ts
@@ -75,7 +75,7 @@ export interface Collection {
/**
* Public links that can be used to access and update the collection.
*/
- publicURLs?: PublicURL[];
+ publicURLs: PublicURL[];
/**
* The last time the collection was updated (epoch microseconds).
*
@@ -522,6 +522,13 @@ export const decryptRemoteCollection = async (
owner: parseRemoteCollectionUser(owner),
name,
sharees: sharees.map(parseRemoteCollectionUser),
+ // TODO:
+ //
+ // See: [Note: strict mode migration]
+ //
+ // We need to add the cast here, otherwise we get a tsc error when this
+ // file is imported in the photos app.
+ publicURLs: rest.publicURLs as PublicURL[],
magicMetadata,
pubMagicMetadata,
sharedMagicMetadata,
diff --git a/web/packages/new/albums/services/public-albums-fdb.ts b/web/packages/new/albums/services/public-albums-fdb.ts
new file mode 100644
index 0000000000..0f305b4c51
--- /dev/null
+++ b/web/packages/new/albums/services/public-albums-fdb.ts
@@ -0,0 +1,29 @@
+/**
+ * @file Public albums app specific files DB. See: [Note: Files DB].
+ */
+
+import { LocalCollections } from "ente-gallery/services/files-db";
+import { type Collection } from "ente-media/collection";
+import localForage from "ente-shared/storage/localForage";
+
+/**
+ * Return all public collections present in our local database.
+ *
+ * Use {@link savePublicCollections} to update the database.
+ */
+export const savedPublicCollections = async (): Promise<Collection[]> =>
+ // TODO:
+ //
+ // See: [Note: strict mode migration]
+ //
+ // We need to add the cast here, otherwise we get a tsc error when this
+ // file is imported in the photos app.
+ LocalCollections.parse(
+ (await localForage.getItem("public-collections")) ?? [],
+ ) as Collection[];
+
+/**
+ * Replace the list of public collections stored in our local database.
+ */
+export const savePublicCollections = (collections: Collection[]) =>
+ localForage.setItem("public-collections", collections);
diff --git a/web/packages/new/photos/services/collections.ts b/web/packages/new/photos/services/collections.ts
index 072a23c7b9..f64b839a54 100644
--- a/web/packages/new/photos/services/collections.ts
+++ b/web/packages/new/photos/services/collections.ts
@@ -26,6 +26,7 @@ import {
getCollectionChanges,
isHiddenCollection,
} from "./collection";
+import { savedCollections } from "./photos-fdb";
const COLLECTION_TABLE = "collections";
const HIDDEN_COLLECTION_IDS = "hidden-collection-ids";
@@ -40,11 +41,8 @@ export const getLocalCollections = async (
: collections.filter((c) => isHiddenCollection(c));
};
-export const getAllLocalCollections = async (): Promise<Collection[]> => {
- const collections: Collection[] =
- (await localForage.getItem(COLLECTION_TABLE)) ?? [];
- return collections;
-};
+export const getAllLocalCollections = async (): Promise<Collection[]> =>
+ savedCollections();
export const getCollectionLastSyncTime = async (collection: Collection) =>
(await localForage.getItem<number>(`${collection.id}-time`)) ?? 0;
diff --git a/web/packages/new/photos/services/photos-fdb.ts b/web/packages/new/photos/services/photos-fdb.ts
new file mode 100644
index 0000000000..00123ec499
--- /dev/null
+++ b/web/packages/new/photos/services/photos-fdb.ts
@@ -0,0 +1,35 @@
+/**
+ * @file Photos app specific files DB. See: [Note: Files DB].
+ */
+
+import { LocalCollections } from "ente-gallery/services/files-db";
+import { type Collection } from "ente-media/collection";
+import localForage from "ente-shared/storage/localForage";
+
+/**
+ * Return all collections present in our local database.
+ *
+ * This includes both normal (non-hidden) and hidden collections.
+ *
+ * Use {@link saveCollections} to update the database.
+ */
+export const savedCollections = async (): Promise<Collection[]> =>
+ // TODO:
+ //
+ // See: [Note: strict mode migration]
+ //
+ // We need to add the cast here, otherwise we get a tsc error when this
+ // file is imported in the photos app.
+ LocalCollections.parse(
+ (await localForage.getItem("collections")) ?? [],
+ ) as Collection[];
+
+/**
+ * Replace the list of collections stored in our local database.
+ *
+ * This updates the underlying storage of both normal (non-hidden) and hidden
+ * collections (the split between normal and hidden is not at the database level
+ * but is a filter when they are accessed).
+ */
+export const saveCollections = (collections: Collection[]) =>
+ localForage.setItem("collections", collections);