Skip to content

Commit

Permalink
10.9 support (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
crobibero authored May 11, 2024
1 parent 0cb6cc2 commit 96d9f34
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 183 deletions.
1 change: 0 additions & 1 deletion Jellyfin.Plugin.Webhook/Configuration/Web/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
template: document.querySelector("#template-notification-type"),
values: {
"ItemAdded": "Item Added",
"Generic": "Generic",
"PlaybackStart": "Playback Start",
"PlaybackProgress": "Playback Progress",
"PlaybackStop": "Playback Stop",
Expand Down
2 changes: 1 addition & 1 deletion Jellyfin.Plugin.Webhook/Destinations/Mqtt/MqttClients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task UpdateClients(MqttOption[] options)

if (option.UseTls)
{
messageBuilder.WithTls();
messageBuilder.WithTlsOptions(c => c.UseTls());
}

var clientOptions = messageBuilder.Build();
Expand Down
2 changes: 1 addition & 1 deletion Jellyfin.Plugin.Webhook/Helpers/DataObjectHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,4 @@ public static Dictionary<string, object> AddUserItemData(this Dictionary<string,
/// <returns>Escaped string.</returns>
private static string Escape(this string? input)
=> input?.Replace("\"", "\\\"", StringComparison.Ordinal) ?? string.Empty;
}
}
12 changes: 6 additions & 6 deletions Jellyfin.Plugin.Webhook/Jellyfin.Plugin.Webhook.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
Expand All @@ -12,15 +12,15 @@

<ItemGroup>
<PackageReference Include="Jellyfin.Controller" Version="10.*-*" />
<PackageReference Include="MailKit" Version="3.6.0" />
<PackageReference Include="Handlebars.Net" Version="2.1.4" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.*" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.2.1.781" />
<PackageReference Include="MailKit" Version="4.5.0" />
<PackageReference Include="Handlebars.Net" Version="2.1.6" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.3.3.952" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.507" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" PrivateAssets="All" />
<PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />
</ItemGroup>

Expand Down
52 changes: 0 additions & 52 deletions Jellyfin.Plugin.Webhook/Notifiers/GenericNotifier.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Plugin.Webhook.Notifiers.ItemAddedNotifier;
Expand All @@ -18,7 +19,6 @@ public class ItemAddedManager : IItemAddedManager
private readonly ILogger<ItemAddedManager> _logger;
private readonly ILibraryManager _libraryManager;
private readonly IServerApplicationHost _applicationHost;
private readonly IWebhookSender _webhookSender;
private readonly ConcurrentDictionary<Guid, QueuedItemContainer> _itemProcessQueue;

/// <summary>
Expand All @@ -27,17 +27,14 @@ public class ItemAddedManager : IItemAddedManager
/// <param name="logger">Instance of the <see cref="ILogger{ItemAddedManager}"/> interface.</param>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
/// <param name="applicationHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param>
/// <param name="webhookSender">Instance of the <see cref="IWebhookSender"/> interface.</param>
public ItemAddedManager(
ILogger<ItemAddedManager> logger,
ILibraryManager libraryManager,
IServerApplicationHost applicationHost,
IWebhookSender webhookSender)
IServerApplicationHost applicationHost)
{
_logger = logger;
_libraryManager = libraryManager;
_applicationHost = applicationHost;
_webhookSender = webhookSender;
_itemProcessQueue = new ConcurrentDictionary<Guid, QueuedItemContainer>();
}

