2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2019-01-13 19:18:07 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2018-09-12 17:26:21 +00:00
|
|
|
using MediaBrowser.Controller;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2019-01-13 19:18:07 +00:00
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2018-09-12 17:26:21 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Notifications;
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
2019-01-13 19:18:07 +00:00
|
|
|
using MediaBrowser.Model.Activity;
|
|
|
|
using MediaBrowser.Model.Events;
|
|
|
|
using MediaBrowser.Model.Globalization;
|
2018-09-12 17:26:21 +00:00
|
|
|
using MediaBrowser.Model.Notifications;
|
2019-01-13 19:18:07 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
namespace Emby.Notifications
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-12-20 20:30:51 +00:00
|
|
|
/// Creates notifications for various system events.
|
2018-09-12 17:26:21 +00:00
|
|
|
/// </summary>
|
2019-12-20 20:30:51 +00:00
|
|
|
public class NotificationEntryPoint : IServerEntryPoint
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2020-06-06 00:15:56 +00:00
|
|
|
private readonly ILogger<NotificationEntryPoint> _logger;
|
2019-12-20 20:30:51 +00:00
|
|
|
private readonly IActivityManager _activityManager;
|
|
|
|
private readonly ILocalizationManager _localization;
|
2018-09-12 17:26:21 +00:00
|
|
|
private readonly INotificationManager _notificationManager;
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
2019-12-20 20:30:51 +00:00
|
|
|
private readonly IConfigurationManager _config;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
private readonly object _libraryChangedSyncLock = new object();
|
2019-12-20 20:30:51 +00:00
|
|
|
private readonly List<BaseItem> _itemsAdded = new List<BaseItem>();
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2019-12-20 20:30:51 +00:00
|
|
|
private Timer? _libraryUpdateTimer;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
private string[] _coreNotificationTypes;
|
|
|
|
|
2019-12-20 20:30:51 +00:00
|
|
|
private bool _disposed = false;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="NotificationEntryPoint" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
/// <param name="activityManager">The activity manager.</param>
|
|
|
|
/// <param name="localization">The localization manager.</param>
|
|
|
|
/// <param name="notificationManager">The notification manager.</param>
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
|
|
|
/// <param name="appHost">The application host.</param>
|
|
|
|
/// <param name="config">The configuration manager.</param>
|
|
|
|
public NotificationEntryPoint(
|
|
|
|
ILogger<NotificationEntryPoint> logger,
|
2019-02-06 20:00:21 +00:00
|
|
|
IActivityManager activityManager,
|
|
|
|
ILocalizationManager localization,
|
|
|
|
INotificationManager notificationManager,
|
|
|
|
ILibraryManager libraryManager,
|
|
|
|
IServerApplicationHost appHost,
|
|
|
|
IConfigurationManager config)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2019-12-20 20:30:51 +00:00
|
|
|
_activityManager = activityManager;
|
|
|
|
_localization = localization;
|
2018-09-12 17:26:21 +00:00
|
|
|
_notificationManager = notificationManager;
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
_appHost = appHost;
|
|
|
|
_config = config;
|
|
|
|
|
2019-02-06 19:53:05 +00:00
|
|
|
_coreNotificationTypes = new CoreNotificationTypes(localization).GetNotificationTypes().Select(i => i.Type).ToArray();
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 20:30:51 +00:00
|
|
|
/// <inheritdoc />
|
2019-01-27 14:40:37 +00:00
|
|
|
public Task RunAsync()
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2019-12-20 20:30:51 +00:00
|
|
|
_libraryManager.ItemAdded += OnLibraryManagerItemAdded;
|
|
|
|
_appHost.HasPendingRestartChanged += OnAppHostHasPendingRestartChanged;
|
|
|
|
_appHost.HasUpdateAvailableChanged += OnAppHostHasUpdateAvailableChanged;
|
|
|
|
_activityManager.EntryCreated += OnActivityManagerEntryCreated;
|
2019-01-27 14:40:37 +00:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 20:30:51 +00:00
|
|
|
private async void OnAppHostHasPendingRestartChanged(object sender, EventArgs e)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var type = NotificationType.ServerRestartRequired.ToString();
|
|
|
|
|
|
|
|
var notification = new NotificationRequest
|
|
|
|
{
|
|
|
|
NotificationType = type,
|
2019-12-20 20:30:51 +00:00
|
|
|
Name = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
_localization.GetLocalizedString("ServerNameNeedsToBeRestarted"),
|
|
|
|
_appHost.Name)
|
2018-09-12 17:26:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
await SendNotification(notification, null).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
2019-12-20 20:30:51 +00:00
|
|
|
private async void OnActivityManagerEntryCreated(object sender, GenericEventArgs<ActivityLogEntry> e)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var entry = e.Argument;
|
|
|
|
|
|
|
|
var type = entry.Type;
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(type) || !_coreNotificationTypes.Contains(type, StringComparer.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var userId = e.Argument.UserId;
|
|
|
|
|
|
|
|
if (!userId.Equals(Guid.Empty) && !GetOptions().IsEnabledToMonitorUser(type, userId))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var notification = new NotificationRequest
|
|
|
|
{
|
|
|
|
NotificationType = type,
|
|
|
|
Name = entry.Name,
|
|
|
|
Description = entry.Overview
|
|
|
|
};
|
|
|
|
|
|
|
|
await SendNotification(notification, null).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private NotificationOptions GetOptions()
|
|
|
|
{
|
|
|
|
return _config.GetConfiguration<NotificationOptions>("notifications");
|
|
|
|
}
|
|
|
|
|
2019-12-20 20:30:51 +00:00
|
|
|
private async void OnAppHostHasUpdateAvailableChanged(object sender, EventArgs e)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2019-02-06 19:53:05 +00:00
|
|
|
if (!_appHost.HasUpdateAvailable)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var type = NotificationType.ApplicationUpdateAvailable.ToString();
|
|
|
|
|
|
|
|
var notification = new NotificationRequest
|
|
|
|
{
|
2020-04-09 15:40:04 +00:00
|
|
|
Description = "Please see jellyfin.org for details.",
|
2018-09-12 17:26:21 +00:00
|
|
|
NotificationType = type,
|
|
|
|
Name = _localization.GetLocalizedString("NewVersionIsAvailable")
|
|
|
|
};
|
|
|
|
|
|
|
|
await SendNotification(notification, null).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
2019-12-20 20:30:51 +00:00
|
|
|
private void OnLibraryManagerItemAdded(object sender, ItemChangeEventArgs e)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
if (!FilterItem(e.Item))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (_libraryChangedSyncLock)
|
|
|
|
{
|
2019-12-20 20:30:51 +00:00
|
|
|
if (_libraryUpdateTimer == null)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2019-12-20 20:30:51 +00:00
|
|
|
_libraryUpdateTimer = new Timer(
|
|
|
|
LibraryUpdateTimerCallback,
|
|
|
|
null,
|
|
|
|
5000,
|
|
|
|
Timeout.Infinite);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-20 20:30:51 +00:00
|
|
|
_libraryUpdateTimer.Change(5000, Timeout.Infinite);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_itemsAdded.Add(e.Item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool FilterItem(BaseItem item)
|
|
|
|
{
|
|
|
|
if (item.IsFolder)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!item.HasPathProtocol)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item is IItemByName)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item.SourceType == SourceType.Library;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async void LibraryUpdateTimerCallback(object state)
|
|
|
|
{
|
|
|
|
List<BaseItem> items;
|
|
|
|
|
|
|
|
lock (_libraryChangedSyncLock)
|
|
|
|
{
|
|
|
|
items = _itemsAdded.ToList();
|
|
|
|
_itemsAdded.Clear();
|
2019-12-20 20:30:51 +00:00
|
|
|
_libraryUpdateTimer!.Dispose(); // Shouldn't be null as it just set off this callback
|
|
|
|
_libraryUpdateTimer = null;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
items = items.Take(10).ToList();
|
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
{
|
|
|
|
var notification = new NotificationRequest
|
|
|
|
{
|
|
|
|
NotificationType = NotificationType.NewLibraryContent.ToString(),
|
2019-12-20 20:30:51 +00:00
|
|
|
Name = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
_localization.GetLocalizedString("ValueHasBeenAddedToLibrary"),
|
|
|
|
GetItemName(item)),
|
2018-09-12 17:26:21 +00:00
|
|
|
Description = item.Overview
|
|
|
|
};
|
|
|
|
|
|
|
|
await SendNotification(notification, item).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 21:25:44 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a human readable name for the item.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <returns>A human readable name for the item.</returns>
|
|
|
|
public static string GetItemName(BaseItem item)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var name = item.Name;
|
2019-08-29 20:28:33 +00:00
|
|
|
if (item is Episode episode)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
if (episode.IndexNumber.HasValue)
|
|
|
|
{
|
2019-08-29 20:28:33 +00:00
|
|
|
name = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"Ep{0} - {1}",
|
|
|
|
episode.IndexNumber.Value,
|
|
|
|
name);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2019-12-20 20:30:51 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (episode.ParentIndexNumber.HasValue)
|
|
|
|
{
|
2019-08-29 20:28:33 +00:00
|
|
|
name = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"S{0}, {1}",
|
|
|
|
episode.ParentIndexNumber.Value,
|
|
|
|
name);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 20:28:33 +00:00
|
|
|
if (item is IHasSeries hasSeries)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
name = hasSeries.SeriesName + " - " + name;
|
|
|
|
}
|
|
|
|
|
2019-08-29 20:28:33 +00:00
|
|
|
if (item is IHasAlbumArtist hasAlbumArtist)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var artists = hasAlbumArtist.AlbumArtists;
|
|
|
|
|
2019-08-29 20:28:33 +00:00
|
|
|
if (artists.Count > 0)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
name = artists[0] + " - " + name;
|
|
|
|
}
|
|
|
|
}
|
2019-08-29 20:28:33 +00:00
|
|
|
else if (item is IHasArtist hasArtist)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2019-08-29 20:28:33 +00:00
|
|
|
var artists = hasArtist.Artists;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2019-08-29 20:28:33 +00:00
|
|
|
if (artists.Count > 0)
|
|
|
|
{
|
|
|
|
name = artists[0] + " - " + name;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2019-12-20 20:30:51 +00:00
|
|
|
private async Task SendNotification(NotificationRequest notification, BaseItem? relatedItem)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await _notificationManager.SendNotification(notification, relatedItem, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
_logger.LogError(ex, "Error sending notification");
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 20:30:51 +00:00
|
|
|
/// <inheritdoc />
|
2018-09-12 17:26:21 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
2019-12-20 20:30:51 +00:00
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 20:30:51 +00:00
|
|
|
/// <summary>
|
2020-02-08 21:25:44 +00:00
|
|
|
/// Releases unmanaged and optionally managed resources.
|
2019-12-20 20:30:51 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
protected virtual void Dispose(bool disposing)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2019-12-20 20:30:51 +00:00
|
|
|
if (_disposed)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disposing)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2019-12-20 20:30:51 +00:00
|
|
|
_libraryUpdateTimer?.Dispose();
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
2019-12-20 20:30:51 +00:00
|
|
|
|
|
|
|
_libraryUpdateTimer = null;
|
|
|
|
|
|
|
|
_libraryManager.ItemAdded -= OnLibraryManagerItemAdded;
|
|
|
|
_appHost.HasPendingRestartChanged -= OnAppHostHasPendingRestartChanged;
|
|
|
|
_appHost.HasUpdateAvailableChanged -= OnAppHostHasUpdateAvailableChanged;
|
|
|
|
_activityManager.EntryCreated -= OnActivityManagerEntryCreated;
|
|
|
|
|
|
|
|
_disposed = true;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|