Use a list for ChannelTags and DeviceIds in Pushbullet settings

pull/3113/head
Mark McDowall 10 years ago
parent 461be2b388
commit 370406c3de

@ -28,15 +28,15 @@ namespace NzbDrone.Api.ClientSchema
{ {
var field = new Field var field = new Field
{ {
Name = propertyInfo.Name, Name = propertyInfo.Name,
Label = fieldAttribute.Label, Label = fieldAttribute.Label,
HelpText = fieldAttribute.HelpText, HelpText = fieldAttribute.HelpText,
HelpLink = fieldAttribute.HelpLink, HelpLink = fieldAttribute.HelpLink,
Order = fieldAttribute.Order, Order = fieldAttribute.Order,
Advanced = fieldAttribute.Advanced, Advanced = fieldAttribute.Advanced,
Type = fieldAttribute.Type.ToString().ToLowerInvariant() Type = fieldAttribute.Type.ToString().ToLowerInvariant()
}; };
var value = propertyInfo.GetValue(model, null); var value = propertyInfo.GetValue(model, null);
if (value != null) if (value != null)
@ -101,20 +101,37 @@ namespace NzbDrone.Api.ClientSchema
propertyInfo.SetValue(target, value, null); propertyInfo.SetValue(target, value, null);
} }
else if (propertyInfo.PropertyType == typeof (IEnumerable<Int32>)) else if (propertyInfo.PropertyType == typeof(IEnumerable<Int32>))
{ {
IEnumerable<Int32> value; IEnumerable<Int32> value;
if (field.Value.GetType() == typeof (JArray)) if (field.Value.GetType() == typeof(JArray))
{ {
value = ((JArray) field.Value).Select(s => s.Value<Int32>()); value = ((JArray)field.Value).Select(s => s.Value<Int32>());
} }
else else
{ {
value = field.Value.ToString().Split(new []{','}, StringSplitOptions.RemoveEmptyEntries).Select(s => Convert.ToInt32(s)); value = field.Value.ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(s => Convert.ToInt32(s));
} }
propertyInfo.SetValue(target, value, null);
}
else if (propertyInfo.PropertyType == typeof(IEnumerable<string>))
{
IEnumerable<string> value;
if (field.Value.GetType() == typeof(JArray))
{
value = ((JArray)field.Value).Select(s => s.Value<string>());
}
else
{
value = field.Value.ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
propertyInfo.SetValue(target, value, null); propertyInfo.SetValue(target, value, null);
} }
@ -142,4 +159,4 @@ namespace NzbDrone.Api.ClientSchema
return options.OrderBy(o => o.Value).ToList(); return options.OrderBy(o => o.Value).ToList();
} }
} }
} }

@ -84,7 +84,7 @@ namespace NzbDrone.Core.Test.Datastore.Migration
var items = Mocker.Resolve<NotificationRepository>().All(); var items = Mocker.Resolve<NotificationRepository>().All();
items.Should().HaveCount(1); items.Should().HaveCount(1);
items.First().Settings.As<PushBulletSettings>().DeviceIds.Should().Be(deviceId); items.First().Settings.As<PushBulletSettings>().DeviceIds.First().Should().Be(deviceId);
} }
} }
} }

@ -0,0 +1,44 @@
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_devices_channels : MigrationTest<Core.Datastore.Migration.pushbullet_devices_channels_list>
{
[Test]
public void should_convert_comma_separted_string_to_list()
{
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",
ChannelTags = "channel1,channel2"
}.ToJson(),
ConfigContract = "PushBulletSettings"
});
});
var items = Mocker.Resolve<NotificationRepository>().All();
items.Should().HaveCount(1);
var settings = items.First().Settings.As<PushBulletSettings>();
settings.ChannelTags.Should().HaveCount(2);
}
}
}

@ -120,6 +120,7 @@
<Compile Include="Datastore\Migration\071_unknown_quality_in_profileFixture.cs" /> <Compile Include="Datastore\Migration\071_unknown_quality_in_profileFixture.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\087_pushbullet_devices_channels.cs" />
<Compile Include="Datastore\Migration\086_pushbullet_device_idsFixture.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" />

