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

Commit d327eb7

Browse files
authored
Merge pull request #7 from crobibero/master
Fix Notification & Update Auth method
2 parents 14bb248 + 3e0c540 commit d327eb7

File tree

7 files changed

+59
-114
lines changed

7 files changed

+59
-114
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,5 @@ Generated_Code #added for RIA/Silverlight projects
106106
_UpgradeReport_Files/
107107
Backup*/
108108
UpgradeLog*.XML
109+
110+
.idea

Pushbullet/Api/ServerApiEntryPoints.cs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,67 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using System.Linq;
5-
using System.Net;
6-
using System.Text;
74
using MediaBrowser.Common.Net;
8-
using MediaBrowser.Controller.Net;
95
using MediaBrowser.Model.Serialization;
106
using Microsoft.Extensions.Logging;
117
using MediaBrowser.Model.Services;
128
using Pushbullet.Configuration;
13-
using System.Threading;
149
using System.Threading.Tasks;
1510

1611
namespace Pushbullet.Api
1712
{
18-
[Route("/Notification/Pushbullet/Test/{UserID}", "POST", Summary = "Tests Pushbullet")]
13+
[Route("/Notification/Pushbullet/Test/{UserId}", "POST", Summary = "Tests Pushbullet")]
1914
public class TestNotification : IReturnVoid
2015
{
21-
[ApiMember(Name = "UserID", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
22-
public string UserID { get; set; }
16+
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string",
17+
ParameterType = "path", Verb = "GET")]
18+
public string UserId { get; set; }
2319
}
2420

25-
class ServerApiEndpoints : IService
21+
public class ServerApiEndpoints : IService
2622
{
2723
private readonly IHttpClient _httpClient;
2824
private readonly IJsonSerializer _jsonSerializer;
29-
private readonly ILogger _logger;
3025

31-
public ServerApiEndpoints(ILogger logger, IJsonSerializer jsonSerializer, IHttpClient httpClient)
26+
public ServerApiEndpoints(IJsonSerializer jsonSerializer, IHttpClient httpClient)
3227
{
33-
_logger = logger;
34-
_jsonSerializer = jsonSerializer;
35-
_httpClient = httpClient;
28+
_jsonSerializer = jsonSerializer;
29+
_httpClient = httpClient;
3630
}
37-
private PushbulletOptions GetOptions(String userID)
31+
32+
private static PushbulletOptions GetOptions(string userId)
3833
{
3934
return Plugin.Instance.Configuration.Options
40-
.FirstOrDefault(i => string.Equals(i.MediaBrowserUserId, userID, StringComparison.OrdinalIgnoreCase));
35+
.FirstOrDefault(i => string.Equals(i.UserId, userId, StringComparison.OrdinalIgnoreCase));
4136
}
4237

4338
public void Post(TestNotification request)
4439
{
45-
var task = PostAsync(request);
46-
Task.WaitAll(task);
40+
PostAsync(request)
41+
.GetAwaiter()
42+
.GetResult();
4743
}
4844

4945
public async Task PostAsync(TestNotification request)
5046
{
51-
var options = GetOptions(request.UserID);
47+
var options = GetOptions(request.UserId);
5248

5349
var parameters = new Dictionary<string, string>
5450
{
5551
{"type", "note"},
56-
{"title", "Test Notification" },
52+
{"title", "Test Notification"},
5753
{"body", "This is a test notification from Jellyfin"}
5854
};
5955

60-
var _httpRequest = new HttpRequestOptions();
61-
62-
//Create Basic HTTP Auth Header...
63-
64-
string authInfo = options.Token;
65-
authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));
66-
6756
var requestOptions = new HttpRequestOptions
6857
{
69-
Url = "https://api.pushbullet.com/v2/pushes",
58+
Url = PluginConfiguration.Url,
7059
RequestContent = _jsonSerializer.SerializeToString(parameters),
71-
BufferContent = false,
7260
RequestContentType = "application/json",
7361
LogErrorResponseBody = true,
74-
DecompressionMethod = CompressionMethod.None,
75-
EnableKeepAlive = false
62+
RequestHeaders = {["Access-Token"] = options.Token}
7663
};
77-
requestOptions.RequestHeaders["Authorization"] = "Basic " + authInfo;
64+
7865
await _httpClient.Post(requestOptions).ConfigureAwait(false);
7966
}
8067
}

Pushbullet/Configuration/PluginConfiguration.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
2-
using System.Collections.Generic;
31
using MediaBrowser.Model.Plugins;
42

