This repository has been archived by the owner on Aug 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from jellyfin/v10.6
v10.6 compatibility
- Loading branch information
Showing
9 changed files
with
264 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using MediaBrowser.Model.Services; | ||
|
||
namespace Pushbullet.Api | ||
{ | ||
/// <summary> | ||
/// Test notification request. | ||
/// </summary> | ||
[Route("/Notification/Pushbullet/Test/{UserId}", "POST", Summary = "Tests Pushbullet")] | ||
public class TestNotification : IReturnVoid | ||
{ | ||
/// <summary> | ||
/// Gets or sets user Id to test. | ||
/// </summary> | ||
[ApiMember( | ||
Name = "UserId", | ||
Description = "User Id", | ||
IsRequired = true, | ||
DataType = "string", | ||
ParameterType = "path", | ||
Verb = "GET")] | ||
public string? UserId { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using MediaBrowser.Model.Plugins; | ||
|
||
namespace Pushbullet.Configuration | ||
{ | ||
/// <summary> | ||
/// The plugin configuration. | ||
/// </summary> | ||
public class PluginConfiguration : BasePluginConfiguration | ||
{ | ||
/// <summary> | ||
/// Pushbullet API url. | ||
/// </summary> | ||
public const string Url = "https://api.pushbullet.com/v2/pushes"; | ||
|
||
public PushbulletOptions[] Options { get; set; } | ||
private readonly PushbulletOptions[] _options; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PluginConfiguration"/> class. | ||
/// </summary> | ||
public PluginConfiguration() | ||
{ | ||
Options = new PushbulletOptions[] { }; | ||
_options = Array.Empty<PushbulletOptions>(); | ||
} | ||
} | ||
|
||
public class PushbulletOptions | ||
{ | ||
public bool Enabled { get; set; } | ||
public string Token { get; set; } | ||
public string DeviceId { get; set; } | ||
public string Channel { get; set; } | ||
public string UserId { get; set; } | ||
/// <summary> | ||
/// Get configured options. | ||
/// </summary> | ||
/// <returns><see cref="IEnumerable{PushbulletOptions}"/>.</returns> | ||
public IEnumerable<PushbulletOptions> GetOptions() | ||
=> _options; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace Pushbullet.Configuration | ||
{ | ||
/// <summary> | ||
/// Pushbullet Options container. | ||
/// </summary> | ||
public class PushbulletOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets a value indicating whether option is enabled. | ||
/// </summary> | ||
public bool Enabled { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the token. | ||
/// </summary> | ||
public string Token { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets or sets the device id. | ||
/// Unused. | ||
/// </summary> | ||
public string? DeviceId { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the channel. | ||
/// Unused. | ||
/// </summary> | ||
public string? Channel { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the user id for this configuration. | ||
/// </summary> | ||
public string UserId { get; set; } = string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Jellyfin.Data.Entities; | ||
using MediaBrowser.Common.Net; | ||
using MediaBrowser.Controller.Entities; | ||
using MediaBrowser.Controller.Notifications; | ||
using Microsoft.Extensions.Logging; | ||
using MediaBrowser.Model.Serialization; | ||
using Microsoft.Extensions.Logging; | ||
using Pushbullet.Configuration; | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Pushbullet | ||
{ | ||
/// <summary> | ||
/// Notifier service. | ||
/// </summary> | ||
public class Notifier : INotificationService | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly IHttpClient _httpClient; | ||
private readonly IJsonSerializer _jsonSerializer; | ||
private readonly ILogger<Notifier> _logger; | ||
|
||
public Notifier(ILogger logger, IHttpClient httpClient, IJsonSerializer jsonSerializer) | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Notifier"/> class. | ||
/// </summary> | ||
/// <param name="logger">Instance of the <see cref="ILogger{Notifier}"/> interface.</param> | ||
/// <param name="httpClient">Instance of the <see cref="IHttpClient"/> interface.</param> | ||
/// <param name="jsonSerializer">Instance of the <see cref="IJsonSerializer"/> interface.</param> | ||
public Notifier(ILogger<Notifier> logger, IHttpClient httpClient, IJsonSerializer jsonSerializer) | ||
{ | ||
_logger = logger; | ||
_httpClient = httpClient; | ||
_jsonSerializer = jsonSerializer; | ||
_logger = logger; | ||
_httpClient = httpClient; | ||
_jsonSerializer = jsonSerializer; | ||
} | ||
|
||
/// <summary> | ||
/// Gets plugin name. | ||
/// </summary> | ||
public string Name => Plugin.Instance!.Name; | ||
|
||
/// <inheritdoc /> | ||
public bool IsEnabledForUser(User user) | ||
{ | ||
var options = GetOptions(user); | ||
|
||
return options != null && IsValid(options) && options.Enabled; | ||
} | ||
|
||
private static PushbulletOptions GetOptions(BaseItem user) | ||
{ | ||
return Plugin.Instance.Configuration.Options | ||
.FirstOrDefault(i => string.Equals(i.UserId, user.Id.ToString("N"), StringComparison.OrdinalIgnoreCase)); | ||
} | ||
|
||
public string Name => Plugin.Instance.Name; | ||
|
||
/// <inheritdoc /> | ||
public async Task SendNotification(UserNotification request, CancellationToken cancellationToken) | ||
{ | ||
var options = GetOptions(request.User); | ||
|
||
var parameters = new Dictionary<string, string> | ||
{ | ||
{"channel_tag", options.Channel}, | ||
{"type", "note"}, | ||
{"title", request.Name}, | ||
{"body", request.Description} | ||
{ "channel_tag", options.Channel! }, | ||
{ "type", "note" }, | ||
{ "title", request.Name }, | ||
{ "body", request.Description } | ||
}; | ||
|
||
_logger.LogDebug("Pushbullet to Token : {0} - {1} - {2}", options.Token, options.DeviceId, request.Description); | ||
|
||
_logger.LogDebug( | ||
"Pushbullet to Token : {0} - {1} - {2}", | ||
options.Token, | ||
options.DeviceId, | ||
request.Description); | ||
|
||
var requestOptions = new HttpRequestOptions | ||
{ | ||
Url = PluginConfiguration.Url, | ||
RequestContent = _jsonSerializer.SerializeToString(parameters), | ||
RequestContentType = "application/json", | ||
LogErrorResponseBody = true, | ||
RequestHeaders = {["Access-Token"] = options.Token} | ||
RequestHeaders = { ["Access-Token"] = options.Token } | ||
}; | ||
|
||
await _httpClient.Post(requestOptions).ConfigureAwait(false); | ||
} | ||
|
||
private static PushbulletOptions GetOptions(User user) | ||
{ | ||
return Plugin.Instance!.Configuration.GetOptions() | ||
.FirstOrDefault(i => | ||
string.Equals(i.UserId, user.Id.ToString("N", CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase)); | ||
} | ||
|
||
private static bool IsValid(PushbulletOptions options) | ||
{ | ||
return !string.IsNullOrEmpty(options.Token); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.