1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

New: PushOver will now require an application per user to avoid API limiting issues

This commit is contained in:
Mark McDowall 2013-12-30 20:48:14 -08:00
parent 72e56a1845
commit c42777086b
7 changed files with 82 additions and 10 deletions

View File

@ -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<PushoverSettingsForV33>(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; }
}
}
}

View File

@ -20,14 +20,14 @@ public override void OnGrab(string message)
{ {
const string title = "Episode Grabbed"; 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) public override void OnDownload(DownloadMessage message)
{ {
const string title = "Episode Downloaded"; 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) public override void AfterRename(Series series)

View File

@ -5,6 +5,6 @@ public enum PushoverPriority
Quiet = -1, Quiet = -1,
Normal = 0, Normal = 0,
High = 1, High = 1,
Emergency = 2 //Emergency = 2
} }
} }

View File

@ -6,19 +6,18 @@ namespace NzbDrone.Core.Notifications.Pushover
{ {
public interface IPushoverProxy 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<TestPushoverCommand> public class PushoverProxy : IPushoverProxy, IExecute<TestPushoverCommand>
{ {
private const string TOKEN = "yz9b4U215iR4vrKFRfjNXP24NMNPKJ";
private const string URL = "https://api.pushover.net/1/messages.json"; 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 client = new RestClient(URL);
var request = new RestRequest(Method.POST); var request = new RestRequest(Method.POST);
request.AddParameter("token", TOKEN); request.AddParameter("token", apiKey);
request.AddParameter("user", userKey); request.AddParameter("user", userKey);
request.AddParameter("title", title); request.AddParameter("title", title);
request.AddParameter("message", message); request.AddParameter("message", message);
@ -32,7 +31,7 @@ public void Execute(TestPushoverCommand message)
const string title = "Test Notification"; const string title = "Test Notification";
const string body = "This is a test message from NzbDrone"; 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);
} }
} }
} }

View File

@ -18,10 +18,13 @@ public class PushoverSettings : IProviderConfig
{ {
private static readonly PushoverSettingsValidator Validator = new PushoverSettingsValidator(); 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; } 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 Int32 Priority { get; set; }
public bool IsValid public bool IsValid

View File

@ -12,6 +12,8 @@ public override bool SendUpdatesToClient
return true; return true;
} }
} }
public string ApiKey { get; set; }
public string UserKey { get; set; } public string UserKey { get; set; }
public int Priority { get; set; } public int Priority { get; set; }
} }

View File

@ -189,6 +189,7 @@
<Compile Include="Datastore\Migration\031_delete_old_naming_config_columns.cs" /> <Compile Include="Datastore\Migration\031_delete_old_naming_config_columns.cs" />
<Compile Include="Datastore\Migration\030_add_season_folder_format_to_naming_config.cs" /> <Compile Include="Datastore\Migration\030_add_season_folder_format_to_naming_config.cs" />
<Compile Include="Datastore\Migration\032_set_default_release_group.cs" /> <Compile Include="Datastore\Migration\032_set_default_release_group.cs" />
<Compile Include="Datastore\Migration\033_add_api_key_to_pushover.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationContext.cs" /> <Compile Include="Datastore\Migration\Framework\MigrationContext.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationController.cs" /> <Compile Include="Datastore\Migration\Framework\MigrationController.cs" />
<Compile Include="Datastore\Migration\Framework\MigrationExtension.cs" /> <Compile Include="Datastore\Migration\Framework\MigrationExtension.cs" />