diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index 6b8b80e73..b2e2ca24d 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -492,7 +492,7 @@ class LibraryController { } // 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 diff --git a/server/models/Collection.js b/server/models/Collection.js index 9d3a8e0a1..c6de0aa76 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -40,9 +40,10 @@ class Collection extends Model { * @param {[oldUser]} user * @param {[string]} libraryId * @param {[string[]]} include + * @param {[number]} limitCollectionSize * @returns {Promise} oldCollection.toJSONExpanded */ - static async getOldCollectionsJsonExpanded(user, libraryId, include) { + static async getOldCollectionsJsonExpanded(user, libraryId, include, limitCollectionSize) { let collectionWhere = null if (libraryId) { collectionWhere = { @@ -88,7 +89,7 @@ class Collection extends Model { }) // TODO: Handle user permission restrictions on initial query return collections.map(c => { - const oldCollection = this.getOldCollection(c) + const oldCollection = this.getOldCollection(c, limitCollectionSize) // Filter books using user permissions const books = c.books?.filter(b => { @@ -199,11 +200,15 @@ class Collection extends Model { /** * Get old collection from Collection - * @param {Collection} collectionExpanded + * @param {Collection} collectionExpanded + * @param {number} limitCollectionSize * @returns {oldCollection} */ - static getOldCollection(collectionExpanded) { - const libraryItemIds = collectionExpanded.books?.map(b => b.libraryItem?.id || null).filter(lid => lid) || [] + static getOldCollection(collectionExpanded, limitCollectionSize) { + let libraryItemIds = collectionExpanded.books?.map(b => b.libraryItem?.id || null).filter(lid => lid) || [] + if (limitCollectionSize) { + libraryItemIds = libraryItemIds.slice(0,limitCollectionSize) + } return new oldCollection({ id: collectionExpanded.id, libraryId: collectionExpanded.libraryId, @@ -239,7 +244,7 @@ class Collection extends Model { /** * Get old collection by id - * @param {string} collectionId + * @param {string} collectionId * @returns {Promise} returns null if not found */ static async getOldById(collectionId) { @@ -287,7 +292,7 @@ class Collection extends Model { /** * Remove all collections belonging to library - * @param {string} libraryId + * @param {string} libraryId * @returns {Promise} number of collections destroyed */ static async removeAllForLibrary(libraryId) { @@ -316,7 +321,7 @@ class Collection extends Model { /** * Initialize model - * @param {import('../Database').sequelize} sequelize + * @param {import('../Database').sequelize} sequelize */ static init(sequelize) { super.init({ @@ -339,4 +344,4 @@ class Collection extends Model { } } -module.exports = Collection \ No newline at end of file +module.exports = Collection