Skip to content

Commit

Permalink
Merge pull request MindFlavor#6 from MindFlavor/support_10.7.0
Browse files Browse the repository at this point in the history
Support for Jellyfin 10.7.0
  • Loading branch information
MindFlavor authored Mar 17, 2021
2 parents bca1f5f + 4deddc0 commit 9b906a7
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 97 deletions.
58 changes: 0 additions & 58 deletions Jellyfin.Plugins.Telegram/Api/ServerApiEntryPoint.cs

This file was deleted.

53 changes: 53 additions & 0 deletions Jellyfin.Plugins.Telegram/Api/TelegramNotificationsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Threading.Tasks;
using Jellyfin.Plugins.Telegram.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Serialization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Net.Http;

namespace Jellyfin.Plugins.Telegram.Api
{
[ApiController]
[Route("Notification/Telegram")]
[Produces(MediaTypeNames.Application.Json)]
public class TelegramNotificationsController : ControllerBase
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IJsonSerializer _jsonSerializer;
private readonly ILogger _logger;

public TelegramNotificationsController(IHttpClientFactory httpClientFactory, IJsonSerializer jsonSerializer, ILoggerFactory loggerFactory)
{
_httpClientFactory = httpClientFactory;
_jsonSerializer = jsonSerializer;
_logger = loggerFactory.CreateLogger<TelegramNotificationsController>();
}

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

[HttpPost("Test/{userId}")]
[ProducesResponseType(Microsoft.AspNetCore.Http.StatusCodes.Status204NoContent)]
public async Task<ActionResult> PostAsync([FromRoute] string userId)
{
var options = GetOptions(userId);

await Notifier.SendNotification(
_httpClientFactory,
options.Token, options.ChatId, options.SilentNotificationEnabled,
"This is a test notification from Jellyfin"
);

return NoContent();
}
}
}
44 changes: 27 additions & 17 deletions Jellyfin.Plugins.Telegram/Configuration/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,35 +73,42 @@ <h2 class="sectionTitle">Telegram Settings:</h2>
</div>

<script type="text/javascript">

var TelegramPluginConfig = {
uniquePluginId: "0269B736-58C7-436C-995B-0F7127092D5F"
};

function loadUserConfig(page, userId) {
//console.log("in Telegram::loadUserconfig with userId == " + userId);
Dashboard.showLoadingMsg();
ApiClient.getPluginConfiguration(TelegramPluginConfig.uniquePluginId).then(function (config) {
//console.log(config);

var telegramConfig = config.Options.filter(function (c) {
return userId === c.UserId;
})[0] || {};

$('#chkEnableTelegram', page).checked(telegramConfig.Enabled || false).checkboxradio("refresh");
$('#txtTelegramToken', page).val(telegramConfig.Token || '');
$('#txtTelegramChatId', page).val(telegramConfig.ChatId || '');
$('#chkSilentNotificationEnabled', page).val(telegramConfig.SilentNotificationEnabled || '');
//console.log(telegramConfig);

page.querySelector('#chkEnableTelegram').checked = telegramConfig.Enabled || false;
page.querySelector('#txtTelegramToken').value = telegramConfig.Token || '';
page.querySelector('#txtTelegramChatId').value = telegramConfig.ChatId || '';
page.querySelector('#chkSilentNotificationEnabled').checked = telegramConfig.SilentNotificationEnabled || false;

Dashboard.hideLoadingMsg();
});
}

