Fix FeedEpisodes using a new ID when updating #3757

This commit is contained in:
advplyr 2025-01-01 11:32:39 -06:00
parent 8c4d0c503b
commit 5201625d38
3 changed files with 71 additions and 24 deletions

View file

@ -107,6 +107,9 @@ class Feed extends Model {
entityUpdatedAt = libraryItem.media.podcastEpisodes.reduce((mostRecent, episode) => {
return episode.updatedAt > mostRecent ? episode.updatedAt : mostRecent
}, entityUpdatedAt)
} else if (libraryItem.media.updatedAt > entityUpdatedAt) {
// Book feeds will use Book.updatedAt if more recent
entityUpdatedAt = libraryItem.media.updatedAt
}
const feedObj = {
@ -472,6 +475,8 @@ class Feed extends Model {
/** @type {typeof import('./FeedEpisode')} */
const feedEpisodeModel = this.sequelize.models.feedEpisode
this.feedEpisodes = await this.getFeedEpisodes()
let feedObj = null
let feedEpisodeCreateFunc = null
let feedEpisodeCreateFuncEntity = null
@ -516,17 +521,24 @@ class Feed extends Model {
try {
const updatedFeed = await this.update(feedObj, { transaction })
// Remove existing feed episodes
await feedEpisodeModel.destroy({
where: {
feedId: this.id
},
transaction
})
const existingFeedEpisodeIds = this.feedEpisodes.map((ep) => ep.id)
// Create new feed episodes
updatedFeed.feedEpisodes = await feedEpisodeCreateFunc(feedEpisodeCreateFuncEntity, updatedFeed, this.slug, transaction)
const newFeedEpisodeIds = updatedFeed.feedEpisodes.map((ep) => ep.id)
const feedEpisodeIdsToRemove = existingFeedEpisodeIds.filter((epid) => !newFeedEpisodeIds.includes(epid))
if (feedEpisodeIdsToRemove.length) {
Logger.info(`[Feed] Removing ${feedEpisodeIdsToRemove.length} episodes from feed ${this.id}`)
await feedEpisodeModel.destroy({
where: {
id: feedEpisodeIdsToRemove
},
transaction
})
}
await transaction.commit()
return updatedFeed