@ -0,0 +1,64 @@
using System;
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(87)]
public class pushbullet_devices_channels_list : 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("deviceIds"))
{
var deviceIdsString = settings.GetValueOrDefault("deviceIds", "") as string;
var deviceIds = deviceIdsString.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
settings["deviceIds"] = deviceIds;
}
if (settings.ContainsKey("channelTags"))
{
var channelTagsString = settings.GetValueOrDefault("channelTags", "") as string;
var channelTags = channelTagsString.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries);
settings["channelTags"] = channelTags;
}
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();
}
}
}
}
}
}
}

@ -1,10 +1,8 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; 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;
@ -28,13 +26,11 @@ namespace NzbDrone.Core.Notifications.PushBullet
public void SendNotification(string title, string message, PushBulletSettings settings) public void SendNotification(string title, string message, PushBulletSettings settings)
{ {
var channelTags = GetIds(settings.ChannelTags);
var deviceIds = GetIds(settings.DeviceIds);
var error = false; var error = false;
if (channelTags.Any()) if (settings.ChannelTags.Any())
{ {
foreach (var channelTag in channelTags) foreach (var channelTag in settings.ChannelTags)
{ {
var request = BuildChannelRequest(channelTag); var request = BuildChannelRequest(channelTag);
@ -51,9 +47,9 @@ namespace NzbDrone.Core.Notifications.PushBullet
} }
else else
{ {
if (deviceIds.Any()) if (settings.DeviceIds.Any())
{ {
foreach (var deviceId in deviceIds) foreach (var deviceId in settings.DeviceIds)
{ {
var request = BuildDeviceRequest(deviceId); var request = BuildDeviceRequest(deviceId);
@ -169,12 +165,5 @@ namespace NzbDrone.Core.Notifications.PushBullet
throw new PushBulletException("Unable to send text message: {0}", ex, ex.Message); 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();
}
} }
} }

@ -1,4 +1,4 @@
using System; using System.Collections.Generic;
using FluentValidation; using FluentValidation;
using NzbDrone.Core.Annotations; using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.ThingiProvider;
@ -18,14 +18,20 @@ namespace NzbDrone.Core.Notifications.PushBullet
{ {
private static readonly PushBulletSettingsValidator Validator = new PushBulletSettingsValidator(); private static readonly PushBulletSettingsValidator Validator = new PushBulletSettingsValidator();
public PushBulletSettings()
{
DeviceIds = new string[]{};
ChannelTags = new string[]{};
}
[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 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)] [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 DeviceIds { get; set; } public IEnumerable<string> DeviceIds { get; set; }
[FieldDefinition(2, Label = "Channel Tags", HelpText = "List of Channel Tags to send notifications to", Type = FieldType.Tag)] [FieldDefinition(2, Label = "Channel Tags", HelpText = "List of Channel Tags to send notifications to", Type = FieldType.Tag)]
public String ChannelTags { get; set; } public IEnumerable<string> ChannelTags { get; set; }
public NzbDroneValidationResult Validate() public NzbDroneValidationResult Validate()
{ {

@ -262,6 +262,7 @@
<Compile Include="Datastore\Migration\084_update_quality_minmax_size.cs" /> <Compile Include="Datastore\Migration\084_update_quality_minmax_size.cs" />
<Compile Include="Datastore\Migration\083_additonal_blacklist_columns.cs" /> <Compile Include="Datastore\Migration\083_additonal_blacklist_columns.cs" />
<Compile Include="Datastore\Migration\082_add_fanzub_settings.cs" /> <Compile Include="Datastore\Migration\082_add_fanzub_settings.cs" />
<Compile Include="Datastore\Migration\087_pushbullet_devices_channels_list.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\MigrationDbFactory.cs" /> <Compile Include="Datastore\Migration\Framework\MigrationDbFactory.cs" />

Loading…
Cancel
Save