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