jellyfin/Emby.Server.Implementations/Notifications/Notifications.cs

565 lines
19 KiB
C#
Raw Normal View History

2014-08-27 05:25:39 +02:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Plugins;
2013-07-06 23:23:32 +02:00
using MediaBrowser.Common.Updates;
2014-04-26 04:55:07 +02:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Devices;
2013-07-07 04:01:14 +02:00
using MediaBrowser.Controller.Entities;
2014-05-10 19:28:03 +02:00
using MediaBrowser.Controller.Entities.Audio;
2013-07-06 23:23:32 +02:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Controller.Plugins;
2014-04-26 04:55:07 +02:00
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Entities;
2014-05-08 22:09:53 +02:00
using MediaBrowser.Model.Events;
2013-07-06 23:23:32 +02:00
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Notifications;
2013-09-18 04:43:34 +02:00
using MediaBrowser.Model.Tasks;
2014-05-08 07:04:39 +02:00
using MediaBrowser.Model.Updates;
2013-07-06 23:23:32 +02:00
using System;
2014-04-27 05:42:05 +02:00
using System.Collections.Generic;
2014-05-16 19:11:07 +02:00
using System.Globalization;
2013-07-06 23:23:32 +02:00
using System.Linq;
using System.Threading;
2014-04-26 04:55:07 +02:00
using System.Threading.Tasks;
2016-04-13 18:17:52 +02:00
using MediaBrowser.Controller.Entities.TV;
2016-11-03 08:14:14 +01:00
using MediaBrowser.Model.Threading;
2013-07-06 23:23:32 +02:00
2016-11-03 08:14:14 +01:00
namespace Emby.Server.Implementations.Notifications
2013-07-06 23:23:32 +02:00
{
/// <summary>
/// Creates notifications for various system events
/// </summary>
public class Notifications : IServerEntryPoint
{
private readonly IInstallationManager _installationManager;
private readonly IUserManager _userManager;
private readonly ILogger _logger;
private readonly ITaskManager _taskManager;
2014-04-25 22:15:50 +02:00
private readonly INotificationManager _notificationManager;
2013-07-06 23:23:32 +02:00
2014-04-26 04:55:07 +02:00
private readonly ILibraryManager _libraryManager;
private readonly ISessionManager _sessionManager;
private readonly IServerApplicationHost _appHost;
2016-11-03 08:14:14 +01:00
private readonly ITimerFactory _timerFactory;
2014-04-25 22:47:56 +02:00
2016-11-03 08:14:14 +01:00
private ITimer LibraryUpdateTimer { get; set; }
2014-05-08 07:04:39 +02:00
private readonly object _libraryChangedSyncLock = new object();
2014-08-27 05:25:39 +02:00
private readonly IConfigurationManager _config;
private readonly IDeviceManager _deviceManager;
2014-08-27 05:25:39 +02:00
2016-11-03 08:14:14 +01:00
public Notifications(IInstallationManager installationManager, IUserManager userManager, ILogger logger, ITaskManager taskManager, INotificationManager notificationManager, ILibraryManager libraryManager, ISessionManager sessionManager, IServerApplicationHost appHost, IConfigurationManager config, IDeviceManager deviceManager, ITimerFactory timerFactory)
2013-07-06 23:23:32 +02:00
{
_installationManager = installationManager;
_userManager = userManager;
_logger = logger;
_taskManager = taskManager;
2014-04-25 22:15:50 +02:00
_notificationManager = notificationManager;
2014-04-26 04:55:07 +02:00
_libraryManager = libraryManager;
_sessionManager = sessionManager;
_appHost = appHost;
2014-08-27 05:25:39 +02:00
_config = config;
_deviceManager = deviceManager;
2016-11-03 08:14:14 +01:00
_timerFactory = timerFactory;
2013-07-06 23:23:32 +02:00
}
public void Run()
{
2014-04-27 05:42:05 +02:00
_installationManager.PluginInstalled += _installationManager_PluginInstalled;
_installationManager.PluginUpdated += _installationManager_PluginUpdated;
2013-07-06 23:23:32 +02:00
_installationManager.PackageInstallationFailed += _installationManager_PackageInstallationFailed;
_installationManager.PluginUninstalled += _installationManager_PluginUninstalled;
_taskManager.TaskCompleted += _taskManager_TaskCompleted;
2013-07-07 04:01:14 +02:00
_userManager.UserCreated += _userManager_UserCreated;
2014-04-26 04:55:07 +02:00
_libraryManager.ItemAdded += _libraryManager_ItemAdded;
_sessionManager.PlaybackStart += _sessionManager_PlaybackStart;
2014-05-16 19:11:07 +02:00
_sessionManager.PlaybackStopped += _sessionManager_PlaybackStopped;
2014-04-26 04:55:07 +02:00
_appHost.HasPendingRestartChanged += _appHost_HasPendingRestartChanged;
_appHost.HasUpdateAvailableChanged += _appHost_HasUpdateAvailableChanged;
2014-04-29 05:56:20 +02:00
_appHost.ApplicationUpdated += _appHost_ApplicationUpdated;
2015-11-08 02:17:32 +01:00
_deviceManager.CameraImageUploaded += _deviceManager_CameraImageUploaded;
2015-03-02 06:16:29 +01:00
2015-11-08 02:17:32 +01:00
_userManager.UserLockedOut += _userManager_UserLockedOut;
2015-03-02 06:16:29 +01:00
}
async void _userManager_UserLockedOut(object sender, GenericEventArgs<User> e)
{
var type = NotificationType.UserLockedOut.ToString();
var notification = new NotificationRequest
{
NotificationType = type
};
notification.Variables["UserName"] = e.Argument.Name;
await SendNotification(notification).ConfigureAwait(false);
}
async void _deviceManager_CameraImageUploaded(object sender, GenericEventArgs<CameraImageUploadInfo> e)
{
var type = NotificationType.CameraImageUploaded.ToString();
var notification = new NotificationRequest
{
NotificationType = type
};
notification.Variables["DeviceName"] = e.Argument.Device.Name;
await SendNotification(notification).ConfigureAwait(false);
2014-04-29 05:56:20 +02:00
}
2014-04-30 17:07:02 +02:00
async void _appHost_ApplicationUpdated(object sender, GenericEventArgs<PackageVersionInfo> e)
2014-04-29 05:56:20 +02:00
{
var type = NotificationType.ApplicationUpdateInstalled.ToString();
var notification = new NotificationRequest
{
2016-04-03 20:23:17 +02:00
NotificationType = type,
Url = e.Argument.infoUrl
2014-04-29 05:56:20 +02:00
};
2014-04-30 17:07:02 +02:00
notification.Variables["Version"] = e.Argument.versionStr;
notification.Variables["ReleaseNotes"] = e.Argument.description;
2014-05-04 01:38:23 +02:00
2014-04-29 05:56:20 +02:00
await SendNotification(notification).ConfigureAwait(false);
2013-07-07 04:01:14 +02:00
}
2014-04-27 05:42:05 +02:00
async void _installationManager_PluginUpdated(object sender, GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>> e)
2013-07-07 04:01:14 +02:00
{
2014-04-27 05:42:05 +02:00
var type = NotificationType.PluginUpdateInstalled.ToString();
var installationInfo = e.Argument.Item1;
var notification = new NotificationRequest
2014-04-26 04:55:07 +02:00
{
2014-05-30 21:23:56 +02:00
Description = e.Argument.Item2.description,
2014-04-27 05:42:05 +02:00
NotificationType = type
};
notification.Variables["Name"] = installationInfo.Name;
notification.Variables["Version"] = installationInfo.Version.ToString();
2014-04-30 17:07:02 +02:00
notification.Variables["ReleaseNotes"] = e.Argument.Item2.description;
2014-04-27 05:42:05 +02:00
await SendNotification(notification).ConfigureAwait(false);
}
async void _installationManager_PluginInstalled(object sender, GenericEventArgs<PackageVersionInfo> e)
{
var type = NotificationType.PluginInstalled.ToString();
var installationInfo = e.Argument;
2014-04-26 04:55:07 +02:00
2014-04-25 22:15:50 +02:00
var notification = new NotificationRequest
2013-07-07 04:01:14 +02:00
{
2014-04-27 05:42:05 +02:00
Description = installationInfo.description,
NotificationType = type
2013-07-07 04:01:14 +02:00
};
2014-04-27 05:42:05 +02:00
notification.Variables["Name"] = installationInfo.name;
notification.Variables["Version"] = installationInfo.versionStr;
2014-04-26 04:55:07 +02:00
await SendNotification(notification).ConfigureAwait(false);
}
2014-04-27 05:42:05 +02:00
async void _appHost_HasUpdateAvailableChanged(object sender, EventArgs e)
2014-04-26 04:55:07 +02:00
{
2014-04-27 05:42:05 +02:00
// This notification is for users who can't auto-update (aka running as service)
if (!_appHost.HasUpdateAvailable || _appHost.CanSelfUpdate)
2013-07-07 04:01:14 +02:00
{
2014-04-26 04:55:07 +02:00
return;
2013-07-07 04:01:14 +02:00
}
2014-04-26 04:55:07 +02:00
2014-04-27 05:42:05 +02:00
var type = NotificationType.ApplicationUpdateAvailable.ToString();
2014-04-26 04:55:07 +02:00
var notification = new NotificationRequest
2013-07-07 04:01:14 +02:00
{
2015-03-21 19:12:12 +01:00
Description = "Please see emby.media for details.",
2014-04-27 05:42:05 +02:00
NotificationType = type
2014-04-26 04:55:07 +02:00
};
await SendNotification(notification).ConfigureAwait(false);
}
2014-04-27 05:42:05 +02:00
async void _appHost_HasPendingRestartChanged(object sender, EventArgs e)
2014-04-26 04:55:07 +02:00
{
2014-04-27 05:42:05 +02:00
if (!_appHost.HasPendingRestart)
2014-04-26 04:55:07 +02:00
{
return;
}
2014-04-27 05:42:05 +02:00
var type = NotificationType.ServerRestartRequired.ToString();
var notification = new NotificationRequest
{
NotificationType = type
};
2014-04-26 04:55:07 +02:00
2014-04-27 05:42:05 +02:00
await SendNotification(notification).ConfigureAwait(false);
}
2014-04-26 04:55:07 +02:00
2014-08-27 05:25:39 +02:00
private NotificationOptions GetOptions()
{
return _config.GetConfiguration<NotificationOptions>("notifications");
}
2014-05-16 19:11:07 +02:00
2014-08-27 05:25:39 +02:00
void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e)
{
var item = e.MediaInfo;
2014-05-16 19:11:07 +02:00
2014-08-27 05:25:39 +02:00
if (item == null)
{
_logger.Warn("PlaybackStart reported with null media info.");
return;
}
2014-04-26 04:55:07 +02:00
2016-04-27 04:59:43 +02:00
var video = e.Item as Video;
if (video != null && video.IsThemeMedia)
{
return;
}
2014-08-27 05:25:39 +02:00
var type = GetPlaybackNotificationType(item.MediaType);
SendPlaybackNotification(type, e);
}
2014-05-16 19:11:07 +02:00
void _sessionManager_PlaybackStopped(object sender, PlaybackStopEventArgs e)
{
2014-04-27 05:42:05 +02:00
var item = e.MediaInfo;
2014-04-26 04:55:07 +02:00
2014-05-04 16:19:46 +02:00
if (item == null)
{
2014-05-16 19:11:07 +02:00
_logger.Warn("PlaybackStopped reported with null media info.");
2014-05-04 16:19:46 +02:00
return;
}
2016-04-27 04:59:43 +02:00
var video = e.Item as Video;
if (video != null && video.IsThemeMedia)
{
return;
}
2014-05-16 19:11:07 +02:00
var type = GetPlaybackStoppedNotificationType(item.MediaType);
SendPlaybackNotification(type, e);
}
private async void SendPlaybackNotification(string type, PlaybackProgressEventArgs e)
{
var user = e.Users.FirstOrDefault();
2014-08-27 05:25:39 +02:00
if (user != null && !GetOptions().IsEnabledToMonitorUser(type, user.Id.ToString("N")))
{
return;
}
2014-05-16 19:11:07 +02:00
var item = e.MediaInfo;
2016-10-31 05:28:23 +01:00
if ( item.IsThemeMedia)
2014-04-28 17:05:28 +02:00
{
// Don't report theme song or local trailer playback
return;
}
2014-04-26 04:55:07 +02:00
var notification = new NotificationRequest
{
2014-08-26 04:30:52 +02:00
NotificationType = type
2014-04-26 04:55:07 +02:00
};
2015-07-14 18:39:34 +02:00
if (e.Item != null)
{
notification.Variables["ItemName"] = GetItemName(e.Item);
}
else
{
notification.Variables["ItemName"] = item.Name;
}
2014-04-27 05:42:05 +02:00
notification.Variables["UserName"] = user == null ? "Unknown user" : user.Name;
notification.Variables["AppName"] = e.ClientName;
notification.Variables["DeviceName"] = e.DeviceName;
2014-04-26 04:55:07 +02:00
await SendNotification(notification).ConfigureAwait(false);
}
2014-05-04 01:38:23 +02:00
2014-04-27 19:54:43 +02:00
private string GetPlaybackNotificationType(string mediaType)
2014-04-26 04:55:07 +02:00
{
if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
{
2014-04-27 19:54:43 +02:00
return NotificationType.AudioPlayback.ToString();
2014-04-26 04:55:07 +02:00
}
if (string.Equals(mediaType, MediaType.Game, StringComparison.OrdinalIgnoreCase))
{
2014-04-27 19:54:43 +02:00
return NotificationType.GamePlayback.ToString();
2014-04-26 04:55:07 +02:00
}
if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
{
2014-04-27 19:54:43 +02:00
return NotificationType.VideoPlayback.ToString();
2014-04-26 04:55:07 +02:00
}
2014-04-27 19:54:43 +02:00
return null;
2014-04-26 04:55:07 +02:00
}
2014-05-16 19:11:07 +02:00
private string GetPlaybackStoppedNotificationType(string mediaType)
{
if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
{
return NotificationType.AudioPlaybackStopped.ToString();
}
if (string.Equals(mediaType, MediaType.Game, StringComparison.OrdinalIgnoreCase))
{
return NotificationType.GamePlaybackStopped.ToString();
}
if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
{
return NotificationType.VideoPlaybackStopped.ToString();
}
return null;
}
2014-05-08 07:04:39 +02:00
private readonly List<BaseItem> _itemsAdded = new List<BaseItem>();
void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
2014-04-26 04:55:07 +02:00
{
2015-11-08 02:17:32 +01:00
if (!FilterItem(e.Item))
2014-04-26 04:55:07 +02:00
{
2015-11-08 02:17:32 +01:00
return;
}
lock (_libraryChangedSyncLock)
{
if (LibraryUpdateTimer == null)
2014-05-08 07:04:39 +02:00
{
2016-11-03 08:14:14 +01:00
LibraryUpdateTimer = _timerFactory.Create(LibraryUpdateTimerCallback, null, 5000,
2015-11-08 02:17:32 +01:00
Timeout.Infinite);
2014-05-08 07:04:39 +02:00
}
2015-11-08 02:17:32 +01:00
else
{
LibraryUpdateTimer.Change(5000, Timeout.Infinite);
}
_itemsAdded.Add(e.Item);
2014-05-08 07:04:39 +02:00
}
}
2015-11-08 02:17:32 +01:00
private bool FilterItem(BaseItem item)
{
2016-04-09 23:16:17 +02:00
if (item.IsFolder)
2015-11-08 02:17:32 +01:00
{
return false;
}
2016-04-09 23:16:17 +02:00
if (item.LocationType == LocationType.Virtual)
{
return false;
}
if (item is IItemByName)
2015-11-08 02:17:32 +01:00
{
return false;
}
2016-03-19 05:05:33 +01:00
return item.SourceType == SourceType.Library;
2015-11-08 02:17:32 +01:00
}
2014-05-08 07:04:39 +02:00
private async void LibraryUpdateTimerCallback(object state)
{
List<BaseItem> items;
lock (_libraryChangedSyncLock)
{
items = _itemsAdded.ToList();
_itemsAdded.Clear();
DisposeLibraryUpdateTimer();
}
2014-04-27 05:42:05 +02:00
2016-10-07 17:08:13 +02:00
items = items.Take(10).ToList();
2016-10-07 17:08:13 +02:00
foreach (var item in items)
{
2014-04-26 04:55:07 +02:00
var notification = new NotificationRequest
{
2014-05-08 07:04:39 +02:00
NotificationType = NotificationType.NewLibraryContent.ToString()
2014-04-26 04:55:07 +02:00
};
2014-05-10 19:28:03 +02:00
notification.Variables["Name"] = GetItemName(item);
2014-05-04 01:38:23 +02:00
2014-04-26 04:55:07 +02:00
await SendNotification(notification).ConfigureAwait(false);
}
}
2014-08-11 00:13:17 +02:00
public static string GetItemName(BaseItem item)
2014-05-10 19:28:03 +02:00
{
var name = item.Name;
2016-04-13 18:17:52 +02:00
var episode = item as Episode;
if (episode != null)
{
if (episode.IndexNumber.HasValue)
{
name = string.Format("Ep{0} - {1}", episode.IndexNumber.Value.ToString(CultureInfo.InvariantCulture), name);
}
if (episode.ParentIndexNumber.HasValue)
{
name = string.Format("S{0}, {1}", episode.ParentIndexNumber.Value.ToString(CultureInfo.InvariantCulture), name);
}
}
2014-05-10 19:28:03 +02:00
var hasSeries = item as IHasSeries;
if (hasSeries != null)
{
name = hasSeries.SeriesName + " - " + name;
}
var hasArtist = item as IHasArtist;
if (hasArtist != null)
{
var artists = hasArtist.AllArtists;
if (artists.Count > 0)
{
name = hasArtist.AllArtists[0] + " - " + name;
}
}
return name;
}
2017-01-13 08:26:22 +01:00
public static string GetItemName(BaseItemInfo item)
{
var name = item.Name;
if (!string.IsNullOrWhiteSpace(item.SeriesName))
{
name = item.SeriesName + " - " + name;
}
if (item.Artists != null && item.Artists.Count > 0)
{
name = item.Artists[0] + " - " + name;
}
return name;
}
2014-04-26 04:55:07 +02:00
async void _userManager_UserCreated(object sender, GenericEventArgs<User> e)
{
var notification = new NotificationRequest
{
2014-04-27 05:42:05 +02:00
UserIds = new List<string> { e.Argument.Id.ToString("N") },
2015-03-21 17:10:02 +01:00
Name = "Welcome to Emby!",
2014-04-26 04:55:07 +02:00
Description = "Check back here for more notifications."
};
await SendNotification(notification).ConfigureAwait(false);
2013-07-06 23:23:32 +02:00
}
2014-05-10 19:28:03 +02:00
async void _taskManager_TaskCompleted(object sender, TaskCompletionEventArgs e)
2013-07-06 23:23:32 +02:00
{
2014-05-10 19:28:03 +02:00
var result = e.Result;
2013-07-06 23:23:32 +02:00
2014-04-27 05:42:05 +02:00
if (result.Status == TaskCompletionStatus.Failed)
2013-07-06 23:23:32 +02:00
{
2014-04-27 05:42:05 +02:00
var type = NotificationType.TaskFailed.ToString();
2014-04-25 22:15:50 +02:00
var notification = new NotificationRequest
{
Description = result.ErrorMessage,
2014-04-27 05:42:05 +02:00
Level = NotificationLevel.Error,
NotificationType = type
2014-04-25 22:15:50 +02:00
};
2014-05-10 19:28:03 +02:00
notification.Variables["Name"] = result.Name;
notification.Variables["ErrorMessage"] = result.ErrorMessage;
2014-04-27 05:42:05 +02:00
2014-04-26 04:55:07 +02:00
await SendNotification(notification).ConfigureAwait(false);
2013-07-06 23:23:32 +02:00
}
}
async void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
{
2014-04-27 05:42:05 +02:00
var type = NotificationType.PluginUninstalled.ToString();
2014-05-04 01:38:23 +02:00
2013-07-06 23:23:32 +02:00
var plugin = e.Argument;
2014-04-25 22:15:50 +02:00
var notification = new NotificationRequest
2013-07-06 23:23:32 +02:00
{
2014-04-27 05:42:05 +02:00
NotificationType = type
2014-04-25 22:15:50 +02:00
};
2013-07-06 23:23:32 +02:00
2014-04-27 05:42:05 +02:00
notification.Variables["Name"] = plugin.Name;
notification.Variables["Version"] = plugin.Version.ToString();
2014-05-04 01:38:23 +02:00
2014-04-26 04:55:07 +02:00
await SendNotification(notification).ConfigureAwait(false);
2013-07-06 23:23:32 +02:00
}
async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
{
var installationInfo = e.InstallationInfo;
2014-04-27 05:42:05 +02:00
var type = NotificationType.InstallationFailed.ToString();
2014-04-25 22:15:50 +02:00
var notification = new NotificationRequest
2013-07-06 23:23:32 +02:00
{
2014-04-25 22:15:50 +02:00
Level = NotificationLevel.Error,
2014-04-27 05:42:05 +02:00
Description = e.Exception.Message,
NotificationType = type
2014-04-25 22:15:50 +02:00
};
2013-07-06 23:23:32 +02:00
2014-04-27 05:42:05 +02:00
notification.Variables["Name"] = installationInfo.Name;
notification.Variables["Version"] = installationInfo.Version;
2014-04-26 04:55:07 +02:00
await SendNotification(notification).ConfigureAwait(false);
}
private async Task SendNotification(NotificationRequest notification)
{
2014-04-25 22:15:50 +02:00
try
{
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error sending notification", ex);
2013-07-06 23:23:32 +02:00
}
}
public void Dispose()
{
2014-05-08 07:04:39 +02:00
DisposeLibraryUpdateTimer();
2014-04-27 05:42:05 +02:00
_installationManager.PluginInstalled -= _installationManager_PluginInstalled;
_installationManager.PluginUpdated -= _installationManager_PluginUpdated;
2013-07-06 23:23:32 +02:00
_installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
2013-07-07 04:01:14 +02:00
_installationManager.PluginUninstalled -= _installationManager_PluginUninstalled;
_taskManager.TaskCompleted -= _taskManager_TaskCompleted;
_userManager.UserCreated -= _userManager_UserCreated;
2014-04-26 04:55:07 +02:00
_libraryManager.ItemAdded -= _libraryManager_ItemAdded;
_sessionManager.PlaybackStart -= _sessionManager_PlaybackStart;
_appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged;
_appHost.HasUpdateAvailableChanged -= _appHost_HasUpdateAvailableChanged;
2014-04-29 05:56:20 +02:00
_appHost.ApplicationUpdated -= _appHost_ApplicationUpdated;
_deviceManager.CameraImageUploaded -= _deviceManager_CameraImageUploaded;
2015-03-02 06:16:29 +01:00
_userManager.UserLockedOut -= _userManager_UserLockedOut;
2013-07-06 23:23:32 +02:00
}
2014-05-08 07:04:39 +02:00
private void DisposeLibraryUpdateTimer()
{
if (LibraryUpdateTimer != null)
{
LibraryUpdateTimer.Dispose();
LibraryUpdateTimer = null;
}
}
2013-07-06 23:23:32 +02:00
}
}