From 626d94d43536a5475593f3db8245e70113d1b4b5 Mon Sep 17 00:00:00 2001 From: Christopher Heath Date: Tue, 26 Jul 2016 11:12:09 -0500 Subject: [PATCH] New: Join notifications Closes #1197 --- src/NzbDrone.Core/Notifications/Join/Join.cs | 65 +++++++++++++ .../Notifications/Join/JoinAuthException.cs | 16 ++++ .../Notifications/Join/JoinException.cs | 16 ++++ .../Notifications/Join/JoinProxy.cs | 95 +++++++++++++++++++ .../Notifications/Join/JoinResponseModel.cs | 13 +++ .../Notifications/Join/JoinSettings.cs | 29 ++++++ src/NzbDrone.Core/NzbDrone.Core.csproj | 6 ++ 7 files changed, 240 insertions(+) create mode 100644 src/NzbDrone.Core/Notifications/Join/Join.cs create mode 100644 src/NzbDrone.Core/Notifications/Join/JoinAuthException.cs create mode 100644 src/NzbDrone.Core/Notifications/Join/JoinException.cs create mode 100644 src/NzbDrone.Core/Notifications/Join/JoinProxy.cs create mode 100644 src/NzbDrone.Core/Notifications/Join/JoinResponseModel.cs create mode 100644 src/NzbDrone.Core/Notifications/Join/JoinSettings.cs diff --git a/src/NzbDrone.Core/Notifications/Join/Join.cs b/src/NzbDrone.Core/Notifications/Join/Join.cs new file mode 100644 index 000000000..78a47dd5e --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Join/Join.cs @@ -0,0 +1,65 @@ +using System.Collections.Generic; +using FluentValidation.Results; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.Notifications.Join +{ + public class Join : NotificationBase + { + private readonly IJoinProxy _proxy; + + public Join(IJoinProxy proxy) + { + _proxy = proxy; + } + + public override string Link + { + get { return "https://joinjoaomgcd.appspot.com/"; } + } + + public override void OnGrab(GrabMessage grabMessage) + { + const string title = "Sonarr - Episode Grabbed"; + + _proxy.SendNotification(title, grabMessage.Message, Settings); + } + + public override void OnDownload(DownloadMessage message) + { + const string title = "Sonarr - Episode Downloaded"; + + _proxy.SendNotification(title, message.Message, Settings); + } + + public override void OnRename(Series series) + { + } + + public override string Name + { + get + { + return "Join"; + } + } + + public override bool SupportsOnRename + { + get + { + return false; + } + } + + public override ValidationResult Test() + { + var failures = new List(); + + failures.AddIfNotNull(_proxy.Test(Settings)); + + return new ValidationResult(failures); + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Join/JoinAuthException.cs b/src/NzbDrone.Core/Notifications/Join/JoinAuthException.cs new file mode 100644 index 000000000..888bf78fa --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Join/JoinAuthException.cs @@ -0,0 +1,16 @@ +using System; +using NzbDrone.Common.Exceptions; + +namespace NzbDrone.Core.Notifications.Join +{ + public class JoinAuthException : JoinException + { + public JoinAuthException(string message) : base(message) + { + } + + public JoinAuthException(string message, Exception innerException, params object[] args) : base(message, innerException, args) + { + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Join/JoinException.cs b/src/NzbDrone.Core/Notifications/Join/JoinException.cs new file mode 100644 index 000000000..e0ad0e1c3 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Join/JoinException.cs @@ -0,0 +1,16 @@ +using System; +using NzbDrone.Common.Exceptions; + +namespace NzbDrone.Core.Notifications.Join +{ + public class JoinException : NzbDroneException + { + public JoinException(string message) : base(message) + { + } + + public JoinException(string message, Exception innerException, params object[] args) : base(message, innerException, args) + { + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Join/JoinProxy.cs b/src/NzbDrone.Core/Notifications/Join/JoinProxy.cs new file mode 100644 index 000000000..a9da72656 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Join/JoinProxy.cs @@ -0,0 +1,95 @@ +using System; +using System.Linq; +using System.Net; +using FluentValidation.Results; +using NLog; +using RestSharp; +using NzbDrone.Core.Rest; +using NzbDrone.Common.Serializer; + +namespace NzbDrone.Core.Notifications.Join +{ + public interface IJoinProxy + { + void SendNotification(string title, string message, JoinSettings settings); + ValidationFailure Test(JoinSettings settings); + } + + public class JoinProxy : IJoinProxy + { + private readonly Logger _logger; + private const string URL = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?"; + + public JoinProxy(Logger logger) + { + _logger = logger; + } + + public void SendNotification(string title, string message, JoinSettings settings) + { + var request = new RestRequest(Method.GET); + + try + { + SendNotification(title, message, request, settings); + } + catch (JoinException ex) + { + _logger.Error(ex, "Unable to send Join message."); + throw new JoinException("Unable to send Join notifications. " + ex.Message); + } + } + + public ValidationFailure Test(JoinSettings settings) + { + const string title = "Test Notification"; + const string body = "This is a test message from Sonarr."; + + try + { + SendNotification(title, body, settings); + return null; + } + catch (JoinException ex) + { + _logger.Error(ex, "Unable to send test Join message."); + return new ValidationFailure("APIKey", ex.Message); + } + } + + private void SendNotification(string title, string message, RestRequest request, JoinSettings settings) + { + try + { + var client = RestClientFactory.BuildClient(URL); + + request.AddParameter("deviceId", "group.all"); + request.AddParameter("apikey", settings.APIKey); + request.AddParameter("title", title); + request.AddParameter("text", message); + request.AddParameter("icon", "https://cdn.rawgit.com/Sonarr/Sonarr/develop/Logo/256.png"); // Use the Sonarr logo. + + var response = client.ExecuteAndValidate(request); + var res = Json.Deserialize(response.Content); + + if (res.success) return; + + if (res.errorMessage != null) + { + throw new JoinException(res.errorMessage); + } + + if (res.userAuthError) + { + throw new JoinAuthException("Authentication failed."); + } + + throw new JoinException("Unknown error. Join message failed to send."); + } + catch(Exception e) + { + throw new JoinException(e.Message, e); + } + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Join/JoinResponseModel.cs b/src/NzbDrone.Core/Notifications/Join/JoinResponseModel.cs new file mode 100644 index 000000000..67da52822 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Join/JoinResponseModel.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace NzbDrone.Core.Notifications.Join +{ + public class JoinResponseModel + { + public bool success { get; set; } + public bool userAuthError { get; set; } + public string errorMessage { get; set; } + public string kind { get; set; } + public string etag { get; set; } + } +} diff --git a/src/NzbDrone.Core/Notifications/Join/JoinSettings.cs b/src/NzbDrone.Core/Notifications/Join/JoinSettings.cs new file mode 100644 index 000000000..a8adb77b9 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Join/JoinSettings.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using FluentValidation; +using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; +using NzbDrone.Core.Validation; + +namespace NzbDrone.Core.Notifications.Join +{ + public class JoinSettingsValidator : AbstractValidator + { + public JoinSettingsValidator() + { + RuleFor(c => c.APIKey).NotEmpty(); + } + } + + public class JoinSettings : IProviderConfig + { + private static readonly JoinSettingsValidator Validator = new JoinSettingsValidator(); + + [FieldDefinition(0, Label = "API Key", HelpText = "The API Key from your Join account settings (click Join API button).", HelpLink = "https://joinjoaomgcd.appspot.com/")] + public string APIKey { get; set; } + + public NzbDroneValidationResult Validate() + { + return new NzbDroneValidationResult(Validator.Validate(this)); + } + } +} diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 4a2f3f438..e4e8b1b48 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -779,6 +779,12 @@ + + + + + +