mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2024-12-20 19:06:06 +01:00
Refactor RssFeedManager.init to use new model only
This commit is contained in:
parent
302b651e7b
commit
837a180dc1
@ -11,43 +11,44 @@ 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({
|
||||||
|
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) {
|
for (const feed of feeds) {
|
||||||
// Remove invalid feeds
|
if (!feed.entity) {
|
||||||
if (!(await this.validateFeedEntity(feed))) {
|
Logger.error(`[RssFeedManager] Removing feed "${feed.title}". Entity not found`)
|
||||||
await Database.removeFeed(feed.id)
|
feedIdsToRemove.push(feed.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (feedIdsToRemove.length) {
|
||||||
|
Logger.info(`[RssFeedManager] Removing ${feedIdsToRemove.length} invalid feeds`)
|
||||||
|
await Database.feedModel.destroy({
|
||||||
|
where: {
|
||||||
|
id: feedIdsToRemove
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user