Skip to content

armgit / ente

Public Imported from GitHub
0

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

Conv

Manav Rathi7607d2a14ad3
diff --git a/web/apps/photos/src/components/Collections/CollectionHeader.tsx b/web/apps/photos/src/components/Collections/CollectionHeader.tsx
index 31a174b009..dad4574566 100644
--- a/web/apps/photos/src/components/Collections/CollectionHeader.tsx
+++ b/web/apps/photos/src/components/Collections/CollectionHeader.tsx
@@ -33,6 +33,7 @@ import {
GalleryItemsSummary,
} from "ente-new/photos/components/gallery/ListHeader";
import {
+ deleteCollection,
isHiddenCollection,
leaveSharedCollection,
renameCollection,
@@ -50,7 +51,6 @@ import { usePhotosAppContext } from "ente-new/photos/types/context";
import { t } from "i18next";
import React, { useCallback, useRef } from "react";
import { Trans } from "react-i18next";
-import * as CollectionAPI from "services/collectionService";
import { SetFilesDownloadProgressAttributesCreator } from "types/gallery";
import {
downloadCollectionHelper,
@@ -195,12 +195,12 @@ const CollectionHeaderOptions: React.FC<CollectionHeaderProps> = ({
};
const deleteCollectionAlongWithFiles = wrap(async () => {
- await CollectionAPI.deleteCollection(activeCollection.id, false);
+ await deleteCollection(activeCollection.id);
setActiveCollectionID(PseudoCollectionID.all);
});
const deleteCollectionButKeepFiles = wrap(async () => {
- await CollectionAPI.deleteCollection(activeCollection.id, true);
+ await deleteCollection(activeCollection.id, { keepFiles: true });
setActiveCollectionID(PseudoCollectionID.all);
});
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 82e63a52ef..ab516a7aee 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -74,8 +74,10 @@ import { useIsOffline } from "ente-new/photos/components/utils/use-is-offline";
import { usePeopleStateSnapshot } from "ente-new/photos/components/utils/use-snapshot";
import { shouldShowWhatsNew } from "ente-new/photos/services/changelog";
import {
+ addToFavoritesCollection,
createAlbum,
removeFromCollection,
+ removeFromFavoritesCollection,
} from "ente-new/photos/services/collection";
import {
haveOnlySystemCollections,
@@ -120,10 +122,6 @@ import { useRouter, type NextRouter } from "next/router";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { FileWithPath } from "react-dropzone";
import { Trans } from "react-i18next";
-import {
- addToFavorites1,
- removeFromFavorites1,
-} from "services/collectionService";
import { uploadManager } from "services/upload-manager";
import {
SelectedState,
@@ -828,9 +826,10 @@ const Page: React.FC = () => {
dispatch({ type: "addPendingFavoriteUpdate", fileID });
try {
- await (isFavorite ? removeFromFavorites1 : addToFavorites1)(
- file,
- );
+ const action = isFavorite
+ ? removeFromFavoritesCollection
+ : addToFavoritesCollection;
+ await action([file]);
dispatch({
type: "unsyncedFavoriteUpdate",
fileID,
diff --git a/web/apps/photos/src/services/collectionService.ts b/web/apps/photos/src/services/collectionService.ts
index 2492685df2..672ff93aa5 100644
--- a/web/apps/photos/src/services/collectionService.ts
+++ b/web/apps/photos/src/services/collectionService.ts
@@ -1,55 +1,7 @@
-import log from "ente-base/log";
-import { apiURL } from "ente-base/origins";
import { sortFiles } from "ente-gallery/utils/file";
import { EnteFile } from "ente-media/file";
-import {
- addToFavoritesCollection,
- removeFromOwnCollection,
- savedUserFavoritesCollection,
-} from "ente-new/photos/services/collection";
import type { CollectionSummary } from "ente-new/photos/services/collection-summary";
import { CollectionsSortBy } from "ente-new/photos/services/collection-summary";
-import { savedCollectionFiles } from "ente-new/photos/services/photos-fdb";
-import HTTPService from "ente-shared/network/HTTPService";
-import { getToken } from "ente-shared/storage/localStorage/helpers";
-
-export const addToFavorites1 = async (file: EnteFile) => {
- await addToFavoritesCollection([file]);
-};
-
-export const removeFromFavorites1 = async (file: EnteFile) => {
- const favCollection = await savedUserFavoritesCollection();
- if (!favCollection) {
- throw Error("favorite collection missing");
- }
- await removeFromOwnCollection(favCollection.id, [file]);
-};
-
-export const deleteCollection = async (
- collectionID: number,
- keepFiles: boolean,
-) => {
- try {
- if (keepFiles) {
- const allFiles = await savedCollectionFiles();
- const collectionFiles = allFiles.filter(
- (file) => file.collectionID == collectionID,
- );
- await removeFromOwnCollection(collectionID, collectionFiles);
- }
- const token = getToken();
-
- await HTTPService.delete(
- await apiURL(`/collections/v3/${collectionID}`),
- null,
- { collectionID, keepFiles },
- { "X-Auth-Token": token },
- );
- } catch (e) {
- log.error("delete collection failed ", e);
- throw e;
- }
-};
export const sortCollectionSummaries = (
collectionSummaries: CollectionSummary[],
diff --git a/web/packages/new/photos/services/collection.ts b/web/packages/new/photos/services/collection.ts
index a60f96d974..d655bcae5f 100644
--- a/web/packages/new/photos/services/collection.ts
+++ b/web/packages/new/photos/services/collection.ts
@@ -901,6 +901,43 @@ const removeNonCollectionOwnerFiles = async (
),
);
+/**
+ * Delete a collection on remote.
+ *
+ * Reads local state but does not modify it. The effects are on remote.
+ *
+ * @param collectionID The ID of the collection to delete.
+ *
+ * @param opts Deletion options. In particular, if {@link keepFiles} is true,
+ * then the any of the user's files that only exist in this collection are
+ * first moved to another one of the user's collection (or Uncategorized if no
+ * such collection exists) before deleting the collection.
+ *
+ * See: [Note: Removing files from a collection]
+ */
+export const deleteCollection = async (
+ collectionID: number,
+ opts?: { keepFiles?: boolean },
+) => {
+ const keepFiles = opts?.keepFiles ?? false;
+
+ if (keepFiles) {
+ const collectionFiles = await savedCollectionFiles();
+ await removeFromOwnCollection(
+ collectionID,
+ collectionFiles.filter((f) => f.collectionID == collectionID),
+ );
+ }
+
+ ensureOk(
+ await fetch(await apiURL(`/collections/v3/${collectionID}`), {
+ method: "DELETE",
+ headers: await authenticatedRequestHeaders(),
+ body: JSON.stringify({ collectionID, keepFiles }),
+ }),
+ );
+};
+
/**
* Rename a collection on remote.
*
@@ -1179,6 +1216,11 @@ export const savedUserFavoritesCollection = async () => {
export const addToFavoritesCollection = async (files: EnteFile[]) =>
addToCollection(await savedOrCreateUserFavoritesCollection(), files);
+export const removeFromFavoritesCollection = async (files: EnteFile[]) =>
+ // Non-null assertion because if we get here and a favorites collection does
+ // not already exist, then something is wrong.
+ removeFromOwnCollection((await savedUserFavoritesCollection())!.id, files);
+
/**
* Return the default hidden collection for the user if one is found in the
* local database. Otherwise create a new one and return that.