diff --git a/web/apps/photos/src/services/watch.ts b/web/apps/photos/src/services/watch.ts index 339ae699d..afb172795 100644 --- a/web/apps/photos/src/services/watch.ts +++ b/web/apps/photos/src/services/watch.ts @@ -38,8 +38,6 @@ class FolderWatcher { * If the file system directory corresponding to the (root) folder path of a * folder watch is deleted on disk, we note down that in this queue so that * we can ignore any file system events that come for it next. - * - * TODO: is this really needed? the mappings are pre-checked first. */ private deletedFolderPaths: string[] = []; /** `true` if we are using the uploader. */ @@ -89,12 +87,12 @@ class FolderWatcher { this.syncWithDisk(); } - /** `true` if we are currently using the uploader */ + /** Return `true` if we are currently using the uploader. */ isUploadRunning() { return this.uploadRunning; } - /** `true` if syncing has been temporarily paused */ + /** Return `true` if syncing has been temporarily paused. */ isSyncPaused() { return this.isPaused; } @@ -501,31 +499,30 @@ class FolderWatcher { this.eventQueue = this.eventQueue.filter( (event) => !event.filePath.startsWith(deletedFolderPath), ); + return true; } private async moveToTrash(syncedFiles: FolderWatch["syncedFiles"]) { - const files = await getLocalFiles(); - const toTrashFilesMap = new Map(); - for (const file of syncedFiles) { - toTrashFilesMap.set(file.uploadedFileID, file); - } - const filesToTrash = files.filter((file) => { - if (toTrashFilesMap.has(file.id)) { - const fileToTrash = toTrashFilesMap.get(file.id); - if (fileToTrash.collectionID === file.collectionID) { - return true; - } - } - }); - const groupFilesByCollectionId = - groupFilesBasedOnCollectionID(filesToTrash); + const syncedFileForID = new Map(); + for (const file of syncedFiles) + syncedFileForID.set(file.uploadedFileID, file); - for (const [ - collectionID, - filesToTrash, - ] of groupFilesByCollectionId.entries()) { - await removeFromCollection(collectionID, filesToTrash); + const files = await getLocalFiles(); + const filesToTrash = files.filter((file) => { + const correspondingSyncedFile = syncedFileForID.get(file.id); + if ( + correspondingSyncedFile && + correspondingSyncedFile.collectionID == file.collectionID + ) { + return true; + } + return false; + }); + + const filesByCollectionID = groupFilesBasedOnCollectionID(filesToTrash); + for (const [id, files] of filesByCollectionID.entries()) { + await removeFromCollection(id, files); } this.requestSyncWithRemote(); diff --git a/web/apps/photos/src/utils/file/index.ts b/web/apps/photos/src/utils/file/index.ts index f65d36bd9..cc3ddc5e1 100644 --- a/web/apps/photos/src/utils/file/index.ts +++ b/web/apps/photos/src/utils/file/index.ts @@ -132,16 +132,16 @@ export async function downloadFile(file: EnteFile) { } } -export function groupFilesBasedOnCollectionID(files: EnteFile[]) { - const collectionWiseFiles = new Map(); +/** Segment the given {@link files} into lists indexed by their collection ID */ +export const groupFilesBasedOnCollectionID = (files: EnteFile[]) => { + const result = new Map(); for (const file of files) { - if (!collectionWiseFiles.has(file.collectionID)) { - collectionWiseFiles.set(file.collectionID, []); - } - collectionWiseFiles.get(file.collectionID).push(file); + const id = file.collectionID; + if (!result.has(id)) result.set(id, []); + result.get(id).push(file); } - return collectionWiseFiles; -} + return result; +}; function getSelectedFileIds(selectedFiles: SelectedState) { const filesIDs: number[] = [];