jellyfin-server/Emby.Notifications/Notifications.cs

292 lines
9.2 KiB
C#
Raw Normal View History

using System;
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;
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;
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Globalization;
2018-09-12 17:26:21 +00:00
using MediaBrowser.Model.Notifications;
using Microsoft.Extensions.Logging;
2018-09-12 17:26:21 +00:00
namespace Emby.Notifications
{
/// <summary>
/// Creates notifications for various system events
/// </summary>
public class Notifications : IServerEntryPoint
{
private readonly ILogger _logger;
private readonly INotificationManager _notificationManager;
private readonly ILibraryManager _libraryManager;
private readonly IServerApplicationHost _appHost;
2019-02-05 08:49:46 +00:00
private Timer LibraryUpdateTimer { get; set; }
2018-09-12 17:26:21 +00:00
private readonly object _libraryChangedSyncLock = new object();
private readonly IConfigurationManager _config;
private readonly ILocalizationManager _localization;
private readonly IActivityManager _activityManager;
private string[] _coreNotificationTypes;
2019-02-06 20:00:21 +00:00
public Notifications(
IActivityManager activityManager,
ILocalizationManager localization,
ILogger logger,
INotificationManager notificationManager,
ILibraryManager libraryManager,
IServerApplicationHost appHost,
IConfigurationManager config)
2018-09-12 17:26:21 +00:00
{
_logger = logger;
_notificationManager = notificationManager;
_libraryManager = libraryManager;
_appHost = appHost;
_config = config;
_localization = localization;
_activityManager = activityManager;
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-01-27 14:40:37 +00:00
public Task RunAsync()
2018-09-12 17:26:21 +00:00
{
_libraryManager.ItemAdded += _libraryManager_ItemAdded;
_appHost.HasPendingRestartChanged += _appHost_HasPendingRestartChanged;
_appHost.HasUpdateAvailableChanged += _appHost_HasUpdateAvailableChanged;
_activityManager.EntryCreated += _activityManager_EntryCreated;
2019-01-27 14:40:37 +00:00
return Task.CompletedTask;
2018-09-12 17:26:21 +00:00
}
private async void _appHost_HasPendingRestartChanged(object sender, EventArgs e)
{
var type = NotificationType.ServerRestartRequired.ToString();
var notification = new NotificationRequest
{
NotificationType = type,
Name = string.Format(_localization.GetLocalizedString("ServerNameNeedsToBeRestarted"), _appHost.Name)
};
await SendNotification(notification, null).ConfigureAwait(false);
}
private async void _activityManager_EntryCreated(object sender, GenericEventArgs<ActivityLogEntry> e)
{
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-02-06 20:00:21 +00:00
private async void _appHost_HasUpdateAvailableChanged(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
{
Description = "Please see jellyfin.media for details.",
2018-09-12 17:26:21 +00:00
NotificationType = type,
Name = _localization.GetLocalizedString("NewVersionIsAvailable")
};
await SendNotification(notification, null).ConfigureAwait(false);
}
private readonly List<BaseItem> _itemsAdded = new List<BaseItem>();
2019-02-06 20:00:21 +00:00
private void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
2018-09-12 17:26:21 +00:00
{
if (!FilterItem(e.Item))
{
return;
}
lock (_libraryChangedSyncLock)
{
if (LibraryUpdateTimer == null)
{
2019-02-05 08:49:46 +00:00
LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, 5000,
2018-09-12 17:26:21 +00:00
Timeout.Infinite);
}
else
{
LibraryUpdateTimer.Change(5000, Timeout.Infinite);
}
_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();
DisposeLibraryUpdateTimer();
}
items = items.Take(10).ToList();
foreach (var item in items)
{
var notification = new NotificationRequest
{
NotificationType = NotificationType.NewLibraryContent.ToString(),
Name = string.Format(_localization.GetLocalizedString("ValueHasBeenAddedToLibrary"), GetItemName(item)),
Description = item.Overview
};
await SendNotification(notification, item).ConfigureAwait(false);
}
}
public static string GetItemName(BaseItem item)
{
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
}
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;
}
private async Task SendNotification(NotificationRequest notification, BaseItem relatedItem)
{
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
}
}
public void Dispose()
{
DisposeLibraryUpdateTimer();
_libraryManager.ItemAdded -= _libraryManager_ItemAdded;
_appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged;
_appHost.HasUpdateAvailableChanged -= _appHost_HasUpdateAvailableChanged;
_activityManager.EntryCreated -= _activityManager_EntryCreated;
}
private void DisposeLibraryUpdateTimer()
{
if (LibraryUpdateTimer != null)
{
LibraryUpdateTimer.Dispose();
LibraryUpdateTimer = null;
}
}
}
}