Expand All @@ -47,40 +44,48 @@ public async Task ProcessItemsAsync()
_logger.LogDebug("ProcessItemsAsync");
// Attempt to process all items in queue.
var currentItems = _itemProcessQueue.ToArray();
foreach (var (key, container) in currentItems)
if (currentItems.Length != 0)
{
var item = _libraryManager.GetItemById(key);
if (item is null)
var scope = _applicationHost.ServiceProvider!.CreateAsyncScope();
await using (scope.ConfigureAwait(false))
{
// Remove item from queue.
_itemProcessQueue.TryRemove(key, out _);
return;
}
var webhookSender = scope.ServiceProvider.GetRequiredService<IWebhookSender>();
foreach (var (key, container) in currentItems)
{
var item = _libraryManager.GetItemById(key);
if (item is null)
{
// Remove item from queue.
_itemProcessQueue.TryRemove(key, out _);
return;
}

_logger.LogDebug("Item {ItemName}", item.Name);
_logger.LogDebug("Item {ItemName}", item.Name);

// Metadata not refreshed yet and under retry limit.
if (item.ProviderIds.Keys.Count == 0 && container.RetryCount < MaxRetries)
{
_logger.LogDebug("Requeue {ItemName}, no provider ids", item.Name);
container.RetryCount++;
_itemProcessQueue.AddOrUpdate(key, container, (_, _) => container);
continue;
}
// Metadata not refreshed yet and under retry limit.
if (item.ProviderIds.Keys.Count == 0 && container.RetryCount < MaxRetries)
{
_logger.LogDebug("Requeue {ItemName}, no provider ids", item.Name);
container.RetryCount++;
_itemProcessQueue.AddOrUpdate(key, container, (_, _) => container);
continue;
}

_logger.LogDebug("Notifying for {ItemName}", item.Name);
_logger.LogDebug("Notifying for {ItemName}", item.Name);

// Send notification to each configured destination.
var dataObject = DataObjectHelpers
.GetBaseDataObject(_applicationHost, NotificationType.ItemAdded)
.AddBaseItemData(item);
// Send notification to each configured destination.
var dataObject = DataObjectHelpers
.GetBaseDataObject(_applicationHost, NotificationType.ItemAdded)
.AddBaseItemData(item);

var itemType = item.GetType();
await _webhookSender.SendNotification(NotificationType.ItemAdded, dataObject, itemType)
.ConfigureAwait(false);
var itemType = item.GetType();
await webhookSender.SendNotification(NotificationType.ItemAdded, dataObject, itemType)
.ConfigureAwait(false);

// Remove item from queue.
_itemProcessQueue.TryRemove(key, out _);
// Remove item from queue.
_itemProcessQueue.TryRemove(key, out _);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webhook.Helpers;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using Microsoft.Extensions.Hosting;

namespace Jellyfin.Plugin.Webhook.Notifiers.ItemAddedNotifier;

/// <summary>
/// Notifier when a library item is added.
/// </summary>
public class ItemAddedNotifierEntryPoint : IServerEntryPoint
public class ItemAddedNotifierEntryPoint : IHostedService
{
private readonly IItemAddedManager _itemAddedManager;
private readonly ILibraryManager _libraryManager;
Expand All @@ -27,41 +27,29 @@ public ItemAddedNotifierEntryPoint(
_libraryManager = libraryManager;
}

/// <inheritdoc />
public void Dispose()
private void ItemAddedHandler(object? sender, ItemChangeEventArgs itemChangeEventArgs)
{
Dispose(true);
GC.SuppressFinalize(this);
// Never notify on virtual items.
if (itemChangeEventArgs.Item.IsVirtualItem)
{
return;
}

_itemAddedManager.AddItem(itemChangeEventArgs.Item);
}

/// <inheritdoc />
public Task RunAsync()
public Task StartAsync(CancellationToken cancellationToken)
{
_libraryManager.ItemAdded += ItemAddedHandler;
HandlebarsFunctionHelpers.RegisterHelpers();
return Task.CompletedTask;
}

/// <summary>
/// Dispose.
/// </summary>
/// <param name="disposing">Dispose all assets.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_libraryManager.ItemAdded -= ItemAddedHandler;
}
}

private void ItemAddedHandler(object? sender, ItemChangeEventArgs itemChangeEventArgs)
/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken)
{
// Never notify on virtual items.
if (itemChangeEventArgs.Item.IsVirtualItem)
{
return;
}

_itemAddedManager.AddItem(itemChangeEventArgs.Item);
_libraryManager.ItemAdded -= ItemAddedHandler;
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webhook.Destinations;
using Jellyfin.Plugin.Webhook.Helpers;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Plugin.Webhook.Notifiers.UserDataSavedNotifier;

/// <summary>
/// User data saved notifier.
/// </summary>
public class UserDataSavedNotifierEntryPoint : IServerEntryPoint
public class UserDataSavedNotifierEntryPoint : IHostedService
{
private readonly IWebhookSender _webhookSender;
private readonly IServerApplicationHost _applicationHost;
private readonly IUserDataManager _userDataManager;
private readonly IUserManager _userManager;
Expand All @@ -23,13 +24,11 @@ public class UserDataSavedNotifierEntryPoint : IServerEntryPoint
/// <summary>
/// Initializes a new instance of the <see cref="UserDataSavedNotifierEntryPoint"/> class.
/// </summary>
/// <param name="webhookSender">Instance of the <see cref="IWebhookSender"/> interface.</param>
/// <param name="userDataManager">Instance of the <see cref="IUserDataManager"/> interface.</param>
/// <param name="applicationHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param>
/// <param name="logger">Instance of the <see cref="ILogger{UserDataChangedNotifierEntryPoint}"/> interface.</param>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
public UserDataSavedNotifierEntryPoint(
IWebhookSender webhookSender,
IServerApplicationHost applicationHost,
IUserDataManager userDataManager,
ILogger<UserDataSavedNotifierEntryPoint> logger,
Expand All @@ -39,33 +38,6 @@ public UserDataSavedNotifierEntryPoint(
_logger = logger;
_userManager = userManager;
_applicationHost = applicationHost;
_webhookSender = webhookSender;
}

/// <inheritdoc />
public Task RunAsync()
{
_userDataManager.UserDataSaved += UserDataSavedHandler;
return Task.CompletedTask;
}

/// <inheritdoc />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Dispose.
/// </summary>
/// <param name="disposing">Dispose all assets.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_userDataManager.UserDataSaved -= UserDataSavedHandler;
}
}

private async void UserDataSavedHandler(object? sender, UserDataSaveEventArgs eventArgs)
Expand Down Expand Up @@ -98,12 +70,32 @@ private async void UserDataSavedHandler(object? sender, UserDataSaveEventArgs ev
dataObject["NotificationUsername"] = user.Username;
dataObject["UserId"] = user.Id;

await _webhookSender.SendNotification(NotificationType.UserDataSaved, dataObject, eventArgs.Item.GetType())
.ConfigureAwait(false);
var scope = _applicationHost.ServiceProvider!.CreateAsyncScope();
await using (scope.ConfigureAwait(false))
{
var webhookSender = scope.ServiceProvider.GetRequiredService<IWebhookSender>();
await webhookSender
.SendNotification(NotificationType.UserDataSaved, dataObject, eventArgs.Item.GetType())
.ConfigureAwait(false);
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Unable to send notification");
}
}

/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken)
{
_userDataManager.UserDataSaved += UserDataSavedHandler;
return Task.CompletedTask;
}

/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken)
{
_userDataManager.UserDataSaved -= UserDataSavedHandler;
return Task.CompletedTask;
}
}
Loading

0 comments on commit 96d9f34

Please sign in to comment.