2019-11-01 10:38:54 -07:00
|
|
|
#pragma warning disable CS1591
|
2019-12-10 16:13:57 -07:00
|
|
|
#pragma warning disable SA1600
|
2019-11-01 10:38:54 -07:00
|
|
|
|
2013-05-21 20:42:25 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2017-06-23 09:04:45 -07:00
|
|
|
using System.Globalization;
|
2013-05-17 08:29:22 -07:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
2019-01-27 07:40:37 -07:00
|
|
|
using System.Threading.Tasks;
|
2017-06-23 09:04:45 -07:00
|
|
|
using MediaBrowser.Controller.Channels;
|
2019-01-13 12:20:41 -07:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2015-11-07 18:13:38 -07:00
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2019-01-13 12:20:41 -07:00
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
2017-06-23 09:04:45 -07:00
|
|
|
using MediaBrowser.Controller.Providers;
|
2019-01-13 12:20:41 -07:00
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2017-06-23 09:04:45 -07:00
|
|
|
using MediaBrowser.Model.Events;
|
2019-01-13 12:20:41 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-05-17 08:29:22 -07:00
|
|
|
|
2016-11-03 15:06:00 -07:00
|
|
|
namespace Emby.Server.Implementations.EntryPoints
|
2013-05-17 08:29:22 -07:00
|
|
|
{
|
|
|
|
public class LibraryChangedNotifier : IServerEntryPoint
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-11-21 05:51:17 -07:00
|
|
|
/// The library manager.
|
2013-05-17 08:29:22 -07:00
|
|
|
/// </summary>
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
|
|
private readonly ISessionManager _sessionManager;
|
2013-05-21 20:42:25 -07:00
|
|
|
private readonly IUserManager _userManager;
|
2013-05-24 12:52:41 -07:00
|
|
|
private readonly ILogger _logger;
|
2013-05-21 20:42:25 -07:00
|
|
|
|
2013-05-17 08:29:22 -07:00
|
|
|
/// <summary>
|
2019-11-21 05:51:17 -07:00
|
|
|
/// The library changed sync lock.
|
2013-05-17 08:29:22 -07:00
|
|
|
/// </summary>
|
|
|
|
private readonly object _libraryChangedSyncLock = new object();
|
|
|
|
|
2013-05-21 20:42:25 -07: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 08:29:22 -07:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the library update timer.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The library update timer.</value>
|
2019-02-05 01:49:46 -07:00
|
|
|
private Timer LibraryUpdateTimer { get; set; }
|
2013-05-17 08:29:22 -07:00
|
|
|
|
|
|
|
/// <summary>
|
2019-11-21 05:51:17 -07:00
|
|
|
/// The library update duration.
|
2013-05-17 08:29:22 -07:00
|
|
|
/// </summary>
|
2018-09-12 10:26:21 -07:00
|
|
|
private const int LibraryUpdateDuration = 30000;
|
2013-05-17 08:29:22 -07:00
|
|
|
|
2017-06-23 09:04:45 -07:00
|
|
|
private readonly IProviderManager _providerManager;
|
|
|
|
|
2019-02-05 01:49:46 -07:00
|
|
|
public LibraryChangedNotifier(ILibraryManager libraryManager, ISessionManager sessionManager, IUserManager userManager, ILogger logger, IProviderManager providerManager)
|
2013-05-17 08:29:22 -07:00
|
|
|
{
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
_sessionManager = sessionManager;
|
2013-05-21 20:42:25 -07:00
|
|
|
_userManager = userManager;
|
2013-07-03 17:11:11 -07:00
|
|
|
_logger = logger;
|
2017-06-23 09:04:45 -07:00
|
|
|
_providerManager = providerManager;
|
2013-05-17 08:29:22 -07:00
|
|
|
}
|
|
|
|
|
2019-01-27 07:40:37 -07:00
|
|
|
public Task RunAsync()
|
2013-05-17 08:29:22 -07:00
|
|
|
{
|
|
|
|
_libraryManager.ItemAdded += libraryManager_ItemAdded;
|
|
|
|
_libraryManager.ItemUpdated += libraryManager_ItemUpdated;
|
|
|
|
_libraryManager.ItemRemoved += libraryManager_ItemRemoved;
|
|
|
|
|
2017-06-23 09:04:45 -07:00
|
|
|
_providerManager.RefreshCompleted += _providerManager_RefreshCompleted;
|
|
|
|
_providerManager.RefreshStarted += _providerManager_RefreshStarted;
|
|
|
|
_providerManager.RefreshProgress += _providerManager_RefreshProgress;
|
2019-01-27 07:40:37 -07:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2017-06-23 09:04:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private Dictionary<Guid, DateTime> _lastProgressMessageTimes = new Dictionary<Guid, DateTime>();
|
|
|
|
|
|
|
|
private void _providerManager_RefreshProgress(object sender, GenericEventArgs<Tuple<BaseItem, double>> e)
|
|
|
|
{
|
|
|
|
var item = e.Argument.Item1;
|
|
|
|
|
|
|
|
if (!EnableRefreshMessage(item))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var progress = e.Argument.Item2;
|
|
|
|
|
2019-01-13 13:46:33 -07:00
|
|
|
if (_lastProgressMessageTimes.TryGetValue(item.Id, out var lastMessageSendTime))
|
2017-06-23 09:04:45 -07:00
|
|
|
{
|
|
|
|
if (progress > 0 && progress < 100 && (DateTime.UtcNow - lastMessageSendTime).TotalMilliseconds < 1000)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_lastProgressMessageTimes[item.Id] = DateTime.UtcNow;
|
|
|
|
|
|
|
|
var dict = new Dictionary<string, string>();
|
2019-02-28 15:22:57 -07:00
|
|
|
dict["ItemId"] = item.Id.ToString("N", CultureInfo.InvariantCulture);
|
2017-06-23 09:04:45 -07:00
|
|
|
dict["Progress"] = progress.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_sessionManager.SendMessageToAdminSessions("RefreshProgress", dict, CancellationToken.None);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
var collectionFolders = _libraryManager.GetCollectionFolders(item).ToList();
|
|
|
|
|
|
|
|
foreach (var collectionFolder in collectionFolders)
|
|
|
|
{
|
|
|
|
var collectionFolderDict = new Dictionary<string, string>();
|
2019-02-28 15:22:57 -07:00
|
|
|
collectionFolderDict["ItemId"] = collectionFolder.Id.ToString("N", CultureInfo.InvariantCulture);
|
2017-06-23 09:04:45 -07:00
|
|
|
collectionFolderDict["Progress"] = (collectionFolder.GetRefreshProgress() ?? 0).ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_sessionManager.SendMessageToAdminSessions("RefreshProgress", collectionFolderDict, CancellationToken.None);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void _providerManager_RefreshStarted(object sender, GenericEventArgs<BaseItem> e)
|
|
|
|
{
|
|
|
|
_providerManager_RefreshProgress(sender, new GenericEventArgs<Tuple<BaseItem, double>>(new Tuple<BaseItem, double>(e.Argument, 0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void _providerManager_RefreshCompleted(object sender, GenericEventArgs<BaseItem> e)
|
|
|
|
{
|
|
|
|
_providerManager_RefreshProgress(sender, new GenericEventArgs<Tuple<BaseItem, double>>(new Tuple<BaseItem, double>(e.Argument, 100)));
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static bool EnableRefreshMessage(BaseItem item)
|
2017-06-23 09:04:45 -07:00
|
|
|
{
|
|
|
|
var folder = item as Folder;
|
|
|
|
|
|
|
|
if (folder == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (folder.IsRoot)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (folder is AggregateFolder || folder is UserRootFolder)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (folder is UserView || folder is Channel)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!folder.IsTopParent)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-05-17 08:29:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
{
|
2015-04-11 10:59:55 -07:00
|
|
|
if (!FilterItem(e.Item))
|
2014-02-17 14:35:08 -07:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-17 08:29:22 -07:00
|
|
|
lock (_libraryChangedSyncLock)
|
|
|
|
{
|
|
|
|
if (LibraryUpdateTimer == null)
|
|
|
|
{
|
2019-11-21 05:51:17 -07:00
|
|
|
LibraryUpdateTimer = new Timer(
|
|
|
|
LibraryUpdateTimerCallback,
|
|
|
|
null,
|
|
|
|
LibraryUpdateDuration,
|
|
|
|
Timeout.Infinite);
|
2013-05-17 08:29:22 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite);
|
|
|
|
}
|
|
|
|
|
2017-09-18 09:52:22 -07:00
|
|
|
var parent = e.Item.GetParent() as Folder;
|
|
|
|
if (parent != null)
|
2013-05-17 08:29:22 -07:00
|
|
|
{
|
2017-09-18 09:52:22 -07:00
|
|
|
_foldersAddedTo.Add(parent);
|
2013-05-17 08:29:22 -07:00
|
|
|
}
|
|
|
|
|
2013-05-21 20:42:25 -07:00
|
|
|
_itemsAdded.Add(e.Item);
|
2013-05-17 08:29:22 -07: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)
|
|
|
|
{
|
2015-04-11 10:59:55 -07:00
|
|
|
if (!FilterItem(e.Item))
|
2014-02-17 14:35:08 -07:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-17 08:29:22 -07:00
|
|
|
lock (_libraryChangedSyncLock)
|
|
|
|
{
|
|
|
|
if (LibraryUpdateTimer == null)
|
|
|
|
{
|
2019-02-05 01:49:46 -07:00
|
|
|
LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration,
|
2013-05-17 08:29:22 -07:00
|
|
|
Timeout.Infinite);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite);
|
|
|
|
}
|
|
|
|
|
2013-05-21 20:42:25 -07:00
|
|
|
_itemsUpdated.Add(e.Item);
|
2013-05-17 08:29:22 -07: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)
|
|
|
|
{
|
2015-04-11 10:59:55 -07:00
|
|
|
if (!FilterItem(e.Item))
|
2014-02-17 14:35:08 -07:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-17 08:29:22 -07:00
|
|
|
lock (_libraryChangedSyncLock)
|
|
|
|
{
|
|
|
|
if (LibraryUpdateTimer == null)
|
|
|
|
{
|
2019-02-05 01:49:46 -07:00
|
|
|
LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration,
|
2013-05-17 08:29:22 -07:00
|
|
|
Timeout.Infinite);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite);
|
|
|
|
}
|
|
|
|
|
2017-11-25 21:48:12 -07:00
|
|
|
var parent = e.Parent as Folder;
|
2017-09-18 09:52:22 -07:00
|
|
|
if (parent != null)
|
2013-05-17 08:29:22 -07:00
|
|
|
{
|
2017-09-18 09:52:22 -07:00
|
|
|
_foldersRemovedFrom.Add(parent);
|
2013-05-17 08:29:22 -07:00
|
|
|
}
|
|
|
|
|
2013-05-21 20:42:25 -07:00
|
|
|
_itemsRemoved.Add(e.Item);
|
2013-05-17 08:29:22 -07: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
|
2019-02-02 04:27:06 -07:00
|
|
|
var foldersAddedTo = _foldersAddedTo
|
|
|
|
.GroupBy(x => x.Id)
|
|
|
|
.Select(x => x.First())
|
|
|
|
.ToList();
|
2013-05-17 08:29:22 -07:00
|
|
|
|
2019-02-02 04:27:06 -07:00
|
|
|
var foldersRemovedFrom = _foldersRemovedFrom
|
|
|
|
.GroupBy(x => x.Id)
|
|
|
|
.Select(x => x.First())
|
|
|
|
.ToList();
|
2013-05-17 08:29:22 -07:00
|
|
|
|
2013-05-21 20:42:25 -07:00
|
|
|
var itemsUpdated = _itemsUpdated
|
2019-02-02 04:27:06 -07:00
|
|
|
.Where(i => !_itemsAdded.Contains(i))
|
|
|
|
.GroupBy(x => x.Id)
|
|
|
|
.Select(x => x.First())
|
|
|
|
.ToList();
|
2013-05-17 08:29:22 -07:00
|
|
|
|
2013-05-21 20:42:25 -07:00
|
|
|
SendChangeNotifications(_itemsAdded.ToList(), itemsUpdated, _itemsRemoved.ToList(), foldersAddedTo, foldersRemovedFrom, CancellationToken.None);
|
2013-05-17 08:29:22 -07:00
|
|
|
|
|
|
|
if (LibraryUpdateTimer != null)
|
|
|
|
{
|
|
|
|
LibraryUpdateTimer.Dispose();
|
|
|
|
LibraryUpdateTimer = null;
|
|
|
|
}
|
|
|
|
|
2013-05-21 20:42:25 -07: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-02 18:22:50 -07:00
|
|
|
private async void SendChangeNotifications(List<BaseItem> itemsAdded, List<BaseItem> itemsUpdated, List<BaseItem> itemsRemoved, List<Folder> foldersAddedTo, List<Folder> foldersRemovedFrom, CancellationToken cancellationToken)
|
2013-05-21 20:42:25 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
var userIds = _sessionManager.Sessions
|
|
|
|
.Select(i => i.UserId)
|
|
|
|
.Where(i => !i.Equals(Guid.Empty))
|
|
|
|
.Distinct()
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
foreach (var userId in userIds)
|
2013-05-21 20:42:25 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
LibraryUpdateInfo info;
|
2013-05-21 20:42:25 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
try
|
2013-05-24 12:52:41 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
info = GetLibraryUpdateInfo(itemsAdded, itemsUpdated, itemsRemoved, foldersAddedTo, foldersRemovedFrom, userId);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
_logger.LogError(ex, "Error in GetLibraryUpdateInfo");
|
2018-09-12 10:26:21 -07:00
|
|
|
return;
|
2013-05-24 12:52:41 -07:00
|
|
|
}
|
2013-10-02 18:22:50 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (info.IsEmpty)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await _sessionManager.SendMessageToUserSessions(new List<Guid> { userId }, "LibraryChanged", info, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
_logger.LogError(ex, "Error sending LibraryChanged message");
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
2013-05-21 20:42:25 -07: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>
|
2017-11-25 21:48:12 -07:00
|
|
|
private LibraryUpdateInfo GetLibraryUpdateInfo(List<BaseItem> itemsAdded, List<BaseItem> itemsUpdated, List<BaseItem> itemsRemoved, List<Folder> foldersAddedTo, List<Folder> foldersRemovedFrom, Guid userId)
|
2013-05-21 20:42:25 -07:00
|
|
|
{
|
|
|
|
var user = _userManager.GetUserById(userId);
|
|
|
|
|
2017-11-25 21:48:12 -07:00
|
|
|
var newAndRemoved = new List<BaseItem>();
|
|
|
|
newAndRemoved.AddRange(foldersAddedTo);
|
|
|
|
newAndRemoved.AddRange(foldersRemovedFrom);
|
|
|
|
|
|
|
|
var allUserRootChildren = _libraryManager.GetUserRootFolder().GetChildren(user, true).OfType<Folder>().ToList();
|
|
|
|
|
2013-05-21 20:42:25 -07:00
|
|
|
return new LibraryUpdateInfo
|
|
|
|
{
|
2019-02-28 15:22:57 -07:00
|
|
|
ItemsAdded = itemsAdded.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user)).Select(i => i.Id.ToString("N", CultureInfo.InvariantCulture)).Distinct().ToArray(),
|
2013-05-21 20:42:25 -07:00
|
|
|
|
2019-02-28 15:22:57 -07:00
|
|
|
ItemsUpdated = itemsUpdated.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user)).Select(i => i.Id.ToString("N", CultureInfo.InvariantCulture)).Distinct().ToArray(),
|
2013-05-21 20:42:25 -07:00
|
|
|
|
2019-02-28 15:22:57 -07:00
|
|
|
ItemsRemoved = itemsRemoved.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user, true)).Select(i => i.Id.ToString("N", CultureInfo.InvariantCulture)).Distinct().ToArray(),
|
2013-05-21 20:42:25 -07:00
|
|
|
|
2019-02-28 15:22:57 -07:00
|
|
|
FoldersAddedTo = foldersAddedTo.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user)).Select(i => i.Id.ToString("N", CultureInfo.InvariantCulture)).Distinct().ToArray(),
|
2013-05-21 20:42:25 -07:00
|
|
|
|
2019-02-28 15:22:57 -07:00
|
|
|
FoldersRemovedFrom = foldersRemovedFrom.SelectMany(i => TranslatePhysicalItemToUserLibrary(i, user)).Select(i => i.Id.ToString("N", CultureInfo.InvariantCulture)).Distinct().ToArray(),
|
2017-11-25 21:48:12 -07:00
|
|
|
|
2019-03-25 14:25:32 -07:00
|
|
|
CollectionFolders = GetTopParentIds(newAndRemoved, allUserRootChildren).ToArray()
|
2013-05-21 20:42:25 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static bool FilterItem(BaseItem item)
|
2015-04-11 10:59:55 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
if (!item.IsFolder && !item.HasPathProtocol)
|
2015-04-11 10:59:55 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-07 18:13:38 -07:00
|
|
|
|
|
|
|
if (item is IItemByName && !(item is MusicArtist))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-18 21:05:33 -07:00
|
|
|
return item.SourceType == SourceType.Library;
|
2015-04-11 10:59:55 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 14:25:32 -07:00
|
|
|
private IEnumerable<string> GetTopParentIds(List<BaseItem> items, List<Folder> allUserRootChildren)
|
2017-11-25 21:48:12 -07:00
|
|
|
{
|
|
|
|
var list = new List<string>();
|
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
{
|
|
|
|
// If the physical root changed, return the user root
|
|
|
|
if (item is AggregateFolder)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var collectionFolders = _libraryManager.GetCollectionFolders(item, allUserRootChildren);
|
|
|
|
foreach (var folder in allUserRootChildren)
|
|
|
|
{
|
2019-02-28 15:22:57 -07:00
|
|
|
list.Add(folder.Id.ToString("N", CultureInfo.InvariantCulture));
|
2017-11-25 21:48:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.Distinct(StringComparer.Ordinal);
|
|
|
|
}
|
|
|
|
|
2013-05-21 20:42:25 -07:00
|
|
|
/// <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>
|
2013-07-29 09:03:19 -07:00
|
|
|
/// <param name="includeIfNotFound">if set to <c>true</c> [include if not found].</param>
|
2013-05-21 20:42:25 -07:00
|
|
|
/// <returns>IEnumerable{``0}.</returns>
|
2015-04-10 07:49:03 -07:00
|
|
|
private IEnumerable<T> TranslatePhysicalItemToUserLibrary<T>(T item, User user, bool includeIfNotFound = false)
|
2013-05-21 20:42:25 -07:00
|
|
|
where T : BaseItem
|
|
|
|
{
|
|
|
|
// If the physical root changed, return the user root
|
|
|
|
if (item is AggregateFolder)
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
return new[] { _libraryManager.GetUserRootFolder() as T };
|
2013-05-21 20:42:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return it only if it's in the user's library
|
2015-04-05 20:47:01 -07:00
|
|
|
if (includeIfNotFound || item.IsVisibleStandalone(user))
|
2013-05-21 20:42:25 -07:00
|
|
|
{
|
2013-07-29 09:03:19 -07:00
|
|
|
return new[] { item };
|
2013-05-21 20:42:25 -07:00
|
|
|
}
|
|
|
|
|
2019-10-25 03:47:20 -07:00
|
|
|
return Array.Empty<T>();
|
2013-05-17 08:29:22 -07: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;
|
|
|
|
}
|
2019-01-07 16:27:46 -07:00
|
|
|
|
2013-05-17 08:29:22 -07:00
|
|
|
_libraryManager.ItemAdded -= libraryManager_ItemAdded;
|
|
|
|
_libraryManager.ItemUpdated -= libraryManager_ItemUpdated;
|
|
|
|
_libraryManager.ItemRemoved -= libraryManager_ItemRemoved;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
|
|
|
_providerManager.RefreshCompleted -= _providerManager_RefreshCompleted;
|
|
|
|
_providerManager.RefreshStarted -= _providerManager_RefreshStarted;
|
|
|
|
_providerManager.RefreshProgress -= _providerManager_RefreshProgress;
|
2013-05-17 08:29:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|