diff --git a/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs b/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs index 893d7f02e..64ce13992 100644 --- a/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs +++ b/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs @@ -1,22 +1,20 @@ -using System; using System.Collections.Generic; -using System.Collections.Specialized; -using System.IO; -using System.Linq; using FluentValidation.Results; using NzbDrone.Common.Extensions; -using NzbDrone.Core.HealthCheck; +using NzbDrone.Core.Configuration; using NzbDrone.Core.MediaFiles; -using NzbDrone.Core.MediaFiles.MediaInfo; +using NzbDrone.Core.Notifications.Webhook; using NzbDrone.Core.Tv; +using NzbDrone.Core.Validation; namespace NzbDrone.Core.Notifications.Notifiarr { - public class Notifiarr : NotificationBase + public class Notifiarr : WebhookBase { private readonly INotifiarrProxy _proxy; - public Notifiarr(INotifiarrProxy proxy) + public Notifiarr(INotifiarrProxy proxy, IConfigFileProvider configFileProvider, IConfigService configService) + : base(configFileProvider, configService) { _proxy = proxy; } @@ -26,202 +24,60 @@ namespace NzbDrone.Core.Notifications.Notifiarr public override void OnGrab(GrabMessage message) { - var series = message.Series; - var remoteEpisode = message.Episode; - var releaseGroup = remoteEpisode.ParsedEpisodeInfo.ReleaseGroup; - var variables = new StringDictionary(); - - variables.Add("Sonarr_EventType", "Grab"); - variables.Add("Sonarr_Series_Id", series.Id.ToString()); - variables.Add("Sonarr_Series_Title", series.Title); - variables.Add("Sonarr_Series_TvdbId", series.TvdbId.ToString()); - variables.Add("Sonarr_Series_TvMazeId", series.TvMazeId.ToString()); - variables.Add("Sonarr_Series_ImdbId", series.ImdbId ?? string.Empty); - variables.Add("Sonarr_Series_Type", series.SeriesType.ToString()); - variables.Add("Sonarr_Release_EpisodeCount", remoteEpisode.Episodes.Count.ToString()); - variables.Add("Sonarr_Release_SeasonNumber", remoteEpisode.Episodes.First().SeasonNumber.ToString()); - variables.Add("Sonarr_Release_EpisodeNumbers", string.Join(",", remoteEpisode.Episodes.Select(e => e.EpisodeNumber))); - variables.Add("Sonarr_Release_AbsoluteEpisodeNumbers", string.Join(",", remoteEpisode.Episodes.Select(e => e.AbsoluteEpisodeNumber))); - variables.Add("Sonarr_Release_EpisodeAirDates", string.Join(",", remoteEpisode.Episodes.Select(e => e.AirDate))); - variables.Add("Sonarr_Release_EpisodeAirDatesUtc", string.Join(",", remoteEpisode.Episodes.Select(e => e.AirDateUtc))); - variables.Add("Sonarr_Release_EpisodeTitles", string.Join("|", remoteEpisode.Episodes.Select(e => e.Title))); - variables.Add("Sonarr_Release_Title", remoteEpisode.Release.Title); - variables.Add("Sonarr_Release_Indexer", remoteEpisode.Release.Indexer ?? string.Empty); - variables.Add("Sonarr_Release_Size", remoteEpisode.Release.Size.ToString()); - variables.Add("Sonarr_Release_Quality", remoteEpisode.ParsedEpisodeInfo.Quality.Quality.Name); - variables.Add("Sonarr_Release_QualityVersion", remoteEpisode.ParsedEpisodeInfo.Quality.Revision.Version.ToString()); - variables.Add("Sonarr_Release_ReleaseGroup", releaseGroup ?? string.Empty); - variables.Add("Sonarr_Download_Client", message.DownloadClientName ?? string.Empty); - variables.Add("Sonarr_Download_Client_Type", message.DownloadClientType ?? string.Empty); - variables.Add("Sonarr_Download_Id", message.DownloadId ?? string.Empty); - variables.Add("Sonarr_Release_CustomFormat", string.Join("|", remoteEpisode.CustomFormats)); - variables.Add("Sonarr_Release_CustomFormatScore", remoteEpisode.CustomFormatScore.ToString()); - - _proxy.SendNotification(variables, Settings); + _proxy.SendNotification(BuildOnGrabPayload(message), Settings); } public override void OnDownload(DownloadMessage message) { - var series = message.Series; - var episodeFile = message.EpisodeFile; - var sourcePath = message.SourcePath; - var variables = new StringDictionary(); - - variables.Add("Sonarr_EventType", "Download"); - variables.Add("Sonarr_IsUpgrade", message.OldFiles.Any().ToString()); - variables.Add("Sonarr_Series_Id", series.Id.ToString()); - variables.Add("Sonarr_Series_Title", series.Title); - variables.Add("Sonarr_Series_Path", series.Path); - variables.Add("Sonarr_Series_TvdbId", series.TvdbId.ToString()); - variables.Add("Sonarr_Series_TvMazeId", series.TvMazeId.ToString()); - variables.Add("Sonarr_Series_ImdbId", series.ImdbId ?? string.Empty); - variables.Add("Sonarr_Series_Type", series.SeriesType.ToString()); - variables.Add("Sonarr_EpisodeFile_Id", episodeFile.Id.ToString()); - variables.Add("Sonarr_EpisodeFile_EpisodeCount", episodeFile.Episodes.Value.Count.ToString()); - variables.Add("Sonarr_EpisodeFile_RelativePath", episodeFile.RelativePath); - variables.Add("Sonarr_EpisodeFile_Path", Path.Combine(series.Path, episodeFile.RelativePath)); - variables.Add("Sonarr_EpisodeFile_EpisodeIds", string.Join(",", episodeFile.Episodes.Value.Select(e => e.Id))); - variables.Add("Sonarr_EpisodeFile_SeasonNumber", episodeFile.SeasonNumber.ToString()); - variables.Add("Sonarr_EpisodeFile_EpisodeNumbers", string.Join(",", episodeFile.Episodes.Value.Select(e => e.EpisodeNumber))); - variables.Add("Sonarr_EpisodeFile_EpisodeAirDates", string.Join(",", episodeFile.Episodes.Value.Select(e => e.AirDate))); - variables.Add("Sonarr_EpisodeFile_EpisodeAirDatesUtc", string.Join(",", episodeFile.Episodes.Value.Select(e => e.AirDateUtc))); - variables.Add("Sonarr_EpisodeFile_EpisodeTitles", string.Join("|", episodeFile.Episodes.Value.Select(e => e.Title))); - variables.Add("Sonarr_EpisodeFile_Quality", episodeFile.Quality.Quality.Name); - variables.Add("Sonarr_EpisodeFile_QualityVersion", episodeFile.Quality.Revision.Version.ToString()); - variables.Add("Sonarr_EpisodeFile_ReleaseGroup", episodeFile.ReleaseGroup ?? string.Empty); - variables.Add("Sonarr_EpisodeFile_SceneName", episodeFile.SceneName ?? string.Empty); - variables.Add("Sonarr_EpisodeFile_SourcePath", sourcePath); - variables.Add("Sonarr_EpisodeFile_SourceFolder", Path.GetDirectoryName(sourcePath)); - variables.Add("Sonarr_Download_Client", message.DownloadClientInfo?.Name ?? string.Empty); - variables.Add("Sonarr_Download_Client_Type", message.DownloadClientInfo?.Type ?? string.Empty); - variables.Add("Sonarr_Download_Id", message.DownloadId ?? string.Empty); - variables.Add("Sonarr_EpisodeFile_MediaInfo_AudioChannels", MediaInfoFormatter.FormatAudioChannels(episodeFile.MediaInfo).ToString()); - variables.Add("Sonarr_EpisodeFile_MediaInfo_AudioCodec", MediaInfoFormatter.FormatAudioCodec(episodeFile.MediaInfo, null)); - variables.Add("Sonarr_EpisodeFile_MediaInfo_AudioLanguages", episodeFile.MediaInfo.AudioLanguages.Distinct().ConcatToString(" / ")); - variables.Add("Sonarr_EpisodeFile_MediaInfo_Languages", episodeFile.MediaInfo.AudioLanguages.ConcatToString(" / ")); - variables.Add("Sonarr_EpisodeFile_MediaInfo_Height", episodeFile.MediaInfo.Height.ToString()); - variables.Add("Sonarr_EpisodeFile_MediaInfo_Width", episodeFile.MediaInfo.Width.ToString()); - variables.Add("Sonarr_EpisodeFile_MediaInfo_Subtitles", episodeFile.MediaInfo.Subtitles.ConcatToString(" / ")); - variables.Add("Sonarr_EpisodeFile_MediaInfo_VideoCodec", MediaInfoFormatter.FormatVideoCodec(episodeFile.MediaInfo, null)); - variables.Add("Sonarr_EpisodeFile_MediaInfo_VideoDynamicRangeType", MediaInfoFormatter.FormatVideoDynamicRangeType(episodeFile.MediaInfo)); - variables.Add("Sonarr_EpisodeFile_CustomFormat", string.Join("|", message.EpisodeInfo.CustomFormats)); - variables.Add("Sonarr_EpisodeFile_CustomFormatScore", message.EpisodeInfo.CustomFormatScore.ToString()); - - if (message.OldFiles.Any()) - { - variables.Add("Sonarr_DeletedRelativePaths", string.Join("|", message.OldFiles.Select(e => e.RelativePath))); - variables.Add("Sonarr_DeletedPaths", string.Join("|", message.OldFiles.Select(e => Path.Combine(series.Path, e.RelativePath)))); - variables.Add("Sonarr_DeletedDateAdded", string.Join("|", message.OldFiles.Select(e => e.DateAdded))); - } - - _proxy.SendNotification(variables, Settings); + _proxy.SendNotification(BuildOnDownloadPayload(message), Settings); } public override void OnRename(Series series, List renamedFiles) { - var variables = new StringDictionary(); - - variables.Add("Sonarr_EventType", "Rename"); - variables.Add("Sonarr_Series_Id", series.Id.ToString()); - variables.Add("Sonarr_Series_Title", series.Title); - variables.Add("Sonarr_Series_Path", series.Path); - variables.Add("Sonarr_Series_TvdbId", series.TvdbId.ToString()); - variables.Add("Sonarr_Series_TvMazeId", series.TvMazeId.ToString()); - variables.Add("Sonarr_Series_ImdbId", series.ImdbId ?? string.Empty); - variables.Add("Sonarr_Series_Type", series.SeriesType.ToString()); - variables.Add("Sonarr_EpisodeFile_Ids", string.Join(",", renamedFiles.Select(e => e.EpisodeFile.Id))); - variables.Add("Sonarr_EpisodeFile_RelativePaths", string.Join("|", renamedFiles.Select(e => e.EpisodeFile.RelativePath))); - variables.Add("Sonarr_EpisodeFile_Paths", string.Join("|", renamedFiles.Select(e => e.EpisodeFile.Path))); - variables.Add("Sonarr_EpisodeFile_PreviousRelativePaths", string.Join("|", renamedFiles.Select(e => e.PreviousRelativePath))); - variables.Add("Sonarr_EpisodeFile_PreviousPaths", string.Join("|", renamedFiles.Select(e => e.PreviousPath))); - - _proxy.SendNotification(variables, Settings); + _proxy.SendNotification(BuildOnRenamePayload(series, renamedFiles), Settings); } public override void OnEpisodeFileDelete(EpisodeDeleteMessage deleteMessage) { - var series = deleteMessage.Series; - var episodeFile = deleteMessage.EpisodeFile; - - var variables = new StringDictionary(); - - variables.Add("Sonarr_EventType", "EpisodeFileDelete"); - variables.Add("Sonarr_EpisodeFile_DeleteReason", deleteMessage.Reason.ToString()); - variables.Add("Sonarr_Series_Id", series.Id.ToString()); - variables.Add("Sonarr_Series_Title", series.Title); - variables.Add("Sonarr_Series_Path", series.Path); - variables.Add("Sonarr_Series_TvdbId", series.TvdbId.ToString()); - variables.Add("Sonarr_Series_TvMazeId", series.TvMazeId.ToString()); - variables.Add("Sonarr_Series_ImdbId", series.ImdbId ?? string.Empty); - variables.Add("Sonarr_Series_Type", series.SeriesType.ToString()); - variables.Add("Sonarr_EpisodeFile_Id", episodeFile.Id.ToString()); - variables.Add("Sonarr_EpisodeFile_EpisodeCount", episodeFile.Episodes.Value.Count.ToString()); - variables.Add("Sonarr_EpisodeFile_RelativePath", episodeFile.RelativePath); - variables.Add("Sonarr_EpisodeFile_Path", Path.Combine(series.Path, episodeFile.RelativePath)); - variables.Add("Sonarr_EpisodeFile_EpisodeIds", string.Join(",", episodeFile.Episodes.Value.Select(e => e.Id))); - variables.Add("Sonarr_EpisodeFile_SeasonNumber", episodeFile.SeasonNumber.ToString()); - variables.Add("Sonarr_EpisodeFile_EpisodeNumbers", string.Join(",", episodeFile.Episodes.Value.Select(e => e.EpisodeNumber))); - variables.Add("Sonarr_EpisodeFile_EpisodeAirDates", string.Join(",", episodeFile.Episodes.Value.Select(e => e.AirDate))); - variables.Add("Sonarr_EpisodeFile_EpisodeAirDatesUtc", string.Join(",", episodeFile.Episodes.Value.Select(e => e.AirDateUtc))); - variables.Add("Sonarr_EpisodeFile_EpisodeTitles", string.Join("|", episodeFile.Episodes.Value.Select(e => e.Title))); - variables.Add("Sonarr_EpisodeFile_Quality", episodeFile.Quality.Quality.Name); - variables.Add("Sonarr_EpisodeFile_QualityVersion", episodeFile.Quality.Revision.Version.ToString()); - variables.Add("Sonarr_EpisodeFile_ReleaseGroup", episodeFile.ReleaseGroup ?? string.Empty); - variables.Add("Sonarr_EpisodeFile_SceneName", episodeFile.SceneName ?? string.Empty); - - _proxy.SendNotification(variables, Settings); + _proxy.SendNotification(BuildOnEpisodeFileDelete(deleteMessage), Settings); } public override void OnSeriesDelete(SeriesDeleteMessage deleteMessage) { - var series = deleteMessage.Series; - var variables = new StringDictionary(); - - variables.Add("Sonarr_EventType", "SeriesDelete"); - variables.Add("Sonarr_Series_Id", series.Id.ToString()); - variables.Add("Sonarr_Series_Title", series.Title); - variables.Add("Sonarr_Series_Path", series.Path); - variables.Add("Sonarr_Series_TvdbId", series.TvdbId.ToString()); - variables.Add("Sonarr_Series_TvMazeId", series.TvMazeId.ToString()); - variables.Add("Sonarr_Series_ImdbId", series.ImdbId ?? string.Empty); - variables.Add("Sonarr_Series_Type", series.SeriesType.ToString()); - variables.Add("Sonarr_Series_DeletedFiles", deleteMessage.DeletedFiles.ToString()); - - _proxy.SendNotification(variables, Settings); + _proxy.SendNotification(BuildOnSeriesDelete(deleteMessage), Settings); } public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) { - var variables = new StringDictionary(); - - variables.Add("Sonarr_EventType", "HealthIssue"); - variables.Add("Sonarr_Health_Issue_Level", Enum.GetName(typeof(HealthCheckResult), healthCheck.Type)); - variables.Add("Sonarr_Health_Issue_Message", healthCheck.Message); - variables.Add("Sonarr_Health_Issue_Type", healthCheck.Source.Name); - variables.Add("Sonarr_Health_Issue_Wiki", healthCheck.WikiUrl.ToString() ?? string.Empty); - - _proxy.SendNotification(variables, Settings); + _proxy.SendNotification(BuildHealthPayload(healthCheck), Settings); } public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage) { - var variables = new StringDictionary(); - - variables.Add("Sonarr_EventType", "ApplicationUpdate"); - variables.Add("Sonarr_Update_Message", updateMessage.Message); - variables.Add("Sonarr_Update_NewVersion", updateMessage.NewVersion.ToString()); - variables.Add("Sonarr_Update_PreviousVersion", updateMessage.PreviousVersion.ToString()); - - _proxy.SendNotification(variables, Settings); + _proxy.SendNotification(BuildApplicationUpdatePayload(updateMessage), Settings); } public override ValidationResult Test() { var failures = new List(); - failures.AddIfNotNull(_proxy.Test(Settings)); + failures.AddIfNotNull(SendWebhookTest()); return new ValidationResult(failures); } + + private ValidationFailure SendWebhookTest() + { + try + { + _proxy.SendNotification(BuildTestPayload(), Settings); + } + catch (NotifiarrException ex) + { + return new NzbDroneValidationFailure("APIKey", ex.Message); + } + + return null; + } } } diff --git a/src/NzbDrone.Core/Notifications/Notifiarr/NotifiarrProxy.cs b/src/NzbDrone.Core/Notifications/Notifiarr/NotifiarrProxy.cs index aad2fc8ac..81d41b862 100644 --- a/src/NzbDrone.Core/Notifications/Notifiarr/NotifiarrProxy.cs +++ b/src/NzbDrone.Core/Notifications/Notifiarr/NotifiarrProxy.cs @@ -1,81 +1,46 @@ -using System; -using System.Collections.Specialized; -using FluentValidation.Results; +using System.Net.Http; using NLog; -using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; -using NzbDrone.Core.Configuration; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Notifications.Webhook; namespace NzbDrone.Core.Notifications.Notifiarr { public interface INotifiarrProxy { - void SendNotification(StringDictionary message, NotifiarrSettings settings); - ValidationFailure Test(NotifiarrSettings settings); + void SendNotification(WebhookPayload payload, NotifiarrSettings settings); } public class NotifiarrProxy : INotifiarrProxy { private const string URL = "https://notifiarr.com"; private readonly IHttpClient _httpClient; - private readonly IConfigFileProvider _configFileProvider; private readonly Logger _logger; - public NotifiarrProxy(IHttpClient httpClient, IConfigFileProvider configFileProvider, Logger logger) + public NotifiarrProxy(IHttpClient httpClient, Logger logger) { _httpClient = httpClient; - _configFileProvider = configFileProvider; _logger = logger; } - public void SendNotification(StringDictionary message, NotifiarrSettings settings) + public void SendNotification(WebhookPayload payload, NotifiarrSettings settings) { - try - { - ProcessNotification(message, settings); - } - catch (NotifiarrException ex) - { - throw ex; - } - } - - public ValidationFailure Test(NotifiarrSettings settings) - { - try - { - var variables = new StringDictionary(); - variables.Add("Sonarr_EventType", "Test"); - - SendNotification(variables, settings); - return null; - } - catch (NotifiarrException ex) - { - return new ValidationFailure("ApiKey", ex.Message); - } - catch (Exception ex) - { - _logger.Error(ex, ex.Message); - return new ValidationFailure(string.Empty, "$Unable to send test notification: {ex.Message}. Check the log surrounding this error for details"); - } + ProcessNotification(payload, settings); } - private void ProcessNotification(StringDictionary message, NotifiarrSettings settings) + private void ProcessNotification(WebhookPayload payload, NotifiarrSettings settings) { try { - var instanceName = _configFileProvider.InstanceName; - var requestBuilder = new HttpRequestBuilder(URL + "/api/v1/notification/sonarr").Post(); - requestBuilder.AddFormParameter("instanceName", instanceName).Build(); - requestBuilder.SetHeader("X-API-Key", settings.ApiKey); + var request = new HttpRequestBuilder(URL + "/api/v1/notification/sonarr") + .Accept(HttpAccept.Json) + .SetHeader("X-API-Key", settings.ApiKey) + .Build(); - foreach (string key in message.Keys) - { - requestBuilder.AddFormParameter(key, message[key]); - } + request.Method = HttpMethod.Post; - var request = requestBuilder.Build(); + request.Headers.ContentType = "application/json"; + request.SetContent(payload.ToJson()); _httpClient.Post(request); } @@ -85,22 +50,21 @@ namespace NzbDrone.Core.Notifications.Notifiarr switch ((int)responseCode) { case 401: - _logger.Error("Unauthorized", "HTTP 401 - API key is invalid"); + _logger.Error("HTTP 401 - API key is invalid"); throw new NotifiarrException("API key is invalid"); case 400: - _logger.Error("Invalid Request", "HTTP 400 - Unable to send notification. Ensure Sonarr Integration is enabled & assigned a channel on Notifiarr"); + _logger.Error("HTTP 400 - Unable to send notification. Ensure Sonarr Integration is enabled & assigned a channel on Notifiarr"); throw new NotifiarrException("Unable to send notification. Ensure Sonarr Integration is enabled & assigned a channel on Notifiarr"); case 502: case 503: case 504: - _logger.Error("Service Unavailable", "Unable to send notification. Service Unavailable"); + _logger.Error("Unable to send notification. Service Unavailable"); throw new NotifiarrException("Unable to send notification. Service Unavailable", ex); case 520: case 521: case 522: case 523: case 524: - _logger.Error(ex, "Cloudflare Related HTTP Error - Unable to send notification"); throw new NotifiarrException("Cloudflare Related HTTP Error - Unable to send notification", ex); default: _logger.Error(ex, "Unknown HTTP Error - Unable to send notification"); diff --git a/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs b/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs index 59da139a4..6e19a6f28 100644 --- a/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs @@ -1,6 +1,4 @@ using System.Collections.Generic; -using System.IO; -using System.Linq; using FluentValidation.Results; using NzbDrone.Common.Extensions; using NzbDrone.Core.Configuration; @@ -10,18 +8,13 @@ using NzbDrone.Core.Validation; namespace NzbDrone.Core.Notifications.Webhook { - public class Webhook : NotificationBase + public class Webhook : WebhookBase { - private readonly IConfigFileProvider _configFileProvider; - private readonly IConfigService _configService; private readonly IWebhookProxy _proxy; - public Webhook(IConfigFileProvider configFileProvider, - IConfigService configService, - IWebhookProxy proxy) + public Webhook(IWebhookProxy proxy, IConfigFileProvider configFileProvider, IConfigService configService) + : base(configFileProvider, configService) { - _configFileProvider = configFileProvider; - _configService = configService; _proxy = proxy; } @@ -29,129 +22,37 @@ namespace NzbDrone.Core.Notifications.Webhook public override void OnGrab(GrabMessage message) { - var remoteEpisode = message.Episode; - var quality = message.Quality; - - var payload = new WebhookGrabPayload - { - EventType = WebhookEventType.Grab, - InstanceName = _configFileProvider.InstanceName, - ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries(message.Series), - Episodes = remoteEpisode.Episodes.ConvertAll(x => new WebhookEpisode(x)), - Release = new WebhookRelease(quality, remoteEpisode), - DownloadClient = message.DownloadClientName, - DownloadClientType = message.DownloadClientType, - DownloadId = message.DownloadId, - CustomFormatInfo = new WebhookCustomFormatInfo(remoteEpisode.CustomFormats, remoteEpisode.CustomFormatScore) - }; - - _proxy.SendWebhook(payload, Settings); + _proxy.SendWebhook(BuildOnGrabPayload(message), Settings); } public override void OnDownload(DownloadMessage message) { - var episodeFile = message.EpisodeFile; - - var payload = new WebhookImportPayload - { - EventType = WebhookEventType.Download, - InstanceName = _configFileProvider.InstanceName, - ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries(message.Series), - Episodes = episodeFile.Episodes.Value.ConvertAll(x => new WebhookEpisode(x)), - EpisodeFile = new WebhookEpisodeFile(episodeFile), - IsUpgrade = message.OldFiles.Any(), - DownloadClient = message.DownloadClientInfo?.Name, - DownloadClientType = message.DownloadClientInfo?.Type, - DownloadId = message.DownloadId, - CustomFormatInfo = new WebhookCustomFormatInfo(message.EpisodeInfo.CustomFormats, message.EpisodeInfo.CustomFormatScore) - }; - - if (message.OldFiles.Any()) - { - payload.DeletedFiles = message.OldFiles.ConvertAll(x => new WebhookEpisodeFile(x) - { - Path = Path.Combine(message.Series.Path, x.RelativePath) - }); - } - - _proxy.SendWebhook(payload, Settings); + _proxy.SendWebhook(BuildOnDownloadPayload(message), Settings); } public override void OnRename(Series series, List renamedFiles) { - var payload = new WebhookRenamePayload - { - EventType = WebhookEventType.Rename, - InstanceName = _configFileProvider.InstanceName, - ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries(series), - RenamedEpisodeFiles = renamedFiles.ConvertAll(x => new WebhookRenamedEpisodeFile(x)) - }; - - _proxy.SendWebhook(payload, Settings); + _proxy.SendWebhook(BuildOnRenamePayload(series, renamedFiles), Settings); } public override void OnEpisodeFileDelete(EpisodeDeleteMessage deleteMessage) { - var payload = new WebhookEpisodeDeletePayload - { - EventType = WebhookEventType.EpisodeFileDelete, - InstanceName = _configFileProvider.InstanceName, - ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries(deleteMessage.Series), - Episodes = deleteMessage.EpisodeFile.Episodes.Value.ConvertAll(x => new WebhookEpisode(x)), - EpisodeFile = deleteMessage.EpisodeFile, - DeleteReason = deleteMessage.Reason - }; - - _proxy.SendWebhook(payload, Settings); + _proxy.SendWebhook(BuildOnEpisodeFileDelete(deleteMessage), Settings); } public override void OnSeriesDelete(SeriesDeleteMessage deleteMessage) { - var payload = new WebhookSeriesDeletePayload - { - EventType = WebhookEventType.SeriesDelete, - InstanceName = _configFileProvider.InstanceName, - ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries(deleteMessage.Series), - DeletedFiles = deleteMessage.DeletedFiles - }; - - _proxy.SendWebhook(payload, Settings); + _proxy.SendWebhook(BuildOnSeriesDelete(deleteMessage), Settings); } public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) { - var payload = new WebhookHealthPayload - { - EventType = WebhookEventType.Health, - InstanceName = _configFileProvider.InstanceName, - ApplicationUrl = _configService.ApplicationUrl, - Level = healthCheck.Type, - Message = healthCheck.Message, - Type = healthCheck.Source.Name, - WikiUrl = healthCheck.WikiUrl?.ToString() - }; - - _proxy.SendWebhook(payload, Settings); + _proxy.SendWebhook(BuildHealthPayload(healthCheck), Settings); } public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage) { - var payload = new WebhookApplicationUpdatePayload - { - EventType = WebhookEventType.ApplicationUpdate, - InstanceName = _configFileProvider.InstanceName, - ApplicationUrl = _configService.ApplicationUrl, - Message = updateMessage.Message, - PreviousVersion = updateMessage.PreviousVersion.ToString(), - NewVersion = updateMessage.NewVersion.ToString() - }; - - _proxy.SendWebhook(payload, Settings); + _proxy.SendWebhook(BuildApplicationUpdatePayload(updateMessage), Settings); } public override string Name => "Webhook"; @@ -169,31 +70,7 @@ namespace NzbDrone.Core.Notifications.Webhook { try { - var payload = new WebhookGrabPayload - { - EventType = WebhookEventType.Test, - InstanceName = _configFileProvider.InstanceName, - ApplicationUrl = _configService.ApplicationUrl, - Series = new WebhookSeries() - { - Id = 1, - Title = "Test Title", - Path = "C:\\testpath", - TvdbId = 1234 - }, - Episodes = new List() - { - new WebhookEpisode() - { - Id = 123, - EpisodeNumber = 1, - SeasonNumber = 1, - Title = "Test title" - } - } - }; - - _proxy.SendWebhook(payload, Settings); + _proxy.SendWebhook(BuildTestPayload(), Settings); } catch (WebhookException ex) { diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs new file mode 100644 index 000000000..d50c6e04c --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs @@ -0,0 +1,163 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.MediaFiles; +using NzbDrone.Core.ThingiProvider; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.Notifications.Webhook +{ + public abstract class WebhookBase : NotificationBase + where TSettings : IProviderConfig, new() + { + private readonly IConfigFileProvider _configFileProvider; + private readonly IConfigService _configService; + + protected WebhookBase(IConfigFileProvider configFileProvider, IConfigService configService) + { + _configFileProvider = configFileProvider; + _configService = configService; + } + + protected WebhookGrabPayload BuildOnGrabPayload(GrabMessage message) + { + var remoteEpisode = message.Episode; + var quality = message.Quality; + + return new WebhookGrabPayload + { + EventType = WebhookEventType.Grab, + InstanceName = _configFileProvider.InstanceName, + ApplicationUrl = _configService.ApplicationUrl, + Series = new WebhookSeries(message.Series), + Episodes = remoteEpisode.Episodes.ConvertAll(x => new WebhookEpisode(x)), + Release = new WebhookRelease(quality, remoteEpisode), + DownloadClient = message.DownloadClientName, + DownloadClientType = message.DownloadClientType, + DownloadId = message.DownloadId, + CustomFormatInfo = new WebhookCustomFormatInfo(remoteEpisode.CustomFormats, remoteEpisode.CustomFormatScore) + }; + } + + protected WebhookImportPayload BuildOnDownloadPayload(DownloadMessage message) + { + var episodeFile = message.EpisodeFile; + + var payload = new WebhookImportPayload + { + EventType = WebhookEventType.Download, + InstanceName = _configFileProvider.InstanceName, + ApplicationUrl = _configService.ApplicationUrl, + Series = new WebhookSeries(message.Series), + Episodes = episodeFile.Episodes.Value.ConvertAll(x => new WebhookEpisode(x)), + EpisodeFile = new WebhookEpisodeFile(episodeFile), + IsUpgrade = message.OldFiles.Any(), + DownloadClient = message.DownloadClientInfo?.Name, + DownloadClientType = message.DownloadClientInfo?.Type, + DownloadId = message.DownloadId, + CustomFormatInfo = new WebhookCustomFormatInfo(message.EpisodeInfo.CustomFormats, message.EpisodeInfo.CustomFormatScore) + }; + + if (message.OldFiles.Any()) + { + payload.DeletedFiles = message.OldFiles.ConvertAll(x => new WebhookEpisodeFile(x) + { + Path = Path.Combine(message.Series.Path, x.RelativePath) + }); + } + + return payload; + } + + protected WebhookEpisodeDeletePayload BuildOnEpisodeFileDelete(EpisodeDeleteMessage deleteMessage) + { + return new WebhookEpisodeDeletePayload + { + EventType = WebhookEventType.EpisodeFileDelete, + InstanceName = _configFileProvider.InstanceName, + ApplicationUrl = _configService.ApplicationUrl, + Series = new WebhookSeries(deleteMessage.Series), + Episodes = deleteMessage.EpisodeFile.Episodes.Value.ConvertAll(x => new WebhookEpisode(x)), + EpisodeFile = deleteMessage.EpisodeFile, + DeleteReason = deleteMessage.Reason + }; + } + + protected WebhookSeriesDeletePayload BuildOnSeriesDelete(SeriesDeleteMessage deleteMessage) + { + return new WebhookSeriesDeletePayload + { + EventType = WebhookEventType.SeriesDelete, + InstanceName = _configFileProvider.InstanceName, + ApplicationUrl = _configService.ApplicationUrl, + Series = new WebhookSeries(deleteMessage.Series), + DeletedFiles = deleteMessage.DeletedFiles + }; + } + + protected WebhookRenamePayload BuildOnRenamePayload(Series series, List renamedFiles) + { + return new WebhookRenamePayload + { + EventType = WebhookEventType.Rename, + InstanceName = _configFileProvider.InstanceName, + ApplicationUrl = _configService.ApplicationUrl, + Series = new WebhookSeries(series), + RenamedEpisodeFiles = renamedFiles.ConvertAll(x => new WebhookRenamedEpisodeFile(x)) + }; + } + + protected WebhookHealthPayload BuildHealthPayload(HealthCheck.HealthCheck healthCheck) + { + return new WebhookHealthPayload + { + EventType = WebhookEventType.Health, + InstanceName = _configFileProvider.InstanceName, + Level = healthCheck.Type, + Message = healthCheck.Message, + Type = healthCheck.Source.Name, + WikiUrl = healthCheck.WikiUrl?.ToString() + }; + } + + protected WebhookApplicationUpdatePayload BuildApplicationUpdatePayload(ApplicationUpdateMessage updateMessage) + { + return new WebhookApplicationUpdatePayload + { + EventType = WebhookEventType.ApplicationUpdate, + InstanceName = _configFileProvider.InstanceName, + Message = updateMessage.Message, + PreviousVersion = updateMessage.PreviousVersion.ToString(), + NewVersion = updateMessage.NewVersion.ToString() + }; + } + + protected WebhookPayload BuildTestPayload() + { + return new WebhookGrabPayload + { + EventType = WebhookEventType.Test, + InstanceName = _configFileProvider.InstanceName, + ApplicationUrl = _configService.ApplicationUrl, + Series = new WebhookSeries() + { + Id = 1, + Title = "Test Title", + Path = "C:\\testpath", + TvdbId = 1234 + }, + Episodes = new List() + { + new WebhookEpisode() + { + Id = 123, + EpisodeNumber = 1, + SeasonNumber = 1, + Title = "Test title" + } + } + }; + } + } +}