Improved PushBullet implementation (v2 API, multiple devices, channels)

New: PushBullet supports multiple devices
New: PushBullet channels

Closes #641
pull/6/head
Mark McDowall 10 years ago
parent 120be567d3
commit 77e9493ccf

@ -0,0 +1,90 @@
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Notifications;
using NzbDrone.Core.Notifications.PushBullet;
using NzbDrone.Core.Notifications.Pushover;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Datastore.Migration
{
[TestFixture]
public class pushbullet_device_idsFixture : MigrationTest<Core.Datastore.Migration.pushbullet_device_ids>
{
[Test]
public void should_not_fail_if_no_pushbullet()
{
WithTestDb(c =>
{
c.Insert.IntoTable("Notifications").Row(new
{
OnGrab = false,
OnDownload = false,
OnUpgrade = false,
Name = "Pushover",
Implementation = "Pushover",
Settings = new PushoverSettings().ToJson(),
ConfigContract = "PushoverSettings"
});
});
var items = Mocker.Resolve<NotificationRepository>().All();
items.Should().HaveCount(1);
}
[Test]
public void should_not_fail_if_deviceId_is_not_set()
{
WithTestDb(c =>
{
c.Insert.IntoTable("Notifications").Row(new
{
OnGrab = false,
OnDownload = false,
OnUpgrade = false,
Name = "PushBullet",
Implementation = "PushBullet",
Settings = new
{
ApiKey = "my_api_key",
}.ToJson(),
ConfigContract = "PushBulletSettings"
});
});
var items = Mocker.Resolve<NotificationRepository>().All();
items.Should().HaveCount(1);
}
[Test]
public void should_add_deviceIds_setting_matching_deviceId()
{
var deviceId = "device_id";
WithTestDb(c =>
{
c.Insert.IntoTable("Notifications").Row(new
{
OnGrab = false,
OnDownload = false,
OnUpgrade = false,
Name = "PushBullet",
Implementation = "PushBullet",
Settings = new
{
ApiKey = "my_api_key",
DeviceId = deviceId
}.ToJson(),
ConfigContract = "PushBulletSettings"
});
});
var items = Mocker.Resolve<NotificationRepository>().All();
items.Should().HaveCount(1);
items.First().Settings.As<PushBulletSettings>().DeviceIds.Should().Be(deviceId);
}
}
}

@ -121,6 +121,7 @@
<Compile Include="Datastore\Migration\074_disable_eztvFixture.cs" /> <Compile Include="Datastore\Migration\074_disable_eztvFixture.cs" />
<Compile Include="Datastore\Migration\072_history_grabIdFixture.cs" /> <Compile Include="Datastore\Migration\072_history_grabIdFixture.cs" />
<Compile Include="Datastore\Migration\070_delay_profileFixture.cs" /> <Compile Include="Datastore\Migration\070_delay_profileFixture.cs" />
<Compile Include="Datastore\Migration\086_pushbullet_device_idsFixture.cs" />
<Compile Include="Datastore\Migration\085_expand_transmission_urlbaseFixture.cs" /> <Compile Include="Datastore\Migration\085_expand_transmission_urlbaseFixture.cs" />
<Compile Include="Datastore\Migration\084_update_quality_minmax_sizeFixture.cs" /> <Compile Include="Datastore\Migration\084_update_quality_minmax_sizeFixture.cs" />
<Compile Include="Datastore\Migration\081_move_dot_prefix_to_transmission_categoryFixture.cs" /> <Compile Include="Datastore\Migration\081_move_dot_prefix_to_transmission_categoryFixture.cs" />

@ -26,6 +26,7 @@ namespace NzbDrone.Core.Annotations
Checkbox, Checkbox,
Select, Select,
Path, Path,
Hidden Hidden,
Tag
} }
} }

