Skip to content

armgit / ente

Public Imported from GitHub
0

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

Don't use exceptions for flow control

Manav Rathi1cc911597230
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 1e81c09df4..fd77960b24 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -76,7 +76,6 @@ import { shouldShowWhatsNew } from "ente-new/photos/services/changelog";
import {
createAlbum,
removeFromCollection,
- removeOtherOtherNotSupportErrorMessage,
} from "ente-new/photos/services/collection";
import {
haveOnlySystemCollections,
@@ -640,22 +639,15 @@ const Page: React.FC = () => {
const handleRemoveFilesFromCollection = (collection: Collection) => {
void (async () => {
showLoadingBar();
- let notifyOtherFiles = false;
+ let notifyOthersFiles = false;
try {
setOpenCollectionSelector(false);
const selectedFiles = getSelectedFiles(selected, filteredFiles);
- try {
- await removeFromCollection(collection, selectedFiles);
- } catch (e) {
- if (
- e instanceof Error &&
- e.message == removeOtherOtherNotSupportErrorMessage
- ) {
- notifyOtherFiles = true;
- } else {
- throw e;
- }
- }
+ const processedCount = await removeFromCollection(
+ collection,
+ selectedFiles,
+ );
+ notifyOthersFiles = processedCount != selectedFiles.length;
clearSelection();
await remotePull({ silent: true });
} catch (e) {
@@ -664,7 +656,7 @@ const Page: React.FC = () => {
hideLoadingBar();
}
- if (notifyOtherFiles) {
+ if (notifyOthersFiles) {
showMiniDialog(notifyOthersFilesDialogAttributes());
}
})();
diff --git a/web/packages/new/photos/services/collection.ts b/web/packages/new/photos/services/collection.ts
index e49c0c0daf..5290a6f9c6 100644
--- a/web/packages/new/photos/services/collection.ts
+++ b/web/packages/new/photos/services/collection.ts
@@ -678,9 +678,6 @@ export const deleteFromTrash = async (fileIDs: number[]) =>
),
);
-export const removeOtherOtherNotSupportErrorMessage =
- "Cannot remove other user's files in other user's collections";
-
/**
* Remove the given files from the specified collection owned by the user.
*
@@ -690,11 +687,16 @@ export const removeOtherOtherNotSupportErrorMessage =
* user).
*
* @param files The files to remove from the collection. The files owned by the
- * user will be removed. If the collection is not owned by the user and some of
- * the given files are also not owned by the user, then this function will throw
- * an error with the message {@link removeOtherOtherNotSupportErrorMessage}
+ * user will be removed. If the collection is not owned by the user, then any
+ * files that are not owned by the user will not be processed. In such cases,
+ * this function will return a count less than the count of the provided files
* (after having removed what can be removed).
*
+ * @returns The count of files that were processed. This can be less than the
+ * count of the provided {@link files} if some files were not processed because
+ * because they belong to other users (and {@link collection} also does not
+ * belong to the current user).
+ *
* [Note: Removing files from a collection]
*
* There are three scenarios
@@ -752,7 +754,7 @@ export const removeOtherOtherNotSupportErrorMessage =
export const removeFromCollection = async (
collection: Collection,
files: EnteFile[],
-) =>
+): Promise<number> =>
collection.owner.id == ensureLocalUser().id
? removeFromOwnCollection(collection.id, files)
: removeFromOthersCollection(collection.id, files);
@@ -772,6 +774,7 @@ export const removeFromOwnCollection = async (
if (nonUserFiles.length) {
await removeNonCollectionOwnerFiles(collectionID, nonUserFiles);
}
+ return files.length;
};
const removeFromOthersCollection = async (
@@ -779,16 +782,11 @@ const removeFromOthersCollection = async (
files: EnteFile[],
) => {
const userID = ensureLocalUser().id;
- const [userFiles, nonUserFiles] = splitByPredicate(
- files,
- (f) => f.ownerID == userID,
- );
+ const [userFiles] = splitByPredicate(files, (f) => f.ownerID == userID);
if (userFiles.length) {
await removeNonCollectionOwnerFiles(collectionID, userFiles);
}
- if (nonUserFiles.length) {
- throw new Error(removeOtherOtherNotSupportErrorMessage);
- }
+ return userFiles.length;
};
/**