π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Move
Manav Rathicae16c1580e2
diff --git a/web/apps/photos/src/components/Collections/CollectionHeader.tsx b/web/apps/photos/src/components/Collections/CollectionHeader.tsxindex f445cca217..188d3dbdc4 100644--- a/web/apps/photos/src/components/Collections/CollectionHeader.tsx+++ b/web/apps/photos/src/components/Collections/CollectionHeader.tsx@@ -12,6 +12,7 @@ import { SpaceBetweenFlex } from "@/new/photos/components/mui";import {ALL_SECTION,HIDDEN_ITEMS_SECTION,+ isHiddenCollection,} from "@/new/photos/services/collection";import type {CollectionSummary,@@ -58,7 +59,6 @@ import {changeCollectionVisibility,downloadCollectionHelper,downloadDefaultHiddenCollectionHelper,- isHiddenCollection,} from "utils/collection";interface CollectionHeaderProps {diff --git a/web/apps/photos/src/components/Upload/Uploader.tsx b/web/apps/photos/src/components/Upload/Uploader.tsxindex 5463aa466b..faceedc49c 100644--- a/web/apps/photos/src/components/Upload/Uploader.tsx+++ b/web/apps/photos/src/components/Upload/Uploader.tsx@@ -38,7 +38,7 @@ import type {} from "services/upload/uploadManager";import uploadManager from "services/upload/uploadManager";import watcher from "services/watch";-import { SetCollections, SetFiles, SetLoading } from "types/gallery";+import { SetFiles, SetLoading } from "types/gallery";import { getOrCreateAlbum } from "utils/collection";import { PublicCollectionGalleryContext } from "utils/publicCollectionGallery";import { getRootLevelFileWithFolderNotAllowMessage } from "utils/ui";@@ -73,7 +73,7 @@ interface Props {setShouldDisableDropzone: (value: boolean) => void;showCollectionSelector?: () => void;setFiles: SetFiles;- setCollections?: SetCollections;+ setCollections?: (cs: Collection[]) => void;isFirstUpload?: boolean;uploadTypeSelectorView: boolean;showSessionExpiredMessage: () => void;diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsxindex 1ea326ea1a..8754f46d93 100644--- a/web/apps/photos/src/pages/gallery.tsx+++ b/web/apps/photos/src/pages/gallery.tsx@@ -33,6 +33,7 @@ import {DUMMY_UNCATEGORIZED_COLLECTION,HIDDEN_ITEMS_SECTION,TRASH_SECTION,+ isHiddenCollection,} from "@/new/photos/services/collection";import type { CollectionSummaries } from "@/new/photos/services/collection/ui";import { areOnlySystemCollections } from "@/new/photos/services/collection/ui";@@ -50,6 +51,7 @@ import {} from "@/new/photos/services/search";import type { SearchOption } from "@/new/photos/services/search/types";import { AppContext } from "@/new/photos/types/context";+import { splitByPredicate } from "@/utils/array";import { ensure } from "@/utils/ensure";import { wait } from "@/utils/promise";import {@@ -142,7 +144,6 @@ import {constructCollectionNameMap,getSelectedCollection,handleCollectionOps,- splitNormalAndHiddenCollections,} from "utils/collection";import {FILE_OPS_TYPE,@@ -190,9 +191,6 @@ export const GalleryContext = createContext<GalleryContextType>(*/export default function Gallery() {const [state, dispatch] = useGalleryReducer();- const [collections, setCollections] = useState<Collection[]>(null);- const [hiddenCollections, setHiddenCollections] =- useState<Collection[]>(null);const [defaultHiddenCollectionIDs, setDefaultHiddenCollectionIDs] =useState<Set<number>>();const [files, setFiles] = useState<EnteFile[]>(null);@@ -352,6 +350,8 @@ export default function Gallery() {// TODO: Tempconst user = state.user;const familyData = state.familyData;+ const collections = state.collections;+ const hiddenCollections = state.hiddenCollections;const router = useRouter();@@ -400,17 +400,12 @@ export default function Gallery() {const hiddenFiles = sortFiles(mergeMetadata(await getLocalFiles("hidden")),);- const collections = await getAllLocalCollections();- const { normalCollections, hiddenCollections } =- await splitNormalAndHiddenCollections(collections);+ const allCollections = await getAllLocalCollections();const trashedFiles = await getLocalTrashedFiles();-- dispatch({ type: "mount", user, familyData });+ dispatch({ type: "mount", user, familyData, allCollections });setFiles(files);setTrashedFiles(trashedFiles);setHiddenFiles(hiddenFiles);- setCollections(normalCollections);- setHiddenCollections(hiddenCollections);await syncWithRemote(true);setIsFirstLoad(false);setJustSignedUp(false);@@ -829,14 +824,19 @@ export default function Gallery() {}!silent && startLoading();await preFileInfoSync();- const collections = await getAllLatestCollections();- const { normalCollections, hiddenCollections } =- await splitNormalAndHiddenCollections(collections);- setCollections(normalCollections);- setHiddenCollections(hiddenCollections);- await syncFiles("normal", normalCollections, setFiles);+ const allCollections = await getAllLatestCollections();+ const [hiddenCollections, collections] = splitByPredicate(+ allCollections,+ isHiddenCollection,+ );+ dispatch({+ type: "setAllCollections",+ collections,+ hiddenCollections,+ });+ await syncFiles("normal", collections, setFiles);await syncFiles("hidden", hiddenCollections, setHiddenFiles);- await syncTrash(collections, setTrashedFiles);+ await syncTrash(allCollections, setTrashedFiles);// syncWithRemote is called with the force flag set to true before// doing an upload. So it is possible, say when resuming a pending// upload, that we get two syncWithRemotes happening in parallel.@@ -1252,7 +1252,9 @@ export default function Gallery() {setCollectionNamerAttributes={setCollectionNamerAttributes}setShouldDisableDropzone={setShouldDisableDropzone}setFiles={setFiles}- setCollections={setCollections}+ setCollections={(collections) =>+ dispatch({ type: "setNormalCollections", collections })+ }isFirstUpload={areOnlySystemCollections(collectionSummaries,)}diff --git a/web/apps/photos/src/pages/shared-albums.tsx b/web/apps/photos/src/pages/shared-albums.tsxindex 34f3015527..11c7798cbe 100644--- a/web/apps/photos/src/pages/shared-albums.tsx+++ b/web/apps/photos/src/pages/shared-albums.tsx@@ -72,7 +72,8 @@ import {SetFilesDownloadProgressAttributes,SetFilesDownloadProgressAttributesCreator,} from "types/gallery";-import { downloadCollectionFiles, isHiddenCollection } from "utils/collection";+import { downloadCollectionFiles } from "utils/collection";+import { isHiddenCollection } from "@/new/photos/services/collection";import { downloadSelectedFiles, getSelectedFiles } from "utils/file";import { formatNumber } from "utils/number/format";import { PublicCollectionGalleryContext } from "utils/publicCollectionGallery";diff --git a/web/apps/photos/src/services/collectionService.ts b/web/apps/photos/src/services/collectionService.tsindex 13e2d432f6..8faf99f9ed 100644--- a/web/apps/photos/src/services/collectionService.ts+++ b/web/apps/photos/src/services/collectionService.ts@@ -42,10 +42,10 @@ import {changeCollectionSubType,getHiddenCollections,getNonHiddenCollections,- isHiddenCollection,isQuickLinkCollection,isValidMoveTarget,} from "utils/collection";+import { isHiddenCollection } from "@/new/photos/services/collection";import { UpdateMagicMetadataRequest } from "./fileService";import { getPublicKey } from "./userService";diff --git a/web/apps/photos/src/types/gallery/index.ts b/web/apps/photos/src/types/gallery/index.tsindex d42aa2b7ee..79fa715173 100644--- a/web/apps/photos/src/types/gallery/index.ts+++ b/web/apps/photos/src/types/gallery/index.ts@@ -1,4 +1,3 @@-import type { Collection } from "@/media/collection";import { EnteFile } from "@/media/file";import { type SelectionContext } from "@/new/photos/components/gallery";import type { User } from "@ente/shared/user/types";@@ -21,7 +20,6 @@ export type SetSelectedState = React.Dispatch<React.SetStateAction<SelectedState>>;export type SetFiles = React.Dispatch<React.SetStateAction<EnteFile[]>>;-export type SetCollections = React.Dispatch<React.SetStateAction<Collection[]>>;export type SetLoading = React.Dispatch<React.SetStateAction<boolean>>;export type SetFilesDownloadProgressAttributes = (value:diff --git a/web/apps/photos/src/utils/collection.ts b/web/apps/photos/src/utils/collection.tsindex dd58dc31fc..5d0c151bed 100644--- a/web/apps/photos/src/utils/collection.ts+++ b/web/apps/photos/src/utils/collection.ts@@ -13,6 +13,7 @@ import { ItemVisibility } from "@/media/file-metadata";import {getDefaultHiddenCollectionIDs,isDefaultHiddenCollection,+ isHiddenCollection,isIncomingShare,} from "@/new/photos/services/collection";import { getAllLocalFiles, getLocalFiles } from "@/new/photos/services/files";@@ -335,9 +336,6 @@ export const getUserOwnedCollections = (collections: Collection[]) => {return collections.filter((collection) => collection.owner.id === user.id);};-export const isHiddenCollection = (collection: Collection) =>- collection.magicMetadata?.data.visibility === ItemVisibility.hidden;-export const isQuickLinkCollection = (collection: Collection) =>collection.magicMetadata?.data.subType === SUB_TYPE.QUICK_LINK_COLLECTION;@@ -393,24 +391,6 @@ export function getHiddenCollections(collections: Collection[]): Collection[] {return collections.filter((collection) => isHiddenCollection(collection));}-export async function splitNormalAndHiddenCollections(- collections: Collection[],-): Promise<{- normalCollections: Collection[];- hiddenCollections: Collection[];-}> {- const normalCollections = [];- const hiddenCollections = [];- for (const collection of collections) {- if (isHiddenCollection(collection)) {- hiddenCollections.push(collection);- } else {- normalCollections.push(collection);- }- }- return { normalCollections, hiddenCollections };-}-export function constructCollectionNameMap(collections: Collection[],): Map<number, string> {diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.tsindex 0743542a4a..7ceed78834 100644--- a/web/packages/new/photos/components/gallery/reducer.ts+++ b/web/packages/new/photos/components/gallery/reducer.ts@@ -4,6 +4,8 @@ import {type Collection,} from "@/media/collection";import type { EnteFile } from "@/media/file";+import { isHiddenCollection } from "@/new/photos/services/collection";+import { splitByPredicate } from "@/utils/array";import type { User } from "@ente/shared/user/types";import { t } from "i18next";import React, { useReducer } from "react";@@ -53,6 +55,15 @@ export interface GalleryState {*/familyData: FamilyData | undefined;+ /**+ * The user's non-hidden collections.+ */+ collections: Collection[];+ /**+ * The user's hidden collections.+ */+ hiddenCollections: Collection[];+filteredData: EnteFile[];/*** File IDs of all the files that the user has marked as a favorite.@@ -71,12 +82,12 @@ export interface GalleryState {people: Person[] | undefined;}-// TODO: dummy actions for gradual migration to reducersexport type GalleryAction =| {type: "mount";user: User;familyData: FamilyData;+ allCollections: Collection[];}| {type: "set";@@ -86,16 +97,22 @@ export type GalleryAction =| undefined;}| { type: "setDerived"; favFileIDs: Set<number> }+ | {+ type: "setNormalCollections";+ collections: Collection[];+ }+ | {+ type: "setAllCollections";+ collections: Collection[];+ hiddenCollections: Collection[];+ }| { type: "setFavorites"; favFileIDs: Set<number> };-// | {-// type: "setNormalAndHiddenCollections";-// normalCollections: Collection[];-// hiddenCollections: Collection[];-// };const initialGalleryState: GalleryState = {user: undefined,familyData: undefined,+ collections: [],+ hiddenCollections: [],filteredData: [],favFileIDs: new Set(),activePerson: undefined,@@ -107,12 +124,20 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (action,) => {switch (action.type) {- case "mount":+ case "mount": {+ const [hiddenCollections, collections] = splitByPredicate(+ action.allCollections,+ isHiddenCollection,+ );+return {...state,user: action.user,familyData: action.familyData,+ collections: collections,+ hiddenCollections: hiddenCollections,};+ }case "set":return {...state,@@ -125,6 +150,17 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (...state,favFileIDs: action.favFileIDs,};+ case "setNormalCollections":+ return {+ ...state,+ collections: action.collections,+ };+ case "setAllCollections":+ return {+ ...state,+ collections: action.collections,+ hiddenCollections: action.hiddenCollections,+ };case "setFavorites":return {...state,diff --git a/web/packages/new/photos/services/collection/index.ts b/web/packages/new/photos/services/collection/index.tsindex 122ebc832a..5a3e85de7b 100644--- a/web/packages/new/photos/services/collection/index.ts+++ b/web/packages/new/photos/services/collection/index.ts@@ -1,4 +1,5 @@import { SUB_TYPE, type Collection } from "@/media/collection";+import { ItemVisibility } from "@/media/file-metadata";import type { User } from "@ente/shared/user/types";export const ARCHIVE_SECTION = -1;@@ -23,3 +24,8 @@ export const isDefaultHiddenCollection = (collection: Collection) =>export function isIncomingShare(collection: Collection, user: User) {return collection.owner.id !== user.id;}++export const isHiddenCollection = (collection: Collection) =>+ // TODO: Need to audit the types+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition+ collection.magicMetadata?.data.visibility === ItemVisibility.hidden;diff --git a/web/packages/utils/array.ts b/web/packages/utils/array.tsindex 5f72779f83..0da52c0a30 100644--- a/web/packages/utils/array.ts+++ b/web/packages/utils/array.ts@@ -59,3 +59,28 @@ export const batch = <T>(xs: T[], batchSize: number): T[][] => {}return batches;};++/**+ * Split an array into two arrays - those that satisify the given+ * {@link predicate}, and those that don't.+ *+ * @param xs The array to split.+ *+ * @param predicate A function invoked for each element, to decide which part of+ * the split it will belong to.+ *+ * @returns A tuple with two arrays. The first of these arrays contains all+ * elements for which the predicate returned `true`, and the second array+ * contains the rest of the elements.+ */+export const splitByPredicate = <T>(+ xs: T[],+ predicate: (t: T) => boolean,+): [T[], T[]] =>+ xs.reduce<[T[], T[]]>(+ (result, x) => {+ (predicate(x) ? result[0] : result[1]).push(x);+ return result;+ },+ [[], []],+ );