From a0a05560378e248d5d075fc4bde5028093b0d578 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Fri, 27 Jun 2025 07:32:05 +0530 Subject: [PATCH] Conv --- .../photos/src/services/collectionService.ts | 43 +++---------------- .../new/photos/services/collection.ts | 30 +++++++++++++ web/packages/new/photos/services/file.ts | 7 ++- 3 files changed, 41 insertions(+), 39 deletions(-) diff --git a/web/apps/photos/src/services/collectionService.ts b/web/apps/photos/src/services/collectionService.ts index 6cbbf10b2d..def8eba9cb 100644 --- a/web/apps/photos/src/services/collectionService.ts +++ b/web/apps/photos/src/services/collectionService.ts @@ -7,6 +7,7 @@ import { addToFavorites, createUncategorizedCollection, moveFromCollection, + removeNonUserFilesFromCollection, savedUserFavoritesCollection, } from "ente-new/photos/services/collection"; import type { CollectionSummary } from "ente-new/photos/services/collection-summary"; @@ -21,11 +22,8 @@ import { import HTTPService from "ente-shared/network/HTTPService"; import { getData } from "ente-shared/storage/localStorage"; import { getToken } from "ente-shared/storage/localStorage/helpers"; -import { batch } from "ente-utils/array"; import { isValidMoveTarget } from "utils/collection"; -const REQUEST_BATCH_SIZE = 1000; - export const addToFavorites1 = async (file: EnteFile) => { await addToFavorites([file]); }; @@ -55,7 +53,10 @@ export const removeFromCollection = async ( } if (nonUserFiles.length > 0) { - await removeNonUserFiles(collectionID, nonUserFiles); + await removeNonUserFilesFromCollection( + collectionID, + nonUserFiles.map((f) => f.id), + ); } if (userFiles.length > 0) { await removeUserFiles(collectionID, userFiles); @@ -66,7 +67,7 @@ export const removeFromCollection = async ( } }; -export const removeUserFiles = async ( +const removeUserFiles = async ( sourceCollectionID: number, toRemoveFiles: EnteFile[], ) => { @@ -131,38 +132,6 @@ export const removeUserFiles = async ( } }; -export interface RemoveFromCollectionRequest { - collectionID: number; - fileIDs: number[]; -} - -export const removeNonUserFiles = async ( - collectionID: number, - nonUserFiles: EnteFile[], -) => { - try { - const fileIDs = nonUserFiles.map((f) => f.id); - const token = getToken(); - const batchedFileIDs = batch(fileIDs, REQUEST_BATCH_SIZE); - for (const batch of batchedFileIDs) { - const request: RemoveFromCollectionRequest = { - collectionID, - fileIDs: batch, - }; - - await HTTPService.post( - await apiURL("/collections/v3/remove-files"), - request, - null, - { "X-Auth-Token": token }, - ); - } - } catch (e) { - log.error("remove non user files failed ", e); - throw e; - } -}; - export const deleteCollection = async ( collectionID: number, keepFiles: boolean, diff --git a/web/packages/new/photos/services/collection.ts b/web/packages/new/photos/services/collection.ts index 8d9afac292..98136e77e4 100644 --- a/web/packages/new/photos/services/collection.ts +++ b/web/packages/new/photos/services/collection.ts @@ -689,6 +689,36 @@ export const deleteFromTrash = async (fileIDs: number[]) => ), ); +/** + * Remove the provided files (none of which are owned by the user) from the + * provided collection on remote. + * + * Only files which do not belong to the collection owner can be removed from + * the collection. If the collection owner wants to remove files owned by them, + * then their client should first move those files first to other collections + * owned by the collection owner. + * + * Remote only, does not modify local state. + * + * @param collectionID The ID of collection from which to remove the files. + * + * @param files A list of files which do not belong to the user, and which we + * the user wants to remove from the given collection. + */ +export const removeNonUserFilesFromCollection = async ( + collectionID: number, + fileIDs: number[], +) => + batched(fileIDs, async (batchFileIDs) => + ensureOk( + await fetch(await apiURL("/collections/v3/remove-files"), { + method: "POST", + headers: await authenticatedRequestHeaders(), + body: JSON.stringify({ collectionID, fileIDs: batchFileIDs }), + }), + ), + ); + /** * Rename a collection on remote. * diff --git a/web/packages/new/photos/services/file.ts b/web/packages/new/photos/services/file.ts index 77cfe7b9ae..b25e8c782c 100644 --- a/web/packages/new/photos/services/file.ts +++ b/web/packages/new/photos/services/file.ts @@ -40,8 +40,11 @@ const requestBatchSize = 1000; * * @param op The operation to perform on each batch. * - * @returns An array of results, one from each batch operation. For details, - * including behaviour on errors, see `Promise.all`. + * @returns A promise for an array of results, one from each batch operation. If + * any operations fails, then the promise rejects with the first failure reason. + * + * For more details see the documentation for the `Promise.all` primitive which + * this function uses. */ export const batched = ( items: T[],