jellyfin-server/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs

333 lines
12 KiB
C#
Raw Normal View History

2013-07-06 21:23:32 +00:00
using MediaBrowser.Common.Events;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Common.Updates;
2014-04-26 02:55:07 +00:00
using MediaBrowser.Controller;
2014-04-25 20:47:56 +00:00
using MediaBrowser.Controller.Configuration;
2013-07-07 02:01:14 +00:00
using MediaBrowser.Controller.Entities;
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;
2014-04-27 03:42:05 +00:00
using MediaBrowser.Model.Configuration;
2014-04-26 02:55:07 +00:00
using MediaBrowser.Model.Entities;
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;
2013-07-06 21:23:32 +00:00
using System;
2014-04-27 03:42:05 +00:00
using System.Collections.Generic;
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;
2014-04-27 03:42:05 +00:00
using MediaBrowser.Model.Updates;
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 IServerConfigurationManager _config;
private readonly ILibraryManager _libraryManager;
private readonly ISessionManager _sessionManager;
private readonly IServerApplicationHost _appHost;
2014-04-25 20:47:56 +00:00
2014-04-26 02:55:07 +00:00
public Notifications(IInstallationManager installationManager, IUserManager userManager, ILogger logger, ITaskManager taskManager, INotificationManager notificationManager, IServerConfigurationManager config, ILibraryManager libraryManager, ISessionManager sessionManager, IServerApplicationHost appHost)
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
_config = config;
_libraryManager = libraryManager;
_sessionManager = sessionManager;
_appHost = appHost;
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;
_appHost.HasPendingRestartChanged += _appHost_HasPendingRestartChanged;
_appHost.HasUpdateAvailableChanged += _appHost_HasUpdateAvailableChanged;
2014-04-29 03:56:20 +00:00
_appHost.ApplicationUpdated += _appHost_ApplicationUpdated;
}
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-04-27 03:42:05 +00:00
Description = installationInfo.Description,
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
{
2014-04-27 03:42:05 +00:00
Description = "Please see mediabrowser3.com for details.",
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-04-27 03:42:05 +00:00
async void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e)
{
2014-04-26 02:55:07 +00:00
var user = e.Users.FirstOrDefault();
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)
{
_logger.Warn("PlaybackStart reported with null media info.");
return;
}
2014-05-03 23:38:23 +00:00
if (e.Item != null && e.Item.Parent == null)
2014-04-28 15:05:28 +00:00
{
// Don't report theme song or local trailer playback
// TODO: This will also cause movie specials to not be reported
return;
}
2014-04-26 02:55:07 +00:00
var notification = new NotificationRequest
{
2014-04-27 19:30:12 +00:00
NotificationType = GetPlaybackNotificationType(item.MediaType),
ExcludeUserIds = e.Users.Select(i => i.Id.ToString("N")).ToList()
2014-04-26 02:55:07 +00:00
};
2014-04-27 03:42:05 +00:00
notification.Variables["ItemName"] = item.Name;
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
}
async void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
{
2014-04-27 03:42:05 +00:00
if (e.Item.LocationType == LocationType.FileSystem)
2014-04-26 02:55:07 +00:00
{
2014-04-27 03:42:05 +00:00
var type = NotificationType.NewLibraryContent.ToString();
2014-04-26 02:55:07 +00:00
var item = e.Item;
var notification = new NotificationRequest
{
2014-04-27 03:42:05 +00:00
NotificationType = type
2014-04-26 02:55:07 +00:00
};
2014-04-27 03:42:05 +00:00
notification.Variables["Name"] = item.Name;
2014-05-03 23:38:23 +00:00
2014-04-26 02:55:07 +00:00
await SendNotification(notification).ConfigureAwait(false);
}
}
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") },
2014-04-26 02:55:07 +00:00
Name = "Welcome to Media Browser!",
Description = "Check back here for more notifications."
};
await SendNotification(notification).ConfigureAwait(false);
2013-07-06 21:23:32 +00:00
}
async void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e)
{
var result = e.Argument;
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-04-27 03:42:05 +00:00
notification.Variables["Name"] = e.Argument.Name;
2014-04-30 15:07:02 +00:00
notification.Variables["ErrorMessage"] = e.Argument.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-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;
2013-07-06 21:23:32 +00:00
}
}
}