Closes #1989 Closes #1990 Co-Authored-By: Timm Schäuble <Timm.Schaeuble@gmail.com> (cherry picked from commit 4c7df31070fbd370b26dbcc07131f21eb88d35fc)pull/2158/head
parent
1caa49c895
commit
bc63587428
@ -0,0 +1,73 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using FluentValidation.Results;
|
||||||
|
using NzbDrone.Common.Extensions;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Notifications.Simplepush
|
||||||
|
{
|
||||||
|
public class Simplepush : NotificationBase<SimplepushSettings>
|
||||||
|
{
|
||||||
|
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(BOOK_GRABBED_TITLE, grabMessage.Message, Settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnReleaseImport(BookDownloadMessage message)
|
||||||
|
{
|
||||||
|
_proxy.SendNotification(BOOK_DOWNLOADED_TITLE, message.Message, Settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnAuthorDelete(AuthorDeleteMessage deleteMessage)
|
||||||
|
{
|
||||||
|
_proxy.SendNotification(AUTHOR_DELETED_TITLE, deleteMessage.Message, Settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnBookDelete(BookDeleteMessage deleteMessage)
|
||||||
|
{
|
||||||
|
_proxy.SendNotification(BOOK_DELETED_TITLE, deleteMessage.Message, Settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnBookFileDelete(BookFileDeleteMessage deleteMessage)
|
||||||
|
{
|
||||||
|
_proxy.SendNotification(BOOK_FILE_DELETED_TITLE, deleteMessage.Message, Settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck)
|
||||||
|
{
|
||||||
|
_proxy.SendNotification(HEALTH_ISSUE_TITLE, healthCheck.Message, Settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnDownloadFailure(DownloadFailedMessage message)
|
||||||
|
{
|
||||||
|
_proxy.SendNotification(DOWNLOAD_FAILURE_TITLE, message.Message, Settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnImportFailure(BookDownloadMessage message)
|
||||||
|
{
|
||||||
|
_proxy.SendNotification(IMPORT_FAILURE_TITLE, message.Message, Settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage)
|
||||||
|
{
|
||||||
|
_proxy.SendNotification(APPLICATION_UPDATE_TITLE, updateMessage.Message, Settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override ValidationResult Test()
|
||||||
|
{
|
||||||
|
var failures = new List<ValidationFailure>();
|
||||||
|
|
||||||
|
failures.AddIfNotNull(_proxy.Test(Settings));
|
||||||
|
|
||||||
|
return new ValidationResult(failures);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
using System;
|
||||||
|
using FluentValidation.Results;
|
||||||
|
using NLog;
|
||||||
|
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 Readarr";
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
using FluentValidation;
|
||||||
|
using NzbDrone.Core.Annotations;
|
||||||
|
using NzbDrone.Core.ThingiProvider;
|
||||||
|
using NzbDrone.Core.Validation;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Notifications.Simplepush
|
||||||
|
{
|
||||||
|
public class SimplepushSettingsValidator : AbstractValidator<SimplepushSettings>
|
||||||
|
{
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue