From d0fbcffa4210dc326dfd303c826f8332d6f90765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20Sch=C3=A4uble?= Date: Sat, 2 Oct 2021 17:15:54 +0200 Subject: [PATCH] New: Add support for Simplepush notifications (#6597) * New: Add support for Simplepush notifications * Add HelpText for Simplepush Events field --- .../Notifications/Simplepush/Simplepush.cs | 53 +++++++++++++++++ .../Simplepush/SimplepushProxy.cs | 59 +++++++++++++++++++ .../Simplepush/SimplepushSettings.cs | 34 +++++++++++ 3 files changed, 146 insertions(+) create mode 100644 src/NzbDrone.Core/Notifications/Simplepush/Simplepush.cs create mode 100644 src/NzbDrone.Core/Notifications/Simplepush/SimplepushProxy.cs create mode 100644 src/NzbDrone.Core/Notifications/Simplepush/SimplepushSettings.cs diff --git a/src/NzbDrone.Core/Notifications/Simplepush/Simplepush.cs b/src/NzbDrone.Core/Notifications/Simplepush/Simplepush.cs new file mode 100644 index 000000000..af332f1cd --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Simplepush/Simplepush.cs @@ -0,0 +1,53 @@ +using System.Collections.Generic; +using FluentValidation.Results; +using NzbDrone.Common.Extensions; + +namespace NzbDrone.Core.Notifications.Simplepush +{ + public class Simplepush : NotificationBase + { + private readonly ISimplepushProxy _proxy; + + public Simplepush(ISimplepushProxy proxy) + { + _proxy = proxy; + } + + public override string Name => "Simplepush"; + public override string Link => "https://simplepush.io/"; + + public override void OnGrab(GrabMessage grabMessage) + { + _proxy.SendNotification(MOVIE_GRABBED_TITLE, grabMessage.Message, Settings); + } + + public override void OnDownload(DownloadMessage message) + { + _proxy.SendNotification(MOVIE_DOWNLOADED_TITLE, message.Message, Settings); + } + + public override void OnMovieFileDelete(MovieFileDeleteMessage deleteMessage) + { + _proxy.SendNotification(MOVIE_FILE_DELETED_TITLE, deleteMessage.Message, Settings); + } + + public override void OnMovieDelete(MovieDeleteMessage deleteMessage) + { + _proxy.SendNotification(MOVIE_DELETED_TITLE, deleteMessage.Message, Settings); + } + + public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) + { + _proxy.SendNotification(HEALTH_ISSUE_TITLE, healthCheck.Message, Settings); + } + + public override ValidationResult Test() + { + var failures = new List(); + + failures.AddIfNotNull(_proxy.Test(Settings)); + + return new ValidationResult(failures); + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Simplepush/SimplepushProxy.cs b/src/NzbDrone.Core/Notifications/Simplepush/SimplepushProxy.cs new file mode 100644 index 000000000..5254b73de --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Simplepush/SimplepushProxy.cs @@ -0,0 +1,59 @@ +using System; +using FluentValidation.Results; +using NLog; +using NzbDrone.Common.Extensions; +using NzbDrone.Common.Http; + +namespace NzbDrone.Core.Notifications.Simplepush +{ + public interface ISimplepushProxy + { + void SendNotification(string title, string message, SimplepushSettings settings); + ValidationFailure Test(SimplepushSettings settings); + } + + public class SimplepushProxy : ISimplepushProxy + { + private const string URL = "https://api.simplepush.io/send"; + private readonly IHttpClient _httpClient; + private readonly Logger _logger; + + public SimplepushProxy(IHttpClient httpClient, Logger logger) + { + _httpClient = httpClient; + _logger = logger; + } + + public void SendNotification(string title, string message, SimplepushSettings settings) + { + var requestBuilder = new HttpRequestBuilder(URL).Post(); + + requestBuilder.AddFormParameter("key", settings.Key) + .AddFormParameter("event", settings.Event) + .AddFormParameter("title", title) + .AddFormParameter("msg", message); + + var request = requestBuilder.Build(); + + _httpClient.Post(request); + } + + public ValidationFailure Test(SimplepushSettings settings) + { + try + { + const string title = "Test Notification"; + const string body = "This is a test message from Radarr"; + + SendNotification(title, body, settings); + } + catch (Exception ex) + { + _logger.Error(ex, "Unable to send test message"); + return new ValidationFailure("ApiKey", "Unable to send test message"); + } + + return null; + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Simplepush/SimplepushSettings.cs b/src/NzbDrone.Core/Notifications/Simplepush/SimplepushSettings.cs new file mode 100644 index 000000000..92daff3b4 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Simplepush/SimplepushSettings.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using FluentValidation; +using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; +using NzbDrone.Core.Validation; + +namespace NzbDrone.Core.Notifications.Simplepush +{ + public class SimplepushSettingsValidator : AbstractValidator + { + public SimplepushSettingsValidator() + { + RuleFor(c => c.Key).NotEmpty(); + } + } + + public class SimplepushSettings : IProviderConfig + { + private static readonly SimplepushSettingsValidator Validator = new SimplepushSettingsValidator(); + + [FieldDefinition(0, Label = "Key", Privacy = PrivacyLevel.ApiKey, HelpLink = "https://simplepush.io/features")] + public string Key { get; set; } + + [FieldDefinition(1, Label = "Event", HelpText = "Customize the behavior of push notifications", HelpLink = "https://simplepush.io/features")] + public string Event { get; set; } + + public bool IsValid => !string.IsNullOrWhiteSpace(Key); + + public NzbDroneValidationResult Validate() + { + return new NzbDroneValidationResult(Validator.Validate(this)); + } + } +}