2013-10-03 01:22:50 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-05-17 15:29:22 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-05-24 19:52:41 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-05-22 03:42:25 +00:00
|
|
|
|
using MoreLinq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-05-17 15:29:22 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
2013-07-04 00:11:11 +00:00
|
|
|
|
namespace MediaBrowser.Server.Implementations.EntryPoints
|
2013-05-17 15:29:22 +00:00
|
|
|
|
{
|
|
|
|
|
public class LibraryChangedNotifier : IServerEntryPoint
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
|
|
|
|
private readonly ISessionManager _sessionManager;
|
2013-05-22 03:42:25 +00:00
|
|
|
|
private readonly IUserManager _userManager;
|
2013-05-24 19:52:41 +00:00
|
|
|
|
private readonly ILogger _logger;
|
2013-05-22 03:42:25 +00:00
|
|
|
|
|
2013-05-17 15:29:22 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library changed sync lock
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly object _libraryChangedSyncLock = new object();
|
|
|
|
|
|
2013-05-22 03:42:25 +00:00
|
|
|
|
private readonly List<Folder> _foldersAddedTo = new List<Folder>();
|
|
|
|
|
private readonly List<Folder> _foldersRemovedFrom = new List<Folder>();
|
|
|
|
|
|
|
|
|
|
private readonly List<BaseItem> _itemsAdded = new List<BaseItem>();
|
|
|
|
|
private readonly List<BaseItem> _itemsRemoved = new List<BaseItem>();
|
|
|
|
|
private readonly List<BaseItem> _itemsUpdated = new List<BaseItem>();
|
2013-05-17 15:29:22 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the library update timer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The library update timer.</value>
|
|
|
|
|
private Timer LibraryUpdateTimer { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The library update duration
|
|
|
|
|
/// </summary>
|
2013-07-29 16:03:19 +00:00
|
|
|
|
private const int LibraryUpdateDuration = 20000;
|
2013-05-17 15:29:22 +00:00
|
|
|
|
|
2013-10-03 01:22:50 +00:00
|
|
|
|
public LibraryChangedNotifier(ILibraryManager libraryManager, ISessionManager sessionManager, IUserManager userManager, ILogger logger)
|
2013-05-17 15:29:22 +00:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_sessionManager = sessionManager;
|
2013-05-22 03:42:25 +00:00
|
|
|
|
_userManager = userManager;
|
2013-07-04 00:11:11 +00:00
|
|
|
|
_logger = logger;
|
2013-05-17 15:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
|
|
|
|
_libraryManager.ItemAdded += libraryManager_ItemAdded;
|
|
|
|
|
_libraryManager.ItemUpdated += libraryManager_ItemUpdated;
|
|
|
|
|
_libraryManager.ItemRemoved += libraryManager_ItemRemoved;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the ItemAdded event of the libraryManager control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="ItemChangeEventArgs"/> instance containing the event data.</param>
|
|
|
|
|
void libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
lock (_libraryChangedSyncLock)
|
|
|
|
|
{
|
|
|
|
|
if (LibraryUpdateTimer == null)
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration,
|
|
|
|
|
Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.Item.Parent != null)
|
|
|
|
|
{
|
2013-05-22 03:42:25 +00:00
|
|
|
|
_foldersAddedTo.Add(e.Item.Parent);
|
2013-05-17 15:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 03:42:25 +00:00
|
|
|
|
_itemsAdded.Add(e.Item);
|
2013-05-17 15:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the ItemUpdated event of the libraryManager control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="ItemChangeEventArgs"/> instance containing the event data.</param>
|
|
|
|
|
void libraryManager_ItemUpdated(object sender, ItemChangeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
lock (_libraryChangedSyncLock)
|
|
|
|
|
{
|
|
|
|
|
if (LibraryUpdateTimer == null)
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration,
|
|
|
|
|
Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 03:42:25 +00:00
|
|
|
|
_itemsUpdated.Add(e.Item);
|
2013-05-17 15:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the ItemRemoved event of the libraryManager control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="ItemChangeEventArgs"/> instance containing the event data.</param>
|
|
|
|
|
void libraryManager_ItemRemoved(object sender, ItemChangeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
lock (_libraryChangedSyncLock)
|
|
|
|
|
{
|
|
|
|
|
if (LibraryUpdateTimer == null)
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration,
|
|
|
|
|
Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.Item.Parent != null)
|
|
|
|
|
{
|
2013-05-22 03:42:25 +00:00
|
|
|
|
_foldersRemovedFrom.Add(e.Item.Parent);
|
2013-05-17 15:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 03:42:25 +00:00
|
|
|
|
_itemsRemoved.Add(e.Item);
|
2013-05-17 15:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Libraries the update timer callback.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
private void LibraryUpdateTimerCallback(object state)
|
|
|
|
|
{
|
|
|
|
|
lock (_libraryChangedSyncLock)
|
|
|
|
|
{
|
|
|
|
|
// Remove dupes in case some were saved multiple times
|
2013-05-22 03:42:25 +00:00
|
|
|
|
var foldersAddedTo = _foldersAddedTo.DistinctBy(i => i.Id).ToList();
|
2013-05-17 15:29:22 +00:00
|
|
|
|
|
2013-05-22 03:42:25 +00:00
|
|
|
|
var foldersRemovedFrom = _foldersRemovedFrom.DistinctBy(i => i.Id).ToList();
|
2013-05-17 15:29:22 +00:00
|
|
|
|
|
2013-05-22 03:42:25 +00:00
|
|
|
|
var itemsUpdated = _itemsUpdated
|
|
|
|
|
.Where(i => !_itemsAdded.Contains(i))
|
|
|
|
|
.DistinctBy(i => i.Id)
|
2013-05-17 15:29:22 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2013-05-22 03:42:25 +00:00
|
|
|
|
SendChangeNotifications(_itemsAdded.ToList(), itemsUpdated, _itemsRemoved.ToList(), foldersAddedTo, foldersRemovedFrom, CancellationToken.None);
|
2013-05-17 15:29:22 +00:00
|
|
|
|
|
|
|
|
|
if (LibraryUpdateTimer != null)
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer.Dispose();
|
|
|
|
|
LibraryUpdateTimer = null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 03:42:25 +00:00
|
|
|
|
_itemsAdded.Clear();
|
|
|
|
|
_itemsRemoved.Clear();
|
|
|
|
|
_itemsUpdated.Clear();
|
|
|
|
|
_foldersAddedTo.Clear();
|
|
|
|
|
_foldersRemovedFrom.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the change notifications.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="itemsAdded">The items added.</param>
|
|
|
|
|
/// <param name="itemsUpdated">The items updated.</param>
|
|
|
|
|
/// <param name="itemsRemoved">The items removed.</param>
|
|
|
|
|
/// <param name="foldersAddedTo">The folders added to.</param>
|
|
|
|
|
/// <param name="foldersRemovedFrom">The folders removed from.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
2013-10-03 01:22:50 +00:00
|
|
|
|
private async void SendChangeNotifications(List<BaseItem> itemsAdded, List<BaseItem> itemsUpdated, List<BaseItem> itemsRemoved, List<Folder> foldersAddedTo, List<Folder> foldersRemovedFrom, CancellationToken cancellationToken)
|
2013-05-22 03:42:25 +00:00
|
|
|
|
{
|
2013-10-03 01:22:50 +00:00
|
|
|
|
foreach (var user in _userManager.Users.ToList())
|
2013-05-22 03:42:25 +00:00
|
|
|
|
{
|
2013-10-03 01:22:50 +00:00
|
|
|
|
var id = user.Id;
|
|
|
|
|
var userSessions = _sessionManager.Sessions
|
2014-01-04 02:35:41 +00:00
|
|
|
|
.Where(u => u.UserId.HasValue && u.UserId.Value == id && u.SessionController != null && u.IsActive)
|
2013-05-30 22:22:15 +00:00
|
|
|
|
.ToList();
|
2013-05-22 03:42:25 +00:00
|
|
|
|
|
2013-10-03 01:22:50 +00:00
|
|
|
|
if (userSessions.Count > 0)
|
2013-05-24 19:52:41 +00:00
|
|
|
|
{
|
2013-10-03 01:22:50 +00:00
|
|
|
|
var info = GetLibraryUpdateInfo(itemsAdded, itemsUpdated, itemsRemoved, foldersAddedTo,
|
|
|
|
|
foldersRemovedFrom, id);
|
|
|
|
|
|
|
|
|
|
foreach (var userSession in userSessions)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await userSession.SessionController.SendLibraryUpdateInfo(info, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error sending LibraryChanged message", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-24 19:52:41 +00:00
|
|
|
|
}
|
2013-10-03 01:22:50 +00:00
|
|
|
|
|
2013-05-22 03:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the library update info.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="itemsAdded">The items added.</param>
|
|
|
|
|
/// <param name="itemsUpdated">The items updated.</param>
|
|
|
|
|
/// <param name="itemsRemoved">The items removed.</param>
|
|
|
|
|
/// <param name="foldersAddedTo">The folders added to.</param>
|
|
|
|
|
/// <param name="foldersRemovedFrom">The folders removed from.</param>
|
|
|
|
|
/// <param name="userId">The user id.</param>
|
|
|
|
|
/// <returns>LibraryUpdateInfo.</returns>
|
|
|
|
|
private LibraryUpdateInfo GetLibraryUpdateInfo(IEnumerable<BaseItem> itemsAdded, IEnumerable<BaseItem> itemsUpdated, IEnumerable<BaseItem> itemsRemoved, IEnumerable<Folder> foldersAddedTo, IEnumerable<Folder> foldersRemovedFrom, Guid userId)
|
|
|
|
|
{
|
|
|
|
|
var user = _userManager.GetUserById(userId);
|
|
|
|
|
|
2013-07-05 13:47:10 +00:00
|
|
|
|
var collections = user.RootFolder.GetChildren(user, true).ToList();
|
2013-05-22 03:42:25 +00:00
|
|
|
|
|
2013-10-04 19:48:31 +00:00
|
|
|
|
var allRecursiveChildren = user.RootFolder.GetRecursiveChildren(user)
|
|
|
|
|
.Select(i => i.Id)
|
2013-10-05 17:13:32 +00:00
|
|
|
|
.Distinct()
|
2013-10-04 19:48:31 +00:00
|
|
|
|
.ToDictionary(i => i);
|
2013-05-22 03:42:25 +00:00
|
|
|
|
|
|
|
|
|
return new LibraryUpdateInfo
|
|
|
|
|
{
|
|
|
|
|
ItemsAdded = itemsAdded.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, collections, allRecursiveChildren)).Select(i => i.Id).Distinct().ToList(),
|
|
|
|
|
|
|
|
|
|
ItemsUpdated = itemsUpdated.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, collections, allRecursiveChildren)).Select(i => i.Id).Distinct().ToList(),
|
|
|
|
|
|
2013-07-29 16:03:19 +00:00
|
|
|
|
ItemsRemoved = itemsRemoved.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, collections, allRecursiveChildren, true)).Select(i => i.Id).Distinct().ToList(),
|
2013-05-22 03:42:25 +00:00
|
|
|
|
|
|
|
|
|
FoldersAddedTo = foldersAddedTo.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, collections, allRecursiveChildren)).Select(i => i.Id).Distinct().ToList(),
|
|
|
|
|
|
|
|
|
|
FoldersRemovedFrom = foldersRemovedFrom.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, collections, allRecursiveChildren)).Select(i => i.Id).Distinct().ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Translates the physical item to user library.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
|
/// <param name="collections">The collections.</param>
|
|
|
|
|
/// <param name="allRecursiveChildren">All recursive children.</param>
|
2013-07-29 16:03:19 +00:00
|
|
|
|
/// <param name="includeIfNotFound">if set to <c>true</c> [include if not found].</param>
|
2013-05-22 03:42:25 +00:00
|
|
|
|
/// <returns>IEnumerable{``0}.</returns>
|
2013-10-04 19:48:31 +00:00
|
|
|
|
private IEnumerable<T> TranslatePhysicalItemToUserLibrary<T>(T item, User user, IEnumerable<BaseItem> collections, Dictionary<Guid, Guid> allRecursiveChildren, bool includeIfNotFound = false)
|
2013-05-22 03:42:25 +00:00
|
|
|
|
where T : BaseItem
|
|
|
|
|
{
|
|
|
|
|
// If the physical root changed, return the user root
|
|
|
|
|
if (item is AggregateFolder)
|
|
|
|
|
{
|
2013-07-29 16:03:19 +00:00
|
|
|
|
return new[] { user.RootFolder as T };
|
2013-05-22 03:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Need to find what user collection folder this belongs to
|
|
|
|
|
if (item.Parent is AggregateFolder)
|
|
|
|
|
{
|
2013-05-25 03:22:45 +00:00
|
|
|
|
if (item.LocationType == LocationType.FileSystem)
|
|
|
|
|
{
|
2014-02-10 20:11:46 +00:00
|
|
|
|
return collections.Where(i => i.PhysicalLocations.Contains(item.Path)).Cast<T>();
|
2013-05-25 03:22:45 +00:00
|
|
|
|
}
|
2013-05-22 03:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If it's a user root, return it only if it's the right one
|
|
|
|
|
if (item is UserRootFolder)
|
|
|
|
|
{
|
|
|
|
|
if (item.Id == user.RootFolder.Id)
|
|
|
|
|
{
|
2014-02-10 20:11:46 +00:00
|
|
|
|
return new[] { item };
|
2013-05-22 03:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new T[] { };
|
2013-05-17 15:29:22 +00:00
|
|
|
|
}
|
2013-05-22 03:42:25 +00:00
|
|
|
|
|
|
|
|
|
// Return it only if it's in the user's library
|
2013-07-29 16:03:19 +00:00
|
|
|
|
if (includeIfNotFound || allRecursiveChildren.ContainsKey(item.Id))
|
2013-05-22 03:42:25 +00:00
|
|
|
|
{
|
2013-07-29 16:03:19 +00:00
|
|
|
|
return new[] { item };
|
2013-05-22 03:42:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new T[] { };
|
2013-05-17 15:29:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
|
{
|
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
if (LibraryUpdateTimer != null)
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer.Dispose();
|
|
|
|
|
LibraryUpdateTimer = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_libraryManager.ItemAdded -= libraryManager_ItemAdded;
|
|
|
|
|
_libraryManager.ItemUpdated -= libraryManager_ItemUpdated;
|
|
|
|
|
_libraryManager.ItemRemoved -= libraryManager_ItemRemoved;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|