$('.telegramConfigurationPage').on('pageinit', function (event) {
document.querySelector('.telegramConfigurationPage').addEventListener('pageinit', function (event) {
//console.log("in Telegram::pageinit - " + this);
var page = this;
$('#selectUser', page).on('change', function () {

page.querySelector("#selectUser").addEventListener('change', function () {
//console.log("in Telegram::selectUser::change");
loadUserConfig(page, this.value);
});

$('#testNotification', page).on('click', function (event) {
page.querySelector('#testNotification').addEventListener('click', function (event) {
//console.log("in Telegram::testNotification::click");
Dashboard.showLoadingMsg();
var onError = function () {
alert("There was an error sending the test notification. Please check your notification settings and try again.");
Expand Down Expand Up @@ -130,20 +137,23 @@ <h2 class="sectionTitle">Telegram Settings:</h2>
});
});

}).on('pageshow', function (event) {
});

document.querySelector('.telegramConfigurationPage').addEventListener('pageshow', function (event) {
//console.log("in Telegram::pageshow");

Dashboard.showLoadingMsg();

var page = this;

ApiClient.getUsers().then(function (users) {

$('#selectUser', page).html(users.map(function (user) {

//console.log("Telegram - got Users");
document.getElementById('selectUser').innerHTML = users.map(function (user) {
return '<option value="' + user.Id + '">' + user.Name + '</option>';

})).selectmenu('refresh').trigger('change');

});
let selectedUser = document.getElementById('selectUser').value;
//console.log('selected user after got Users == ' + selectedUser);
loadUserConfig(page, selectedUser);
});

Dashboard.hideLoadingMsg();
Expand All @@ -170,10 +180,10 @@ <h2 class="sectionTitle">Telegram Settings:</h2>

telegramConfig.UserId = userId;

telegramConfig.Enabled = $('#chkEnableTelegram', form).checked();
telegramConfig.Enabled = document.getElementById('chkEnableTelegram').checked;
telegramConfig.Token = $('#txtTelegramToken', form).val();
telegramConfig.ChatId = $('#txtTelegramChatId', form).val();
telegramConfig.SilentNotificationEnabled = $('#chkSilentNotificationEnabled', form).checked();
telegramConfig.SilentNotificationEnabled = document.getElementById('chkSilentNotificationEnabled').checked;

ApiClient.updatePluginConfiguration(TelegramPluginConfig.uniquePluginId, config).then(Dashboard.processPluginConfigurationUpdateResult);
});
Expand Down
12 changes: 7 additions & 5 deletions Jellyfin.Plugins.Telegram/Jellyfin.Plugins.Telegram.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<TargetFramework>net5.0</TargetFramework>
<AssemblyVersion>1.7.0.0</AssemblyVersion>
<FileVersion>1.7.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -14,8 +14,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Jellyfin.Controller" Version="10.6-*" />
<PackageReference Include="Jellyfin.Model" Version="10.6-*" />
<PackageReference Include="Jellyfin.Controller" Version="10.7-*" />
<PackageReference Include="Jellyfin.Model" Version="10.7-*" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
</ItemGroup>

</Project>
34 changes: 18 additions & 16 deletions Jellyfin.Plugins.Telegram/Notifier.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
using System;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugins.Telegram.Configuration;
using System.Text.Json;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
using Microsoft.Extensions.Logging;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Model.Serialization;
using System.Net.Mime;
using Jellyfin.Data.Entities;
using System.Net.Http;

namespace Jellyfin.Plugins.Telegram
{
public class Notifier : INotificationService
{
private readonly IHttpClient _httpClient;
private readonly IJsonSerializer _jsonSerializer;
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger<Notifier> _logger;

public Notifier(IHttpClient httpClient, IJsonSerializer jsonSerializer)
public Notifier(ILogger<Notifier> logger, IHttpClientFactory httpClientFactory)
{
_httpClient = httpClient;
_jsonSerializer = jsonSerializer;
_httpClientFactory = httpClientFactory;
_logger = logger;
}

public static async Task SendNotification(IHttpClient httpClient, IJsonSerializer jsonSerializer, string token, string chatId, bool fSilentNotificationEnabled, string text)
public static async Task SendNotification(IHttpClientFactory httpClientFactory, string token, string chatId, bool fSilentNotificationEnabled, string text)
{
var body = new Dictionary<string, object>() {
{"chat_id", chatId},
Expand All @@ -32,23 +37,20 @@ public static async Task SendNotification(IHttpClient httpClient, IJsonSerialize
{"disable_notification", fSilentNotificationEnabled},
};

var requestOptions = new HttpRequestOptions
{
Url = $"https://api.telegram.org/bot{token}/sendMessage",
RequestContent = jsonSerializer.SerializeToString(body),
RequestContentType = "application/json",
LogErrorResponseBody = true
};
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"https://api.telegram.org/bot{token}/sendMessage");
requestMessage.Content = new StringContent(JsonSerializer.Serialize(body),
System.Text.Encoding.UTF8, MediaTypeNames.Application.Json);

await httpClient.Post(requestOptions).ConfigureAwait(false);
var httpClient = httpClientFactory.CreateClient(NamedClient.Default);
using var responseMessage = await httpClient.SendAsync(requestMessage).ConfigureAwait(false);
}

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

await Notifier.SendNotification(
_httpClient, _jsonSerializer,
_httpClientFactory,
options.Token, options.ChatId, options.SilentNotificationEnabled,
string.IsNullOrEmpty(request.Description) ? request.Name : request.Description
);
Expand All @@ -60,7 +62,7 @@ public bool IsEnabledForUser(User user)
return options != null && IsValid(options) && options.Enabled;
}

public string Name => Plugin.Instance.Name;
public string Name => Plugin.Instance!.Name;

private static TelegramOptions GetOptions(User user)
{
Expand Down
2 changes: 1 addition & 1 deletion Jellyfin.Plugins.Telegram/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IEnumerable<PluginPageInfo> GetPages()
};
}

private readonly Guid _id = new Guid("0269b736-58c7-436c-995b-0f7127092d5f");
private readonly Guid _id = new Guid("0269B736-58C7-436C-995B-0F7127092D5F");
public override Guid Id => _id;

public static Plugin Instance { get; private set; }
Expand Down

0 comments on commit 9b906a7

Please sign in to comment.