From c42777086beb00bd8ebf07eea4ed17409587b9a1 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Mon, 30 Dec 2013 20:48:14 -0800 Subject: [PATCH] New: PushOver will now require an application per user to avoid API limiting issues --- .../Migration/033_add_api_key_to_pushover.cs | 67 +++++++++++++++++++ .../Notifications/Pushover/Pushover.cs | 4 +- .../Pushover/PushoverPriority.cs | 2 +- .../Notifications/Pushover/PushoverService.cs | 9 ++- .../Pushover/PushoverSettings.cs | 7 +- .../Pushover/TestPushoverCommand.cs | 2 + src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 7 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 src/NzbDrone.Core/Datastore/Migration/033_add_api_key_to_pushover.cs diff --git a/src/NzbDrone.Core/Datastore/Migration/033_add_api_key_to_pushover.cs b/src/NzbDrone.Core/Datastore/Migration/033_add_api_key_to_pushover.cs new file mode 100644 index 000000000..284316203 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/033_add_api_key_to_pushover.cs @@ -0,0 +1,67 @@ +using System; +using System.Data; +using FluentMigrator; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(33)] + public class add_api_key_to_pushover : NzbDroneMigrationBase + { + private const string API_KEY = "yz9b4U215iR4vrKFRfjNXP24NMNPKJ"; + + protected override void MainDbUpgrade() + { + Execute.WithConnection(UpdatePushoverSettings); + } + + private void UpdatePushoverSettings(IDbConnection conn, IDbTransaction tran) + { + using (IDbCommand selectCommand = conn.CreateCommand()) + { + selectCommand.Transaction = tran; + selectCommand.CommandText = @"SELECT * FROM Notifications WHERE ConfigContract = 'PushoverSettings'"; + + using (IDataReader reader = selectCommand.ExecuteReader()) + { + while (reader.Read()) + { + var idIndex = reader.GetOrdinal("Id"); + var settingsIndex = reader.GetOrdinal("Settings"); + + var id = reader.GetInt32(idIndex); + var settings = Json.Deserialize(reader.GetString(settingsIndex)); + settings.ApiKey = API_KEY; + + //Set priority to high if its currently emergency + if (settings.Priority == 2) + { + settings.Priority = 1; + } + + using (IDbCommand updateCmd = conn.CreateCommand()) + { + var text = String.Format("UPDATE Notifications " + + "SET Settings = '{0}'" + + "WHERE Id = {1}", + settings.ToJson(), id + ); + + updateCmd.Transaction = tran; + updateCmd.CommandText = text; + updateCmd.ExecuteNonQuery(); + } + } + } + } + } + + private class PushoverSettingsForV33 + { + public String ApiKey { get; set; } + public String UserKey { get; set; } + public Int32 Priority { get; set; } + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Pushover/Pushover.cs b/src/NzbDrone.Core/Notifications/Pushover/Pushover.cs index a9f447c0f..36258ebea 100644 --- a/src/NzbDrone.Core/Notifications/Pushover/Pushover.cs +++ b/src/NzbDrone.Core/Notifications/Pushover/Pushover.cs @@ -20,14 +20,14 @@ namespace NzbDrone.Core.Notifications.Pushover { const string title = "Episode Grabbed"; - _pushoverProxy.SendNotification(title, message, Settings.UserKey, (PushoverPriority)Settings.Priority); + _pushoverProxy.SendNotification(title, message, Settings.ApiKey, Settings.UserKey, (PushoverPriority)Settings.Priority); } public override void OnDownload(DownloadMessage message) { const string title = "Episode Downloaded"; - _pushoverProxy.SendNotification(title, message.Message, Settings.UserKey, (PushoverPriority)Settings.Priority); + _pushoverProxy.SendNotification(title, message.Message, Settings.ApiKey, Settings.UserKey, (PushoverPriority)Settings.Priority); } public override void AfterRename(Series series) diff --git a/src/NzbDrone.Core/Notifications/Pushover/PushoverPriority.cs b/src/NzbDrone.Core/Notifications/Pushover/PushoverPriority.cs index 2e159b200..6ad6c5339 100644 --- a/src/NzbDrone.Core/Notifications/Pushover/PushoverPriority.cs +++ b/src/NzbDrone.Core/Notifications/Pushover/PushoverPriority.cs @@ -5,6 +5,6 @@ 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 9813b9464..7527d3b66 100644 --- a/src/NzbDrone.Core/Notifications/Pushover/PushoverService.cs +++ b/src/NzbDrone.Core/Notifications/Pushover/PushoverService.cs @@ -6,19 +6,18 @@ namespace NzbDrone.Core.Notifications.Pushover { public interface IPushoverProxy { - void SendNotification(string title, string message, string userKey, PushoverPriority priority); + void SendNotification(string title, string message, string apiKey, string userKey, PushoverPriority priority); } public class PushoverProxy : IPushoverProxy, IExecute { - private const string TOKEN = "yz9b4U215iR4vrKFRfjNXP24NMNPKJ"; private const string URL = "https://api.pushover.net/1/messages.json"; - public void SendNotification(string title, string message, string userKey, PushoverPriority priority) + public void SendNotification(string title, string message, string apiKey, string userKey, PushoverPriority priority) { var client = new RestClient(URL); var request = new RestRequest(Method.POST); - request.AddParameter("token", TOKEN); + request.AddParameter("token", apiKey); request.AddParameter("user", userKey); request.AddParameter("title", title); request.AddParameter("message", message); @@ -32,7 +31,7 @@ namespace NzbDrone.Core.Notifications.Pushover const string title = "Test Notification"; const string body = "This is a test message from NzbDrone"; - SendNotification(title, body, message.UserKey, (PushoverPriority)message.Priority); + SendNotification(title, body, message.ApiKey, message.UserKey, (PushoverPriority)message.Priority); } } } diff --git a/src/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs b/src/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs index debf640e2..178ef3bd8 100644 --- a/src/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs +++ b/src/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs @@ -18,10 +18,13 @@ namespace NzbDrone.Core.Notifications.Pushover { private static readonly PushoverSettingsValidator Validator = new PushoverSettingsValidator(); - [FieldDefinition(0, Label = "User Key", HelpLink = "https://pushover.net/")] + [FieldDefinition(0, Label = "API Key", HelpLink = "https://pushover.net/apps/clone/nzbdrone")] + public String ApiKey { get; set; } + + [FieldDefinition(1, Label = "User Key", HelpLink = "https://pushover.net/")] public String UserKey { get; set; } - [FieldDefinition(1, Label = "Priority", Type = FieldType.Select, SelectOptions = typeof(PushoverPriority) )] + [FieldDefinition(2, Label = "Priority", Type = FieldType.Select, SelectOptions = typeof(PushoverPriority) )] public Int32 Priority { get; set; } public bool IsValid diff --git a/src/NzbDrone.Core/Notifications/Pushover/TestPushoverCommand.cs b/src/NzbDrone.Core/Notifications/Pushover/TestPushoverCommand.cs index 13e232401..235679d57 100644 --- a/src/NzbDrone.Core/Notifications/Pushover/TestPushoverCommand.cs +++ b/src/NzbDrone.Core/Notifications/Pushover/TestPushoverCommand.cs @@ -12,6 +12,8 @@ namespace NzbDrone.Core.Notifications.Pushover return true; } } + + public string ApiKey { get; set; } public string UserKey { get; set; } public int Priority { get; set; } } diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index af8e20cee..599e2cdb1 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -189,6 +189,7 @@ +