From 5ec8406653e49b98d3b119c046966922cec77939 Mon Sep 17 00:00:00 2001 From: advplyr Date: Wed, 4 Sep 2024 18:00:59 -0500 Subject: [PATCH] Cleanup Collection model to remove oldCollection references --- server/models/Collection.js | 205 ++++++++++++++++++------------------ 1 file changed, 103 insertions(+), 102 deletions(-) diff --git a/server/models/Collection.js b/server/models/Collection.js index dcc86e5a..f2950d4e 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -115,78 +115,6 @@ class Collection extends Model { .filter((c) => c) } - /** - * Get old collection toJSONExpanded, items filtered for user permissions - * - * @param {import('./User')|null} user - * @param {string[]} [include] - * @returns {Promise} oldCollection.toJSONExpanded - */ - async getOldJsonExpanded(user, include) { - this.books = - (await this.getBooks({ - include: [ - { - model: this.sequelize.models.libraryItem - }, - { - model: this.sequelize.models.author, - through: { - attributes: [] - } - }, - { - model: this.sequelize.models.series, - through: { - attributes: ['sequence'] - } - } - ], - order: [Sequelize.literal('`collectionBook.order` ASC')] - })) || [] - - const oldCollection = this.sequelize.models.collection.getOldCollection(this) - - // Filter books using user permissions - // TODO: Handle user permission restrictions on initial query - const books = - this.books?.filter((b) => { - if (user) { - if (b.tags?.length && !user.checkCanAccessLibraryItemWithTags(b.tags)) { - return false - } - if (b.explicit === true && !user.canAccessExplicitContent) { - return false - } - } - return true - }) || [] - - // Map to library items - const libraryItems = books.map((b) => { - const libraryItem = b.libraryItem - delete b.libraryItem - libraryItem.media = b - return this.sequelize.models.libraryItem.getOldLibraryItem(libraryItem) - }) - - // Users with restricted permissions will not see this collection - if (!books.length && oldCollection.books.length) { - return null - } - - const collectionExpanded = oldCollection.toJSONExpanded(libraryItems) - - if (include?.includes('rssfeed')) { - const feeds = await this.getFeeds() - if (feeds?.length) { - collectionExpanded.rssFeed = this.sequelize.models.feed.getOldFeed(feeds[0]) - } - } - - return collectionExpanded - } - /** * Get old collection from Collection * @param {Collection} collectionExpanded @@ -250,36 +178,6 @@ class Collection extends Model { return this.getOldCollection(collection) } - /** - * Get old collection from current - * @returns {Promise} - */ - async getOld() { - this.books = - (await this.getBooks({ - include: [ - { - model: this.sequelize.models.libraryItem - }, - { - model: this.sequelize.models.author, - through: { - attributes: [] - } - }, - { - model: this.sequelize.models.series, - through: { - attributes: ['sequence'] - } - } - ], - order: [Sequelize.literal('`collectionBook.order` ASC')] - })) || [] - - return this.sequelize.models.collection.getOldCollection(this) - } - /** * Remove all collections belonging to library * @param {string} libraryId @@ -320,6 +218,109 @@ class Collection extends Model { library.hasMany(Collection) Collection.belongsTo(library) } + + /** + * Get old collection toJSONExpanded, items filtered for user permissions + * + * @param {import('./User')|null} user + * @param {string[]} [include] + * @returns {Promise} oldCollection.toJSONExpanded + */ + async getOldJsonExpanded(user, include) { + this.books = + (await this.getBooks({ + include: [ + { + model: this.sequelize.models.libraryItem + }, + { + model: this.sequelize.models.author, + through: { + attributes: [] + } + }, + { + model: this.sequelize.models.series, + through: { + attributes: ['sequence'] + } + } + ], + order: [Sequelize.literal('`collectionBook.order` ASC')] + })) || [] + + // Filter books using user permissions + // TODO: Handle user permission restrictions on initial query + const books = + this.books?.filter((b) => { + if (user) { + if (b.tags?.length && !user.checkCanAccessLibraryItemWithTags(b.tags)) { + return false + } + if (b.explicit === true && !user.canAccessExplicitContent) { + return false + } + } + return true + }) || [] + + // Map to library items + const libraryItems = books.map((b) => { + const libraryItem = b.libraryItem + delete b.libraryItem + libraryItem.media = b + return this.sequelize.models.libraryItem.getOldLibraryItem(libraryItem) + }) + + // Users with restricted permissions will not see this collection + if (!books.length && this.books.length) { + return null + } + + const collectionExpanded = this.toOldJSONExpanded(libraryItems) + + if (include?.includes('rssfeed')) { + const feeds = await this.getFeeds() + if (feeds?.length) { + collectionExpanded.rssFeed = this.sequelize.models.feed.getOldFeed(feeds[0]) + } + } + + return collectionExpanded + } + + /** + * + * @param {string[]} libraryItemIds + * @returns + */ + toOldJSON(libraryItemIds) { + return { + id: this.id, + libraryId: this.libraryId, + name: this.name, + description: this.description, + books: [...libraryItemIds], + lastUpdate: this.updatedAt.valueOf(), + createdAt: this.createdAt.valueOf() + } + } + + /** + * + * @param {import('../objects/LibraryItem')} oldLibraryItems + * @returns + */ + toOldJSONExpanded(oldLibraryItems) { + const json = this.toOldJSON(oldLibraryItems.map((li) => li.id)) + json.books = json.books + .map((libraryItemId) => { + const book = oldLibraryItems.find((li) => li.id === libraryItemId) + return book ? book.toJSONExpanded() : null + }) + .filter((b) => !!b) + return json + } } module.exports = Collection