jellyfin/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifications.cs

527 lines
18 KiB
C#
Raw Normal View History

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