Add:Batch add/remove books from collection

This commit is contained in:
advplyr 2021-11-27 16:01:53 -06:00
parent b76e3e4c54
commit 448514af9e
7 changed files with 166 additions and 42 deletions

View File

@ -48,6 +48,9 @@
<ui-tooltip :text="`Mark as ${selectedIsRead ? 'Not Read' : 'Read'}`" direction="bottom"> <ui-tooltip :text="`Mark as ${selectedIsRead ? 'Not Read' : 'Read'}`" direction="bottom">
<ui-read-icon-btn :disabled="processingBatch" :is-read="selectedIsRead" @click="toggleBatchRead" class="mx-1.5" /> <ui-read-icon-btn :disabled="processingBatch" :is-read="selectedIsRead" @click="toggleBatchRead" class="mx-1.5" />
</ui-tooltip> </ui-tooltip>
<ui-tooltip text="Add to Collection" direction="bottom">
<ui-icon-btn :disabled="processingBatch" icon="collections_bookmark" @click="batchAddToCollectionClick" class="mx-1.5" />
</ui-tooltip>
<template v-if="userCanUpdate"> <template v-if="userCanUpdate">
<ui-icon-btn v-show="!processingBatchDelete" icon="edit" bg-color="warning" class="mx-1.5" @click="batchEditClick" /> <ui-icon-btn v-show="!processingBatchDelete" icon="edit" bg-color="warning" class="mx-1.5" @click="batchEditClick" />
</template> </template>
@ -196,6 +199,9 @@ export default {
}, },
batchEditClick() { batchEditClick() {
this.$router.push('/batch') this.$router.push('/batch')
},
batchAddToCollectionClick() {
this.$store.commit('globals/setShowBatchUserCollectionsModal', true)
} }
}, },
mounted() {} mounted() {}

View File

