π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
normal + hidden - wip 3
Manav Rathib5f12b56fa71
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsxindex 1f519def67..59942f4224 100644--- a/web/apps/photos/src/pages/gallery.tsx+++ b/web/apps/photos/src/pages/gallery.tsx@@ -78,7 +78,6 @@ import { updateFilesVisibility } from "ente-new/photos/services/file";import {savedCollectionFiles,savedCollections,- savedHiddenCollectionIDs,savedTrashItems,} from "ente-new/photos/services/photos-fdb";import {@@ -330,7 +329,6 @@ const Page: React.FC = () => {user,familyData,collections: await savedCollections(),- hiddenCollectionsIDs: await savedHiddenCollectionIDs(),collectionFiles: await savedCollectionFiles(),trashItems: await savedTrashItems(),});@@ -538,12 +536,8 @@ const Page: React.FC = () => {*/const fileAndCollectionSyncWithRemote = useCallback(async () => {const didUpdateFiles = await syncCollectionAndFiles({- onSetCollections: (collections, hiddenCollectionIDs) =>- dispatch({- type: "setCollections",- collections,- hiddenCollectionIDs,- }),+ onSetCollections: (collections) =>+ dispatch({ type: "setCollections", collections }),onSetCollectionFiles: (collectionFiles) =>dispatch({ type: "setCollectionFiles", collectionFiles }),onAugmentCollectionFiles: (collectionFiles) =>diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.tsindex 4152b74193..f48768ed9e 100644--- a/web/packages/new/photos/components/gallery/reducer.ts+++ b/web/packages/new/photos/components/gallery/reducer.ts@@ -121,10 +121,6 @@ export interface GalleryState {* The user's collections.*/collections: Collection[];- /**- * The collection IDs of the user's hidden collections.- */- hiddenCollectionsIDs: Set<number>;/*** The user's files, without any unsynced modifications applied to them.*@@ -172,13 +168,23 @@ export interface GalleryState {*/collectionFiles: EnteFile[];/**- * Collection IDs of archived collections.+ * Collection IDs of hidden collections.*/- archivedCollectionIDs: Set<number>;+ hiddenCollectionIDs: Set<number>;/*** Collection IDs of default hidden collections.+ *+ * The default hidden collection contains files that have been hidden+ * individually. Rarely, but it is a technical possibility, multiple clients+ * might create such "default" hidden collections. So this needs to be a set+ * of IDs, but usually will be only one. In either case, the client shows a+ * "merged" default hidden collection to the user.*/defaultHiddenCollectionIDs: Set<number>;+ /**+ * Collection IDs of archived collections.+ */+ archivedCollectionIDs: Set<number>;/*** File IDs of the files that the user has archived.*@@ -403,15 +409,10 @@ export type GalleryAction =user: User;familyData: FamilyData;collections: Collection[];- hiddenCollectionsIDs: Set<number>;collectionFiles: EnteFile[];trashItems: TrashItem[];}- | {- type: "setCollections";- collections: Collection[];- hiddenCollectionIDs: Set<number>;- }+ | { type: "setCollections"; collections: Collection[] }| { type: "setCollectionFiles"; collectionFiles: EnteFile[] }| { type: "augmentCollectionFiles"; collectionFiles: EnteFile[] }| { type: "uploadFile"; file: EnteFile }@@ -447,13 +448,13 @@ const initialGalleryState: GalleryState = {user: undefined,familyData: undefined,collections: [],- hiddenCollectionsIDs: new Set(),lastSyncedCollectionFiles: [],trashItems: [],peopleState: undefined,collectionFiles: [],- archivedCollectionIDs: new Set(),+ hiddenCollectionIDs: new Set(),defaultHiddenCollectionIDs: new Set(),+ archivedCollectionIDs: new Set(),archivedFileIDs: new Set(),favoriteFileIDs: new Set(),collectionNameByID: new Map(),@@ -494,16 +495,22 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (// use the provided files.const collectionFiles = lastSyncedCollectionFiles;- const [hiddenCollections, collections] = splitByPredicate(- action.collections,+ const collections = action.collections;+ const [hiddenCollections, normalCollections] = splitByPredicate(+ collections,isHiddenCollection,);+ const hiddenCollectionIDs = new Set(+ hiddenCollections.map((c) => c.id),+ );+const archivedCollectionIDs =- deriveArchivedCollectionIDs(collections);+ deriveArchivedCollectionIDs(normalCollections);const archivedFileIDs = deriveArchivedFileIDs(archivedCollectionIDs,collectionFiles,);+const view = {type: "albums" as const,activeCollectionSummaryID: PseudoCollectionID.all,@@ -515,13 +522,13 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (user: action.user,familyData: action.familyData,collections,- hiddenCollections,lastSyncedCollectionFiles,trashItems,collectionFiles,- archivedCollectionIDs,+ hiddenCollectionIDs,defaultHiddenCollectionIDs:deriveDefaultHiddenCollectionIDs(hiddenCollections),+ archivedCollectionIDs,archivedFileIDs,favoriteFileIDs: deriveFavoriteFileIDs(action.user,@@ -532,12 +539,16 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (collectionNameByID: createCollectionNameByID(action.collections,),- fileNormalCollectionIDs: createFileCollectionIDs(normalFiles),+ fileNormalCollectionIDs: deriveFileNormalCollectionIDs(+ collectionFiles,+ hiddenCollectionIDs,+ ),normalCollectionSummaries: deriveNormalCollectionSummaries(action.user,normalCollections,collectionFiles,trashItems,+ hiddenCollectionIDs,archivedFileIDs,),hiddenCollectionSummaries: deriveHiddenCollectionSummaries(@@ -1108,6 +1119,12 @@ const deriveFiles = (});};+/**+ * Compute the default hidden collection IDs from their dependencies.+ */+const deriveDefaultHiddenCollectionIDs = (hiddenCollections: Collection[]) =>+ findDefaultHiddenCollectionIDs(hiddenCollections);+/*** Compute archived collection IDs from their dependencies.*/@@ -1118,18 +1135,6 @@ const deriveArchivedCollectionIDs = (collections: Collection[]) =>.map((collection) => collection.id),);-/**- * Compute the default hidden collection IDs from their dependencies.- */-const deriveDefaultHiddenCollectionIDs = (hiddenCollections: Collection[]) =>- findDefaultHiddenCollectionIDs(hiddenCollections);--/**- * Compute hidden file IDs from their dependencies.- */-const deriveHiddenFileIDs = (hiddenFiles: EnteFile[]) =>- new Set<number>(hiddenFiles.map((f) => f.id));-/*** Compute archived file IDs from their dependencies.*/@@ -1152,7 +1157,7 @@ const deriveArchivedFileIDs = (*/const deriveFavoriteFileIDs = (user: User,- collections: Collection[],+ collections: GalleryState["collections"],collectionFiles: GalleryState["collectionFiles"],unsyncedFavoriteUpdates: GalleryState["unsyncedFavoriteUpdates"],) => {@@ -1175,6 +1180,17 @@ const deriveFavoriteFileIDs = (return favoriteFileIDs;};+/**+ * Compute file to (non-hidden) collection mapping from its dependencies.+ */+const deriveFileNormalCollectionIDs = (+ collectionFiles: GalleryState["collectionFiles"],+ hiddenCollectionIDs: GalleryState["hiddenCollectionIDs"],+) =>+ createFileCollectionIDs(+ collectionFiles.filter((f) => !hiddenCollectionIDs.has(f.collectionID)),+ );+/*** Construct a map from file IDs to the list of collections (IDs) to which the* file belongs.@@ -1195,8 +1211,9 @@ const deriveNormalCollectionSummaries = (user: User,normalCollections: Collection[],collectionFiles: GalleryState["collectionFiles"],- trashItems: TrashItem[],- archivedFileIDs: Set<number>,+ trashItems: GalleryState["trashItems"],+ hiddenCollectionIDs: GalleryState["hiddenCollectionIDs"],+ archivedFileIDs: GalleryState["archivedFileIDs"],) => {const normalCollectionSummaries = createCollectionSummaries(user,@@ -1218,11 +1235,21 @@ const deriveNormalCollectionSummaries = (});}- const allSectionFiles = findAllSectionVisibleFiles(- collectionFiles,- archivedFileIDs,- hidden,+ const allSectionFiles = uniqueFilesByID(+ collectionFiles.filter(+ (f) =>+ !hiddenCollectionIDs.has(f.collectionID) &&+ !archivedFileIDs.has(f.id),+ ),+ );++ const archiveItemsFiles = uniqueFilesByID(+ collectionFiles.filter(+ (f) =>+ !hiddenCollectionIDs.has(f.collectionID) && isArchivedFile(f),+ ),);+normalCollectionSummaries.set(PseudoCollectionID.all, {...pseudoCollectionOptionsForFiles(allSectionFiles),id: PseudoCollectionID.all,@@ -1230,6 +1257,7 @@ const deriveNormalCollectionSummaries = (attributes: ["all"],name: t("section_all"),});+normalCollectionSummaries.set(PseudoCollectionID.trash, {...pseudoCollectionOptionsForLatestFileAndCount(trashItems[0]?.file,@@ -1241,11 +1269,9 @@ const deriveNormalCollectionSummaries = (attributes: ["trash"],coverFile: undefined,});- const archivedFiles = uniqueFilesByID(- normalFiles.filter((file) => isArchivedFile(file)),- );+normalCollectionSummaries.set(PseudoCollectionID.archiveItems, {- ...pseudoCollectionOptionsForFiles(archivedFiles),+ ...pseudoCollectionOptionsForFiles(archiveItemsFiles),id: PseudoCollectionID.archiveItems,name: t("section_archive"),type: "archive",@@ -1311,7 +1337,7 @@ const deriveUncategorizedCollectionSummaryID = (const createCollectionSummaries = (user: User,collections: Collection[],- collectionFiles: GalleryState["collectionFiles"],+ collectionFiles: EnteFile[],) => {const collectionSummaries = new Map<number, CollectionSummary>();@@ -1489,14 +1515,6 @@ const isSharedOnlyViaLink = (collection: Collection) =>// eslint-disable-next-line @typescript-eslint/no-unnecessary-conditioncollection.publicURLs?.length && !collection.sharees?.length;-/**- * Return all list of files that should be shown in the "All" section.- */-const findAllSectionVisibleFiles = (- files: EnteFile[],- archivedFileIDs: Set<number>,-) => uniqueFilesByID(files.filter(({ id }) => !archivedFileIDs.has(id)));-/*** Compute the {@link GalleryView} from its dependencies when we are switching* to (or back to) the "albums" view, or the underlying collections might'vediff --git a/web/packages/new/photos/services/collections.ts b/web/packages/new/photos/services/collections.tsindex ee16188295..ae4e165311 100644--- a/web/packages/new/photos/services/collections.ts+++ b/web/packages/new/photos/services/collections.ts@@ -1,14 +1,9 @@import { type Collection } from "ente-media/collection";import localForage from "ente-shared/storage/localForage";import { getCollections, isHiddenCollection } from "./collection";-import {- savedCollections,- savedHiddenCollectionIDs,- saveHiddenCollectionIDs,-} from "./photos-fdb";+import { savedCollections } from "./photos-fdb";const COLLECTION_TABLE = "collections";-const HIDDEN_COLLECTION_IDS = "hidden-collection-ids";const COLLECTION_UPDATION_TIME = "collection-updation-time";export const getLocalCollections = async (@@ -31,9 +26,6 @@ export const setCollectionLastSyncTime = async (export const removeCollectionIDLastSyncTime = async (collectionID: number) =>await localForage.removeItem(`${collectionID}-time`);-export const getHiddenCollectionIDs = async (): Promise<number[]> =>- (await localForage.getItem<number[]>(HIDDEN_COLLECTION_IDS)) ?? [];-export const getCollectionUpdationTime = async (): Promise<number> =>(await localForage.getItem<number>(COLLECTION_UPDATION_TIME)) ?? 0;@@ -43,61 +35,33 @@ export const getCollectionUpdationTime = async (): Promise<number> =>* This function uses a delta diff, pulling only changes since the timestamp* saved by the last pull.*- * @returns- *- * 1. The latest list of collections, reflecting both the state in our local- * database and on remote; and- *- * 2. The IDs of the collections that are hidden.+ * @returns the latest list of collections, reflecting both the state in our+ * local database and on remote.*/-export const pullCollections = async (): Promise<{- collections: Collection[];- hiddenCollectionIDs: Set<number>;-}> => {+export const pullCollections = async (): Promise<Collection[]> => {const collections = await savedCollections();- const hiddenCollectionIDs = await savedHiddenCollectionIDs();let sinceTime = await getCollectionUpdationTime();const changes = await getCollections(sinceTime);- if (!changes.length) return { collections, hiddenCollectionIDs };+ if (!changes.length) return collections;const collectionsByID = new Map(collections.map((c) => [c.id, c]));for (const { id, updationTime, collection } of changes) {sinceTime = Math.max(sinceTime, updationTime);- let removeSyncTime = false;if (collection) {collectionsByID.set(id, collection);-- const wasHidden = hiddenCollectionIDs.has(collection.id);- const isHidden = isHiddenCollection(collection);- // If hidden state changes.- removeSyncTime = wasHidden != isHidden;} else {// Collection was deleted on remote.- collectionsByID.delete(id);-- removeSyncTime = true;- }-- if (removeSyncTime) {await removeCollectionIDLastSyncTime(id);+ collectionsByID.delete(id);}}const updatedCollections = [...collectionsByID.values()];- const updatedHiddenCollectionIDs = new Set(- updatedCollections- .filter((collection) => isHiddenCollection(collection))- .map((collection) => collection.id),- );await localForage.setItem(COLLECTION_TABLE, updatedCollections);await localForage.setItem(COLLECTION_UPDATION_TIME, sinceTime);- await saveHiddenCollectionIDs(updatedHiddenCollectionIDs);- return {- collections: updatedCollections,- hiddenCollectionIDs: updatedHiddenCollectionIDs,- };+ return updatedCollections;};diff --git a/web/packages/new/photos/services/photos-fdb.ts b/web/packages/new/photos/services/photos-fdb.tsindex 0a0e817160..788b0c356f 100644--- a/web/packages/new/photos/services/photos-fdb.ts+++ b/web/packages/new/photos/services/photos-fdb.ts@@ -45,29 +45,6 @@ export const saveCollections = async (collections: Collection[]) => {await localForage.setItem("collections", collections);};-/**- * Return the collectionIDs of hidden collections as per our local database.- *- * Use {@link saveHiddenCollectionIDs} to update the database.- */-export const savedHiddenCollectionIDs = async (): Promise<Set<number>> =>- new Set(- z- .array(z.number())- .parse((await localForage.getItem("hidden-collection-ids")) ?? []),- );--/**- * Replace the list of hidden collectionIDs stored in our local database.- *- * This is the setter corresponding to {@link savedHiddenCollectionIDs}.- */-export const saveHiddenCollectionIDs = async (- hc: Set<number>,-): Promise<void> => {- await localForage.setItem("hidden-collection-ids", [...hc]);-};-const TrashItemCollectionKey = z.object({/*** Collection ID.@@ -173,6 +150,9 @@ export const savedCollectionFiles = async (): Promise<EnteFile[]> => {files = files.concat(previousHiddenFiles);await saveCollectionFiles(files);await localForage.removeItem("hidden-files");+ // While we're cleaning up, also remove this unused field related to+ // hidden collections also being separately stored earlier.+ await localForage.removeItem("hidden-collection-ids");}return transformFilesIfNeeded(files);diff --git a/web/packages/new/photos/services/sync.ts b/web/packages/new/photos/services/sync.tsindex f3739766f6..5f6a5f834d 100644--- a/web/packages/new/photos/services/sync.ts+++ b/web/packages/new/photos/services/sync.ts@@ -67,14 +67,8 @@ interface PullFilesOpts {/*** Called when the saved collections were replaced by the given* {@link collections}.- *- * It is also passed the IDs of the collections from amongst- * {@link collections} that are hidden.*/- onSetCollections: (- collections: Collection[],- hiddenCollectionIDs: Set<number>,- ) => void;+ onSetCollections: (collections: Collection[]) => void;/*** Called when saved collection files were replaced by the given* {@link collectionFiles}.@@ -106,12 +100,11 @@ interface PullFilesOpts {* state in tandem with the sync. The callbacks are optional since we might not* have local state to update, as is the case when this is invoked post dedup.*- * @returns `true` if one or more normal or hidden files were updated during the- * sync.+ * @returns `true` if one or more files were updated during the pull.*/export const syncCollectionAndFiles = async (opts?: PullFilesOpts) => {- const { collections, hiddenCollectionIDs } = await pullCollections();- opts?.onSetCollections(collections, hiddenCollectionIDs);+ const collections = await pullCollections();+ opts?.onSetCollections(collections);const didUpdateFiles = await pullCollectionFiles(collections,opts?.onSetCollectionFiles,