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-read-icon-btn :disabled="processingBatch" :is-read="selectedIsRead" @click="toggleBatchRead" class="mx-1.5" />
</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">
<ui-icon-btn v-show="!processingBatchDelete" icon="edit" bg-color="warning" class="mx-1.5" @click="batchEditClick" />
</template>
@ -196,6 +199,9 @@ export default {
},
batchEditClick() {
this.$router.push('/batch')
},
batchAddToCollectionClick() {
this.$store.commit('globals/setShowBatchUserCollectionsModal', true)
}
},
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 v-if="show" class="w-full h-full">
<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 class="w-full overflow-y-auto overflow-x-hidden max-h-96">
<transition-group name="list-complete" tag="div">
@ -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
}

View File

@ -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) {

View File

@ -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": {

View File

@ -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))

View File

@ -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))
}

View File

@ -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
}