aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs')
-rw-r--r--MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs60
1 files changed, 54 insertions, 6 deletions
diff --git a/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs b/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs
index 43e968fa5..0081c6243 100644
--- a/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs
+++ b/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs
@@ -14,12 +14,12 @@ using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Notifications;
using MediaBrowser.Model.Tasks;
+using MediaBrowser.Model.Updates;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
-using MediaBrowser.Model.Updates;
namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
{
@@ -40,6 +40,9 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
private readonly ISessionManager _sessionManager;
private readonly IServerApplicationHost _appHost;
+ private Timer LibraryUpdateTimer { get; set; }
+ private readonly object _libraryChangedSyncLock = new object();
+
public Notifications(IInstallationManager installationManager, IUserManager userManager, ILogger logger, ITaskManager taskManager, INotificationManager notificationManager, IServerConfigurationManager config, ILibraryManager libraryManager, ISessionManager sessionManager, IServerApplicationHost appHost)
{
_installationManager = installationManager;
@@ -210,21 +213,55 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
return null;
}
- async void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
+ private readonly List<BaseItem> _itemsAdded = new List<BaseItem>();
+ void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
{
- if (e.Item.LocationType == LocationType.FileSystem)
+ if (e.Item.LocationType == LocationType.FileSystem && !e.Item.IsFolder)
{
- var type = NotificationType.NewLibraryContent.ToString();
+ 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();
+ }
- var item = e.Item;
+ var item = items.FirstOrDefault();
+ if (item != null)
+ {
var notification = new NotificationRequest
{
- NotificationType = type
+ NotificationType = NotificationType.NewLibraryContent.ToString()
};
notification.Variables["Name"] = item.Name;
+ if (items.Count > 1)
+ {
+ notification.Name = items.Count + " new library items.";
+ }
+
await SendNotification(notification).ConfigureAwait(false);
}
}
@@ -313,6 +350,8 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
public void Dispose()
{
+ DisposeLibraryUpdateTimer();
+
_installationManager.PluginInstalled -= _installationManager_PluginInstalled;
_installationManager.PluginUpdated -= _installationManager_PluginUpdated;
_installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
@@ -328,5 +367,14 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
_appHost.HasUpdateAvailableChanged -= _appHost_HasUpdateAvailableChanged;
_appHost.ApplicationUpdated -= _appHost_ApplicationUpdated;
}
+
+ private void DisposeLibraryUpdateTimer()
+ {
+ if (LibraryUpdateTimer != null)
+ {
+ LibraryUpdateTimer.Dispose();
+ LibraryUpdateTimer = null;
+ }
+ }
}
}