Skip to content

armgit / ente

Public Imported from GitHub
0

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

Refactor

Manav Rathibf8713ba2137
diff --git a/web/apps/photos/src/components/Upload.tsx b/web/apps/photos/src/components/Upload.tsx
index f6419b7962..f53b939934 100644
--- a/web/apps/photos/src/components/Upload.tsx
+++ b/web/apps/photos/src/components/Upload.tsx
@@ -50,7 +50,7 @@ import { UploaderNameInput } from "ente-new/albums/components/UploaderNameInput"
import { CollectionMappingChoice } from "ente-new/photos/components/CollectionMappingChoice";
import type { CollectionSelectorAttributes } from "ente-new/photos/components/CollectionSelector";
import { downloadAppDialogAttributes } from "ente-new/photos/components/utils/download";
-import { getLatestCollections } from "ente-new/photos/services/collections";
+import { savedNormalCollections } from "ente-new/photos/services/collection";
import { redirectToCustomerPortal } from "ente-new/photos/services/user-details";
import { usePhotosAppContext } from "ente-new/photos/types/context";
import { firstNonEmpty } from "ente-utils/array";
@@ -98,6 +98,20 @@ interface UploadProps {
setLoading: SetLoading;
setShouldDisableDropzone: (value: boolean) => void;
showCollectionSelector?: () => void;
+ /**
+ * Called when an action in the uploader requires us to first pull the
+ * latest files and collections from remote.
+ *
+ * See: [Note: Full sync vs file and collection sync]
+ *
+ * Specifically, this is used prior to creating a new album, to obtain
+ * (potential) existing albums from remote so that they can be matched by
+ * name if needed.
+ *
+ * This functionality is not needed during uploads to a public album, so
+ * this property is optional; the public albums code need not provide it.
+ */
+ onFileAndCollectionSyncWithRemote?: () => Promise<void>;
/**
* Callback invoked when a file is uploaded.
*
@@ -111,7 +125,6 @@ interface UploadProps {
* app, where the scenario requiring this will not arise.
*/
onShowPlanSelector?: () => void;
- setCollections?: (cs: Collection[]) => void;
isFirstUpload?: boolean;
uploadTypeSelectorView: boolean;
showSessionExpiredMessage: () => void;
@@ -129,6 +142,7 @@ type UploadType = "files" | "folders" | "zips";
export const Upload: React.FC<UploadProps> = ({
isFirstUpload,
dragAndDropFiles,
+ onFileAndCollectionSyncWithRemote,
onUploadFile,
onShowPlanSelector,
showSessionExpiredMessage,
@@ -592,7 +606,6 @@ export const Upload: React.FC<UploadProps> = ({
) => {
await preCollectionCreationAction();
let uploadItemsWithCollection: UploadItemWithCollection[] = [];
- const collections: Collection[] = [];
let collectionNameToUploadItems = new Map<
string,
UploadItemAndPath[]
@@ -608,8 +621,10 @@ export const Upload: React.FC<UploadProps> = ({
collectionName,
);
}
+ const collections: Collection[] = [];
try {
- const existingCollections = await getLatestCollections();
+ await onFileAndCollectionSyncWithRemote!();
+ const existingCollections = await savedNormalCollections();
let index = 0;
for (const [
collectionName,
@@ -620,7 +635,6 @@ export const Upload: React.FC<UploadProps> = ({
existingCollections,
);
collections.push(collection);
- props.setCollections([...existingCollections, ...collections]);
uploadItemsWithCollection = [
...uploadItemsWithCollection,
...uploadItems.map(([uploadItem, path]) => ({
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 1dd03b2767..b6f454d516 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -1078,13 +1078,13 @@ const Page: React.FC = () => {
onCloseCollectionSelector={handleCloseCollectionSelector}
setLoading={setBlockingLoad}
setShouldDisableDropzone={setShouldDisableDropzone}
+ onFileAndCollectionSyncWithRemote={
+ fileAndCollectionSyncWithRemote
+ }
onUploadFile={(file) =>
dispatch({ type: "uploadNormalFile", file })
}
onShowPlanSelector={showPlanSelector}
- setCollections={(collections) =>
- dispatch({ type: "setNormalCollections", collections })
- }
isFirstUpload={areOnlySystemCollections(
normalCollectionSummaries,
)}
diff --git a/web/apps/photos/src/services/upload-manager.ts b/web/apps/photos/src/services/upload-manager.ts
index 46564076a9..ae3a38221f 100644
--- a/web/apps/photos/src/services/upload-manager.ts
+++ b/web/apps/photos/src/services/upload-manager.ts
@@ -321,6 +321,11 @@ class UploadManager {
* @param itemsWithCollection The items to upload, each paired with the id
* of the collection that they should be uploaded into.
*
+ * @param collections The collections to which the files are being uploaded.
+ *
+ * These are not all the user's collections - these are just the collections
+ * mentioned by one or more {@link itemsWithCollection}.
+ *
* @returns `true` if at least one file was processed
*/
public async uploadItems(
diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.ts
index 11e70b20a7..68c0bd23a6 100644
--- a/web/packages/new/photos/components/gallery/reducer.ts
+++ b/web/packages/new/photos/components/gallery/reducer.ts
@@ -433,7 +433,6 @@ export type GalleryAction =
normalCollections: Collection[];
hiddenCollections: Collection[];
}
- | { type: "setNormalCollections"; collections: Collection[] }
| { type: "setNormalFiles"; files: EnteFile[] }
| { type: "fetchNormalFiles"; files: EnteFile[] }
| { type: "uploadNormalFile"; file: EnteFile }
@@ -655,62 +654,6 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
});
}
- case "setNormalCollections": {
- const normalCollections = action.collections;
- const archivedCollectionIDs =
- deriveArchivedCollectionIDs(normalCollections);
- const archivedFileIDs = deriveArchivedFileIDs(
- archivedCollectionIDs,
- state.normalFiles,
- );
- const normalCollectionSummaries = deriveNormalCollectionSummaries(
- state.user!,
- normalCollections,
- state.normalFiles,
- state.trashItems,
- archivedFileIDs,
- );
-
- // Revalidate the active view if needed.
- let view = state.view;
- let selectedCollectionSummaryID = state.selectedCollectionSummaryID;
- if (state.view?.type == "albums") {
- ({ view, selectedCollectionSummaryID } =
- deriveAlbumsViewAndSelectedID(
- normalCollections,
- normalCollectionSummaries,
- selectedCollectionSummaryID,
- ));
- }
-
- return stateByUpdatingFilteredFiles({
- ...state,
- normalCollections,
- archivedCollectionIDs,
- archivedFileIDs,
- favoriteFileIDs: deriveFavoriteFileIDs(
- state.user!,
- normalCollections,
- state.normalFiles,
- state.unsyncedFavoriteUpdates,
- ),
- collectionNameByID: createCollectionNameByID(
- normalCollections.concat(state.hiddenCollections),
- ),
- normalCollectionSummaries,
- uncategorizedCollectionSummaryID:
- deriveUncategorizedCollectionSummaryID(normalCollections),
- selectedCollectionSummaryID,
- pendingSearchSuggestions:
- enqueuePendingSearchSuggestionsIfNeeded(
- state.searchSuggestion,
- state.pendingSearchSuggestions,
- state.isInSearchMode,
- ),
- view,
- });
- }
-
case "setNormalFiles": {
const unsyncedPrivateMagicMetadataUpdates =
prunedUnsyncedPrivateMagicMetadataUpdates(
diff --git a/web/packages/new/photos/services/collection.ts b/web/packages/new/photos/services/collection.ts
index 9028225e30..2e5ad3221c 100644
--- a/web/packages/new/photos/services/collection.ts
+++ b/web/packages/new/photos/services/collection.ts
@@ -29,9 +29,10 @@ import {
createMagicMetadata,
encryptMagicMetadata,
} from "ente-media/magic-metadata";
-import { batch } from "ente-utils/array";
+import { batch, splitByPredicate } from "ente-utils/array";
import { z } from "zod/v4";
import { requestBatchSize, type UpdateMagicMetadataRequest } from "./file";
+import { savedCollections } from "./photos-fdb";
import { ensureUserKeyPair, getPublicKey } from "./user";
const uncategorizedCollectionName = "Uncategorized";
@@ -234,6 +235,15 @@ export const getCollections = async (
);
};
+/**
+ * Return all normal (non-hidden) collections that are present in our local
+ * database.
+ */
+export const savedNormalCollections = (): Promise<Collection[]> =>
+ savedCollections().then(
+ (cs) => splitByPredicate(cs, isHiddenCollection)[1],
+ );
+
/**
* Return a map of the (user-facing) collection name, indexed by collection ID.
*/
diff --git a/web/packages/new/photos/services/collections.ts b/web/packages/new/photos/services/collections.ts
index 7df89dc8d7..457708a4a7 100644
--- a/web/packages/new/photos/services/collections.ts
+++ b/web/packages/new/photos/services/collections.ts
@@ -33,16 +33,7 @@ export const getHiddenCollectionIDs = async (): Promise<number[]> =>
export const getCollectionUpdationTime = async (): Promise<number> =>
(await localForage.getItem<number>(COLLECTION_UPDATION_TIME)) ?? 0;
-export const getLatestCollections = async (
- type: "normal" | "hidden" = "normal",
-): Promise<Collection[]> => {
- const collections = await getAllLatestCollections();
- return type == "normal"
- ? collections.filter((c) => !isHiddenCollection(c))
- : collections.filter((c) => isHiddenCollection(c));
-};
-
-export const getAllLatestCollections = async (): Promise<Collection[]> => {
+export const pullCollections = async (): Promise<Collection[]> => {
const localCollections = await savedCollections();
let sinceTime = await getCollectionUpdationTime();
diff --git a/web/packages/new/photos/services/sync.ts b/web/packages/new/photos/services/sync.ts
index 7498cac089..0d0cc01738 100644
--- a/web/packages/new/photos/services/sync.ts
+++ b/web/packages/new/photos/services/sync.ts
@@ -6,7 +6,7 @@ import {
import type { Collection } from "ente-media/collection";
import type { EnteFile } from "ente-media/file";
import { isHiddenCollection } from "ente-new/photos/services/collection";
-import { getAllLatestCollections } from "ente-new/photos/services/collections";
+import { pullCollections } from "ente-new/photos/services/collections";
import { syncFiles } from "ente-new/photos/services/files";
import {
isMLSupported,
@@ -125,7 +125,7 @@ interface SyncCallectionAndFilesOpts {
export const syncCollectionAndFiles = async (
opts?: SyncCallectionAndFilesOpts,
) => {
- const collections = await getAllLatestCollections();
+ const collections = await pullCollections();
const [hiddenCollections, normalCollections] = splitByPredicate(
collections,
isHiddenCollection,