Skip to content
This repository was archived by the owner on Aug 29, 2022. It is now read-only.

Commit 63a80b7

Browse files
Merge pull request #12 from jellyfin/v10.6
v10.6 compatibility
2 parents 09aeb83 + def67fb commit 63a80b7

File tree

9 files changed

+264
-73
lines changed

9 files changed

+264
-73
lines changed
Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,64 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Threading.Tasks;
45
using MediaBrowser.Common.Net;
56
using MediaBrowser.Model.Serialization;
6-
using Microsoft.Extensions.Logging;
77
using MediaBrowser.Model.Services;
88
using Pushbullet.Configuration;
9-
using System.Threading.Tasks;
109

1110
namespace Pushbullet.Api
1211
{
13-
[Route("/Notification/Pushbullet/Test/{UserId}", "POST", Summary = "Tests Pushbullet")]
14-
public class TestNotification : IReturnVoid
15-
{
16-
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string",
17-
ParameterType = "path", Verb = "GET")]
18-
public string UserId { get; set; }
19-
}
20-
21-
public class ServerApiEndpoints : IService
12+
/// <summary>
13+
/// API endpoints.
14+
/// </summary>
15+
public class ServerApiEntryPoints : IService
2216
{
2317
private readonly IHttpClient _httpClient;
2418
private readonly IJsonSerializer _jsonSerializer;
2519

26-
public ServerApiEndpoints(IJsonSerializer jsonSerializer, IHttpClient httpClient)
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="ServerApiEntryPoints"/> class.
22+
/// </summary>
23+
/// <param name="jsonSerializer">Instance of the <see cref="IJsonSerializer"/> interface.</param>
24+
/// <param name="httpClient">Instance of the <see cref="IHttpClient"/> interface.</param>
25+
public ServerApiEntryPoints(IJsonSerializer jsonSerializer, IHttpClient httpClient)
2726
{
2827
_jsonSerializer = jsonSerializer;
2928
_httpClient = httpClient;
3029
}
3130

3231
private static PushbulletOptions GetOptions(string userId)
3332
{
34-
return Plugin.Instance.Configuration.Options
33+
return Plugin.Instance!.Configuration.GetOptions()
3534
.FirstOrDefault(i => string.Equals(i.UserId, userId, StringComparison.OrdinalIgnoreCase));
3635
}
3736

37+
/// <summary>
38+
/// Send test notification.
39+
/// </summary>
40+
/// <param name="request">Request to send.</param>
3841
public void Post(TestNotification request)
3942
{
4043
PostAsync(request)
4144
.GetAwaiter()
4245
.GetResult();
4346
}
4447

48+
/// <summary>
49+
/// Send test notification.
50+
/// </summary>
51+
/// <param name="request">Request to send.</param>
52+
/// <returns>A <see cref="Task"/>.</returns>
4553
public async Task PostAsync(TestNotification request)
4654
{
47-
var options = GetOptions(request.UserId);
55+
var options = GetOptions(request.UserId!);
4856

4957
var parameters = new Dictionary<string, string>
5058
{
51-
{"type", "note"},
52-
{"title", "Test Notification"},
53-
{"body", "This is a test notification from Jellyfin"}
59+
{ "type", "note" },
60+
{ "title", "Test Notification" },
61+
{ "body", "This is a test notification from Jellyfin" }
5462
};
5563

5664
var requestOptions = new HttpRequestOptions
@@ -59,10 +67,10 @@ public async Task PostAsync(TestNotification request)
5967
RequestContent = _jsonSerializer.SerializeToString(parameters),
6068
RequestContentType = "application/json",
6169
LogErrorResponseBody = true,
62-
RequestHeaders = {["Access-Token"] = options.Token}
70+
RequestHeaders = { ["Access-Token"] = options.Token }
6371
};
6472

6573
await _httpClient.Post(requestOptions).ConfigureAwait(false);
6674
}
6775
}
68-
}
76+
}

Pushbullet/Api/TestNotification.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using MediaBrowser.Model.Services;
2+
3+
namespace Pushbullet.Api
4+
{
5+
/// <summary>
6+
/// Test notification request.
7+
/// </summary>
8+
[Route("/Notification/Pushbullet/Test/{UserId}", "POST", Summary = "Tests Pushbullet")]
9+
public class TestNotification : IReturnVoid
10+
{
11+
/// <summary>
12+
/// Gets or sets user Id to test.
13+
/// </summary>
14+
[ApiMember(
15+
Name = "UserId",
16+
Description = "User Id",
17+
IsRequired = true,
18+
DataType = "string",
19+
ParameterType = "path",
20+
Verb = "GET")]
21+
public string? UserId { get; set; }
22+
}
23+
}
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
13
using MediaBrowser.Model.Plugins;
24

