From 5defb69eb6100186dedf9eee62748d3336ad5ebe Mon Sep 17 00:00:00 2001 From: Qstick Date: Fri, 26 Jul 2019 17:21:03 -0400 Subject: [PATCH] Fixed: Tracked Download Cache Keeps Deleted Albums (#811) * Fixed: Tracked Download Cache Deleted Albums * Add a test --- .../TrackedDownloadServiceFixture.cs | 50 +++++++++++++++++++ .../TrackedDownloadService.cs | 27 +++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/NzbDrone.Core.Test/Download/TrackedDownloads/TrackedDownloadServiceFixture.cs b/src/NzbDrone.Core.Test/Download/TrackedDownloads/TrackedDownloadServiceFixture.cs index 4e44715aa..1335e6186 100644 --- a/src/NzbDrone.Core.Test/Download/TrackedDownloads/TrackedDownloadServiceFixture.cs +++ b/src/NzbDrone.Core.Test/Download/TrackedDownloads/TrackedDownloadServiceFixture.cs @@ -11,6 +11,7 @@ using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Music; using NzbDrone.Core.Indexers; using System.Linq; +using NzbDrone.Core.Music.Events; namespace NzbDrone.Core.Test.Download.TrackedDownloads { @@ -72,5 +73,54 @@ namespace NzbDrone.Core.Test.Download.TrackedDownloads trackedDownload.RemoteAlbum.Albums.First().Id.Should().Be(4); } + [Test] + public void should_unmap_tracked_download_if_album_deleted() + { + GivenDownloadHistory(); + + var remoteAlbum = new RemoteAlbum + { + Artist = new Artist() { Id = 5 }, + Albums = new List { new Album { Id = 4 } }, + ParsedAlbumInfo = new ParsedAlbumInfo() + { + AlbumTitle = "Audio Album", + ArtistName = "Audio Artist" + } + }; + + Mocker.GetMock() + .Setup(s => s.Map(It.Is(i => i.AlbumTitle == "Audio Album" && i.ArtistName == "Audio Artist"), It.IsAny(), It.IsAny>())) + .Returns(remoteAlbum); + + var client = new DownloadClientDefinition() + { + Id = 1, + Protocol = DownloadProtocol.Torrent + }; + + var item = new DownloadClientItem() + { + Title = "Audio Artist - Audio Album [2018 - FLAC]", + DownloadId = "35238", + }; + + // get a tracked download in place + var trackedDownload = Subject.TrackDownload(client, item); + Subject.GetTrackedDownloads().Should().HaveCount(1); + + // simulate deletion - album no longer maps + Mocker.GetMock() + .Setup(s => s.Map(It.Is(i => i.AlbumTitle == "Audio Album" && i.ArtistName == "Audio Artist"), It.IsAny(), It.IsAny>())) + .Returns(default(RemoteAlbum)); + + // handle deletion event + Subject.Handle(new AlbumDeletedEvent(remoteAlbum.Albums.First(), false)); + + // verify download has null remote album + var trackedDownloads = Subject.GetTrackedDownloads(); + trackedDownloads.Should().HaveCount(1); + trackedDownloads.First().RemoteAlbum.Should().BeNull(); + } } } diff --git a/src/NzbDrone.Core/Download/TrackedDownloads/TrackedDownloadService.cs b/src/NzbDrone.Core/Download/TrackedDownloads/TrackedDownloadService.cs index 8f0ca4e62..c175e50fe 100644 --- a/src/NzbDrone.Core/Download/TrackedDownloads/TrackedDownloadService.cs +++ b/src/NzbDrone.Core/Download/TrackedDownloads/TrackedDownloadService.cs @@ -9,10 +9,11 @@ using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Music; using NzbDrone.Core.Parser; using NzbDrone.Common.Serializer; +using NzbDrone.Core.Music.Events; namespace NzbDrone.Core.Download.TrackedDownloads { - public interface ITrackedDownloadService + public interface ITrackedDownloadService : IHandle { TrackedDownload Find(string downloadId); void StopTracking(string downloadId); @@ -48,6 +49,24 @@ namespace NzbDrone.Core.Download.TrackedDownloads return _cache.Find(downloadId); } + public void UpdateAlbumCache(int albumId) + { + var updateCacheItems = _cache.Values.Where(x => x.RemoteAlbum.Albums.Any(a => a.Id == albumId)).ToList(); + + foreach (var item in updateCacheItems) + { + var parsedAlbumInfo = Parser.Parser.ParseAlbumTitle(item.DownloadItem.Title); + item.RemoteAlbum = null; + + if (parsedAlbumInfo != null) + { + item.RemoteAlbum = _parsingService.Map(parsedAlbumInfo); + } + } + + _eventAggregator.PublishEvent(new TrackedDownloadRefreshedEvent(GetTrackedDownloads())); + } + public void StopTracking(string downloadId) { var trackedDownload = _cache.Find(downloadId); @@ -224,5 +243,11 @@ namespace NzbDrone.Core.Download.TrackedDownloads return TrackedDownloadStage.Downloading; } + + public void Handle(AlbumDeletedEvent message) + { + UpdateAlbumCache(message.Album.Id); + } } } +