diff --git a/src/NzbDrone.Core.Test/Download/CompletedDownloadServiceTests/ImportFixture.cs b/src/NzbDrone.Core.Test/Download/CompletedDownloadServiceTests/ImportFixture.cs index e2471358a..342cae05d 100644 --- a/src/NzbDrone.Core.Test/Download/CompletedDownloadServiceTests/ImportFixture.cs +++ b/src/NzbDrone.Core.Test/Download/CompletedDownloadServiceTests/ImportFixture.cs @@ -23,6 +23,7 @@ namespace NzbDrone.Core.Test.Download.CompletedDownloadServiceTests public class ImportFixture : CoreTest { private TrackedDownload _trackedDownload; + private Author _author; [SetUp] public void Setup() @@ -42,6 +43,9 @@ public void Setup() .With(c => c.RemoteBook = remoteBook) .Build(); + _author = Builder.CreateNew() + .Build(); + Mocker.GetMock() .SetupGet(c => c.Definition) .Returns(new DownloadClientDefinition { Id = 1, Name = "testClient" }); @@ -190,8 +194,8 @@ public void should_mark_as_imported_if_all_tracks_were_imported_but_extra_files_ .Setup(v => v.ProcessPath(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(new List { - new ImportResult(new ImportDecision(new LocalBook { Path = @"C:\TestPath\Droned.S01E01.mkv".AsOsAgnostic() })), - new ImportResult(new ImportDecision(new LocalBook { Path = @"C:\TestPath\Droned.S01E01.mkv".AsOsAgnostic() }), "Test Failure") + new ImportResult(new ImportDecision(new LocalBook { Path = @"C:\TestPath\Droned.S01E01.mkv".AsOsAgnostic(), Author = _author })), + new ImportResult(new ImportDecision(new LocalBook { Path = @"C:\TestPath\Droned.S01E01.mkv".AsOsAgnostic(), Author = _author }), "Test Failure") }); Mocker.GetMock() @@ -286,11 +290,11 @@ public void should_mark_as_imported_if_all_tracks_were_imported() { new ImportResult( new ImportDecision( - new LocalBook { Path = @"C:\TestPath\Droned.S01E01.mkv".AsOsAgnostic() })), + new LocalBook { Path = @"C:\TestPath\Droned.S01E01.mkv".AsOsAgnostic(), Author = _author })), new ImportResult( new ImportDecision( - new LocalBook { Path = @"C:\TestPath\Droned.S01E02.mkv".AsOsAgnostic() })) + new LocalBook { Path = @"C:\TestPath\Droned.S01E02.mkv".AsOsAgnostic(), Author = _author })) }); Subject.Import(_trackedDownload); @@ -311,15 +315,18 @@ public void should_mark_as_imported_if_all_episodes_were_imported_including_hist { new ImportResult( new ImportDecision( - new LocalBook { Path = @"C:\TestPath\Droned.S01E01.mkv", Book = books[0] })), + new LocalBook { Path = @"C:\TestPath\Droned.S01E01.mkv", Book = books[0], Author = _author })), new ImportResult( new ImportDecision( - new LocalBook { Path = @"C:\TestPath\Droned.S01E02.mkv", Book = books[1] }), "Test Failure") + new LocalBook { Path = @"C:\TestPath\Droned.S01E02.mkv", Book = books[1], Author = _author }), "Test Failure") }); var history = Builder.CreateListOfSize(2) - .BuildList(); + .All() + .With(x => x.EventType = EntityHistoryEventType.BookFileImported) + .With(x => x.AuthorId = 1) + .BuildList(); Mocker.GetMock() .Setup(s => s.FindByDownloadId(It.IsAny())) @@ -343,7 +350,7 @@ public void should_mark_as_imported_if_the_download_can_be_tracked_using_the_sou .Setup(v => v.ProcessPath(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(new List { - new ImportResult(new ImportDecision(new LocalBook { Path = @"C:\TestPath\Droned.S01E01.mkv".AsOsAgnostic() })) + new ImportResult(new ImportDecision(new LocalBook { Path = @"C:\TestPath\Droned.S01E01.mkv".AsOsAgnostic(), Author = _author })) }); Subject.Import(_trackedDownload); diff --git a/src/NzbDrone.Core/Download/CompletedDownloadService.cs b/src/NzbDrone.Core/Download/CompletedDownloadService.cs index c15b7445d..c0d0c92bb 100644 --- a/src/NzbDrone.Core/Download/CompletedDownloadService.cs +++ b/src/NzbDrone.Core/Download/CompletedDownloadService.cs @@ -130,7 +130,11 @@ public bool VerifyImport(TrackedDownload trackedDownload, List imp if (allItemsImported) { trackedDownload.State = TrackedDownloadState.Imported; - _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload)); + + var importedAuthorId = importResults.Where(x => x.Result == ImportResultType.Imported) + .Select(c => c.ImportDecision.Item.Author.Id) + .MostCommon(); + _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload, trackedDownload.RemoteBook?.Author.Id ?? importedAuthorId)); return true; } @@ -153,7 +157,11 @@ public bool VerifyImport(TrackedDownload trackedDownload, List imp if (allEpisodesImportedInHistory) { trackedDownload.State = TrackedDownloadState.Imported; - _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload)); + + var importedAuthorId = historyItems.Where(x => x.EventType == EntityHistoryEventType.BookFileImported) + .Select(x => x.AuthorId) + .MostCommon(); + _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload, trackedDownload.RemoteBook?.Author.Id ?? importedAuthorId)); return true; } } diff --git a/src/NzbDrone.Core/Download/DownloadCompletedEvent.cs b/src/NzbDrone.Core/Download/DownloadCompletedEvent.cs index 0957ed860..b5c54e650 100644 --- a/src/NzbDrone.Core/Download/DownloadCompletedEvent.cs +++ b/src/NzbDrone.Core/Download/DownloadCompletedEvent.cs @@ -1,4 +1,4 @@ -using NzbDrone.Common.Messaging; +using NzbDrone.Common.Messaging; using NzbDrone.Core.Download.TrackedDownloads; namespace NzbDrone.Core.Download @@ -6,10 +6,12 @@ namespace NzbDrone.Core.Download public class DownloadCompletedEvent : IEvent { public TrackedDownload TrackedDownload { get; private set; } + public int AuthorId { get; set; } - public DownloadCompletedEvent(TrackedDownload trackedDownload) + public DownloadCompletedEvent(TrackedDownload trackedDownload, int authorId) { TrackedDownload = trackedDownload; + AuthorId = authorId; } } } diff --git a/src/NzbDrone.Core/Download/DownloadProcessingService.cs b/src/NzbDrone.Core/Download/DownloadProcessingService.cs index 1c06336fa..890f84994 100644 --- a/src/NzbDrone.Core/Download/DownloadProcessingService.cs +++ b/src/NzbDrone.Core/Download/DownloadProcessingService.cs @@ -40,7 +40,8 @@ private void RemoveCompletedDownloads() foreach (var trackedDownload in trackedDownloads) { - _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload)); + _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload, + trackedDownload.RemoteBook.Author.Id)); } } diff --git a/src/NzbDrone.Core/Download/History/DownloadHistoryService.cs b/src/NzbDrone.Core/Download/History/DownloadHistoryService.cs index d5c0bcdea..ec4233a2e 100644 --- a/src/NzbDrone.Core/Download/History/DownloadHistoryService.cs +++ b/src/NzbDrone.Core/Download/History/DownloadHistoryService.cs @@ -93,6 +93,12 @@ public DownloadHistory GetLatestDownloadHistoryItem(string downloadId) public void Handle(BookGrabbedEvent message) { + // Don't store grabbed events for clients that don't download IDs + if (message.DownloadId.IsNullOrWhiteSpace()) + { + return; + } + var history = new DownloadHistory { EventType = DownloadHistoryEventType.DownloadGrabbed, @@ -179,7 +185,7 @@ public void Handle(DownloadCompletedEvent message) var history = new DownloadHistory { EventType = DownloadHistoryEventType.DownloadImported, - AuthorId = message.TrackedDownload.RemoteBook.Author.Id, + AuthorId = message.AuthorId, DownloadId = message.TrackedDownload.DownloadItem.DownloadId, SourceTitle = message.TrackedDownload.DownloadItem.OutputPath.ToString(), Date = DateTime.UtcNow, diff --git a/src/NzbDrone.Core/MediaFiles/BookImport/Manual/ManualImportService.cs b/src/NzbDrone.Core/MediaFiles/BookImport/Manual/ManualImportService.cs index 3490521eb..d122887e2 100644 --- a/src/NzbDrone.Core/MediaFiles/BookImport/Manual/ManualImportService.cs +++ b/src/NzbDrone.Core/MediaFiles/BookImport/Manual/ManualImportService.cs @@ -389,7 +389,7 @@ public void Execute(ManualImportCommand message) if (allItemsImported) { trackedDownload.State = TrackedDownloadState.Imported; - _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload)); + _eventAggregator.PublishEvent(new DownloadCompletedEvent(trackedDownload, imported.First().ImportDecision.Item.Author.Id)); } } }