-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from jellyfin/user-item-data
Add UserDataSaved event handler
- Loading branch information
Showing
6 changed files
with
146 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
Jellyfin.Plugin.Webhook/Notifiers/UserDataSavedNotifier/UserDataSavedNotifierEntryPoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Jellyfin.Plugin.Webhook.Destinations; | ||
using Jellyfin.Plugin.Webhook.Helpers; | ||
using MediaBrowser.Common; | ||
using MediaBrowser.Controller.Library; | ||
using MediaBrowser.Controller.Plugins; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Jellyfin.Plugin.Webhook.Notifiers.UserDataSavedNotifier | ||
{ | ||
/// <summary> | ||
/// User data saved notifier. | ||
/// </summary> | ||
public class UserDataSavedNotifierEntryPoint : IServerEntryPoint | ||
{ | ||
private readonly IWebhookSender _webhookSender; | ||
private readonly IApplicationHost _applicationHost; | ||
private readonly IUserDataManager _userDataManager; | ||
private readonly IUserManager _userManager; | ||
private readonly ILogger<UserDataSavedNotifierEntryPoint> _logger; | ||
|
||
/// <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="IApplicationHost"/> 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, | ||
IApplicationHost applicationHost, | ||
IUserDataManager userDataManager, | ||
ILogger<UserDataSavedNotifierEntryPoint> logger, | ||
IUserManager userManager) | ||
{ | ||
_userDataManager = userDataManager; | ||
_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) | ||
{ | ||
try | ||
{ | ||
if (eventArgs.Item is null) | ||
{ | ||
return; | ||
} | ||
|
||
if (eventArgs.Item.IsThemeMedia) | ||
{ | ||
// Don't report theme song or local trailer playback. | ||
return; | ||
} | ||
|
||
var user = _userManager.GetUserById(eventArgs.UserId); | ||
if (user is null) | ||
{ | ||
return; | ||
} | ||
|
||
var dataObject = DataObjectHelpers | ||
.GetBaseDataObject(_applicationHost, NotificationType.UserDataSaved) | ||
.AddBaseItemData(eventArgs.Item) | ||
.AddUserItemData(eventArgs.UserData); | ||
|
||
dataObject["SaveReason"] = eventArgs.SaveReason.ToString(); | ||
dataObject["NotificationUsername"] = user.Username; | ||
dataObject["UserId"] = user.Id; | ||
|
||
await _webhookSender.SendNotification(NotificationType.UserDataSaved, dataObject, eventArgs.Item.GetType()) | ||
.ConfigureAwait(false); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogWarning(ex, "Unable to send notification"); | ||
} | ||
} | ||
} | ||
} |