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 #12 from jellyfin/v10.6
Browse files Browse the repository at this point in the history
v10.6 compatibility
  • Loading branch information
anthonylavado committed Jul 19, 2020
2 parents 09aeb83 + def67fb commit 63a80b7
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 73 deletions.
46 changes: 27 additions & 19 deletions Pushbullet/Api/ServerApiEntryPoints.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.Logging;
using MediaBrowser.Model.Services;
using Pushbullet.Configuration;
using System.Threading.Tasks;

namespace Pushbullet.Api
{
[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; }
}

public class ServerApiEndpoints : IService
/// <summary>
/// API endpoints.
/// </summary>
public class ServerApiEntryPoints : IService
{
private readonly IHttpClient _httpClient;
private readonly IJsonSerializer _jsonSerializer;

public ServerApiEndpoints(IJsonSerializer jsonSerializer, IHttpClient httpClient)
/// <summary>
/// Initializes a new instance of the <see cref="ServerApiEntryPoints"/> class.
/// </summary>
/// <param name="jsonSerializer">Instance of the <see cref="IJsonSerializer"/> interface.</param>
/// <param name="httpClient">Instance of the <see cref="IHttpClient"/> interface.</param>
public ServerApiEntryPoints(IJsonSerializer jsonSerializer, IHttpClient httpClient)
{
_jsonSerializer = jsonSerializer;
_httpClient = httpClient;
}

private static PushbulletOptions GetOptions(string userId)
{
return Plugin.Instance.Configuration.Options
return Plugin.Instance!.Configuration.GetOptions()
.FirstOrDefault(i => string.Equals(i.UserId, userId, StringComparison.OrdinalIgnoreCase));
}

/// <summary>
/// Send test notification.
/// </summary>
/// <param name="request">Request to send.</param>
public void Post(TestNotification request)
{
PostAsync(request)
.GetAwaiter()
.GetResult();
}

/// <summary>
/// Send test notification.
/// </summary>
/// <param name="request">Request to send.</param>
/// <returns>A <see cref="Task"/>.</returns>
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"},
{"body", "This is a test notification from Jellyfin"}
{ "type", "note" },
{ "title", "Test Notification" },
{ "body", "This is a test notification from Jellyfin" }
};

var requestOptions = new HttpRequestOptions
Expand All @@ -59,10 +67,10 @@ public async Task PostAsync(TestNotification request)
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);
}
}
}
}
23 changes: 23 additions & 0 deletions Pushbullet/Api/TestNotification.cs
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; }
}
}
33 changes: 20 additions & 13 deletions Pushbullet/Configuration/PluginConfiguration.cs
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;
}

}
}
35 changes: 35 additions & 0 deletions Pushbullet/Configuration/PushbulletOptions.cs
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;
}
}
76 changes: 48 additions & 28 deletions Pushbullet/Notifier.cs
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);
}
}
}
}
32 changes: 24 additions & 8 deletions Pushbullet/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,39 @@

namespace Pushbullet
{
/// <summary>
/// Plugin with configuration and webpages.
/// </summary>
public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
{
private readonly Guid _id = new Guid("de228f12-e43e-4bd9-9fc0-2830819c3b92");

/// <summary>
/// Initializes a new instance of the <see cref="Plugin"/> class.
/// </summary>
/// <param name="applicationPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
/// <param name="xmlSerializer">Instance of the <see cref="IXmlSerializer"/> interface.</param>
public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
: base(applicationPaths, xmlSerializer)
{
Instance = this;
}

/// <inheritdoc />
public override string Name => "Pushbullet Notifications";

/// <inheritdoc />
public override string Description => "Sends notifications via Pushbullet Service.";

/// <inheritdoc />
public override Guid Id => _id;

/// <summary>
/// Gets plugin instance.
/// </summary>
public static Plugin? Instance { get; private set; }

/// <inheritdoc />
public IEnumerable<PluginPageInfo> GetPages()
{
return new[]
Expand All @@ -29,12 +52,5 @@ public IEnumerable<PluginPageInfo> GetPages()
}
};
}

public override string Description => "Sends notifications via Pushbullet Service.";

private Guid _id = new Guid("de228f12-e43e-4bd9-9fc0-2830819c3b92");
public override Guid Id => _id;

public static Plugin Instance { get; private set; }
}
}
}
Loading

0 comments on commit 63a80b7

Please sign in to comment.