Remove media progress for podcast episodes when episode is removed

This commit is contained in:
advplyr 2023-08-06 14:18:51 -05:00
parent 1372c24535
commit 56e3449db6
2 changed files with 48 additions and 17 deletions

View File

@ -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 => {

View File

@ -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())