@ -0,0 +1,55 @@
using System.Collections.Generic;
using System.Data;
using FluentMigrator;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration
{
[Migration(86)]
public class pushbullet_device_ids : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
Execute.WithConnection(UpdateTransmissionSettings);
}
private void UpdateTransmissionSettings(IDbConnection conn, IDbTransaction tran)
{
using (var cmd = conn.CreateCommand())
{
cmd.Transaction = tran;
cmd.CommandText = "SELECT Id, Settings FROM Notifications WHERE Implementation = 'PushBullet'";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var id = reader.GetInt32(0);
var settingsJson = reader.GetString(1);
var settings = Json.Deserialize<Dictionary<string, object>>(settingsJson);
if (settings.ContainsKey("deviceId"))
{
var deviceId = settings.GetValueOrDefault("deviceId", "") as string;
settings.Add("deviceIds", deviceId);
settings.Remove("deviceId");
using (var updateCmd = conn.CreateCommand())
{
updateCmd.Transaction = tran;
updateCmd.CommandText = "UPDATE Notifications SET Settings = ? WHERE Id = ?";
updateCmd.AddParameter(settings.ToJson());
updateCmd.AddParameter(id);
updateCmd.ExecuteNonQuery();
}
}
}
}
}
}
}
}

@ -23,14 +23,14 @@ namespace NzbDrone.Core.Notifications.PushBullet
{ {
const string title = "Sonarr - Episode Grabbed"; const string title = "Sonarr - Episode Grabbed";
_proxy.SendNotification(title, message, Settings.ApiKey, Settings.DeviceId); _proxy.SendNotification(title, message, Settings);
} }
public override void OnDownload(DownloadMessage message) public override void OnDownload(DownloadMessage message)
{ {
const string title = "Sonarr - Episode Downloaded"; const string title = "Sonarr - Episode Downloaded";
_proxy.SendNotification(title, message.Message, Settings.ApiKey, Settings.DeviceId); _proxy.SendNotification(title, message.Message, Settings);
} }
public override void AfterRename(Series series) public override void AfterRename(Series series)

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

