From 11f7af16d37427a0b639fffaa9305cb9dbfabb20 Mon Sep 17 00:00:00 2001 From: crobibero Date: Wed, 19 Feb 2020 07:46:55 -0700 Subject: [PATCH 1/2] Fix test notification and move to current auth method --- .gitignore | 2 + Pushbullet/Api/ServerApiEntryPoints.cs | 51 +++++++------------ .../Configuration/PluginConfiguration.cs | 6 +-- Pushbullet/Configuration/config.html | 44 +++++----------- Pushbullet/Notifier.cs | 46 +++++++---------- Pushbullet/Plugin.cs | 20 ++------ Pushbullet/Pushbullet.csproj | 6 +-- build.yaml | 2 +- 8 files changed, 61 insertions(+), 116 deletions(-) diff --git a/.gitignore b/.gitignore index bdc3535..a4cfe2a 100644 --- a/.gitignore +++ b/.gitignore @@ -106,3 +106,5 @@ Generated_Code #added for RIA/Silverlight projects _UpgradeReport_Files/ Backup*/ UpgradeLog*.XML + +.idea \ No newline at end of file diff --git a/Pushbullet/Api/ServerApiEntryPoints.cs b/Pushbullet/Api/ServerApiEntryPoints.cs index 5f1fb8e..ae01a42 100644 --- a/Pushbullet/Api/ServerApiEntryPoints.cs +++ b/Pushbullet/Api/ServerApiEntryPoints.cs @@ -1,80 +1,67 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Net; -using System.Text; using MediaBrowser.Common.Net; -using MediaBrowser.Controller.Net; using MediaBrowser.Model.Serialization; using Microsoft.Extensions.Logging; using MediaBrowser.Model.Services; using Pushbullet.Configuration; -using System.Threading; using System.Threading.Tasks; namespace Pushbullet.Api { - [Route("/Notification/Pushbullet/Test/{UserID}", "POST", Summary = "Tests Pushbullet")] + [Route("/Notification/Pushbullet/Test/{UserId}", "POST", Summary = "Tests Pushbullet")] public class TestNotification : IReturnVoid { - [ApiMember(Name = "UserID", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] - public string UserID { get; set; } + [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", + ParameterType = "path", Verb = "GET")] + public string UserId { get; set; } } - class ServerApiEndpoints : IService + public class ServerApiEndpoints : IService { private readonly IHttpClient _httpClient; private readonly IJsonSerializer _jsonSerializer; - private readonly ILogger _logger; - public ServerApiEndpoints(ILogger logger, IJsonSerializer jsonSerializer, IHttpClient httpClient) + public ServerApiEndpoints(IJsonSerializer jsonSerializer, IHttpClient httpClient) { - _logger = logger; - _jsonSerializer = jsonSerializer; - _httpClient = httpClient; + _jsonSerializer = jsonSerializer; + _httpClient = httpClient; } - private PushbulletOptions GetOptions(String userID) + + private static PushbulletOptions GetOptions(string userId) { return Plugin.Instance.Configuration.Options - .FirstOrDefault(i => string.Equals(i.MediaBrowserUserId, userID, StringComparison.OrdinalIgnoreCase)); + .FirstOrDefault(i => string.Equals(i.UserId, userId, StringComparison.OrdinalIgnoreCase)); } public void Post(TestNotification request) { - var task = PostAsync(request); - Task.WaitAll(task); + PostAsync(request) + .GetAwaiter() + .GetResult(); } public async Task PostAsync(TestNotification request) { - var options = GetOptions(request.UserID); + var options = GetOptions(request.UserId); var parameters = new Dictionary { {"type", "note"}, - {"title", "Test Notification" }, + {"title", "Test Notification"}, {"body", "This is a test notification from Jellyfin"} }; - var _httpRequest = new HttpRequestOptions(); - - //Create Basic HTTP Auth Header... - - string authInfo = options.Token; - authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo)); - var requestOptions = new HttpRequestOptions { - Url = "https://api.pushbullet.com/v2/pushes", + Url = PluginConfiguration.Url, RequestContent = _jsonSerializer.SerializeToString(parameters), - BufferContent = false, RequestContentType = "application/json", LogErrorResponseBody = true, - DecompressionMethod = CompressionMethod.None, - EnableKeepAlive = false + RequestHeaders = {["Access-Token"] = options.Token} }; - requestOptions.RequestHeaders["Authorization"] = "Basic " + authInfo; + await _httpClient.Post(requestOptions).ConfigureAwait(false); } } diff --git a/Pushbullet/Configuration/PluginConfiguration.cs b/Pushbullet/Configuration/PluginConfiguration.cs index 764c170..08b7539 100644 --- a/Pushbullet/Configuration/PluginConfiguration.cs +++ b/Pushbullet/Configuration/PluginConfiguration.cs @@ -1,11 +1,11 @@ -using System; -using System.Collections.Generic; using MediaBrowser.Model.Plugins; namespace Pushbullet.Configuration { public class PluginConfiguration : BasePluginConfiguration { + public const string Url = "https://api.pushbullet.com/v2/pushes"; + public PushbulletOptions[] Options { get; set; } public PluginConfiguration() @@ -20,7 +20,7 @@ public class PushbulletOptions public string Token { get; set; } public string DeviceId { get; set; } public string Channel { get; set; } - public string MediaBrowserUserId { get; set; } + public string UserId { get; set; } } } diff --git a/Pushbullet/Configuration/config.html b/Pushbullet/Configuration/config.html index 6ebc451..e8d32f0 100644 --- a/Pushbullet/Configuration/config.html +++ b/Pushbullet/Configuration/config.html @@ -47,18 +47,16 @@ (function () { - var pluginId = "DE228F12-E43E-4BD9-9fC0-2830819C3B92"; - + var PushbulletPluginConfig = { + uniquePluginId: "DE228F12-E43E-4BD9-9fC0-2830819C3B92" + }; + function loadUserConfig(page, userId) { - Dashboard.showLoadingMsg(); - - ApiClient.getPluginConfiguration(pluginId).then(function (config) { + ApiClient.getPluginConfiguration(PushbulletPluginConfig.uniquePluginId).then(function (config) { var PushbulletConfig = config.Options.filter(function (c) { - - return userId == c.MediaBrowserUserId; - + return userId === c.UserId; })[0] || {}; $('#chkEnablePushbullet', page).checked(PushbulletConfig.Enabled || false).checkboxradio("refresh"); @@ -75,14 +73,11 @@ var page = this; $('#selectUser', page).on('change', function () { - loadUserConfig(page, this.value); }); $('#testNotification', page).on('click', function (event) { - Dashboard.showLoadingMsg(); - var onError = function () { alert("There was an error sending the test notification. Please check your notification settings and try again."); require(['alert'], function (alert) { @@ -90,8 +85,7 @@ }); }; - ApiClient.getPluginConfiguration(pluginId).then(function (config) { - + ApiClient.getPluginConfiguration(PushbulletPluginConfig.uniquePluginId).then(function (config) { if (!config.Options.length) { Dashboard.hideLoadingMsg(); require(['alert'], function (alert) { @@ -100,10 +94,9 @@ } config.Options.map(function (c) { - ApiClient.ajax({ type: "POST", - url: ApiClient.getUrl("Notification/Pushbullet/Test/" + c.MediaBrowserUserId) + url: ApiClient.getUrl("Notification/Pushbullet/Test/" + c.UserId) }).then(function () { Dashboard.hideLoadingMsg(); @@ -113,37 +106,24 @@ }); }).on('pageshow', function (event) { - Dashboard.showLoadingMsg(); - var page = this; - ApiClient.getUsers().then(function (users) { - $('#selectUser', page).html(users.map(function (user) { - return ''; - })).selectmenu('refresh').trigger('change'); - }); Dashboard.hideLoadingMsg(); }); $('.PushbulletConfigurationForm').on('submit', function (e) { - Dashboard.showLoadingMsg(); - var form = this; - - ApiClient.getPluginConfiguration(pluginId).then(function (config) { - + ApiClient.getPluginConfiguration(PushbulletPluginConfig.uniquePluginId).then(function (config) { var userId = $('#selectUser', form).val(); - var PushbulletConfig = config.Options.filter(function (c) { - - return userId == c.MediaBrowserUserId; + return userId === c.UserId; })[0]; @@ -152,14 +132,14 @@ config.Options.push(PushbulletConfig); } - PushbulletConfig.MediaBrowserUserId = userId; + PushbulletConfig.UserId = userId; PushbulletConfig.Enabled = $('#chkEnablePushbullet', form).checked(); PushbulletConfig.Channel = $('#txtPushbulletChannel', form).val(); PushbulletConfig.Token = $('#txtPushbulletAuthKey', form).val(); PushbulletConfig.DeviceId = $('#txtPushbulletDeviceId', form).val(); - ApiClient.updatePluginConfiguration(pluginId, config).then(Dashboard.processPluginConfigurationUpdateResult); + ApiClient.updatePluginConfiguration(PushbulletPluginConfig.uniquePluginId, config).then(Dashboard.processPluginConfigurationUpdateResult); }); return false; diff --git a/Pushbullet/Notifier.cs b/Pushbullet/Notifier.cs index c0a0138..cfdbdbd 100644 --- a/Pushbullet/Notifier.cs +++ b/Pushbullet/Notifier.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Text; using MediaBrowser.Common.Net; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Notifications; @@ -33,50 +32,41 @@ public bool IsEnabledForUser(User user) return options != null && IsValid(options) && options.Enabled; } - private PushbulletOptions GetOptions(User user) + private static PushbulletOptions GetOptions(BaseItem user) { return Plugin.Instance.Configuration.Options - .FirstOrDefault(i => string.Equals(i.MediaBrowserUserId, user.Id.ToString("N"), StringComparison.OrdinalIgnoreCase)); + .FirstOrDefault(i => string.Equals(i.UserId, user.Id.ToString("N"), StringComparison.OrdinalIgnoreCase)); } - public string Name - { - get { return Plugin.Instance.Name; } - } + public string Name => Plugin.Instance.Name; public async Task SendNotification(UserNotification request, CancellationToken cancellationToken) { var options = GetOptions(request.User); var parameters = new Dictionary - { - // {"device_iden", options.DeviceId}, - {"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); - string authInfo = options.Token; - authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo)); - var requestOptions = new HttpRequestOptions - { - Url = "https://api.pushbullet.com/v2/pushes", - RequestContent = _jsonSerializer.SerializeToString(parameters), - BufferContent = false, - RequestContentType = "application/json", - LogErrorResponseBody = true, - DecompressionMethod = CompressionMethod.None, - EnableKeepAlive = false - }; - requestOptions.RequestHeaders["Authorization"] = "Basic " + authInfo; + { + Url = PluginConfiguration.Url, + RequestContent = _jsonSerializer.SerializeToString(parameters), + RequestContentType = "application/json", + LogErrorResponseBody = true, + RequestHeaders = {["Access-Token"] = options.Token} + }; + await _httpClient.Post(requestOptions).ConfigureAwait(false); } - private bool IsValid(PushbulletOptions options) + private static bool IsValid(PushbulletOptions options) { return !string.IsNullOrEmpty(options.Token); } diff --git a/Pushbullet/Plugin.cs b/Pushbullet/Plugin.cs index 6cbafc8..ba9be98 100644 --- a/Pushbullet/Plugin.cs +++ b/Pushbullet/Plugin.cs @@ -5,8 +5,6 @@ using MediaBrowser.Model.Plugins; using MediaBrowser.Model.Serialization; using Pushbullet.Configuration; -using MediaBrowser.Model.Drawing; -using System.IO; namespace Pushbullet { @@ -18,10 +16,7 @@ public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer) Instance = this; } - public override string Name - { - get { return "Pushbullet Notifications"; } - } + public override string Name => "Pushbullet Notifications"; public IEnumerable GetPages() { @@ -35,19 +30,10 @@ public IEnumerable GetPages() }; } - public override string Description - { - get - { - return "Sends notifications via Pushbullet Service."; - } - } + public override string Description => "Sends notifications via Pushbullet Service."; private Guid _id = new Guid("de228f12-e43e-4bd9-9fc0-2830819c3b92"); - public override Guid Id - { - get { return _id; } - } + public override Guid Id => _id; public static Plugin Instance { get; private set; } } diff --git a/Pushbullet/Pushbullet.csproj b/Pushbullet/Pushbullet.csproj index 49810d7..1f924d0 100644 --- a/Pushbullet/Pushbullet.csproj +++ b/Pushbullet/Pushbullet.csproj @@ -1,9 +1,9 @@ - netstandard2.0; - 2.0.0 - 2.0.0 + netstandard2.0 + 3.0.0 + 3.0.0 diff --git a/build.yaml b/build.yaml index ecfdcaf..bb9d4f3 100644 --- a/build.yaml +++ b/build.yaml @@ -1,7 +1,7 @@ --- name: "jellyfin-plugin-pushbullet" guid: "de228f12-e43e-4bd9-9fc0-2830819c3b92" -version: "2" # Please increment with each pull request +version: "3" # Please increment with each pull request jellyfin_version: "10.3.7" # The earliest binary-compatible version nicename: "Pushbullet" description: "Jellyfin Pushbullet notification plugin" From 3e0c5409c86aa0b59215cb7c173aeb167b354b97 Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Tue, 21 Apr 2020 16:10:27 -0600 Subject: [PATCH 2/2] Update build.yaml Co-Authored-By: Vasily --- build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.yaml b/build.yaml index fbefe00..cc806a8 100644 --- a/build.yaml +++ b/build.yaml @@ -1,7 +1,7 @@ --- name: "jellyfin-plugin-pushbullet" guid: "de228f12-e43e-4bd9-9fc0-2830819c3b92" -version: "3" +version: "4" jellyfin_version: "10.5.0" nicename: "Pushbullet" description: "Jellyfin Pushbullet notification plugin"