Update get all libraries endpoint to support include stats for android auto to detect audio libraries

This commit is contained in:
advplyr 2025-01-11 14:26:57 -06:00
parent c819afc53b
commit 41daf557aa

View File

@ -170,21 +170,34 @@ class LibraryController {
* GET: /api/libraries * GET: /api/libraries
* Get all libraries * Get all libraries
* *
* ?include=stats to load library stats - used in android auto to filter out libraries with no audio
*
* @param {RequestWithUser} req * @param {RequestWithUser} req
* @param {Response} res * @param {Response} res
*/ */
async findAll(req, res) { async findAll(req, res) {
const libraries = await Database.libraryModel.getAllWithFolders() let libraries = await Database.libraryModel.getAllWithFolders()
const librariesAccessible = req.user.permissions?.librariesAccessible || [] const librariesAccessible = req.user.permissions?.librariesAccessible || []
if (librariesAccessible.length) { if (librariesAccessible.length) {
return res.json({ libraries = libraries.filter((lib) => librariesAccessible.includes(lib.id))
libraries: libraries.filter((lib) => librariesAccessible.includes(lib.id)).map((lib) => lib.toOldJSON()) }
})
libraries = libraries.map((lib) => lib.toOldJSON())
const includeArray = (req.query.include || '').split(',')
if (includeArray.includes('stats')) {
for (const library of libraries) {
if (library.mediaType === 'book') {
library.stats = await libraryItemsBookFilters.getBookLibraryStats(library.id)
} else if (library.mediaType === 'podcast') {
library.stats = await libraryItemsPodcastFilters.getPodcastLibraryStats(library.id)
}
}
} }
res.json({ res.json({
libraries: libraries.map((lib) => lib.toOldJSON()) libraries
}) })
} }