Refactor RssFeedManager.init to use new model only

This commit is contained in:
advplyr 2024-12-15 13:14:55 -06:00
parent 302b651e7b
commit 837a180dc1

View File

@ -11,42 +11,43 @@ const libraryItemsBookFilters = require('../utils/queries/libraryItemsBookFilter
class RssFeedManager { class RssFeedManager {
constructor() {} 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() { async init() {
const feeds = await Database.feedModel.getOldFeeds() const feeds = await Database.feedModel.findAll({
for (const feed of feeds) { attributes: ['id', 'entityId', 'entityType', 'title'],
// Remove invalid feeds include: [
if (!(await this.validateFeedEntity(feed))) { {
await Database.removeFeed(feed.id) model: Database.libraryItemModel,
attributes: ['id']
},
{
model: Database.collectionModel,
attributes: ['id']
},
{
model: Database.seriesModel,
attributes: ['id']
} }
]
})
const feedIdsToRemove = []
for (const feed of feeds) {
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
}
})
} }
} }