Add more properties

This commit is contained in:
crobibero 2020-12-18 08:52:12 -07:00
parent 2ad73e8b6a
commit 6115ec3233
7 changed files with 128 additions and 99 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Jellyfin.Plugin.Webhook.Destinations;
using MediaBrowser.Common;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
@ -13,24 +14,40 @@ namespace Jellyfin.Plugin.Webhook.Helpers
/// </summary>
public static class DataObjectHelpers
{
/// <summary>
/// Gets the default data object.
/// </summary>
/// <param name="applicationHost">Instance of the <see cref="IApplicationHost"/> interface.</param>
/// <param name="notificationType">The notification type.</param>
/// <returns>The default data object.</returns>
public static Dictionary<string, object> GetBaseDataObject(IApplicationHost applicationHost, NotificationType notificationType)
{
var dataObject = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
dataObject["ServerId"] = applicationHost.SystemId;
dataObject["ServerName"] = applicationHost.Name;
dataObject["ServerVersion"] = applicationHost.ApplicationVersionString;
dataObject["ServerUrl"] = WebhookPlugin.Instance?.Configuration.ServerUrl ?? "localhost:8096";
dataObject[nameof(NotificationType)] = notificationType.ToString();
return dataObject;
}
/// <summary>
/// Get data object from <see cref="BaseItem"/>.
/// </summary>
/// <param name="dataObject">The existing data object.</param>
/// <param name="applicationHost">Instance of the <see cref="IApplicationHost"/> interface.</param>
/// <param name="item">Instance of the <see cref="BaseItem"/>.</param>
/// <returns>The data object.</returns>
public static Dictionary<string, object> AddBaseItemData(this Dictionary<string, object> dataObject, IApplicationHost applicationHost, BaseItem item)
public static Dictionary<string, object> AddBaseItemData(this Dictionary<string, object> dataObject, BaseItem item)
{
dataObject["Timestamp"] = DateTime.Now;
dataObject["UtcTimestamp"] = DateTime.UtcNow;
dataObject["Name"] = item.Name;
dataObject["Overview"] = item.Overview;
dataObject["ItemId"] = item.Id;
dataObject["ServerId"] = applicationHost.SystemId;
dataObject["ServerUrl"] = WebhookPlugin.Instance?.Configuration.ServerUrl ?? "localhost:8096";
dataObject["ServerName"] = applicationHost.Name;
dataObject["ItemType"] = item.GetType().Name;
dataObject["RunTimeTicks"] = item.RunTimeTicks ?? 0;
dataObject["RunTime"] = TimeSpan.FromTicks(item.RunTimeTicks ?? 0).ToString(@"hh\:mm\:ss");
if (item.ProductionYear.HasValue)
{
@ -103,6 +120,7 @@ namespace Jellyfin.Plugin.Webhook.Helpers
public static Dictionary<string, object> AddPlaybackProgressData(this Dictionary<string, object> dataObject, PlaybackProgressEventArgs playbackProgressEventArgs)
{
dataObject[nameof(playbackProgressEventArgs.PlaybackPositionTicks)] = playbackProgressEventArgs.PlaybackPositionTicks ?? 0;
dataObject["PlaybackPosition"] = TimeSpan.FromTicks(playbackProgressEventArgs.PlaybackPositionTicks ?? 0).ToString(@"hh\:mm\:ss");
dataObject[nameof(playbackProgressEventArgs.MediaSourceId)] = playbackProgressEventArgs.MediaSourceId;
dataObject[nameof(playbackProgressEventArgs.IsPaused)] = playbackProgressEventArgs.IsPaused;
dataObject[nameof(playbackProgressEventArgs.IsAutomated)] = playbackProgressEventArgs.IsAutomated;

View File

@ -1,8 +1,9 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
using Jellyfin.Plugin.Webhook.Destinations;
using Jellyfin.Plugin.Webhook.Helpers;
using MediaBrowser.Common;
using MediaBrowser.Controller.Notifications;
namespace Jellyfin.Plugin.Webhook.Notifiers
@ -13,14 +14,17 @@ namespace Jellyfin.Plugin.Webhook.Notifiers
public class GenericNotifier : INotificationService
{
private readonly WebhookSender _webhookSender;
private readonly IApplicationHost _applicationHost;
/// <summary>
/// Initializes a new instance of the <see cref="GenericNotifier"/> class.
/// </summary>
/// <param name="webhookSender">Instance of the <see cref="WebhookSender"/>.</param>
public GenericNotifier(WebhookSender webhookSender)
/// <param name="applicationHost">Instance of the <see cref="IApplicationHost"/> interface.</param>
public GenericNotifier(WebhookSender webhookSender, IApplicationHost applicationHost)
{
_webhookSender = webhookSender;
_applicationHost = applicationHost;
}
/// <inheritdoc />
@ -29,18 +33,17 @@ namespace Jellyfin.Plugin.Webhook.Notifiers
/// <inheritdoc />
public async Task SendNotification(UserNotification request, CancellationToken cancellationToken)
{
var parameters = new Dictionary<string, object>
{
{ nameof(request.Name), request.Name },
{ nameof(request.Description), request.Description },
{ nameof(request.Date), request.Date },
{ nameof(request.Level), request.Level },
{ nameof(request.Url), request.Url },
{ nameof(request.User.Username), request.User.Username },
{ "UserId", request.User.Id },
};
var dataObject = DataObjectHelpers
.GetBaseDataObject(_applicationHost, NotificationType.Generic);
dataObject[nameof(request.Name)] = request.Name;
dataObject[nameof(request.Description)] = request.Description;
dataObject[nameof(request.Date)] = request.Date;
dataObject[nameof(request.Level)] = request.Level;
dataObject[nameof(request.Url)] = request.Url;
dataObject[nameof(request.User.Username)] = request.User.Username;
dataObject["UserId"] = request.User.Id;
await _webhookSender.SendGenericNotification(NotificationType.Generic, parameters);
await _webhookSender.SendGenericNotification(NotificationType.Generic, dataObject);
}
/// <inheritdoc />

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webhook.Configuration;
@ -129,8 +128,9 @@ namespace Jellyfin.Plugin.Webhook.Notifiers
_logger.LogDebug("Notifying for {itemName}", item.Name);
// Send notification to each configured destination.
var dataObject = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
.AddBaseItemData(_applicationHost, item);
var dataObject = DataObjectHelpers
.GetBaseDataObject(_applicationHost, NotificationType.ItemAdded)
.AddBaseItemData(item);
var itemType = item.GetType();
await _webhookSender.SendItemNotification(NotificationType.ItemAdded, dataObject, itemType);

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webhook.Destinations;
using Jellyfin.Plugin.Webhook.Helpers;
@ -51,10 +50,10 @@ namespace Jellyfin.Plugin.Webhook.Notifiers
return;
}
var dataObject = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
.AddBaseItemData(_applicationHost, eventArgs.Item)
var dataObject = DataObjectHelpers
.GetBaseDataObject(_applicationHost, NotificationType.PlaybackProgress)
.AddBaseItemData(eventArgs.Item)
.AddPlaybackProgressData(eventArgs);
dataObject[nameof(NotificationType)] = NotificationType.PlaybackProgress;
foreach (var user in eventArgs.Users)
{

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webhook.Destinations;
using Jellyfin.Plugin.Webhook.Helpers;
@ -51,10 +50,10 @@ namespace Jellyfin.Plugin.Webhook.Notifiers
return;
}
var dataObject = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
.AddBaseItemData(_applicationHost, eventArgs.Item)
var dataObject = DataObjectHelpers
.GetBaseDataObject(_applicationHost, NotificationType.PlaybackStart)
.AddBaseItemData(eventArgs.Item)
.AddPlaybackProgressData(eventArgs);
dataObject[nameof(NotificationType)] = NotificationType.PlaybackStart;
foreach (var user in eventArgs.Users)
{

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using Jellyfin.Plugin.Webhook.Destinations;
using Jellyfin.Plugin.Webhook.Helpers;
@ -51,10 +50,10 @@ namespace Jellyfin.Plugin.Webhook.Notifiers
return;
}
var dataObject = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
.AddBaseItemData(_applicationHost, eventArgs.Item)
var dataObject = DataObjectHelpers
.GetBaseDataObject(_applicationHost, NotificationType.PlaybackStop)
.AddBaseItemData(eventArgs.Item)
.AddPlaybackProgressData(eventArgs);
dataObject[nameof(NotificationType)] = NotificationType.PlaybackStop;
dataObject[nameof(eventArgs.PlayedToCompletion)] = eventArgs.PlayedToCompletion;
foreach (var user in eventArgs.Users)

135
README.md
View File

@ -1,4 +1,5 @@
# Jellyfin Webhook Plugin
# Jellyfin Webhook Plugin
## Part of the [Jellyfin Project](https://jellyfin.org)
#### Repository - https://repo.codyrobibero.dev/manifest.json
@ -18,70 +19,80 @@ See [Templates](Jellyfin.Plugin.Webhook/Templates) for sample templates.
#### Variables:
- Every Notifier
- ServerId
- ServerName
- ServerVersion
- `$major.$minor.$build`
- ServerUrl
- Server url
- NotificationType
- The [NotificationType](Jellyfin.Plugin.Webhook/Destinations/NotificationType.cs)
- BaseItem:
- Timestamp
- Current server time local
- UtcTimestamp
- Current server time utc
- Name
- Item name
- Overview
- Item overview
- ItemId
- Item id
- ServerId
- Server id
- ServerUrl
- Server url
- ServerName
- Server name
- ItemType
- Item type
- Year
- Item production year
- SeriesName
- TV series name
- SeasonNumber
- Series number - direct format
- SeasonNumber00
- Series number - padded 00
- SeasonNumber000
- Series number - padded 000
- EpisodeNumber
- Episode number - direct format
- EpisodeNumber00
- Episode number - padded 00
- Timestamp
- Current server time local
- UtcTimestamp
- Current server time utc
- Name
- Item name
- Overview
- Item overview
- ItemId
- Item id
- ItemType
- Item type
- Year
- Item production year
- SeriesName
- TV series name
- SeasonNumber
- Series number - direct format
- SeasonNumber00
- Series number - padded 00
- SeasonNumber000
- Series number - padded 000
- EpisodeNumber
- Episode number - direct format
- EpisodeNumber00
- Episode number - padded 00
- EpisodeNumber000 -
- Episode number - padded 000
- Provider_{providerId_lowercase}
- ProviderId is lowercase.
- Episode number - padded 000
- Provider_{providerId_lowercase}
- ProviderId is lowercase.
- RunTimeTicks
- The media runtime, in [Ticks](https://docs.microsoft.com/en-us/dotnet/api/system.datetime.ticks)
- RunTime
- The media runtime, as `hh:mm:ss`
- Playback
- Includes everything from `BaseItem`
- PlaybackPositionTicks
- The current playback position, in ticks
- MediaSourceId
- The media source id
- IsPaused
- If playback is paused
- IsAutomated
- If notification is automated, or user triggered
- DeviceId
- Playback device id
- DeviceName
- Playback device name
- ClientName
- Playback client name
- Username
- User playing item. Note: multiple notifications will be sent if there are multiple users in a session
- UserId
- The user Id
- NotificationType
- The [NotificationType](Jellyfin.Plugin.Webhook/Destinations/NotificationType.cs)
- PlayedToCompletion
- `true/false`, Only when `NotificationType == PlaybackStop`
- Includes everything from `BaseItem`
- PlaybackPositionTicks
- The current playback position, in [Ticks](https://docs.microsoft.com/en-us/dotnet/api/system.datetime.ticks)
- PlaybackPosition
- The current playback position, as `hh:mm:ss`
- MediaSourceId
- The media source id
- IsPaused
- If playback is paused
- IsAutomated
- If notification is automated, or user triggered
- DeviceId
- Playback device id
- DeviceName
- Playback device name
- ClientName
- Playback client name
- Username
- User playing item. Note: multiple notifications will be sent if there are multiple users in a session
- UserId
- The user Id
- PlayedToCompletion
- `true/false`, Only when `NotificationType == PlaybackStop`
#### Destination Specific:
- Discord
- MentionType
- EmbedColor
@ -98,6 +109,6 @@ See [Templates](Jellyfin.Plugin.Webhook/Templates) for sample templates.
- MessageUrlTitle
- MessagePriority
- NotificationSound
Future events can be created from https://github.com/jellyfin/jellyfin/blob/master/Jellyfin.Server.Implementations/Events/EventingServiceCollectionExtensions.cs
Future events can be created
from https://github.com/jellyfin/jellyfin/blob/master/Jellyfin.Server.Implementations/Events/EventingServiceCollectionExtensions.cs