mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-06 05:47:14 -04:00
341 lines
14 KiB
C#
341 lines
14 KiB
C#
using MediaBrowser.Common.Net;
|
|
using MediaBrowser.Controller.Entities;
|
|
using MediaBrowser.Controller.Library;
|
|
using MediaBrowser.Controller.Plugins;
|
|
using MediaBrowser.Controller.Session;
|
|
using MediaBrowser.Model.Entities;
|
|
using MediaBrowser.Model.Logging;
|
|
using MoreLinq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
|
|
namespace MediaBrowser.Server.Implementations.EntryPoints
|
|
{
|
|
public class LibraryChangedNotifier : IServerEntryPoint
|
|
{
|
|
/// <summary>
|
|
/// The _library manager
|
|
/// </summary>
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
private readonly IServerManager _serverManager;
|
|
private readonly IUserManager _userManager;
|
|
private readonly ILogger _logger;
|
|
|
|
/// <summary>
|
|
/// The _library changed sync lock
|
|
/// </summary>
|
|
private readonly object _libraryChangedSyncLock = new object();
|
|
|
|
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>();
|
|
|
|
/// <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>
|
|
private const int LibraryUpdateDuration = 20000;
|
|
|
|
public LibraryChangedNotifier(ILibraryManager libraryManager, ISessionManager sessionManager, IServerManager serverManager, IUserManager userManager, ILogger logger)
|
|
{
|
|
_libraryManager = libraryManager;
|
|
_sessionManager = sessionManager;
|
|
_serverManager = serverManager;
|
|
_userManager = userManager;
|
|
_logger = logger;
|
|
}
|
|
|
|
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)
|
|
{
|
|
_foldersAddedTo.Add(e.Item.Parent);
|
|
}
|
|
|
|
_itemsAdded.Add(e.Item);
|
|
}
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
|
|
_itemsUpdated.Add(e.Item);
|
|
}
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
_foldersRemovedFrom.Add(e.Item.Parent);
|
|
}
|
|
|
|
_itemsRemoved.Add(e.Item);
|
|
}
|
|
}
|
|
|
|
/// <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
|
|
var foldersAddedTo = _foldersAddedTo.DistinctBy(i => i.Id).ToList();
|
|
|
|
var foldersRemovedFrom = _foldersRemovedFrom.DistinctBy(i => i.Id).ToList();
|
|
|
|
var itemsUpdated = _itemsUpdated
|
|
.Where(i => !_itemsAdded.Contains(i))
|
|
.DistinctBy(i => i.Id)
|
|
.ToList();
|
|
|
|
SendChangeNotifications(_itemsAdded.ToList(), itemsUpdated, _itemsRemoved.ToList(), foldersAddedTo, foldersRemovedFrom, CancellationToken.None);
|
|
|
|
if (LibraryUpdateTimer != null)
|
|
{
|
|
LibraryUpdateTimer.Dispose();
|
|
LibraryUpdateTimer = null;
|
|
}
|
|
|
|
_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>
|
|
private async void SendChangeNotifications(IEnumerable<BaseItem> itemsAdded, IEnumerable<BaseItem> itemsUpdated, IEnumerable<BaseItem> itemsRemoved, IEnumerable<Folder> foldersAddedTo, IEnumerable<Folder> foldersRemovedFrom, CancellationToken cancellationToken)
|
|
{
|
|
var currentSessions = _sessionManager.Sessions.ToList();
|
|
|
|
var users = currentSessions.Select(i => i.User)
|
|
.Where(i => i != null)
|
|
.Select(i => i.Id)
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
foreach (var userId in users)
|
|
{
|
|
var id = userId;
|
|
var webSockets = currentSessions.Where(u => u.User != null && u.User.Id == id)
|
|
.SelectMany(i => i.WebSockets)
|
|
.ToList();
|
|
|
|
try
|
|
{
|
|
await _serverManager.SendWebSocketMessageAsync("LibraryChanged", () => GetLibraryUpdateInfo(itemsAdded, itemsUpdated, itemsRemoved, foldersAddedTo, foldersRemovedFrom, id), webSockets, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.ErrorException("Error sending LibraryChanged message", ex);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <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);
|
|
|
|
var collections = user.RootFolder.GetChildren(user, true).ToList();
|
|
|
|
var allRecursiveChildren = user.RootFolder.GetRecursiveChildren(user).ToDictionary(i => i.Id);
|
|
|
|
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(),
|
|
|
|
ItemsRemoved = itemsRemoved.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, collections, allRecursiveChildren, true)).Select(i => i.Id).Distinct().ToList(),
|
|
|
|
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>
|
|
/// <param name="includeIfNotFound">if set to <c>true</c> [include if not found].</param>
|
|
/// <returns>IEnumerable{``0}.</returns>
|
|
private IEnumerable<T> TranslatePhysicalItemToUserLibrary<T>(T item, User user, IEnumerable<BaseItem> collections, Dictionary<Guid, BaseItem> allRecursiveChildren, bool includeIfNotFound = false)
|
|
where T : BaseItem
|
|
{
|
|
// If the physical root changed, return the user root
|
|
if (item is AggregateFolder)
|
|
{
|
|
return new[] { user.RootFolder as T };
|
|
}
|
|
|
|
// Need to find what user collection folder this belongs to
|
|
if (item.Parent is AggregateFolder)
|
|
{
|
|
if (item.LocationType == LocationType.FileSystem)
|
|
{
|
|
return collections.Where(i =>
|
|
{
|
|
|
|
try
|
|
{
|
|
return i.LocationType == LocationType.FileSystem &&
|
|
i.ResolveArgs.PhysicalLocations.Contains(item.Path);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.ErrorException("Error getting ResolveArgs for {0}", ex, i.Path);
|
|
return false;
|
|
}
|
|
|
|
}).Cast<T>();
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
{
|
|
return new T[] { item };
|
|
}
|
|
|
|
return new T[] { };
|
|
}
|
|
|
|
// Return it only if it's in the user's library
|
|
if (includeIfNotFound || allRecursiveChildren.ContainsKey(item.Id))
|
|
{
|
|
return new[] { item };
|
|
}
|
|
|
|
return new T[] { };
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
}
|
|
}
|
|
}
|