From eb9eb4ec64c0383798e2f5fa8a2e23ad0a0ff759 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 9 Oct 2022 00:02:34 -0500 Subject: [PATCH] Fixed: Cleanup MovieMetadata that was removed from a collection --- .../ImportListMovieRepository.cs | 9 +++++++ .../ImportListMovieService.cs | 6 +++++ .../Movies/MovieMetadataService.cs | 19 +++++++++++++- src/NzbDrone.Core/Movies/MovieRepository.cs | 9 +++++++ src/NzbDrone.Core/Movies/MovieService.cs | 6 +++++ .../Movies/RefreshCollectionService.cs | 25 +++++++++++++++++-- 6 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/NzbDrone.Core/ImportLists/ImportListMovies/ImportListMovieRepository.cs b/src/NzbDrone.Core/ImportLists/ImportListMovies/ImportListMovieRepository.cs index 73ffc15d5..7a1e92094 100644 --- a/src/NzbDrone.Core/ImportLists/ImportListMovies/ImportListMovieRepository.cs +++ b/src/NzbDrone.Core/ImportLists/ImportListMovies/ImportListMovieRepository.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using NzbDrone.Core.Datastore; using NzbDrone.Core.Messaging.Events; @@ -7,6 +8,7 @@ namespace NzbDrone.Core.ImportLists.ImportListMovies public interface IImportListMovieRepository : IBasicRepository { List GetAllForLists(List listIds); + bool ExistsByMetadataId(int metadataId); } public class ImportListMovieRepository : BasicRepository, IImportListMovieRepository @@ -20,5 +22,12 @@ namespace NzbDrone.Core.ImportLists.ImportListMovies { return Query(x => listIds.Contains(x.ListId)); } + + public bool ExistsByMetadataId(int metadataId) + { + var movies = Query(x => x.MovieMetadataId == metadataId); + + return movies.Any(); + } } } diff --git a/src/NzbDrone.Core/ImportLists/ImportListMovies/ImportListMovieService.cs b/src/NzbDrone.Core/ImportLists/ImportListMovies/ImportListMovieService.cs index b16b6f01b..846200587 100644 --- a/src/NzbDrone.Core/ImportLists/ImportListMovies/ImportListMovieService.cs +++ b/src/NzbDrone.Core/ImportLists/ImportListMovies/ImportListMovieService.cs @@ -15,6 +15,7 @@ namespace NzbDrone.Core.ImportLists.ImportListMovies List SyncMoviesForList(List listMovies, int listId); void RemoveListMovie(ImportListMovie listMovie); ImportListMovie GetById(int id); + bool ExistsByMetadataId(int metadataId); } public class ImportListMovieService : IImportListMovieService, IHandleAsync> @@ -79,5 +80,10 @@ namespace NzbDrone.Core.ImportLists.ImportListMovies var moviesOnList = _importListMovieRepository.GetAllForLists(new List { message.ProviderId }); _importListMovieRepository.DeleteMany(moviesOnList); } + + public bool ExistsByMetadataId(int metadataId) + { + return _importListMovieRepository.ExistsByMetadataId(metadataId); + } } } diff --git a/src/NzbDrone.Core/Movies/MovieMetadataService.cs b/src/NzbDrone.Core/Movies/MovieMetadataService.cs index 8a8114c35..2f191db61 100644 --- a/src/NzbDrone.Core/Movies/MovieMetadataService.cs +++ b/src/NzbDrone.Core/Movies/MovieMetadataService.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using NzbDrone.Core.ImportLists.ImportListMovies; namespace NzbDrone.Core.Movies { @@ -11,15 +12,20 @@ namespace NzbDrone.Core.Movies List GetMoviesByCollectionTmdbId(int collectionId); bool Upsert(MovieMetadata movie); bool UpsertMany(List movies); + void DeleteMany(List movies); } public class MovieMetadataService : IMovieMetadataService { private readonly IMovieMetadataRepository _movieMetadataRepository; + private readonly IMovieService _movieService; + private readonly IImportListMovieService _importListMovieService; - public MovieMetadataService(IMovieMetadataRepository movieMetadataRepository) + public MovieMetadataService(IMovieMetadataRepository movieMetadataRepository, IMovieService movieService, IImportListMovieService importListMovieService) { _movieMetadataRepository = movieMetadataRepository; + _movieService = movieService; + _importListMovieService = importListMovieService; } public MovieMetadata FindByTmdbId(int tmdbId) @@ -56,5 +62,16 @@ namespace NzbDrone.Core.Movies { return _movieMetadataRepository.UpsertMany(movies); } + + public void DeleteMany(List movies) + { + foreach (var movie in movies) + { + if (!_importListMovieService.ExistsByMetadataId(movie.Id) && !_movieService.ExistsByMetadataId(movie.Id)) + { + _movieMetadataRepository.Delete(movie); + } + } + } } } diff --git a/src/NzbDrone.Core/Movies/MovieRepository.cs b/src/NzbDrone.Core/Movies/MovieRepository.cs index 569ebf7f1..f3b3df514 100644 --- a/src/NzbDrone.Core/Movies/MovieRepository.cs +++ b/src/NzbDrone.Core/Movies/MovieRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using Dapper; using NzbDrone.Core.Datastore; @@ -30,6 +31,7 @@ namespace NzbDrone.Core.Movies List AllMovieTmdbIds(); Dictionary> AllMovieTags(); List GetRecommendations(); + bool ExistsByMetadataId(int metadataId); } public class MovieRepository : BasicRepository, IMovieRepository @@ -367,5 +369,12 @@ namespace NzbDrone.Core.Movies return recommendations; } + + public bool ExistsByMetadataId(int metadataId) + { + var movies = Query(x => x.MovieMetadataId == metadataId); + + return movies.Any(); + } } } diff --git a/src/NzbDrone.Core/Movies/MovieService.cs b/src/NzbDrone.Core/Movies/MovieService.cs index 473bf64d6..c7e98fbd7 100644 --- a/src/NzbDrone.Core/Movies/MovieService.cs +++ b/src/NzbDrone.Core/Movies/MovieService.cs @@ -48,6 +48,7 @@ namespace NzbDrone.Core.Movies List GetRecommendedTmdbIds(); bool MoviePathExists(string folder); void RemoveAddOptions(Movie movie); + bool ExistsByMetadataId(int metadataId); } public class MovieService : IMovieService, IHandle, @@ -389,6 +390,11 @@ namespace NzbDrone.Core.Movies return _movieRepository.GetRecommendations(); } + public bool ExistsByMetadataId(int metadataId) + { + return _movieRepository.ExistsByMetadataId(metadataId); + } + public void Handle(MovieFileAddedEvent message) { var movie = message.MovieFile.Movie; diff --git a/src/NzbDrone.Core/Movies/RefreshCollectionService.cs b/src/NzbDrone.Core/Movies/RefreshCollectionService.cs index 85b20d8fe..e8604e76f 100644 --- a/src/NzbDrone.Core/Movies/RefreshCollectionService.cs +++ b/src/NzbDrone.Core/Movies/RefreshCollectionService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using NLog; using NzbDrone.Common.Instrumentation.Extensions; @@ -48,6 +49,7 @@ namespace NzbDrone.Core.Movies _logger.ProgressInfo("Updating info for {0}", collection.Title); MovieCollection collectionInfo; + List movies; try { @@ -68,8 +70,27 @@ namespace NzbDrone.Core.Movies collection.LastInfoSync = DateTime.UtcNow; collection.Images = collectionInfo.Images; - collectionInfo.Movies.ForEach(x => x.CollectionTmdbId = collection.TmdbId); - _movieMetadataService.UpsertMany(collectionInfo.Movies); + movies = collectionInfo.Movies; + movies.ForEach(x => x.CollectionTmdbId = collection.TmdbId); + + var existingMetaForCollection = _movieMetadataService.GetMoviesByCollectionTmdbId(collection.TmdbId); + + var updateList = new List(); + + foreach (var remoteMovie in movies) + { + var existing = existingMetaForCollection.FirstOrDefault(e => e.TmdbId == remoteMovie.TmdbId); + + if (existingMetaForCollection.Any(x => x.TmdbId == remoteMovie.TmdbId)) + { + existingMetaForCollection.Remove(existing); + } + + updateList.Add(remoteMovie); + } + + _movieMetadataService.UpsertMany(updateList); + _movieMetadataService.DeleteMany(existingMetaForCollection); _logger.Debug("Finished collection refresh for {0}", collection.Title);