53
namespace Pushbullet.Configuration
64
{
75
public class PluginConfiguration : BasePluginConfiguration
86
{
7+
public const string Url = "https://api.pushbullet.com/v2/pushes";
8+
99
public PushbulletOptions[] Options { get; set; }
1010

1111
public PluginConfiguration()
@@ -20,7 +20,7 @@ public class PushbulletOptions
2020
public string Token { get; set; }
2121
public string DeviceId { get; set; }
2222
public string Channel { get; set; }
23-
public string MediaBrowserUserId { get; set; }
23+
public string UserId { get; set; }
2424
}
2525

2626
}

Pushbullet/Configuration/config.html

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,16 @@
4747

4848
(function () {
4949

50-
var pluginId = "DE228F12-E43E-4BD9-9fC0-2830819C3B92";
51-
50+
var PushbulletPluginConfig = {
51+
uniquePluginId: "DE228F12-E43E-4BD9-9fC0-2830819C3B92"
52+
};
53+
5254
function loadUserConfig(page, userId) {
53-
5455
Dashboard.showLoadingMsg();
55-
56-
ApiClient.getPluginConfiguration(pluginId).then(function (config) {
56+
ApiClient.getPluginConfiguration(PushbulletPluginConfig.uniquePluginId).then(function (config) {
5757

5858
var PushbulletConfig = config.Options.filter(function (c) {
59-
60-
return userId == c.MediaBrowserUserId;
61-
59+
return userId === c.UserId;
6260
})[0] || {};
6361

6462
$('#chkEnablePushbullet', page).checked(PushbulletConfig.Enabled || false).checkboxradio("refresh");
@@ -75,23 +73,19 @@
7573
var page = this;
7674

7775
$('#selectUser', page).on('change', function () {
78-
7976
loadUserConfig(page, this.value);
8077
});
8178

8279
$('#testNotification', page).on('click', function (event) {
83-
8480
Dashboard.showLoadingMsg();
85-
8681
var onError = function () {
8782
alert("There was an error sending the test notification. Please check your notification settings and try again.");
8883
require(['alert'], function (alert) {
8984
Dashboard.hideLoadingMsg();
9085
});
9186
};
9287

93-
ApiClient.getPluginConfiguration(pluginId).then(function (config) {
94-
88+
ApiClient.getPluginConfiguration(PushbulletPluginConfig.uniquePluginId).then(function (config) {
9589
if (!config.Options.length) {
9690
Dashboard.hideLoadingMsg();
9791
require(['alert'], function (alert) {
@@ -100,10 +94,9 @@
10094
}
10195

10296
config.Options.map(function (c) {
103-
10497
ApiClient.ajax({
10598
type: "POST",
106-
url: ApiClient.getUrl("Notification/Pushbullet/Test/" + c.MediaBrowserUserId)
99+
url: ApiClient.getUrl("Notification/Pushbullet/Test/" + c.UserId)
107100

108101
}).then(function () {
109102
Dashboard.hideLoadingMsg();
@@ -113,37 +106,24 @@
113106
});
114107

115108
}).on('pageshow', function (event) {
116-
117109
Dashboard.showLoadingMsg();
118-
119110
var page = this;
120-
121111
ApiClient.getUsers().then(function (users) {
122-
123112
$('#selectUser', page).html(users.map(function (user) {
124-
125113
return '<option value="' + user.Id + '">' + user.Name + '</option>';
126-
127114
})).selectmenu('refresh').trigger('change');
128-
129115
});
130116

131117
Dashboard.hideLoadingMsg();
132118
});
133119

134120
$('.PushbulletConfigurationForm').on('submit', function (e) {
135-
136121
Dashboard.showLoadingMsg();
137-
138122
var form = this;
139-
140-
ApiClient.getPluginConfiguration(pluginId).then(function (config) {
141-
123+
ApiClient.getPluginConfiguration(PushbulletPluginConfig.uniquePluginId).then(function (config) {
142124
var userId = $('#selectUser', form).val();
143-
144125
var PushbulletConfig = config.Options.filter(function (c) {
145-
146-
return userId == c.MediaBrowserUserId;
126+
return userId === c.UserId;
147127

148128
})[0];
149129

@@ -152,14 +132,14 @@
152132
config.Options.push(PushbulletConfig);
153133
}
154134

155-
PushbulletConfig.MediaBrowserUserId = userId;
135+
PushbulletConfig.UserId = userId;
156136

157137
PushbulletConfig.Enabled = $('#chkEnablePushbullet', form).checked();
158138
PushbulletConfig.Channel = $('#txtPushbulletChannel', form).val();
159139
PushbulletConfig.Token = $('#txtPushbulletAuthKey', form).val();
160140
PushbulletConfig.DeviceId = $('#txtPushbulletDeviceId', form).val();
161141

162-
ApiClient.updatePluginConfiguration(pluginId, config).then(Dashboard.processPluginConfigurationUpdateResult);
142+
ApiClient.updatePluginConfiguration(PushbulletPluginConfig.uniquePluginId, config).then(Dashboard.processPluginConfigurationUpdateResult);
163143
});
164144

165145
return false;

Pushbullet/Notifier.cs

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Text;
32
using MediaBrowser.Common.Net;
43
using MediaBrowser.Controller.Entities;
54
using MediaBrowser.Controller.Notifications;
@@ -33,50 +32,41 @@ public bool IsEnabledForUser(User user)
3332
return options != null && IsValid(options) && options.Enabled;
3433
}
3534

36-
private PushbulletOptions GetOptions(User user)
35+
private static PushbulletOptions GetOptions(BaseItem user)
3736
{
3837
return Plugin.Instance.Configuration.Options
39-
.FirstOrDefault(i => string.Equals(i.MediaBrowserUserId, user.Id.ToString("N"), StringComparison.OrdinalIgnoreCase));
38+
.FirstOrDefault(i => string.Equals(i.UserId, user.Id.ToString("N"), StringComparison.OrdinalIgnoreCase));
4039
}
4140

42-
public string Name
43-
{
44-
get { return Plugin.Instance.Name; }
45-
}
41+
public string Name => Plugin.Instance.Name;
4642

4743
public async Task SendNotification(UserNotification request, CancellationToken cancellationToken)
4844
{
4945
var options = GetOptions(request.User);
5046

5147
var parameters = new Dictionary<string, string>
52-
{
53-
// {"device_iden", options.DeviceId},
54-
{"channel_tag", options.Channel},
55-
{"type", "note"},
56-
{"title", request.Name},
57-
{"body", request.Description}
58-
};
48+
{
49+
{"channel_tag", options.Channel},
50+
{"type", "note"},
51+
{"title", request.Name},
52+
{"body", request.Description}
53+
};
5954

6055
_logger.LogDebug("Pushbullet to Token : {0} - {1} - {2}", options.Token, options.DeviceId, request.Description);
6156

62-
string authInfo = options.Token;
63-
authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));
64-
6557
var requestOptions = new HttpRequestOptions
66-
{
67-
Url = "https://api.pushbullet.com/v2/pushes",
68-
RequestContent = _jsonSerializer.SerializeToString(parameters),
69-
BufferContent = false,
70-
RequestContentType = "application/json",
71-
LogErrorResponseBody = true,
72-
DecompressionMethod = CompressionMethod.None,
73-
EnableKeepAlive = false
74-
};
75-
requestOptions.RequestHeaders["Authorization"] = "Basic " + authInfo;
58+
{
59+
Url = PluginConfiguration.Url,
60+
RequestContent = _jsonSerializer.SerializeToString(parameters),
61+
RequestContentType = "application/json",
62+
LogErrorResponseBody = true,
63+
RequestHeaders = {["Access-Token"] = options.Token}
64+
};
65+
7666
await _httpClient.Post(requestOptions).ConfigureAwait(false);
7767
}
7868

79-
private bool IsValid(PushbulletOptions options)
69+
private static bool IsValid(PushbulletOptions options)
8070
{
8171
return !string.IsNullOrEmpty(options.Token);
8272
}

Pushbullet/Plugin.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using MediaBrowser.Model.Plugins;
66
using MediaBrowser.Model.Serialization;
77
using Pushbullet.Configuration;
8-
using MediaBrowser.Model.Drawing;
9-
using System.IO;
108

119
namespace Pushbullet
1210
{
@@ -18,10 +16,7 @@ public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
1816
Instance = this;
1917
}
2018

21-
public override string Name
22-
{
23-
get { return "Pushbullet Notifications"; }
24-
}
19+
public override string Name => "Pushbullet Notifications";
2520

2621
public IEnumerable<PluginPageInfo> GetPages()
2722
{
@@ -35,19 +30,10 @@ public IEnumerable<PluginPageInfo> GetPages()
3530
};
3631
}
3732

38-
public override string Description
39-
{
40-
get
41-
{
42-
return "Sends notifications via Pushbullet Service.";
43-
}
44-
}
33+
public override string Description => "Sends notifications via Pushbullet Service.";
4534

4635
private Guid _id = new Guid("de228f12-e43e-4bd9-9fc0-2830819c3b92");
47-
public override Guid Id
48-
{
49-
get { return _id; }
50-
}
36+
public override Guid Id => _id;
5137

5238
public static Plugin Instance { get; private set; }
5339
}

build.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
name: "jellyfin-plugin-pushbullet"
33
guid: "de228f12-e43e-4bd9-9fc0-2830819c3b92"
4-
version: "3" # Please increment with each pull request
5-
jellyfin_version: "10.5.0" # The earliest binary-compatible version
4+
version: "4"
5+
jellyfin_version: "10.5.0"
66
nicename: "Pushbullet"
77
description: "Jellyfin Pushbullet notification plugin"
88
overview: >

0 commit comments

Comments
 (0)