rework notifications infrastructure

This commit is contained in:
Luke Pulverenti
2014-04-25 16:15:50 -04:00
parent eda8159b44
commit 547291f048
34 changed files with 455 additions and 323 deletions

View File

@@ -20,20 +20,20 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
/// </summary>
public class Notifications : IServerEntryPoint
{
private readonly INotificationsRepository _notificationsRepo;
private readonly IInstallationManager _installationManager;
private readonly IUserManager _userManager;
private readonly ILogger _logger;
private readonly ITaskManager _taskManager;
private readonly INotificationManager _notificationManager;
public Notifications(IInstallationManager installationManager, INotificationsRepository notificationsRepo, IUserManager userManager, ILogger logger, ITaskManager taskManager)
public Notifications(IInstallationManager installationManager, IUserManager userManager, ILogger logger, ITaskManager taskManager, INotificationManager notificationManager)
{
_installationManager = installationManager;
_notificationsRepo = notificationsRepo;
_userManager = userManager;
_logger = logger;
_taskManager = taskManager;
_notificationManager = notificationManager;
}
public void Run()
@@ -49,21 +49,25 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
async void _userManager_UserCreated(object sender, GenericEventArgs<User> e)
{
var notification = new Notification
var userIds = _userManager
.Users
.Select(i => i.Id.ToString("N"))
.ToList();
var notification = new NotificationRequest
{
UserId = e.Argument.Id,
Category = "UserCreated",
UserIds = userIds,
Name = "Welcome to Media Browser!",
Description = "Check back here for more notifications."
};
try
{
await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error adding notification", ex);
_logger.ErrorException("Error sending notification", ex);
}
}
@@ -73,29 +77,27 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
if (result.Status == TaskCompletionStatus.Failed)
{
foreach (var user in _userManager
.Users
.Where(i => i.Configuration.IsAdministrator)
.ToList())
{
var notification = new Notification
{
UserId = user.Id,
Category = "ScheduledTaskFailed",
Name = result.Name + " failed",
RelatedId = result.Name,
Description = result.ErrorMessage,
Level = NotificationLevel.Error
};
var userIds = _userManager
.Users
.Where(i => i.Configuration.IsAdministrator)
.Select(i => i.Id.ToString("N"))
.ToList();
try
{
await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error adding notification", ex);
}
var notification = new NotificationRequest
{
UserIds = userIds,
Name = result.Name + " failed",
Description = result.ErrorMessage,
Level = NotificationLevel.Error
};
try
{
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error sending notification", ex);
}
}
}
@@ -104,27 +106,25 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
{
var plugin = e.Argument;
foreach (var user in _userManager
.Users
.Where(i => i.Configuration.IsAdministrator)
.ToList())
{
var notification = new Notification
{
UserId = user.Id,
Category = "PluginUninstalled",
Name = plugin.Name + " has been uninstalled",
RelatedId = plugin.Id.ToString()
};
var userIds = _userManager
.Users
.Where(i => i.Configuration.IsAdministrator)
.Select(i => i.Id.ToString("N"))
.ToList();
try
{
await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error adding notification", ex);
}
var notification = new NotificationRequest
{
UserIds = userIds,
Name = plugin.Name + " has been uninstalled"
};
try
{
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error sending notification", ex);
}
}
@@ -132,28 +132,26 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
{
var installationInfo = e.InstallationInfo;
foreach (var user in _userManager
.Users
.Where(i => i.Configuration.IsAdministrator)
.ToList())
{
var notification = new Notification
{
UserId = user.Id,
Category = "PackageInstallationCompleted",
Name = installationInfo.Name + " " + installationInfo.Version + " was installed",
RelatedId = installationInfo.Name,
Description = e.PackageVersionInfo.description
};
var userIds = _userManager
.Users
.Where(i => i.Configuration.IsAdministrator)
.Select(i => i.Id.ToString("N"))
.ToList();
try
{
await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error adding notification", ex);
}
var notification = new NotificationRequest
{
UserIds = userIds,
Name = installationInfo.Name + " " + installationInfo.Version + " was installed",
Description = e.PackageVersionInfo.description
};
try
{
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error sending notification", ex);
}
}
@@ -161,29 +159,27 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
{
var installationInfo = e.InstallationInfo;
foreach (var user in _userManager
var userIds = _userManager
.Users
.Where(i => i.Configuration.IsAdministrator)
.ToList())
{
var notification = new Notification
{
UserId = user.Id,
Category = "PackageInstallationFailed",
Level = NotificationLevel.Error,
Name = installationInfo.Name + " " + installationInfo.Version + " installation failed",
RelatedId = installationInfo.Name,
Description = e.Exception.Message
};
.Select(i => i.Id.ToString("N"))
.ToList();
try
{
await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error adding notification", ex);
}
var notification = new NotificationRequest
{
UserIds = userIds,
Level = NotificationLevel.Error,
Name = installationInfo.Name + " " + installationInfo.Version + " installation failed",
Description = e.Exception.Message
};
try
{
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error sending notification", ex);
}
}

View File

@@ -1,7 +1,6 @@
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Controller.Plugins;
@@ -26,22 +25,23 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
private readonly IApplicationPaths _appPaths;
private readonly ILogger _logger;
private readonly IJsonSerializer _json;
private readonly INotificationsRepository _notificationsRepo;
private readonly IUserManager _userManager;
private readonly IFileSystem _fileSystem;
private readonly TimeSpan _frequency = TimeSpan.FromHours(6);
private readonly TimeSpan _maxAge = TimeSpan.FromDays(31);
public RemoteNotifications(IApplicationPaths appPaths, ILogger logger, IHttpClient httpClient, IJsonSerializer json, INotificationsRepository notificationsRepo, IUserManager userManager, IFileSystem fileSystem)
private readonly INotificationManager _notificationManager;
public RemoteNotifications(IApplicationPaths appPaths, ILogger logger, IHttpClient httpClient, IJsonSerializer json, IUserManager userManager, IFileSystem fileSystem, INotificationManager notificationManager)
{
_appPaths = appPaths;
_logger = logger;
_httpClient = httpClient;
_json = json;
_notificationsRepo = notificationsRepo;
_userManager = userManager;
_fileSystem = fileSystem;
_notificationManager = notificationManager;
}
/// <summary>
@@ -107,21 +107,19 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
.Where(i => string.Equals(i.active, "1") && i.date.ToUniversalTime() > lastRunTime && (DateTime.UtcNow - i.date.ToUniversalTime()) <= _maxAge)
.ToList();
foreach (var user in _userManager.Users.ToList())
{
foreach (var notification in notificationList)
{
await _notificationsRepo.AddNotification(new Notification
{
Category = notification.category,
Date = notification.date,
Name = notification.name,
Description = notification.description,
Url = notification.url,
UserId = user.Id
var userIds = _userManager.Users.Select(i => i.Id.ToString("N")).ToList();
}, CancellationToken.None).ConfigureAwait(false);
}
foreach (var notification in notificationList)
{
await _notificationManager.SendNotification(new NotificationRequest
{
Date = notification.date,
Name = notification.name,
Description = notification.description,
Url = notification.url,
UserIds = userIds
}, CancellationToken.None).ConfigureAwait(false);
}
}

View File

@@ -30,9 +30,9 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
void _notificationsRepo_NotificationsMarkedRead(object sender, NotificationReadEventArgs e)
{
var list = e.IdList.Select(i => i.ToString("N")).ToList();
var list = e.IdList.ToList();
list.Add(e.UserId.ToString("N"));
list.Add(e.UserId);
list.Add(e.IsRead.ToString().ToLower());
var msg = string.Join("|", list.ToArray());