Fixed: Tracked Download Cache Keeps Deleted Albums (#811)

* Fixed: Tracked Download Cache Deleted Albums

* Add a test
pull/6/head
Qstick 5 years ago committed by GitHub
parent 2147c52695
commit 5defb69eb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,6 +11,7 @@ using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Music; using NzbDrone.Core.Music;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using System.Linq; using System.Linq;
using NzbDrone.Core.Music.Events;
namespace NzbDrone.Core.Test.Download.TrackedDownloads namespace NzbDrone.Core.Test.Download.TrackedDownloads
{ {
@ -72,5 +73,54 @@ namespace NzbDrone.Core.Test.Download.TrackedDownloads
trackedDownload.RemoteAlbum.Albums.First().Id.Should().Be(4); 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<Album> { new Album { Id = 4 } },
ParsedAlbumInfo = new ParsedAlbumInfo()
{
AlbumTitle = "Audio Album",
ArtistName = "Audio Artist"
}
};
Mocker.GetMock<IParsingService>()
.Setup(s => s.Map(It.Is<ParsedAlbumInfo>(i => i.AlbumTitle == "Audio Album" && i.ArtistName == "Audio Artist"), It.IsAny<int>(), It.IsAny<IEnumerable<int>>()))
.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<IParsingService>()
.Setup(s => s.Map(It.Is<ParsedAlbumInfo>(i => i.AlbumTitle == "Audio Album" && i.ArtistName == "Audio Artist"), It.IsAny<int>(), It.IsAny<IEnumerable<int>>()))
.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();
}
} }
} }

@ -9,10 +9,11 @@ using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Music; using NzbDrone.Core.Music;
using NzbDrone.Core.Parser; using NzbDrone.Core.Parser;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
using NzbDrone.Core.Music.Events;
namespace NzbDrone.Core.Download.TrackedDownloads namespace NzbDrone.Core.Download.TrackedDownloads
{ {
public interface ITrackedDownloadService public interface ITrackedDownloadService : IHandle<AlbumDeletedEvent>
{ {
TrackedDownload Find(string downloadId); TrackedDownload Find(string downloadId);
void StopTracking(string downloadId); void StopTracking(string downloadId);
@ -48,6 +49,24 @@ namespace NzbDrone.Core.Download.TrackedDownloads
return _cache.Find(downloadId); 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) public void StopTracking(string downloadId)
{ {
var trackedDownload = _cache.Find(downloadId); var trackedDownload = _cache.Find(downloadId);
@ -224,5 +243,11 @@ namespace NzbDrone.Core.Download.TrackedDownloads
return TrackedDownloadStage.Downloading; return TrackedDownloadStage.Downloading;
} }
public void Handle(AlbumDeletedEvent message)
{
UpdateAlbumCache(message.Album.Id);
}
} }
} }

Loading…
Cancel
Save