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

457 lines
16 KiB
C#
Raw Normal View History

2014-05-16 17:11:07 +00:00
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;
2014-04-25 20:47:56 +00:00
using MediaBrowser.Controller.Configuration;
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;
2014-04-27 03:42:05 +00:00
using MediaBrowser.Model.Configuration;
2014-04-26 02:55:07 +00:00
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 IServerConfigurationManager _config;
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-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;
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;
}
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
{
2014-05-08 21:23:24 +00:00
Description = "Please see mediabrowser.tv 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-05-16 17:11:07 +00:00
void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e)
{
var item = e.MediaInfo;
if (item == null)
{
_logger.Warn("PlaybackStart reported with null media info.");
return;
}
var type = GetPlaybackNotificationType(item.MediaType);
2014-04-26 02:55:07 +00:00
2014-05-16 17:11:07 +00:00
SendPlaybackNotification(type, e);
}
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();
var item = e.MediaInfo;
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-05-16 17:11:07 +00:00
NotificationType = type,
2014-04-27 19:30:12 +00:00
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
}
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
{
2014-05-08 05:04:39 +00:00
if (e.Item.LocationType == LocationType.FileSystem && !e.Item.IsFolder)
2014-04-26 02:55:07 +00:00
{
2014-05-08 05:04:39 +00:00
lock (_libraryChangedSyncLock)
{
if (LibraryUpdateTimer == null)
{
LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, 5000,
Timeout.Infinite);
}
else
{
LibraryUpdateTimer.Change(5000, Timeout.Infinite);
}
_itemsAdded.Add(e.Item);
}
}
}
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-05-10 17:28:03 +00:00
private string GetItemName(BaseItem item)
{
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") },
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
}
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;
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
}
}