From 448514af9eff12cb24ae95bfc3824506bcac37ae Mon Sep 17 00:00:00 2001 From: advplyr Date: Sat, 27 Nov 2021 16:01:53 -0600 Subject: [PATCH] Add:Batch add/remove books from collection --- client/components/app/Appbar.vue | 6 + .../modals/UserCollectionsModal.vue | 127 +++++++++++++----- client/store/globals.js | 6 + package.json | 2 +- server/ApiController.js | 2 + server/controllers/CollectionController.js | 58 +++++++- server/scanner/AudioFileScanner.js | 7 +- 7 files changed, 166 insertions(+), 42 deletions(-) diff --git a/client/components/app/Appbar.vue b/client/components/app/Appbar.vue index 970f0c8e..cdd26666 100644 --- a/client/components/app/Appbar.vue +++ b/client/components/app/Appbar.vue @@ -48,6 +48,9 @@ + + + @@ -196,6 +199,9 @@ export default { }, batchEditClick() { this.$router.push('/batch') + }, + batchAddToCollectionClick() { + this.$store.commit('globals/setShowBatchUserCollectionsModal', true) } }, mounted() {} diff --git a/client/components/modals/UserCollectionsModal.vue b/client/components/modals/UserCollectionsModal.vue index be519c67..59b47813 100644 --- a/client/components/modals/UserCollectionsModal.vue +++ b/client/components/modals/UserCollectionsModal.vue @@ -9,7 +9,8 @@
-

Add to Collection

+

Add to Collection

+

Add {{ selectedBooks.length }} Books to Collection