35
namespace Pushbullet.Configuration
46
{
7+
/// <summary>
8+
/// The plugin configuration.
9+
/// </summary>
510
public class PluginConfiguration : BasePluginConfiguration
611
{
12+
/// <summary>
13+
/// Pushbullet API url.
14+
/// </summary>
715
public const string Url = "https://api.pushbullet.com/v2/pushes";
8-
9-
public PushbulletOptions[] Options { get; set; }
16+
private readonly PushbulletOptions[] _options;
1017

18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="PluginConfiguration"/> class.
20+
/// </summary>
1121
public PluginConfiguration()
1222
{
13-
Options = new PushbulletOptions[] { };
23+
_options = Array.Empty<PushbulletOptions>();
1424
}
15-
}
1625

17-
public class PushbulletOptions
18-
{
19-
public bool Enabled { get; set; }
20-
public string Token { get; set; }
21-
public string DeviceId { get; set; }
22-
public string Channel { get; set; }
23-
public string UserId { get; set; }
26+
/// <summary>
27+
/// Get configured options.
28+
/// </summary>
29+
/// <returns><see cref="IEnumerable{PushbulletOptions}"/>.</returns>
30+
public IEnumerable<PushbulletOptions> GetOptions()
31+
=> _options;
2432
}
25-
26-
}
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace Pushbullet.Configuration
2+
{
3+
/// <summary>
4+
/// Pushbullet Options container.
5+
/// </summary>
6+
public class PushbulletOptions
7+
{
8+
/// <summary>
9+
/// Gets or sets a value indicating whether option is enabled.
10+
/// </summary>
11+
public bool Enabled { get; set; }
12+
13+
/// <summary>
14+
/// Gets or sets the token.
15+
/// </summary>
16+
public string Token { get; set; } = string.Empty;
17+
18+
/// <summary>
19+
/// Gets or sets the device id.
20+
/// Unused.
21+
/// </summary>
22+
public string? DeviceId { get; set; }
23+
24+
/// <summary>
25+
/// Gets or sets the channel.
26+
/// Unused.
27+
/// </summary>
28+
public string? Channel { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the user id for this configuration.
32+
/// </summary>
33+
public string UserId { get; set; } = string.Empty;
34+
}
35+
}

Pushbullet/Notifier.cs

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,94 @@
1+
using System;
12
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Jellyfin.Data.Entities;
28
using MediaBrowser.Common.Net;
3-
using MediaBrowser.Controller.Entities;
49
using MediaBrowser.Controller.Notifications;
5-
using Microsoft.Extensions.Logging;
610
using MediaBrowser.Model.Serialization;
11+
using Microsoft.Extensions.Logging;
712
using Pushbullet.Configuration;
8-
using System;
9-
using System.Linq;
10-
using System.Threading;
11-
using System.Threading.Tasks;
1213

1314
namespace Pushbullet
1415
{
16+
/// <summary>
17+
/// Notifier service.
18+
/// </summary>
1519
public class Notifier : INotificationService
1620
{
17-
private readonly ILogger _logger;
1821
private readonly IHttpClient _httpClient;
1922
private readonly IJsonSerializer _jsonSerializer;
23+
private readonly ILogger<Notifier> _logger;
2024

21-
public Notifier(ILogger logger, IHttpClient httpClient, IJsonSerializer jsonSerializer)
25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="Notifier"/> class.
27+
/// </summary>
28+
/// <param name="logger">Instance of the <see cref="ILogger{Notifier}"/> interface.</param>
29+
/// <param name="httpClient">Instance of the <see cref="IHttpClient"/> interface.</param>
30+
/// <param name="jsonSerializer">Instance of the <see cref="IJsonSerializer"/> interface.</param>
31+
public Notifier(ILogger<Notifier> logger, IHttpClient httpClient, IJsonSerializer jsonSerializer)
2232
{
23-
_logger = logger;
24-
_httpClient = httpClient;
25-
_jsonSerializer = jsonSerializer;
33+
_logger = logger;
34+
_httpClient = httpClient;
35+
_jsonSerializer = jsonSerializer;
2636
}
2737

38+
/// <summary>
39+
/// Gets plugin name.
40+
/// </summary>
41+
public string Name => Plugin.Instance!.Name;
42+
43+
/// <inheritdoc />
2844
public bool IsEnabledForUser(User user)
2945
{
3046
var options = GetOptions(user);
3147

3248
return options != null && IsValid(options) && options.Enabled;
3349
}
3450

35-
private static PushbulletOptions GetOptions(BaseItem user)
36-
{
37-
return Plugin.Instance.Configuration.Options
38-
.FirstOrDefault(i => string.Equals(i.UserId, user.Id.ToString("N"), StringComparison.OrdinalIgnoreCase));
39-
}
40-
41-
public string Name => Plugin.Instance.Name;
42-
51+
/// <inheritdoc />
4352
public async Task SendNotification(UserNotification request, CancellationToken cancellationToken)
4453
{
4554
var options = GetOptions(request.User);
4655

4756
var parameters = new Dictionary<string, string>
4857
{
49-
{"channel_tag", options.Channel},
50-
{"type", "note"},
51-
{"title", request.Name},
52-
{"body", request.Description}
58+
{ "channel_tag", options.Channel! },
59+
{ "type", "note" },
60+
{ "title", request.Name },
61+
{ "body", request.Description }
5362
};
5463

55-
_logger.LogDebug("Pushbullet to Token : {0} - {1} - {2}", options.Token, options.DeviceId, request.Description);
56-
64+
_logger.LogDebug(
65+
"Pushbullet to Token : {0} - {1} - {2}",
66+
options.Token,
67+
options.DeviceId,
68+
request.Description);
69+
5770
var requestOptions = new HttpRequestOptions
5871
{
5972
Url = PluginConfiguration.Url,
6073
RequestContent = _jsonSerializer.SerializeToString(parameters),
6174
RequestContentType = "application/json",
6275
LogErrorResponseBody = true,
63-
RequestHeaders = {["Access-Token"] = options.Token}
76+
RequestHeaders = { ["Access-Token"] = options.Token }
6477
};
65-
78+
6679
await _httpClient.Post(requestOptions).ConfigureAwait(false);
6780
}
6881

82+
private static PushbulletOptions GetOptions(User user)
83+
{
84+
return Plugin.Instance!.Configuration.GetOptions()
85+
.FirstOrDefault(i =>
86+
string.Equals(i.UserId, user.Id.ToString("N", CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase));
87+
}
88+
6989
private static bool IsValid(PushbulletOptions options)
7090
{
7191
return !string.IsNullOrEmpty(options.Token);
7292
}
7393
}
74-
}
94+
}

Pushbullet/Plugin.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,39 @@
88

99
namespace Pushbullet
1010
{
11+
/// <summary>
12+
/// Plugin with configuration and webpages.
13+
/// </summary>
1114
public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
1215
{
16+
private readonly Guid _id = new Guid("de228f12-e43e-4bd9-9fc0-2830819c3b92");
17+
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="Plugin"/> class.
20+
/// </summary>
21+
/// <param name="applicationPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
22+
/// <param name="xmlSerializer">Instance of the <see cref="IXmlSerializer"/> interface.</param>
1323
public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
1424
: base(applicationPaths, xmlSerializer)
1525
{
1626
Instance = this;
1727
}
1828

29+
/// <inheritdoc />
1930
public override string Name => "Pushbullet Notifications";
2031

32+
/// <inheritdoc />
33+
public override string Description => "Sends notifications via Pushbullet Service.";
34+
35+
/// <inheritdoc />
36+
public override Guid Id => _id;
37+
38+
/// <summary>
39+
/// Gets plugin instance.
40+
/// </summary>
41+
public static Plugin? Instance { get; private set; }
42+
43+
/// <inheritdoc />
2144
public IEnumerable<PluginPageInfo> GetPages()
2245
{
2346
return new[]
@@ -29,12 +52,5 @@ public IEnumerable<PluginPageInfo> GetPages()
2952
}
3053
};
3154
}
32-
33-
public override string Description => "Sends notifications via Pushbullet Service.";
34-
35-
private Guid _id = new Guid("de228f12-e43e-4bd9-9fc0-2830819c3b92");
36-
public override Guid Id => _id;
37-
38-
public static Plugin Instance { get; private set; }
3955
}
40-
}
56+
}

0 commit comments

Comments
 (0)