From 2396af45897fa50e334b17f5c7571a378339cb8d Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 10 Feb 2016 23:43:40 -0800 Subject: [PATCH] New: Pushover Silent and Emergency priorities Closes #878 --- .../Notifications/Pushover/Pushover.cs | 4 ++-- .../Pushover/PushoverPriority.cs | 3 ++- .../Notifications/Pushover/PushoverService.cs | 23 +++++++++++++------ .../Pushover/PushoverSettings.cs | 13 ++++++++--- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/NzbDrone.Core/Notifications/Pushover/Pushover.cs b/src/NzbDrone.Core/Notifications/Pushover/Pushover.cs index d6211504f..c61320d46 100644 --- a/src/NzbDrone.Core/Notifications/Pushover/Pushover.cs +++ b/src/NzbDrone.Core/Notifications/Pushover/Pushover.cs @@ -23,14 +23,14 @@ namespace NzbDrone.Core.Notifications.Pushover { const string title = "Episode Grabbed"; - _proxy.SendNotification(title, grabMessage.Message, Settings.ApiKey, Settings.UserKey, (PushoverPriority)Settings.Priority, Settings.Sound); + _proxy.SendNotification(title, grabMessage.Message, Settings); } public override void OnDownload(DownloadMessage message) { const string title = "Episode Downloaded"; - _proxy.SendNotification(title, message.Message, Settings.ApiKey, Settings.UserKey, (PushoverPriority)Settings.Priority, Settings.Sound); + _proxy.SendNotification(title, message.Message, Settings); } public override void OnRename(Series series) diff --git a/src/NzbDrone.Core/Notifications/Pushover/PushoverPriority.cs b/src/NzbDrone.Core/Notifications/Pushover/PushoverPriority.cs index 6ad6c5339..2d4f705a7 100644 --- a/src/NzbDrone.Core/Notifications/Pushover/PushoverPriority.cs +++ b/src/NzbDrone.Core/Notifications/Pushover/PushoverPriority.cs @@ -2,9 +2,10 @@ { public enum PushoverPriority { + Silent = -1, Quiet = -1, Normal = 0, High = 1, - //Emergency = 2 + Emergency = 2 } } diff --git a/src/NzbDrone.Core/Notifications/Pushover/PushoverService.cs b/src/NzbDrone.Core/Notifications/Pushover/PushoverService.cs index d8306253e..e61b64fcb 100644 --- a/src/NzbDrone.Core/Notifications/Pushover/PushoverService.cs +++ b/src/NzbDrone.Core/Notifications/Pushover/PushoverService.cs @@ -9,7 +9,7 @@ namespace NzbDrone.Core.Notifications.Pushover { public interface IPushoverProxy { - void SendNotification(string title, string message, string apiKey, string userKey, PushoverPriority priority, string sound); + void SendNotification(string title, string message, PushoverSettings settings); ValidationFailure Test(PushoverSettings settings); } @@ -23,17 +23,26 @@ namespace NzbDrone.Core.Notifications.Pushover _logger = logger; } - public void SendNotification(string title, string message, string apiKey, string userKey, PushoverPriority priority, string sound) + public void SendNotification(string title, string message, PushoverSettings settings) { var client = RestClientFactory.BuildClient(URL); var request = new RestRequest(Method.POST); - request.AddParameter("token", apiKey); - request.AddParameter("user", userKey); + request.AddParameter("token", settings.ApiKey); + request.AddParameter("user", settings.UserKey); request.AddParameter("title", title); request.AddParameter("message", message); - request.AddParameter("priority", (int)priority); + request.AddParameter("priority", settings.Priority); - if (!sound.IsNullOrWhiteSpace()) request.AddParameter("sound", sound); + if ((PushoverPriority)settings.Priority == PushoverPriority.Emergency) + { + request.AddParameter("retry", settings.Retry); + request.AddParameter("expire", settings.Expire); + } + + if (!settings.Sound.IsNullOrWhiteSpace()) + { + request.AddParameter("sound", settings.Sound); + } client.ExecuteAndValidate(request); @@ -46,7 +55,7 @@ namespace NzbDrone.Core.Notifications.Pushover const string title = "Test Notification"; const string body = "This is a test message from Sonarr"; - SendNotification(title, body, settings.ApiKey, settings.UserKey, (PushoverPriority)settings.Priority, settings.Sound); + SendNotification(title, body, settings); } catch (Exception ex) { diff --git a/src/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs b/src/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs index b4ec11d13..d985baeed 100644 --- a/src/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs +++ b/src/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs @@ -1,5 +1,4 @@ -using System; -using FluentValidation; +using FluentValidation; using NzbDrone.Core.Annotations; using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Validation; @@ -11,6 +10,8 @@ namespace NzbDrone.Core.Notifications.Pushover public PushoverSettingsValidator() { RuleFor(c => c.UserKey).NotEmpty(); + RuleFor(c => c.Retry).GreaterThanOrEqualTo(30).LessThanOrEqualTo(86400).When(c => (PushoverPriority)c.Priority == PushoverPriority.Emergency); + RuleFor(c => c.Retry).GreaterThanOrEqualTo(0).LessThanOrEqualTo(86400).When(c => (PushoverPriority)c.Priority == PushoverPriority.Emergency); } } @@ -33,7 +34,13 @@ namespace NzbDrone.Core.Notifications.Pushover [FieldDefinition(2, Label = "Priority", Type = FieldType.Select, SelectOptions = typeof(PushoverPriority) )] public int Priority { get; set; } - [FieldDefinition(3, Label = "Sound", Type = FieldType.Textbox, HelpText = "Notification sound, leave blank to use the default", HelpLink = "https://pushover.net/api#sounds")] + [FieldDefinition(3, Label = "Retry", Type = FieldType.Textbox, HelpText = "Interval to retry Emergency alerts, minimum 30 seconds")] + public int Retry { get; set; } + + [FieldDefinition(4, Label = "Expire", Type = FieldType.Textbox, HelpText = "Maximum time to retry Emergency alerts, maximum 86400 seconds")] + public int Expire { get; set; } + + [FieldDefinition(5, Label = "Sound", Type = FieldType.Textbox, HelpText = "Notification sound, leave blank to use the default", HelpLink = "https://pushover.net/api#sounds")] public string Sound { get; set; } public bool IsValid