@@ -63,6 +64,14 @@ export default { } }, title() { + if (this.showBatchUserCollectionModal) { + var title = this.selectedBooks[0] ? this.selectedBooks[0].book.title || '' : '' + if (this.selectedBooks.length > 1 && this.selectedBooks[1]) { + title += ', ' + this.selectedBooks[1].book.title || '' + if (this.selectedBooks.length > 2) title += `, and ${this.selectedBooks.length - 2} other${this.selectedBooks.length > 3 ? 's' : ''}` + } + return title + } return this.selectedAudiobook ? this.selectedAudiobook.book.title : '' }, selectedAudiobook() { @@ -77,13 +86,32 @@ export default { sortedCollections() { return this.collections .map((c) => { - var includesBook = !!c.books.find((b) => b.id === this.selectedAudiobookId) + var includesBook = false + if (this.showBatchUserCollectionModal) { + // Only show collection added if all books are in the collection + var collectionBookIds = c.books.map((b) => b.id) + includesBook = !this.selectedBookIds.find((id) => !collectionBookIds.includes(id)) + } else { + includesBook = !!c.books.find((b) => b.id === this.selectedAudiobookId) + } + return { isBookIncluded: includesBook, ...c } }) .sort((a, b) => (a.isBookIncluded ? -1 : 1)) + }, + showBatchUserCollectionModal() { + return this.$store.state.globals.showBatchUserCollectionModal + }, + selectedBookIds() { + return this.$store.state.selectedAudiobooks || [] + }, + selectedBooks() { + return this.selectedBookIds.map((id) => { + return this.$store.getters['audiobooks/getAudiobook'](id) + }) } }, methods: { @@ -91,48 +119,83 @@ export default { this.$store.dispatch('user/loadUserCollections') }, removeFromCollection(collection) { - if (!this.selectedAudiobookId) return - + if (!this.selectedAudiobookId && !this.selectedBookIds.length) return this.processing = true - this.$axios - .$delete(`/api/collections/${collection.id}/book/${this.selectedAudiobookId}`) - .then((updatedCollection) => { - console.log(`Book removed from collection`, updatedCollection) - this.$toast.success('Book removed from collection') - this.processing = false - }) - .catch((error) => { - console.error('Failed to remove book from collection', error) - this.$toast.error('Failed to remove book from collection') - this.processing = false - }) + if (this.showBatchUserCollectionModal) { + // BATCH Remove books + this.$axios + .$post(`/api/collections/${collection.id}/batch/remove`, { books: this.selectedBookIds }) + .then((updatedCollection) => { + console.log(`Books removed from collection`, updatedCollection) + this.$toast.success('Books removed from collection') + this.processing = false + }) + .catch((error) => { + console.error('Failed to remove books from collection', error) + this.$toast.error('Failed to remove books from collection') + this.processing = false + }) + } else { + // Remove single book + this.$axios + .$delete(`/api/collections/${collection.id}/book/${this.selectedAudiobookId}`) + .then((updatedCollection) => { + console.log(`Book removed from collection`, updatedCollection) + this.$toast.success('Book removed from collection') + this.processing = false + }) + .catch((error) => { + console.error('Failed to remove book from collection', error) + this.$toast.error('Failed to remove book from collection') + this.processing = false + }) + } }, addToCollection(collection) { - if (!this.selectedAudiobookId) return - + if (!this.selectedAudiobookId && !this.selectedBookIds.length) return this.processing = true - this.$axios - .$post(`/api/collections/${collection.id}/book`, { id: this.selectedAudiobookId }) - .then((updatedCollection) => { - console.log(`Book added to collection`, updatedCollection) - this.$toast.success('Book added to collection') - this.processing = false - }) - .catch((error) => { - console.error('Failed to add book to collection', error) - this.$toast.error('Failed to add book to collection') - this.processing = false - }) + if (this.showBatchUserCollectionModal) { + // BATCH Remove books + this.$axios + .$post(`/api/collections/${collection.id}/batch/add`, { books: this.selectedBookIds }) + .then((updatedCollection) => { + console.log(`Books added to collection`, updatedCollection) + this.$toast.success('Books added to collection') + this.processing = false + }) + .catch((error) => { + console.error('Failed to add books to collection', error) + this.$toast.error('Failed to add books to collection') + this.processing = false + }) + } else { + if (!this.selectedAudiobookId) return + + this.$axios + .$post(`/api/collections/${collection.id}/book`, { id: this.selectedAudiobookId }) + .then((updatedCollection) => { + console.log(`Book added to collection`, updatedCollection) + this.$toast.success('Book added to collection') + this.processing = false + }) + .catch((error) => { + console.error('Failed to add book to collection', error) + this.$toast.error('Failed to add book to collection') + this.processing = false + }) + } }, submitCreateCollection() { - if (!this.newCollectionName || !this.selectedAudiobook) { + if (!this.newCollectionName || (!this.selectedAudiobookId && !this.selectedBookIds.length)) { return } this.processing = true + + var books = this.showBatchUserCollectionModal ? this.selectedBookIds : [this.selectedAudiobookId] var newCollection = { - books: [this.selectedAudiobook.id], + books: books, libraryId: this.selectedAudiobook.libraryId, name: this.newCollectionName } diff --git a/client/store/globals.js b/client/store/globals.js index 4dc88143..2ef3d272 100644 --- a/client/store/globals.js +++ b/client/store/globals.js @@ -1,5 +1,6 @@ export const state = () => ({ + showBatchUserCollectionModal: false, showUserCollectionsModal: false, showEditCollectionModal: false, selectedCollection: null @@ -15,6 +16,11 @@ export const actions = { export const mutations = { setShowUserCollectionsModal(state, val) { + state.showBatchUserCollectionModal = false + state.showUserCollectionsModal = val + }, + setShowBatchUserCollectionsModal(state, val) { + state.showBatchUserCollectionModal = true state.showUserCollectionsModal = val }, setShowEditCollectionModal(state, val) { diff --git a/package.json b/package.json index 550c40e9..5148e3a3 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "prod": "npm run client && npm install && node prod.js", "build-win": "pkg -t node12-win-x64 -o ./dist/win/audiobookshelf .", "build-linux": "build/linuxpackager", - "docker": "docker buildx build -t advplyr/audiobookshelf --platform linux/amd64,linux/arm64 --push ." + "docker": "docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 --push . -t advplyr/audiobookshelf" }, "bin": "prod.js", "pkg": { diff --git a/server/ApiController.js b/server/ApiController.js index 287f54b3..4a835aee 100644 --- a/server/ApiController.js +++ b/server/ApiController.js @@ -108,6 +108,8 @@ class ApiController { this.router.post('/collections/:id/book', CollectionController.addBook.bind(this)) this.router.delete('/collections/:id/book/:bookId', CollectionController.removeBook.bind(this)) + this.router.post('/collections/:id/batch/add', CollectionController.addBatch.bind(this)) + this.router.post('/collections/:id/batch/remove', CollectionController.removeBatch.bind(this)) // TEMP: Support old syntax for mobile app this.router.get('/collection/:id', CollectionController.findOne.bind(this)) diff --git a/server/controllers/CollectionController.js b/server/controllers/CollectionController.js index 5638b7c8..d4a74d9c 100644 --- a/server/controllers/CollectionController.js +++ b/server/controllers/CollectionController.js @@ -13,7 +13,7 @@ class CollectionController { } var jsonExpanded = newCollection.toJSONExpanded(this.db.audiobooks) await this.db.insertEntity('collection', newCollection) - this.clientEmitter(req.user.id, 'collection_added', jsonExpanded) + this.emitter('collection_added', jsonExpanded) res.json(jsonExpanded) } @@ -40,7 +40,7 @@ class CollectionController { var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks) if (wasUpdated) { await this.db.updateEntity('collection', collection) - this.clientEmitter(req.user.id, 'collection_updated', jsonExpanded) + this.emitter('collection_updated', jsonExpanded) } res.json(jsonExpanded) } @@ -52,7 +52,7 @@ class CollectionController { } var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks) await this.db.removeEntity('collection', collection.id) - this.clientEmitter(req.user.id, 'collection_removed', jsonExpanded) + this.emitter('collection_removed', jsonExpanded) res.sendStatus(200) } @@ -74,7 +74,7 @@ class CollectionController { collection.addBook(req.body.id) var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks) await this.db.updateEntity('collection', collection) - this.clientEmitter(req.user.id, 'collection_updated', jsonExpanded) + this.emitter('collection_updated', jsonExpanded) res.json(jsonExpanded) } @@ -89,7 +89,55 @@ class CollectionController { collection.removeBook(req.params.bookId) var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks) await this.db.updateEntity('collection', collection) - this.clientEmitter(req.user.id, 'collection_updated', jsonExpanded) + this.emitter('collection_updated', jsonExpanded) + } + res.json(collection.toJSONExpanded(this.db.audiobooks)) + } + + // POST: api/collections/:id/batch/add + async addBatch(req, res) { + var collection = this.db.collections.find(c => c.id === req.params.id) + if (!collection) { + return res.status(404).send('Collection not found') + } + if (!req.body.books || !req.body.books.length) { + return res.status(500).send('Invalid request body') + } + var bookIdsToAdd = req.body.books + var hasUpdated = false + for (let i = 0; i < bookIdsToAdd.length; i++) { + if (!collection.books.includes(bookIdsToAdd[i])) { + collection.addBook(bookIdsToAdd[i]) + hasUpdated = true + } + } + if (hasUpdated) { + await this.db.updateEntity('collection', collection) + this.emitter('collection_updated', collection.toJSONExpanded(this.db.audiobooks)) + } + res.json(collection.toJSONExpanded(this.db.audiobooks)) + } + + // POST: api/collections/:id/batch/remove + async removeBatch(req, res) { + var collection = this.db.collections.find(c => c.id === req.params.id) + if (!collection) { + return res.status(404).send('Collection not found') + } + if (!req.body.books || !req.body.books.length) { + return res.status(500).send('Invalid request body') + } + var bookIdsToRemove = req.body.books + var hasUpdated = false + for (let i = 0; i < bookIdsToRemove.length; i++) { + if (collection.books.includes(bookIdsToRemove[i])) { + collection.removeBook(bookIdsToRemove[i]) + hasUpdated = true + } + } + if (hasUpdated) { + await this.db.updateEntity('collection', collection) + this.emitter('collection_updated', collection.toJSONExpanded(this.db.audiobooks)) } res.json(collection.toJSONExpanded(this.db.audiobooks)) } diff --git a/server/scanner/AudioFileScanner.js b/server/scanner/AudioFileScanner.js index b52a1415..718dcb65 100644 --- a/server/scanner/AudioFileScanner.js +++ b/server/scanner/AudioFileScanner.js @@ -149,15 +149,14 @@ class AudioFileScanner { } } - if (hasUpdated) { - audiobook.rebuildTracks() - } - // Set book details from audio file ID3 tags, optional prefer if (audiobook.setDetailsFromFileMetadata(preferAudioMetadata)) { hasUpdated = true } + if (hasUpdated) { + audiobook.rebuildTracks() + } } return hasUpdated }