@ -1,7 +1,10 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Net; using System.Net;
using FluentValidation.Results; using FluentValidation.Results;
using NLog; using NLog;
using NzbDrone.Common.Extensions;
using RestSharp; using RestSharp;
using NzbDrone.Core.Rest; using NzbDrone.Core.Rest;
@ -9,49 +12,82 @@ namespace NzbDrone.Core.Notifications.PushBullet
{ {
public interface IPushBulletProxy public interface IPushBulletProxy
{ {
void SendNotification(string title, string message, string apiKey, string deviceId); void SendNotification(string title, string message, PushBulletSettings settings);
ValidationFailure Test(PushBulletSettings settings); ValidationFailure Test(PushBulletSettings settings);
} }
public class PushBulletProxy : IPushBulletProxy public class PushBulletProxy : IPushBulletProxy
{ {
private readonly Logger _logger; private readonly Logger _logger;
private const string URL = "https://api.pushbullet.com/api/pushes"; private const string URL = "https://api.pushbullet.com/v2/pushes";
public PushBulletProxy(Logger logger) public PushBulletProxy(Logger logger)
{ {
_logger = logger; _logger = logger;
} }
public void SendNotification(string title, string message, string apiKey, string deviceId) public void SendNotification(string title, string message, PushBulletSettings settings)
{ {
var client = RestClientFactory.BuildClient(URL); var channelTags = GetIds(settings.ChannelTags);
var request = BuildRequest(deviceId); var deviceIds = GetIds(settings.DeviceIds);
var error = false;
request.AddParameter("type", "note");
request.AddParameter("title", title);
request.AddParameter("body", message);
client.Authenticator = new HttpBasicAuthenticator(apiKey, String.Empty);
client.ExecuteAndValidate(request);
}
public RestRequest BuildRequest(string deviceId)
{
var request = new RestRequest(Method.POST);
long integerId;
if (Int64.TryParse(deviceId, out integerId)) if (channelTags.Any())
{ {
request.AddParameter("device_id", integerId); foreach (var channelTag in channelTags)
} {
var request = BuildChannelRequest(channelTag);
try
{
SendNotification(title, message, request, settings);
}
catch (PushBulletException ex)
{
_logger.ErrorException("Unable to send test message to: " + channelTag, ex);
error = true;
}
}
}
else else
{ {
request.AddParameter("device_iden", deviceId); if (deviceIds.Any())
{
foreach (var deviceId in deviceIds)
{
var request = BuildDeviceRequest(deviceId);
try
{
SendNotification(title, message, request, settings);
}
catch (PushBulletException ex)
{
_logger.ErrorException("Unable to send test message to: " + deviceId, ex);
error = true;
}
}
}
else
{
var request = BuildDeviceRequest(null);
try
{
SendNotification(title, message, request, settings);
}
catch (PushBulletException ex)
{
_logger.ErrorException("Unable to send test message to all devices", ex);
error = true;
}
}
} }
return request; if (error)
{
throw new PushBulletException("Unable to send PushBullet notifications to all channels or devices");
}
} }
public ValidationFailure Test(PushBulletSettings settings) public ValidationFailure Test(PushBulletSettings settings)
@ -61,7 +97,7 @@ namespace NzbDrone.Core.Notifications.PushBullet
const string title = "Sonarr - Test Notification"; const string title = "Sonarr - Test Notification";
const string body = "This is a test message from Sonarr"; const string body = "This is a test message from Sonarr";
SendNotification(title, body, settings.ApiKey, settings.DeviceId); SendNotification(title, body, settings);
} }
catch (RestException ex) catch (RestException ex)
{ {
@ -82,5 +118,63 @@ namespace NzbDrone.Core.Notifications.PushBullet
return null; return null;
} }
private RestRequest BuildDeviceRequest(string deviceId)
{
var request = new RestRequest(Method.POST);
long integerId;
if (Int64.TryParse(deviceId, out integerId))
{
request.AddParameter("device_id", integerId);
}
else
{
request.AddParameter("device_iden", deviceId);
}
return request;
}
private RestRequest BuildChannelRequest(string channelTag)
{
var request = new RestRequest(Method.POST);
request.AddParameter("channel_tag", channelTag);
return request;
}
private void SendNotification(string title, string message, RestRequest request, PushBulletSettings settings)
{
try
{
var client = RestClientFactory.BuildClient(URL);
request.AddParameter("type", "note");
request.AddParameter("title", title);
request.AddParameter("body", message);
client.Authenticator = new HttpBasicAuthenticator(settings.ApiKey, String.Empty);
client.ExecuteAndValidate(request);
}
catch (RestException ex)
{
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
{
_logger.ErrorException("API Key is invalid: " + ex.Message, ex);
throw;
}
throw new PushBulletException("Unable to send text message: {0}", ex, ex.Message);
}
}
private List<string> GetIds(string input)
{
if (input.IsNullOrWhiteSpace()) return new List<string>();
return input.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
} }
} }

