From eea34198495ca03b3765334a28481eb56f1b20db Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 3 Jun 2017 09:36:17 -0700 Subject: [PATCH] New: Download client and ID for custom scripts --- .../Episodes/EpisodeModuleWithSignalR.cs | 11 +++++-- .../HistoryTests/HistoryServiceFixture.cs | 9 +++++- .../EpisodeImport/ImportApprovedEpisodes.cs | 14 +-------- .../Events/EpisodeDownloadedEvent.cs | 20 ------------ .../MediaFiles/Events/EpisodeImportedEvent.cs | 21 +++++++------ .../CustomScript/CustomScript.cs | 4 +++ .../Notifications/DownloadMessage.cs | 2 ++ .../Notifications/GrabMessage.cs | 4 ++- .../Notifications/NotificationService.cs | 31 +++++++++++++------ src/NzbDrone.Core/NzbDrone.Core.csproj | 1 - 10 files changed, 58 insertions(+), 59 deletions(-) delete mode 100644 src/NzbDrone.Core/MediaFiles/Events/EpisodeDownloadedEvent.cs diff --git a/src/NzbDrone.Api/Episodes/EpisodeModuleWithSignalR.cs b/src/NzbDrone.Api/Episodes/EpisodeModuleWithSignalR.cs index d4c1deb27..349a629a4 100644 --- a/src/NzbDrone.Api/Episodes/EpisodeModuleWithSignalR.cs +++ b/src/NzbDrone.Api/Episodes/EpisodeModuleWithSignalR.cs @@ -14,7 +14,7 @@ namespace NzbDrone.Api.Episodes { public abstract class EpisodeModuleWithSignalR : NzbDroneRestModuleWithSignalR, IHandle, - IHandle + IHandle { protected readonly IEpisodeService _episodeService; protected readonly ISeriesService _seriesService; @@ -115,9 +115,14 @@ namespace NzbDrone.Api.Episodes } } - public void Handle(EpisodeDownloadedEvent message) + public void Handle(EpisodeImportedEvent message) { - foreach (var episode in message.Episode.Episodes) + if (!message.NewDownload) + { + return; + } + + foreach (var episode in message.EpisodeInfo.Episodes) { BroadcastResourceChange(ModelAction.Updated, episode.Id); } diff --git a/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs b/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs index de657ff6c..660a58d6f 100644 --- a/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs +++ b/src/NzbDrone.Core.Test/HistoryTests/HistoryServiceFixture.cs @@ -13,6 +13,7 @@ using NzbDrone.Core.Qualities; using System.Collections.Generic; using NzbDrone.Core.Test.Qualities; using FluentAssertions; +using NzbDrone.Core.Download; using NzbDrone.Core.Tv; namespace NzbDrone.Core.Test.HistoryTests @@ -81,7 +82,13 @@ namespace NzbDrone.Core.Test.HistoryTests Path = @"C:\Test\Unsorted\Series.s01e01.mkv" }; - Subject.Handle(new EpisodeImportedEvent(localEpisode, episodeFile, true, "sab", "abcd")); + var downloadClientItem = new DownloadClientItem + { + DownloadClient = "sab", + DownloadId = "abcd" + }; + + Subject.Handle(new EpisodeImportedEvent(localEpisode, episodeFile, new List(), true, downloadClientItem)); Mocker.GetMock() .Verify(v => v.Insert(It.Is(h => h.SourceTitle == Path.GetFileNameWithoutExtension(localEpisode.Path)))); diff --git a/src/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs b/src/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs index b6b2c7573..4694803e3 100644 --- a/src/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs +++ b/src/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs @@ -120,19 +120,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport _extraService.ImportExtraFiles(localEpisode, episodeFile, copyOnly); } - if (downloadClientItem != null) - { - _eventAggregator.PublishEvent(new EpisodeImportedEvent(localEpisode, episodeFile, newDownload, downloadClientItem.DownloadClient, downloadClientItem.DownloadId)); - } - else - { - _eventAggregator.PublishEvent(new EpisodeImportedEvent(localEpisode, episodeFile, newDownload)); - } - - if (newDownload) - { - _eventAggregator.PublishEvent(new EpisodeDownloadedEvent(localEpisode, episodeFile, oldFiles)); - } + _eventAggregator.PublishEvent(new EpisodeImportedEvent(localEpisode, episodeFile, oldFiles, newDownload, downloadClientItem)); } catch (Exception e) { diff --git a/src/NzbDrone.Core/MediaFiles/Events/EpisodeDownloadedEvent.cs b/src/NzbDrone.Core/MediaFiles/Events/EpisodeDownloadedEvent.cs deleted file mode 100644 index af22b63fb..000000000 --- a/src/NzbDrone.Core/MediaFiles/Events/EpisodeDownloadedEvent.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Collections.Generic; -using NzbDrone.Common.Messaging; -using NzbDrone.Core.Parser.Model; - -namespace NzbDrone.Core.MediaFiles.Events -{ - public class EpisodeDownloadedEvent : IEvent - { - public LocalEpisode Episode { get; private set; } - public EpisodeFile EpisodeFile { get; private set; } - public List OldFiles { get; private set; } - - public EpisodeDownloadedEvent(LocalEpisode episode, EpisodeFile episodeFile, List oldFiles) - { - Episode = episode; - EpisodeFile = episodeFile; - OldFiles = oldFiles; - } - } -} \ No newline at end of file diff --git a/src/NzbDrone.Core/MediaFiles/Events/EpisodeImportedEvent.cs b/src/NzbDrone.Core/MediaFiles/Events/EpisodeImportedEvent.cs index 44cbff6b1..7c870666b 100644 --- a/src/NzbDrone.Core/MediaFiles/Events/EpisodeImportedEvent.cs +++ b/src/NzbDrone.Core/MediaFiles/Events/EpisodeImportedEvent.cs @@ -1,4 +1,6 @@ -using NzbDrone.Common.Messaging; +using System.Collections.Generic; +using NzbDrone.Common.Messaging; +using NzbDrone.Core.Download; using NzbDrone.Core.Parser.Model; namespace NzbDrone.Core.MediaFiles.Events @@ -7,24 +9,23 @@ namespace NzbDrone.Core.MediaFiles.Events { public LocalEpisode EpisodeInfo { get; private set; } public EpisodeFile ImportedEpisode { get; private set; } + public List OldFiles { get; private set; } public bool NewDownload { get; private set; } public string DownloadClient { get; private set; } public string DownloadId { get; private set; } - public EpisodeImportedEvent(LocalEpisode episodeInfo, EpisodeFile importedEpisode, bool newDownload) + public EpisodeImportedEvent(LocalEpisode episodeInfo, EpisodeFile importedEpisode, List oldFiles, bool newDownload, DownloadClientItem downloadClientItem) { EpisodeInfo = episodeInfo; ImportedEpisode = importedEpisode; + OldFiles = oldFiles; NewDownload = newDownload; - } - public EpisodeImportedEvent(LocalEpisode episodeInfo, EpisodeFile importedEpisode, bool newDownload, string downloadClient, string downloadId) - { - EpisodeInfo = episodeInfo; - ImportedEpisode = importedEpisode; - NewDownload = newDownload; - DownloadClient = downloadClient; - DownloadId = downloadId; + if (downloadClientItem != null) + { + DownloadClient = downloadClientItem.DownloadClient; + DownloadId = downloadClientItem.DownloadId; + } } } } diff --git a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs index d29c956ee..6f183542b 100644 --- a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs +++ b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs @@ -54,6 +54,8 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Sonarr_Release_Quality", remoteEpisode.ParsedEpisodeInfo.Quality.Quality.Name); environmentVariables.Add("Sonarr_Release_QualityVersion", remoteEpisode.ParsedEpisodeInfo.Quality.Revision.Version.ToString()); environmentVariables.Add("Sonarr_Release_ReleaseGroup", releaseGroup); + environmentVariables.Add("Sonarr_Download_Client", message.DownloadClient ?? string.Empty); + environmentVariables.Add("Sonarr_Download_Id", message.DownloadId ?? string.Empty); ExecuteScript(environmentVariables); } @@ -89,6 +91,8 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Sonarr_EpisodeFile_SceneName", episodeFile.SceneName ?? string.Empty); environmentVariables.Add("Sonarr_EpisodeFile_SourcePath", sourcePath); environmentVariables.Add("Sonarr_EpisodeFile_SourceFolder", Path.GetDirectoryName(sourcePath)); + environmentVariables.Add("Sonarr_Download_Client", message.DownloadClient ?? string.Empty); + environmentVariables.Add("Sonarr_Download_Id", message.DownloadId ?? string.Empty); if (message.OldFiles.Any()) { diff --git a/src/NzbDrone.Core/Notifications/DownloadMessage.cs b/src/NzbDrone.Core/Notifications/DownloadMessage.cs index a16ecea80..fb4e7a8ba 100644 --- a/src/NzbDrone.Core/Notifications/DownloadMessage.cs +++ b/src/NzbDrone.Core/Notifications/DownloadMessage.cs @@ -11,6 +11,8 @@ namespace NzbDrone.Core.Notifications public EpisodeFile EpisodeFile { get; set; } public List OldFiles { get; set; } public string SourcePath { get; set; } + public string DownloadClient { get; set; } + public string DownloadId { get; set; } public override string ToString() { diff --git a/src/NzbDrone.Core/Notifications/GrabMessage.cs b/src/NzbDrone.Core/Notifications/GrabMessage.cs index e62dbe701..90688a2c2 100644 --- a/src/NzbDrone.Core/Notifications/GrabMessage.cs +++ b/src/NzbDrone.Core/Notifications/GrabMessage.cs @@ -9,7 +9,9 @@ namespace NzbDrone.Core.Notifications public string Message { get; set; } public Series Series { get; set; } public RemoteEpisode Episode { get; set; } - public QualityModel Quality { get; set; } + public QualityModel Quality { get; set; } + public string DownloadClient { get; set; } + public string DownloadId { get; set; } public override string ToString() { diff --git a/src/NzbDrone.Core/Notifications/NotificationService.cs b/src/NzbDrone.Core/Notifications/NotificationService.cs index 985126f19..6820aee8c 100644 --- a/src/NzbDrone.Core/Notifications/NotificationService.cs +++ b/src/NzbDrone.Core/Notifications/NotificationService.cs @@ -14,7 +14,7 @@ namespace NzbDrone.Core.Notifications { public class NotificationService : IHandle, - IHandle, + IHandle, IHandle { private readonly INotificationFactory _notificationFactory; @@ -95,7 +95,9 @@ namespace NzbDrone.Core.Notifications Message = GetMessage(message.Episode.Series, message.Episode.Episodes, message.Episode.ParsedEpisodeInfo.Quality), Series = message.Episode.Series, Quality = message.Episode.ParsedEpisodeInfo.Quality, - Episode = message.Episode + Episode = message.Episode, + DownloadClient = message.DownloadClient, + DownloadId = message.DownloadId }; foreach (var notification in _notificationFactory.OnGrabEnabled()) @@ -113,20 +115,29 @@ namespace NzbDrone.Core.Notifications } } - public void Handle(EpisodeDownloadedEvent message) + public void Handle(EpisodeImportedEvent message) { - var downloadMessage = new DownloadMessage(); - downloadMessage.Message = GetMessage(message.Episode.Series, message.Episode.Episodes, message.Episode.Quality); - downloadMessage.Series = message.Episode.Series; - downloadMessage.EpisodeFile = message.EpisodeFile; - downloadMessage.OldFiles = message.OldFiles; - downloadMessage.SourcePath = message.Episode.Path; + if (!message.NewDownload) + { + return; + } + + var downloadMessage = new DownloadMessage + { + Message = GetMessage(message.EpisodeInfo.Series, message.EpisodeInfo.Episodes, message.EpisodeInfo.Quality), + Series = message.EpisodeInfo.Series, + EpisodeFile = message.ImportedEpisode, + OldFiles = message.OldFiles, + SourcePath = message.EpisodeInfo.Path, + DownloadClient = message.DownloadClient, + DownloadId = message.DownloadId + }; foreach (var notification in _notificationFactory.OnDownloadEnabled()) { try { - if (ShouldHandleSeries(notification.Definition, message.Episode.Series)) + if (ShouldHandleSeries(notification.Definition, message.EpisodeInfo.Series)) { if (downloadMessage.OldFiles.Empty() || ((NotificationDefinition)notification.Definition).OnUpgrade) { diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 22a8d9ba5..afdd19028 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -782,7 +782,6 @@ -