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

Commit

Permalink
Merge pull request #7 from crobibero/master
Browse files Browse the repository at this point in the history
Fix Notification & Update Auth method
  • Loading branch information
JustAMan authored Apr 29, 2020
2 parents 14bb248 + 3e0c540 commit d327eb7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 114 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,5 @@ Generated_Code #added for RIA/Silverlight projects
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML

.idea
51 changes: 19 additions & 32 deletions Pushbullet/Api/ServerApiEntryPoints.cs
Original file line number Diff line number Diff line change
@@ -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<string, string>
{
{"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);
}
}
Expand Down
6 changes: 3 additions & 3 deletions Pushbullet/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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; }
}

}
44 changes: 12 additions & 32 deletions Pushbullet/Configuration/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -75,23 +73,19 @@
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) {
Dashboard.hideLoadingMsg();
});
};

ApiClient.getPluginConfiguration(pluginId).then(function (config) {

ApiClient.getPluginConfiguration(PushbulletPluginConfig.uniquePluginId).then(function (config) {
if (!config.Options.length) {
Dashboard.hideLoadingMsg();
require(['alert'], function (alert) {
Expand All @@ -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();
Expand All @@ -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 '<option value="' + user.Id + '">' + user.Name + '</option>';

})).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];

Expand All @@ -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;
Expand Down
46 changes: 18 additions & 28 deletions Pushbullet/Notifier.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Text;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Notifications;
Expand Down Expand Up @@ -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<string, string>
{
// {"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);
}
Expand Down
20 changes: 3 additions & 17 deletions Pushbullet/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using MediaBrowser.Model.Plugins;
using MediaBrowser.Model.Serialization;
using Pushbullet.Configuration;
using MediaBrowser.Model.Drawing;
using System.IO;

namespace Pushbullet
{
Expand All @@ -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<PluginPageInfo> GetPages()
{
Expand All @@ -35,19 +30,10 @@ public IEnumerable<PluginPageInfo> 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; }
}
Expand Down
4 changes: 2 additions & 2 deletions build.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
name: "jellyfin-plugin-pushbullet"
guid: "de228f12-e43e-4bd9-9fc0-2830819c3b92"
version: "3" # Please increment with each pull request
jellyfin_version: "10.5.0" # The earliest binary-compatible version
version: "4"
jellyfin_version: "10.5.0"
nicename: "Pushbullet"
description: "Jellyfin Pushbullet notification plugin"
overview: >
Expand Down

0 comments on commit d327eb7

Please sign in to comment.