New: Add back Prowl Notifications

pull/4062/head
Qstick 4 years ago
parent d3af103812
commit 7a1fa71b76

@ -9,7 +9,9 @@ namespace NzbDrone.Core.Datastore.Migration
protected override void MainDbUpgrade()
{
Delete.FromTable("Notifications").Row(new { Implementation = "Growl" });
Delete.FromTable("Notifications").Row(new { Implementation = "Prowl" });
// Prowl Added back
// Delete.FromTable("Notifications").Row(new { Implementation = "Prowl" });
}
}
}

@ -0,0 +1,43 @@
using System.Collections.Generic;
using FluentValidation.Results;
using NzbDrone.Common.Extensions;
namespace NzbDrone.Core.Notifications.Prowl
{
public class Prowl : NotificationBase<ProwlSettings>
{
private readonly IProwlProxy _prowlProxy;
public Prowl(IProwlProxy prowlProxy)
{
_prowlProxy = prowlProxy;
}
public override string Link => "https://www.prowlapp.com/";
public override string Name => "Prowl";
public override void OnGrab(GrabMessage grabMessage)
{
_prowlProxy.SendNotification(MOVIE_GRABBED_TITLE, grabMessage.Message, Settings.ApiKey, (ProwlPriority)Settings.Priority);
}
public override void OnDownload(DownloadMessage message)
{
_prowlProxy.SendNotification(MOVIE_DOWNLOADED_TITLE, message.Message, Settings.ApiKey, (ProwlPriority)Settings.Priority);
}
public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck)
{
_prowlProxy.SendNotification(HEALTH_ISSUE_TITLE, healthCheck.Message, Settings.ApiKey, (ProwlPriority)Settings.Priority);
}
public override ValidationResult Test()
{
var failures = new List<ValidationFailure>();
failures.AddIfNotNull(_prowlProxy.Test(Settings));
return new ValidationResult(failures);
}
}
}

@ -0,0 +1,18 @@
using System;
using NzbDrone.Common.Exceptions;
namespace NzbDrone.Core.Notifications.Prowl
{
public class ProwlException : NzbDroneException
{
public ProwlException(string message)
: base(message)
{
}
public ProwlException(string message, Exception innerException, params object[] args)
: base(message, innerException, args)
{
}
}
}

@ -0,0 +1,11 @@
namespace NzbDrone.Core.Notifications.Prowl
{
public enum ProwlPriority
{
VeryLow = -2,
Low = -1,
Normal = 0,
High = 1,
Emergency = 2
}
}

@ -0,0 +1,74 @@
using System;
using System.Net;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Rest;
using RestSharp;
namespace NzbDrone.Core.Notifications.Prowl
{
public interface IProwlProxy
{
void SendNotification(string title, string message, string apiKey, ProwlPriority priority = ProwlPriority.Normal, string url = null);
ValidationFailure Test(ProwlSettings settings);
}
public class ProwlProxy : IProwlProxy
{
private const string PUSH_URL = "https://api.prowlapp.com/publicapi/add";
private readonly IRestClientFactory _restClientFactory;
private readonly Logger _logger;
public ProwlProxy(IRestClientFactory restClientFactory, Logger logger)
{
_restClientFactory = restClientFactory;
_logger = logger;
}
public void SendNotification(string title, string message, string apiKey, ProwlPriority priority = ProwlPriority.Normal, string url = null)
{
try
{
var client = _restClientFactory.BuildClient(PUSH_URL);
var request = new RestRequest(Method.POST);
request.AddParameter("apikey", apiKey);
request.AddParameter("application", BuildInfo.AppName);
request.AddParameter("event", title);
request.AddParameter("description", message);
request.AddParameter("priority", priority);
request.AddParameter("url", url);
client.ExecuteAndValidate(request);
}
catch (RestException ex)
{
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
{
_logger.Error(ex, "Apikey is invalid: {0}", apiKey);
throw new ProwlException("Apikey is invalid", ex);
}
throw new ProwlException("Unable to send text message: " + ex.Message, ex);
}
}
public ValidationFailure Test(ProwlSettings settings)
{
try
{
const string title = "Test Notification";
const string body = "This is a test message from Radarr";
SendNotification(title, body, settings.ApiKey);
}
catch (Exception ex)
{
return new ValidationFailure("ApiKey", ex.Message);
}
return null;
}
}
}

@ -0,0 +1,33 @@
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.Prowl
{
public class ProwlSettingsValidator : AbstractValidator<ProwlSettings>
{
public ProwlSettingsValidator()
{
RuleFor(c => c.ApiKey).NotEmpty();
}
}
public class ProwlSettings : IProviderConfig
{
private static readonly ProwlSettingsValidator Validator = new ProwlSettingsValidator();
[FieldDefinition(0, Label = "API Key", HelpLink = "https://www.prowlapp.com/api_settings.php")]
public string ApiKey { get; set; }
[FieldDefinition(1, Label = "Priority", Type = FieldType.Select, SelectOptions = typeof(ProwlPriority))]
public int Priority { get; set; }
public bool IsValid => !string.IsNullOrWhiteSpace(ApiKey) && Priority >= -2 && Priority <= 2;
public NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}
Loading…
Cancel
Save