From 208cbda19ac6293297bca87a234ebccc159304cd Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Sun, 21 Jul 2019 14:15:52 +0200 Subject: [PATCH] Scanner delete albums when not found locally --- api/src/Scanner.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/api/src/Scanner.js b/api/src/Scanner.js index 55a574f3..d4b56272 100644 --- a/api/src/Scanner.js +++ b/api/src/Scanner.js @@ -69,9 +69,8 @@ class PhotoScanner { return } - allUserScans.push(this.scanUser(user)) - console.log(`Scanning ${user.username}...`) + allUserScans.push(this.scanUser(user)) }, onCompleted: () => { session.close() @@ -92,6 +91,7 @@ class PhotoScanner { }) }) .catch(error => { + console.log('SYNC ERROR', JSON.stringify(error)) this.pubsub.publish(EVENT_SCANNER_PROGRESS, { scannerStatusUpdate: { progress: 0, @@ -123,6 +123,8 @@ class PhotoScanner { const driver = this.driver const scanAlbum = this.scanAlbum + let foundAlbumIds = [] + async function scanPath(path) { const list = fs.readdirSync(path) @@ -154,6 +156,9 @@ class PhotoScanner { console.log('Album already exists') const album = findAlbumResult.records[0].toObject().a.properties + + foundAlbumIds.push(album.id) + scanAlbum(album) continue @@ -192,6 +197,22 @@ class PhotoScanner { } await scanPath(user.rootPath) + + console.log('Found album ids', foundAlbumIds) + + const session = this.driver.session() + + const userAlbumsResult = await session.run( + 'MATCH (u:User { id: {userId} })-[:OWNS]->(a:Album) WHERE NOT a.id IN {foundAlbums} DETACH DELETE a return a', + { userId: user.id, foundAlbums: foundAlbumIds } + ) + + console.log( + `Deleted ${userAlbumsResult.records.length} albums from ${user.username} that was not found locally` + ) + + session.close() + console.log('User scan complete') }