diff --git a/server/scanner/PodcastScanner.js b/server/scanner/PodcastScanner.js index 4170136d..c9569c3a 100644 --- a/server/scanner/PodcastScanner.js +++ b/server/scanner/PodcastScanner.js @@ -59,17 +59,36 @@ class PodcastScanner { if (libraryItemData.hasAudioFileChanges || libraryItemData.audioLibraryFiles.length !== existingPodcastEpisodes.length) { // Filter out and destroy episodes that were removed - existingPodcastEpisodes = await Promise.all( - existingPodcastEpisodes.filter(async (ep) => { - if (libraryItemData.checkAudioFileRemoved(ep.audioFile)) { - libraryScan.addLog(LogLevel.INFO, `Podcast episode "${ep.title}" audio file was removed`) - // TODO: Should clean up other data linked to this episode - await ep.destroy() - return false + const episodesToRemove = [] + existingPodcastEpisodes = existingPodcastEpisodes.filter((ep) => { + if (libraryItemData.checkAudioFileRemoved(ep.audioFile)) { + episodesToRemove.push(ep) + return false + } + return true + }) + + if (episodesToRemove.length) { + // Remove episodes from playlists and media progress + const episodeIds = episodesToRemove.map((ep) => ep.id) + await Database.playlistModel.removeMediaItemsFromPlaylists(episodeIds) + const mediaProgressRemoved = await Database.mediaProgressModel.destroy({ + where: { + mediaItemId: episodeIds } - return true }) - ) + if (mediaProgressRemoved) { + libraryScan.addLog(LogLevel.INFO, `Removed ${mediaProgressRemoved} media progress for episodes`) + } + + // Remove episodes + await Promise.all( + episodesToRemove.map(async (ep) => { + await ep.destroy() + libraryScan.addLog(LogLevel.INFO, `Podcast episode "${ep.title}" audio file was removed`) + }) + ) + } // Update audio files that were modified if (libraryItemData.audioLibraryFilesModified.length) { @@ -139,7 +158,6 @@ class PodcastScanner { } let hasMediaChanges = false - if (existingPodcastEpisodes.length !== media.numEpisodes) { media.numEpisodes = existingPodcastEpisodes.length hasMediaChanges = true