This commit is contained in:
Manav Rathi
2025-06-27 07:32:05 +05:30
parent be8d1932b1
commit a0a0556037
3 changed files with 41 additions and 39 deletions

View File

@@ -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,

View File

@@ -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.
*

View File

@@ -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 = <T, U>(
items: T[],