@ -9,7 +9,8 @@
<div ref="container" class="w-full rounded-lg bg-primary box-shadow-md overflow-y-auto overflow-x-hidden" style="max-height: 80vh"> <div ref="container" class="w-full rounded-lg bg-primary box-shadow-md overflow-y-auto overflow-x-hidden" style="max-height: 80vh">
<div v-if="show" class="w-full h-full"> <div v-if="show" class="w-full h-full">
<div class="py-4 px-4"> <div class="py-4 px-4">
<h1 class="text-2xl">Add to Collection</h1> <h1 v-if="!showBatchUserCollectionModal" class="text-2xl">Add to Collection</h1>
<h1 v-else class="text-2xl">Add {{ selectedBooks.length }} Books to Collection</h1>
</div> </div>
<div class="w-full overflow-y-auto overflow-x-hidden max-h-96"> <div class="w-full overflow-y-auto overflow-x-hidden max-h-96">
<transition-group name="list-complete" tag="div"> <transition-group name="list-complete" tag="div">
@ -63,6 +64,14 @@ export default {
} }
}, },
title() { 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 : '' return this.selectedAudiobook ? this.selectedAudiobook.book.title : ''
}, },
selectedAudiobook() { selectedAudiobook() {
@ -77,13 +86,32 @@ export default {
sortedCollections() { sortedCollections() {
return this.collections return this.collections
.map((c) => { .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 { return {
isBookIncluded: includesBook, isBookIncluded: includesBook,
...c ...c
} }
}) })
.sort((a, b) => (a.isBookIncluded ? -1 : 1)) .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: { methods: {
@ -91,10 +119,25 @@ export default {
this.$store.dispatch('user/loadUserCollections') this.$store.dispatch('user/loadUserCollections')
}, },
removeFromCollection(collection) { removeFromCollection(collection) {
if (!this.selectedAudiobookId) return if (!this.selectedAudiobookId && !this.selectedBookIds.length) return
this.processing = true this.processing = true
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 this.$axios
.$delete(`/api/collections/${collection.id}/book/${this.selectedAudiobookId}`) .$delete(`/api/collections/${collection.id}/book/${this.selectedAudiobookId}`)
.then((updatedCollection) => { .then((updatedCollection) => {
@ -107,12 +150,29 @@ export default {
this.$toast.error('Failed to remove book from collection') this.$toast.error('Failed to remove book from collection')
this.processing = false this.processing = false
}) })
}
}, },
addToCollection(collection) { addToCollection(collection) {
if (!this.selectedAudiobookId) return if (!this.selectedAudiobookId && !this.selectedBookIds.length) return
this.processing = true this.processing = true
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 this.$axios
.$post(`/api/collections/${collection.id}/book`, { id: this.selectedAudiobookId }) .$post(`/api/collections/${collection.id}/book`, { id: this.selectedAudiobookId })
.then((updatedCollection) => { .then((updatedCollection) => {
@ -125,14 +185,17 @@ export default {
this.$toast.error('Failed to add book to collection') this.$toast.error('Failed to add book to collection')
this.processing = false this.processing = false
}) })
}
}, },
submitCreateCollection() { submitCreateCollection() {
if (!this.newCollectionName || !this.selectedAudiobook) { if (!this.newCollectionName || (!this.selectedAudiobookId && !this.selectedBookIds.length)) {
return return
} }
this.processing = true this.processing = true
var books = this.showBatchUserCollectionModal ? this.selectedBookIds : [this.selectedAudiobookId]
var newCollection = { var newCollection = {
books: [this.selectedAudiobook.id], books: books,
libraryId: this.selectedAudiobook.libraryId, libraryId: this.selectedAudiobook.libraryId,
name: this.newCollectionName name: this.newCollectionName
} }

View File

@ -1,5 +1,6 @@
export const state = () => ({ export const state = () => ({
showBatchUserCollectionModal: false,
showUserCollectionsModal: false, showUserCollectionsModal: false,
showEditCollectionModal: false, showEditCollectionModal: false,
selectedCollection: null selectedCollection: null
@ -15,6 +16,11 @@ export const actions = {
export const mutations = { export const mutations = {
setShowUserCollectionsModal(state, val) { setShowUserCollectionsModal(state, val) {
state.showBatchUserCollectionModal = false
state.showUserCollectionsModal = val
},
setShowBatchUserCollectionsModal(state, val) {
state.showBatchUserCollectionModal = true
state.showUserCollectionsModal = val state.showUserCollectionsModal = val
}, },
setShowEditCollectionModal(state, val) { setShowEditCollectionModal(state, val) {

View File

@ -10,7 +10,7 @@
"prod": "npm run client && npm install && node prod.js", "prod": "npm run client && npm install && node prod.js",
"build-win": "pkg -t node12-win-x64 -o ./dist/win/audiobookshelf .", "build-win": "pkg -t node12-win-x64 -o ./dist/win/audiobookshelf .",
"build-linux": "build/linuxpackager", "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", "bin": "prod.js",
"pkg": { "pkg": {

View File

@ -108,6 +108,8 @@ class ApiController {
this.router.post('/collections/:id/book', CollectionController.addBook.bind(this)) this.router.post('/collections/:id/book', CollectionController.addBook.bind(this))
this.router.delete('/collections/:id/book/:bookId', CollectionController.removeBook.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 // TEMP: Support old syntax for mobile app
this.router.get('/collection/:id', CollectionController.findOne.bind(this)) this.router.get('/collection/:id', CollectionController.findOne.bind(this))

View File

@ -13,7 +13,7 @@ class CollectionController {
} }
var jsonExpanded = newCollection.toJSONExpanded(this.db.audiobooks) var jsonExpanded = newCollection.toJSONExpanded(this.db.audiobooks)
await this.db.insertEntity('collection', newCollection) await this.db.insertEntity('collection', newCollection)
this.clientEmitter(req.user.id, 'collection_added', jsonExpanded) this.emitter('collection_added', jsonExpanded)
res.json(jsonExpanded) res.json(jsonExpanded)
} }
@ -40,7 +40,7 @@ class CollectionController {
var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks) var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks)
if (wasUpdated) { if (wasUpdated) {
await this.db.updateEntity('collection', collection) await this.db.updateEntity('collection', collection)
this.clientEmitter(req.user.id, 'collection_updated', jsonExpanded) this.emitter('collection_updated', jsonExpanded)
} }
res.json(jsonExpanded) res.json(jsonExpanded)
} }
@ -52,7 +52,7 @@ class CollectionController {
} }
var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks) var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks)
await this.db.removeEntity('collection', collection.id) await this.db.removeEntity('collection', collection.id)
this.clientEmitter(req.user.id, 'collection_removed', jsonExpanded) this.emitter('collection_removed', jsonExpanded)
res.sendStatus(200) res.sendStatus(200)
} }
@ -74,7 +74,7 @@ class CollectionController {
collection.addBook(req.body.id) collection.addBook(req.body.id)
var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks) var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks)
await this.db.updateEntity('collection', collection) await this.db.updateEntity('collection', collection)
this.clientEmitter(req.user.id, 'collection_updated', jsonExpanded) this.emitter('collection_updated', jsonExpanded)
res.json(jsonExpanded) res.json(jsonExpanded)
} }
@ -89,7 +89,55 @@ class CollectionController {
collection.removeBook(req.params.bookId) collection.removeBook(req.params.bookId)
var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks) var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks)
await this.db.updateEntity('collection', collection) 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)) res.json(collection.toJSONExpanded(this.db.audiobooks))
} }

View File

@ -149,15 +149,14 @@ class AudioFileScanner {
} }
} }
if (hasUpdated) {
audiobook.rebuildTracks()
}
// Set book details from audio file ID3 tags, optional prefer // Set book details from audio file ID3 tags, optional prefer
if (audiobook.setDetailsFromFileMetadata(preferAudioMetadata)) { if (audiobook.setDetailsFromFileMetadata(preferAudioMetadata)) {
hasUpdated = true hasUpdated = true
} }
if (hasUpdated) {
audiobook.rebuildTracks()
}
} }
return hasUpdated return hasUpdated
} }