From 56e3449db604df0fe28d1339db2d75b30500475f Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 6 Aug 2023 14:18:51 -0500 Subject: [PATCH] Remove media progress for podcast episodes when episode is removed --- server/Server.js | 53 +++++++++++++++++-------- server/controllers/PodcastController.js | 12 +++++- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/server/Server.js b/server/Server.js index 432c1b3f..bf055aee 100644 --- a/server/Server.js +++ b/server/Server.js @@ -1,4 +1,5 @@ const Path = require('path') +const Sequelize = require('sequelize') const express = require('express') const http = require('http') const fs = require('./libs/fsExtra') @@ -248,25 +249,45 @@ class Server { await this.scanner.scanFilesChanged(fileUpdates) } - // Remove user media progress with items that no longer exist & remove seriesHideFrom that no longer exist + /** + * Remove user media progress for items that no longer exist & remove seriesHideFrom that no longer exist + */ async cleanUserData() { + // Get all media progress without an associated media item + const mediaProgressToRemove = await Database.models.mediaProgress.findAll({ + where: { + '$podcastEpisode.id$': null, + '$book.id$': null + }, + attributes: ['id'], + include: [ + { + model: Database.models.book, + attributes: ['id'] + }, + { + model: Database.models.podcastEpisode, + attributes: ['id'] + } + ] + }) + if (mediaProgressToRemove.length) { + // Remove media progress + const mediaProgressRemoved = await Database.models.mediaProgress.destroy({ + where: { + id: { + [Sequelize.Op.in]: mediaProgressToRemove.map(mp => mp.id) + } + } + }) + if (mediaProgressRemoved) { + Logger.info(`[Server] Removed ${mediaProgressRemoved} media progress for media items that no longer exist in db`) + } + } + + // Remove series from hide from continue listening that no longer exist const users = await Database.models.user.getOldUsers() for (const _user of users) { - if (_user.mediaProgress.length) { - for (const mediaProgress of _user.mediaProgress) { - const libraryItem = Database.libraryItems.find(li => li.id === mediaProgress.libraryItemId) - if (libraryItem && mediaProgress.episodeId) { - const episode = libraryItem.media.checkHasEpisode?.(mediaProgress.episodeId) - if (episode) continue - } else { - continue - } - - Logger.debug(`[Server] Removing media progress ${mediaProgress.id} data from user ${_user.username}`) - await Database.removeMediaProgress(mediaProgress.id) - } - } - let hasUpdated = false if (_user.seriesHideFromContinueListening.length) { _user.seriesHideFromContinueListening = _user.seriesHideFromContinueListening.filter(seriesId => { diff --git a/server/controllers/PodcastController.js b/server/controllers/PodcastController.js index 1d1da4f7..a7d8f3ce 100644 --- a/server/controllers/PodcastController.js +++ b/server/controllers/PodcastController.js @@ -263,7 +263,7 @@ class PodcastController { // Remove episode from Podcast and library file const episodeRemoved = libraryItem.media.removeEpisode(episodeId) - if (episodeRemoved && episodeRemoved.audioFile) { + if (episodeRemoved?.audioFile) { libraryItem.removeLibraryFile(episodeRemoved.audioFile.ino) } @@ -283,6 +283,16 @@ class PodcastController { } } + // Remove media progress for this episode + const mediaProgressRemoved = await Database.models.mediaProgress.destroy({ + where: { + mediaItemId: episode.id + } + }) + if (mediaProgressRemoved) { + Logger.info(`[PodcastController] Removed ${mediaProgressRemoved} media progress for episode ${episode.id}`) + } + await Database.updateLibraryItem(libraryItem) SocketAuthority.emitter('item_updated', libraryItem.toJSONExpanded()) res.json(libraryItem.toJSON())