@ -21,16 +21,11 @@ namespace NzbDrone.Core.Notifications.PushBullet
[FieldDefinition(0, Label = "API Key", HelpLink = "https://www.pushbullet.com/")] [FieldDefinition(0, Label = "API Key", HelpLink = "https://www.pushbullet.com/")]
public String ApiKey { get; set; } public String ApiKey { get; set; }
[FieldDefinition(1, Label = "Device ID", HelpText = "device_iden in the device's URL on pushbullet.com (leave blank to send to all devices)")] [FieldDefinition(1, Label = "Device IDs", HelpText = "List of device IDs, use device_iden in the device's URL on pushbullet.com (leave blank to send to all devices)", Type = FieldType.Tag)]
public String DeviceId { get; set; } public String DeviceIds { get; set; }
public bool IsValid [FieldDefinition(2, Label = "Channel Tags", HelpText = "List of Channel Tags to send notifications to", Type = FieldType.Tag)]
{ public String ChannelTags { get; set; }
get
{
return !String.IsNullOrWhiteSpace(ApiKey) && !String.IsNullOrWhiteSpace(DeviceId);
}
}
public NzbDroneValidationResult Validate() public NzbDroneValidationResult Validate()
{ {

@ -254,6 +254,7 @@
<Compile Include="Datastore\Migration\073_clear_ratings.cs" /> <Compile Include="Datastore\Migration\073_clear_ratings.cs" />
<Compile Include="Datastore\Migration\077_add_add_options_to_series.cs" /> <Compile Include="Datastore\Migration\077_add_add_options_to_series.cs" />
<Compile Include="Datastore\Migration\085_expand_transmission_urlbase.cs" /> <Compile Include="Datastore\Migration\085_expand_transmission_urlbase.cs" />
<Compile Include="Datastore\Migration\086_pushbullet_device_ids.cs" />
<Compile Include="Datastore\Migration\081_move_dot_prefix_to_transmission_category.cs" /> <Compile Include="Datastore\Migration\081_move_dot_prefix_to_transmission_category.cs" />
<Compile Include="Datastore\Migration\079_dedupe_tags.cs" /> <Compile Include="Datastore\Migration\079_dedupe_tags.cs" />
<Compile Include="Datastore\Migration\070_delay_profile.cs" /> <Compile Include="Datastore\Migration\070_delay_profile.cs" />
@ -708,6 +709,7 @@
<Compile Include="Notifications\Plex\PlexHomeTheater.cs" /> <Compile Include="Notifications\Plex\PlexHomeTheater.cs" />
<Compile Include="Notifications\Plex\PlexHomeTheaterSettings.cs" /> <Compile Include="Notifications\Plex\PlexHomeTheaterSettings.cs" />
<Compile Include="Notifications\Plex\PlexClientService.cs" /> <Compile Include="Notifications\Plex\PlexClientService.cs" />
<Compile Include="Notifications\PushBullet\PushBulletException.cs" />
<Compile Include="Notifications\Synology\SynologyException.cs" /> <Compile Include="Notifications\Synology\SynologyException.cs" />
<Compile Include="Notifications\Synology\SynologyIndexer.cs" /> <Compile Include="Notifications\Synology\SynologyIndexer.cs" />
<Compile Include="Notifications\Synology\SynologyIndexerProxy.cs" /> <Compile Include="Notifications\Synology\SynologyIndexerProxy.cs" />

@ -32,6 +32,10 @@ var _fieldBuilder = function(field) {
return _templateRenderer.call(field, 'Form/PathTemplate'); return _templateRenderer.call(field, 'Form/PathTemplate');
} }
if (field.type === 'tag') {
return _templateRenderer.call(field, 'Form/TagTemplate');
}
return _templateRenderer.call(field, 'Form/TextboxTemplate'); return _templateRenderer.call(field, 'Form/TextboxTemplate');
}; };

@ -0,0 +1,9 @@
<div class="form-group {{#if advanced}}advanced-setting{{/if}}">
<label class="col-sm-3 control-label">{{label}}</label>
<div class="col-sm-5">
<input type="text" name="fields.{{order}}.value" validation-name="{{name}}" class="form-control x-form-tag"/>
</div>
{{> FormHelpPartial}}
</div>

@ -6,6 +6,7 @@ var AsValidatedView = require('../../../Mixins/AsValidatedView');
var AsEditModalView = require('../../../Mixins/AsEditModalView'); var AsEditModalView = require('../../../Mixins/AsEditModalView');
require('../../../Form/FormBuilder'); require('../../../Form/FormBuilder');
require('../../../Mixins/TagInput'); require('../../../Mixins/TagInput');
require('bootstrap.tagsinput');
var view = Marionette.ItemView.extend({ var view = Marionette.ItemView.extend({
template : 'Settings/Notifications/Edit/NotificationEditViewTemplate', template : 'Settings/Notifications/Edit/NotificationEditViewTemplate',
@ -13,7 +14,8 @@ var view = Marionette.ItemView.extend({
ui : { ui : {
onDownloadToggle : '.x-on-download', onDownloadToggle : '.x-on-download',
onUpgradeSection : '.x-on-upgrade', onUpgradeSection : '.x-on-upgrade',
tags : '.x-tags' tags : '.x-tags',
formTag : '.x-form-tag'
}, },
events : { events : {
@ -29,10 +31,16 @@ var view = Marionette.ItemView.extend({
onRender : function() { onRender : function() {
this._onDownloadChanged(); this._onDownloadChanged();
this.ui.tags.tagInput({ this.ui.tags.tagInput({
model : this.model, model : this.model,
property : 'tags' property : 'tags'
}); });
this.ui.formTag.tagsinput({
trimValue : true,
tagClass : 'label label-default'
});
}, },
_onAfterSave : function() { _onAfterSave : function() {

Loading…
Cancel
Save