Change:Bookshelf to return minified audiobook objects and limit 100 #255

This commit is contained in:
advplyr 2021-12-24 16:37:57 -06:00
parent 18fa7a0b10
commit 3f8551f9a1
6 changed files with 31 additions and 26 deletions

View File

@ -85,7 +85,7 @@ export default {
}, },
async fetchCategories() { async fetchCategories() {
var categories = await this.$axios var categories = await this.$axios
.$get(`/api/libraries/${this.currentLibraryId}/categories`) .$get(`/api/libraries/${this.currentLibraryId}/categories?minified=1`)
.then((data) => { .then((data) => {
return data return data
}) })

View File

@ -54,7 +54,7 @@ export default {
pageLoadQueue: [], pageLoadQueue: [],
isFetchingEntities: false, isFetchingEntities: false,
scrollTimeout: null, scrollTimeout: null,
booksPerFetch: 250, booksPerFetch: 100,
totalShelves: 0, totalShelves: 0,
bookshelfMarginLeft: 0, bookshelfMarginLeft: 0,
isSelectionMode: false, isSelectionMode: false,
@ -220,7 +220,7 @@ export default {
var entityPath = this.entityName === 'books' ? `books/all` : this.entityName var entityPath = this.entityName === 'books' ? `books/all` : this.entityName
if (this.entityName === 'series-books') entityPath = `series/${this.seriesId}` if (this.entityName === 'series-books') entityPath = `series/${this.seriesId}`
var sfQueryString = this.currentSFQueryString ? this.currentSFQueryString + '&' : '' var sfQueryString = this.currentSFQueryString ? this.currentSFQueryString + '&' : ''
var fullQueryString = this.entityName === 'series-books' ? '' : `?${sfQueryString}limit=${this.booksPerFetch}&page=${page}` var fullQueryString = this.entityName === 'series-books' ? '' : `?${sfQueryString}limit=${this.booksPerFetch}&page=${page}&minified=1`
var payload = await this.$axios.$get(`/api/libraries/${this.currentLibraryId}/${entityPath}${fullQueryString}`).catch((error) => { var payload = await this.$axios.$get(`/api/libraries/${this.currentLibraryId}/${entityPath}${fullQueryString}`).catch((error) => {
console.error('failed to fetch books', error) console.error('failed to fetch books', error)
return null return null
@ -438,7 +438,7 @@ export default {
var entitiesPerShelfBefore = this.entitiesPerShelf var entitiesPerShelfBefore = this.entitiesPerShelf
var { clientHeight, clientWidth } = bookshelf var { clientHeight, clientWidth } = bookshelf
console.log('Init bookshelf width', clientWidth, 'window width', window.innerWidth) // console.log('Init bookshelf width', clientWidth, 'window width', window.innerWidth)
this.mountWindowWidth = window.innerWidth this.mountWindowWidth = window.innerWidth
this.bookshelfHeight = clientHeight this.bookshelfHeight = clientHeight
this.bookshelfWidth = clientWidth this.bookshelfWidth = clientWidth

View File

@ -124,7 +124,8 @@ class LibraryController {
page: req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0, page: req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0,
sortBy: req.query.sort, sortBy: req.query.sort,
sortDesc: req.query.desc === '1', sortDesc: req.query.desc === '1',
filterBy: req.query.filter filterBy: req.query.filter,
minified: req.query.minified === '1'
} }
if (payload.filterBy) { if (payload.filterBy) {
@ -147,7 +148,7 @@ class LibraryController {
var startIndex = payload.page * payload.limit var startIndex = payload.page * payload.limit
audiobooks = audiobooks.slice(startIndex, startIndex + payload.limit) audiobooks = audiobooks.slice(startIndex, startIndex + payload.limit)
} }
payload.results = audiobooks.map(ab => ab.toJSONExpanded()) payload.results = audiobooks.map(ab => payload.minified ? ab.toJSONMinified() : ab.toJSONExpanded())
res.json(payload) res.json(payload)
} }
@ -162,10 +163,11 @@ class LibraryController {
page: req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0, page: req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0,
sortBy: req.query.sort, sortBy: req.query.sort,
sortDesc: req.query.desc === '1', sortDesc: req.query.desc === '1',
filterBy: req.query.filter filterBy: req.query.filter,
minified: req.query.minified === '1'
} }
var series = libraryHelpers.getSeriesFromBooks(audiobooks) var series = libraryHelpers.getSeriesFromBooks(audiobooks, payload.minified)
payload.total = series.length payload.total = series.length
if (payload.limit) { if (payload.limit) {
@ -208,10 +210,11 @@ class LibraryController {
page: req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0, page: req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0,
sortBy: req.query.sort, sortBy: req.query.sort,
sortDesc: req.query.desc === '1', sortDesc: req.query.desc === '1',
filterBy: req.query.filter filterBy: req.query.filter,
minified: req.query.minified === '1'
} }
var collections = this.db.collections.filter(c => c.libraryId === req.library.id).map(c => c.toJSONExpanded(audiobooks)) var collections = this.db.collections.filter(c => c.libraryId === req.library.id).map(c => c.toJSONExpanded(audiobooks, payload.minified))
payload.total = collections.length payload.total = collections.length
if (payload.limit) { if (payload.limit) {
@ -235,28 +238,29 @@ class LibraryController {
var library = req.library var library = req.library
var books = this.db.audiobooks.filter(ab => ab.libraryId === library.id) var books = this.db.audiobooks.filter(ab => ab.libraryId === library.id)
var limitPerShelf = req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 12 var limitPerShelf = req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 12
var minified = req.query.minified === '1'
var booksWithUserAb = libraryHelpers.getBooksWithUserAudiobook(req.user, books) var booksWithUserAb = libraryHelpers.getBooksWithUserAudiobook(req.user, books)
var series = libraryHelpers.getSeriesFromBooks(books) var series = libraryHelpers.getSeriesFromBooks(books, minified)
var categories = [ var categories = [
{ {
id: 'continue-reading', id: 'continue-reading',
label: 'Continue Reading', label: 'Continue Reading',
type: 'books', type: 'books',
entities: libraryHelpers.getBooksMostRecentlyRead(booksWithUserAb, limitPerShelf) entities: libraryHelpers.getBooksMostRecentlyRead(booksWithUserAb, limitPerShelf, minified)
}, },
{ {
id: 'recently-added', id: 'recently-added',
label: 'Recently Added', label: 'Recently Added',
type: 'books', type: 'books',
entities: libraryHelpers.getBooksMostRecentlyAdded(books, limitPerShelf) entities: libraryHelpers.getBooksMostRecentlyAdded(books, limitPerShelf, minified)
}, },
{ {
id: 'read-again', id: 'read-again',
label: 'Read Again', label: 'Read Again',
type: 'books', type: 'books',
entities: libraryHelpers.getBooksMostRecentlyFinished(booksWithUserAb, limitPerShelf) entities: libraryHelpers.getBooksMostRecentlyFinished(booksWithUserAb, limitPerShelf, minified)
}, },
{ {
id: 'recent-series', id: 'recent-series',

View File

@ -211,7 +211,7 @@ class Audiobook {
ebooks: this.ebooks.map(ebook => ebook.toJSON()), ebooks: this.ebooks.map(ebook => ebook.toJSON()),
numEbooks: this.ebooks.length, numEbooks: this.ebooks.length,
numTracks: this.tracks.length, numTracks: this.tracks.length,
chapters: this.chapters || [], numChapters: (this.chapters || []).length,
isMissing: !!this.isMissing, isMissing: !!this.isMissing,
isInvalid: !!this.isInvalid, isInvalid: !!this.isInvalid,
hasMissingParts: this.numMissingParts, hasMissingParts: this.numMissingParts,

View File

@ -37,11 +37,11 @@ class UserCollection {
} }
} }
toJSONExpanded(audiobooks) { toJSONExpanded(audiobooks, minifiedBooks = false) {
var json = this.toJSON() var json = this.toJSON()
json.books = json.books.map(bookId => { json.books = json.books.map(bookId => {
var _ab = audiobooks.find(ab => ab.id === bookId) var _ab = audiobooks.find(ab => ab.id === bookId)
return _ab ? _ab.toJSONExpanded() : null return _ab ? minifiedBooks ? _ab.toJSONMinified() : _ab.toJSONExpanded() : null
}).filter(b => !!b) }).filter(b => !!b)
return json return json
} }

View File

@ -74,19 +74,20 @@ module.exports = {
return data return data
}, },
getSeriesFromBooks(books) { getSeriesFromBooks(books, minified = false) {
var _series = {} var _series = {}
books.forEach((audiobook) => { books.forEach((audiobook) => {
if (audiobook.book.series) { if (audiobook.book.series) {
var abJson = minified ? audiobook.toJSONMinified() : audiobook.toJSONExpanded()
if (!_series[audiobook.book.series]) { if (!_series[audiobook.book.series]) {
_series[audiobook.book.series] = { _series[audiobook.book.series] = {
id: audiobook.book.series, id: audiobook.book.series,
name: audiobook.book.series, name: audiobook.book.series,
type: 'series', type: 'series',
books: [audiobook.toJSONExpanded()] books: [abJson]
} }
} else { } else {
_series[audiobook.book.series].books.push(audiobook.toJSONExpanded()) _series[audiobook.book.series].books.push(abJson)
} }
} }
}) })
@ -108,25 +109,25 @@ module.exports = {
}) })
}, },
getBooksMostRecentlyRead(booksWithUserAb, limit) { getBooksMostRecentlyRead(booksWithUserAb, limit, minified = false) {
var booksWithProgress = booksWithUserAb.filter((data) => data.userAudiobook && data.userAudiobook.progress > 0 && !data.userAudiobook.isRead) var booksWithProgress = booksWithUserAb.filter((data) => data.userAudiobook && data.userAudiobook.progress > 0 && !data.userAudiobook.isRead)
booksWithProgress.sort((a, b) => { booksWithProgress.sort((a, b) => {
return b.userAudiobook.lastUpdate - a.userAudiobook.lastUpdate return b.userAudiobook.lastUpdate - a.userAudiobook.lastUpdate
}) })
return booksWithProgress.map(b => b.book.toJSONExpanded()).slice(0, limit) return booksWithProgress.map(b => minified ? b.book.toJSONMinified() : b.book.toJSONExpanded()).slice(0, limit)
}, },
getBooksMostRecentlyAdded(books, limit) { getBooksMostRecentlyAdded(books, limit, minified = false) {
var booksSortedByAddedAt = sort(books).desc(book => book.addedAt) var booksSortedByAddedAt = sort(books).desc(book => book.addedAt)
return booksSortedByAddedAt.map(b => b.toJSONExpanded()).slice(0, limit) return booksSortedByAddedAt.map(b => minified ? b.toJSONMinified() : b.toJSONExpanded()).slice(0, limit)
}, },
getBooksMostRecentlyFinished(booksWithUserAb, limit) { getBooksMostRecentlyFinished(booksWithUserAb, limit, minified = false) {
var booksRead = booksWithUserAb.filter((data) => data.userAudiobook && data.userAudiobook.isRead) var booksRead = booksWithUserAb.filter((data) => data.userAudiobook && data.userAudiobook.isRead)
booksRead.sort((a, b) => { booksRead.sort((a, b) => {
return b.userAudiobook.finishedAt - a.userAudiobook.finishedAt return b.userAudiobook.finishedAt - a.userAudiobook.finishedAt
}) })
return booksRead.map(b => b.book.toJSONExpanded()).slice(0, limit) return booksRead.map(b => minified ? b.book.toJSONMinified() : b.book.toJSONExpanded()).slice(0, limit)
}, },
getSeriesMostRecentlyAdded(series, limit) { getSeriesMostRecentlyAdded(series, limit) {