mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-12 20:31:36 +00:00
Limit collections to return only 2 items when getting list of ALL collections (otherwise, return the entire collection)
This commit is contained in:
parent
36f46072fd
commit
44ff2e7a99
2 changed files with 15 additions and 10 deletions
|
|
@ -492,7 +492,7 @@ class LibraryController {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Create paginated queries
|
// TODO: Create paginated queries
|
||||||
let collections = await Database.collectionModel.getOldCollectionsJsonExpanded(req.user, req.library.id, include)
|
let collections = await Database.collectionModel.getOldCollectionsJsonExpanded(req.user, req.library.id, include, 2)
|
||||||
|
|
||||||
payload.total = collections.length
|
payload.total = collections.length
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,9 +40,10 @@ class Collection extends Model {
|
||||||
* @param {[oldUser]} user
|
* @param {[oldUser]} user
|
||||||
* @param {[string]} libraryId
|
* @param {[string]} libraryId
|
||||||
* @param {[string[]]} include
|
* @param {[string[]]} include
|
||||||
|
* @param {[number]} limitCollectionSize
|
||||||
* @returns {Promise<object[]>} oldCollection.toJSONExpanded
|
* @returns {Promise<object[]>} oldCollection.toJSONExpanded
|
||||||
*/
|
*/
|
||||||
static async getOldCollectionsJsonExpanded(user, libraryId, include) {
|
static async getOldCollectionsJsonExpanded(user, libraryId, include, limitCollectionSize) {
|
||||||
let collectionWhere = null
|
let collectionWhere = null
|
||||||
if (libraryId) {
|
if (libraryId) {
|
||||||
collectionWhere = {
|
collectionWhere = {
|
||||||
|
|
@ -88,7 +89,7 @@ class Collection extends Model {
|
||||||
})
|
})
|
||||||
// TODO: Handle user permission restrictions on initial query
|
// TODO: Handle user permission restrictions on initial query
|
||||||
return collections.map(c => {
|
return collections.map(c => {
|
||||||
const oldCollection = this.getOldCollection(c)
|
const oldCollection = this.getOldCollection(c, limitCollectionSize)
|
||||||
|
|
||||||
// Filter books using user permissions
|
// Filter books using user permissions
|
||||||
const books = c.books?.filter(b => {
|
const books = c.books?.filter(b => {
|
||||||
|
|
@ -199,11 +200,15 @@ class Collection extends Model {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get old collection from Collection
|
* Get old collection from Collection
|
||||||
* @param {Collection} collectionExpanded
|
* @param {Collection} collectionExpanded
|
||||||
|
* @param {number} limitCollectionSize
|
||||||
* @returns {oldCollection}
|
* @returns {oldCollection}
|
||||||
*/
|
*/
|
||||||
static getOldCollection(collectionExpanded) {
|
static getOldCollection(collectionExpanded, limitCollectionSize) {
|
||||||
const libraryItemIds = collectionExpanded.books?.map(b => b.libraryItem?.id || null).filter(lid => lid) || []
|
let libraryItemIds = collectionExpanded.books?.map(b => b.libraryItem?.id || null).filter(lid => lid) || []
|
||||||
|
if (limitCollectionSize) {
|
||||||
|
libraryItemIds = libraryItemIds.slice(0,limitCollectionSize)
|
||||||
|
}
|
||||||
return new oldCollection({
|
return new oldCollection({
|
||||||
id: collectionExpanded.id,
|
id: collectionExpanded.id,
|
||||||
libraryId: collectionExpanded.libraryId,
|
libraryId: collectionExpanded.libraryId,
|
||||||
|
|
@ -239,7 +244,7 @@ class Collection extends Model {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get old collection by id
|
* Get old collection by id
|
||||||
* @param {string} collectionId
|
* @param {string} collectionId
|
||||||
* @returns {Promise<oldCollection|null>} returns null if not found
|
* @returns {Promise<oldCollection|null>} returns null if not found
|
||||||
*/
|
*/
|
||||||
static async getOldById(collectionId) {
|
static async getOldById(collectionId) {
|
||||||
|
|
@ -287,7 +292,7 @@ class Collection extends Model {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove all collections belonging to library
|
* Remove all collections belonging to library
|
||||||
* @param {string} libraryId
|
* @param {string} libraryId
|
||||||
* @returns {Promise<number>} number of collections destroyed
|
* @returns {Promise<number>} number of collections destroyed
|
||||||
*/
|
*/
|
||||||
static async removeAllForLibrary(libraryId) {
|
static async removeAllForLibrary(libraryId) {
|
||||||
|
|
@ -316,7 +321,7 @@ class Collection extends Model {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize model
|
* Initialize model
|
||||||
* @param {import('../Database').sequelize} sequelize
|
* @param {import('../Database').sequelize} sequelize
|
||||||
*/
|
*/
|
||||||
static init(sequelize) {
|
static init(sequelize) {
|
||||||
super.init({
|
super.init({
|
||||||
|
|
@ -339,4 +344,4 @@ class Collection extends Model {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Collection
|
module.exports = Collection
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue