From 837a180dc1eb35fabbf03f1e068a73fdab5652cc Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 15 Dec 2024 13:14:55 -0600 Subject: [PATCH] Refactor RssFeedManager.init to use new model only --- server/managers/RssFeedManager.js | 63 ++++++++++++++++--------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/server/managers/RssFeedManager.js b/server/managers/RssFeedManager.js index da0d8dd2..e92c3259 100644 --- a/server/managers/RssFeedManager.js +++ b/server/managers/RssFeedManager.js @@ -11,43 +11,44 @@ const libraryItemsBookFilters = require('../utils/queries/libraryItemsBookFilter class RssFeedManager { constructor() {} - async validateFeedEntity(feedObj) { - if (feedObj.entityType === 'collection') { - const collection = await Database.collectionModel.getOldById(feedObj.entityId) - if (!collection) { - Logger.error(`[RssFeedManager] Removing feed "${feedObj.id}". Collection "${feedObj.entityId}" not found`) - return false - } - } else if (feedObj.entityType === 'libraryItem') { - const libraryItemExists = await Database.libraryItemModel.checkExistsById(feedObj.entityId) - if (!libraryItemExists) { - Logger.error(`[RssFeedManager] Removing feed "${feedObj.id}". Library item "${feedObj.entityId}" not found`) - return false - } - } else if (feedObj.entityType === 'series') { - const series = await Database.seriesModel.findByPk(feedObj.entityId) - if (!series) { - Logger.error(`[RssFeedManager] Removing feed "${feedObj.id}". Series "${feedObj.entityId}" not found`) - return false - } - } else { - Logger.error(`[RssFeedManager] Removing feed "${feedObj.id}". Invalid entityType "${feedObj.entityType}"`) - return false - } - return true - } - /** - * Validate all feeds and remove invalid + * Remove invalid feeds (invalid if the entity does not exist) */ async init() { - const feeds = await Database.feedModel.getOldFeeds() + const feeds = await Database.feedModel.findAll({ + attributes: ['id', 'entityId', 'entityType', 'title'], + include: [ + { + model: Database.libraryItemModel, + attributes: ['id'] + }, + { + model: Database.collectionModel, + attributes: ['id'] + }, + { + model: Database.seriesModel, + attributes: ['id'] + } + ] + }) + + const feedIdsToRemove = [] for (const feed of feeds) { - // Remove invalid feeds - if (!(await this.validateFeedEntity(feed))) { - await Database.removeFeed(feed.id) + if (!feed.entity) { + Logger.error(`[RssFeedManager] Removing feed "${feed.title}". Entity not found`) + feedIdsToRemove.push(feed.id) } } + + if (feedIdsToRemove.length) { + Logger.info(`[RssFeedManager] Removing ${feedIdsToRemove.length} invalid feeds`) + await Database.feedModel.destroy({ + where: { + id: feedIdsToRemove + } + }) + } } /**