-
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add notification for item deleted #282
Changes from all commits
0dffb60
c64a813
7db3b94
b5d77f5
8cc9f52
23abedd
1db0596
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
using System; | ||
using System; | ||
|
||
namespace Jellyfin.Plugin.Webhook.Models; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Threading.Tasks; | ||
using MediaBrowser.Controller.Entities; | ||
|
||
namespace Jellyfin.Plugin.Webhook.Notifiers.ItemDeletedNotifier; | ||
|
||
/// <summary> | ||
/// Item deleted manager interface. | ||
/// </summary> | ||
public interface IItemDeletedManager | ||
{ | ||
/// <summary> | ||
/// Process the current queue. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
public Task ProcessItemsAsync(); | ||
|
||
/// <summary> | ||
/// Add item to process queue. | ||
/// </summary> | ||
/// <param name="item">The deleted item.</param> | ||
public void AddItem(BaseItem item); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Threading.Tasks; | ||
using Jellyfin.Plugin.Webhook.Destinations; | ||
using Jellyfin.Plugin.Webhook.Helpers; | ||
using Jellyfin.Plugin.Webhook.Models; | ||
using MediaBrowser.Controller; | ||
using MediaBrowser.Controller.Entities; | ||
using MediaBrowser.Controller.Library; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Jellyfin.Plugin.Webhook.Notifiers.ItemDeletedNotifier; | ||
|
||
/// <inheritdoc /> | ||
public class ItemDeletedManager : IItemDeletedManager | ||
{ | ||
private readonly ILogger<ItemDeletedManager> _logger; | ||
private readonly ILibraryManager _libraryManager; | ||
private readonly IServerApplicationHost _applicationHost; | ||
private readonly ConcurrentDictionary<Guid, QueuedItemContainer> _itemProcessQueue; | ||
private readonly ConcurrentDictionary<Guid, BaseItem> _deletedItems; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ItemDeletedManager"/> class. | ||
/// </summary> | ||
/// <param name="logger">Instance of the <see cref="ILogger{ItemDeletedManager}"/> interface.</param> | ||
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> | ||
/// <param name="applicationHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param> | ||
public ItemDeletedManager( | ||
ILogger<ItemDeletedManager> logger, | ||
ILibraryManager libraryManager, | ||
IServerApplicationHost applicationHost) | ||
{ | ||
_logger = logger; | ||
_libraryManager = libraryManager; | ||
_applicationHost = applicationHost; | ||
_itemProcessQueue = new ConcurrentDictionary<Guid, QueuedItemContainer>(); | ||
_deletedItems = new ConcurrentDictionary<Guid, BaseItem>(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task ProcessItemsAsync() | ||
{ | ||
_logger.LogDebug("ProcessItemsAsync"); | ||
// Attempt to process all items in queue. | ||
var currentItems = _itemProcessQueue.ToArray(); | ||
if (currentItems.Length != 0) | ||
{ | ||
var scope = _applicationHost.ServiceProvider!.CreateAsyncScope(); | ||
await using (scope.ConfigureAwait(false)) | ||
{ | ||
var webhookSender = scope.ServiceProvider.GetRequiredService<IWebhookSender>(); | ||
foreach (var (key, container) in currentItems) | ||
{ | ||
if (_deletedItems.TryGetValue(key, out var item) && item != null) | ||
{ | ||
// Skip notification if item type is Studio | ||
if (container.ItemType == "Studio") | ||
Check failure on line 59 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / Analyze (csharp)
Check failure on line 59 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / Analyze (csharp)
Check failure on line 59 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / test
Check failure on line 59 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / test
Check failure on line 59 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / build
|
||
{ | ||
_logger.LogDebug("Skipping notification for item type Studio"); | ||
_itemProcessQueue.TryRemove(key, out _); | ||
_deletedItems.TryRemove(key, out _); | ||
continue; | ||
} | ||
|
||
_logger.LogDebug("Notifying for {ItemName}", container.Name); | ||
Check failure on line 67 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / Analyze (csharp)
Check failure on line 67 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / Analyze (csharp)
Check failure on line 67 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / test
Check failure on line 67 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / test
Check failure on line 67 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / build
|
||
|
||
// Send notification to each configured destination. | ||
var dataObject = DataObjectHelpers | ||
.GetBaseDataObject(_applicationHost, NotificationType.ItemDeleted) | ||
.AddBaseItemData(item); | ||
|
||
var itemType = Type.GetType($"MediaBrowser.Controller.Entities.{container.ItemType}"); | ||
Check failure on line 74 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / Analyze (csharp)
Check failure on line 74 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / Analyze (csharp)
Check failure on line 74 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / test
Check failure on line 74 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / test
Check failure on line 74 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / build
|
||
await webhookSender.SendNotification(NotificationType.ItemDeleted, dataObject, itemType) | ||
.ConfigureAwait(false); | ||
|
||
// Remove item from queue. | ||
_itemProcessQueue.TryRemove(key, out _); | ||
_deletedItems.TryRemove(key, out _); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void AddItem(BaseItem item) | ||
{ | ||
var itemType = item.GetType().Name; | ||
_itemProcessQueue.TryAdd(item.Id, new QueuedItemContainer(item.Id, item.Name, itemType)); | ||
Check failure on line 91 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / Analyze (csharp)
Check failure on line 91 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / Analyze (csharp)
Check failure on line 91 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / test
Check failure on line 91 in Jellyfin.Plugin.Webhook/Notifiers/ItemDeletedNotifier/ItemDeletedManager.cs GitHub Actions / call / test
|
||
_deletedItems.TryAdd(item.Id, item); | ||
_logger.LogDebug("Queued {ItemName} for notification", item.Name); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MediaBrowser.Controller.Library; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Jellyfin.Plugin.Webhook.Notifiers.ItemDeletedNotifier; | ||
|
||
/// <summary> | ||
/// Notifier when a library item is deleted. | ||
/// </summary> | ||
public class ItemDeletedNotifierEntryPoint : IHostedService | ||
{ | ||
private readonly IItemDeletedManager _itemDeletedManager; | ||
private readonly ILibraryManager _libraryManager; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ItemDeletedNotifierEntryPoint"/> class. | ||
/// </summary> | ||
/// <param name="itemDeletedManager">Instance of the <see cref="IItemDeletedManager"/> interface.</param> | ||
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> | ||
public ItemDeletedNotifierEntryPoint( | ||
IItemDeletedManager itemDeletedManager, | ||
ILibraryManager libraryManager) | ||
{ | ||
_itemDeletedManager = itemDeletedManager; | ||
_libraryManager = libraryManager; | ||
} | ||
|
||
private void ItemDeletedHandler(object? sender, ItemChangeEventArgs itemChangeEventArgs) | ||
{ | ||
// Never notify on virtual items. | ||
if (itemChangeEventArgs.Item.IsVirtualItem) | ||
{ | ||
return; | ||
} | ||
|
||
_itemDeletedManager.AddItem(itemChangeEventArgs.Item); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_libraryManager.ItemRemoved += ItemDeletedHandler; | ||
return Task.CompletedTask; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
_libraryManager.ItemRemoved -= ItemDeletedHandler; | ||
return Task.CompletedTask; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MediaBrowser.Model.Globalization; | ||
using MediaBrowser.Model.Tasks; | ||
|
||
namespace Jellyfin.Plugin.Webhook.Notifiers.ItemDeletedNotifier; | ||
|
||
/// <summary> | ||
/// Scheduled task that processes item deleted events. | ||
/// </summary> | ||
public class ItemDeletedScheduledTask : IScheduledTask, IConfigurableScheduledTask | ||
{ | ||
private const int RecheckIntervalSec = 30; | ||
private readonly IItemDeletedManager _itemDeletedManager; | ||
private readonly ILocalizationManager _localizationManager; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ItemDeletedScheduledTask"/> class. | ||
/// </summary> | ||
/// <param name="itemDeletedManager">Instance of the <see cref="IItemDeletedManager"/> interface.</param> | ||
/// <param name="localizationManager">Instance of the <see cref="ILocalizationManager"/> interface.</param> | ||
public ItemDeletedScheduledTask( | ||
IItemDeletedManager itemDeletedManager, | ||
ILocalizationManager localizationManager) | ||
{ | ||
_itemDeletedManager = itemDeletedManager; | ||
_localizationManager = localizationManager; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string Name => "Webhook Item Deleted Notifier"; | ||
|
||
/// <inheritdoc /> | ||
public string Key => "WebhookItemDeleted"; | ||
|
||
/// <inheritdoc /> | ||
public string Description => "Processes item deleted queue"; | ||
|
||
/// <inheritdoc /> | ||
public string Category => _localizationManager.GetLocalizedString("TasksLibraryCategory"); | ||
|
||
/// <inheritdoc /> | ||
public bool IsHidden => false; | ||
|
||
/// <inheritdoc /> | ||
public bool IsEnabled => true; | ||
|
||
/// <inheritdoc /> | ||
public bool IsLogged => false; | ||
|
||
/// <inheritdoc /> | ||
public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken) | ||
{ | ||
return _itemDeletedManager.ProcessItemsAsync(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() | ||
{ | ||
return new[] | ||
{ | ||
new TaskTriggerInfo | ||
{ | ||
Type = TaskTriggerInfo.TriggerInterval, | ||
IntervalTicks = TimeSpan.FromSeconds(RecheckIntervalSec).Ticks | ||
} | ||
}; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of stringifying the item type, why not just pass the type in?