aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations
diff options
context:
space:
mode:
authorShadowghost <Ghost_of_Stone@web.de>2026-01-17 15:58:00 +0100
committerShadowghost <Ghost_of_Stone@web.de>2026-01-18 19:47:02 +0100
commit912a963a2bf7f9534e9395d1a2da8d910f249b5b (patch)
tree6b2889505322416228e175cb97cab8cb12aad092 /Jellyfin.Server.Implementations
parentf26058591729e2c381feca7e1e195dd8e8017a0b (diff)
Add folder-aware filter extensions and descendant query provider
- Add FolderAwareFilterExtensions for LinkedChildren-based filtering - Add IDescendantQueryProvider interface for database-specific queries - Add MatchCriteria classes for folder filtering - Add SqliteDescendantQueryProvider implementation
Diffstat (limited to 'Jellyfin.Server.Implementations')
-rw-r--r--Jellyfin.Server.Implementations/Item/FolderAwareFilterExtensions.cs65
1 files changed, 65 insertions, 0 deletions
diff --git a/Jellyfin.Server.Implementations/Item/FolderAwareFilterExtensions.cs b/Jellyfin.Server.Implementations/Item/FolderAwareFilterExtensions.cs
new file mode 100644
index 0000000000..c63d99d54d
--- /dev/null
+++ b/Jellyfin.Server.Implementations/Item/FolderAwareFilterExtensions.cs
@@ -0,0 +1,65 @@
+using System;
+using System.Linq;
+using System.Linq.Expressions;
+using Jellyfin.Database.Implementations;
+using Jellyfin.Database.Implementations.Entities;
+
+namespace Jellyfin.Server.Implementations.Item;
+
+/// <summary>
+/// Extension methods for applying folder-aware filters that check items and their descendants.
+/// </summary>
+internal static class FolderAwareFilterExtensions
+{
+ /// <summary>
+ /// Filters items where either the item matches the condition (for non-folders)
+ /// or any descendant matches (for folders). Uses reverse traversal through AncestorIds.
+ /// </summary>
+ /// <param name="query">The query to filter.</param>
+ /// <param name="context">The database context.</param>
+ /// <param name="condition">The condition to check on BaseItemEntity.</param>
+ /// <returns>Filtered query.</returns>
+ public static IQueryable<BaseItemEntity> WhereItemOrDescendantMatches(
+ this IQueryable<BaseItemEntity> query,
+ JellyfinDbContext context,
+ Expression<Func<BaseItemEntity, bool>> condition)
+ {
+ var matchingIds = context.BaseItems.Where(condition).Select(b => b.Id);
+ var foldersWithMatchingDescendants = context.AncestorIds
+ .Where(a => matchingIds.Contains(a.ItemId))
+ .Select(a => a.ParentItemId)
+ .Union(context.LinkedChildren
+ .Where(lc => matchingIds.Contains(lc.ChildId))
+ .Select(lc => lc.ParentId));
+
+ return query.Where(e =>
+ matchingIds.Contains(e.Id)
+ || foldersWithMatchingDescendants.Contains(e.Id));
+ }
+
+ /// <summary>
+ /// Filters items where neither the item matches the condition (for non-folders)
+ /// nor any descendant matches (for folders). Uses reverse traversal for infinite depth.
+ /// </summary>
+ /// <param name="query">The query to filter.</param>
+ /// <param name="context">The database context.</param>
+ /// <param name="condition">The condition that should NOT match.</param>
+ /// <returns>Filtered query.</returns>
+ public static IQueryable<BaseItemEntity> WhereNeitherItemNorDescendantMatches(
+ this IQueryable<BaseItemEntity> query,
+ JellyfinDbContext context,
+ Expression<Func<BaseItemEntity, bool>> condition)
+ {
+ var matchingIds = context.BaseItems.Where(condition).Select(b => b.Id);
+ var foldersWithMatchingDescendants = context.AncestorIds
+ .Where(a => matchingIds.Contains(a.ItemId))
+ .Select(a => a.ParentItemId)
+ .Union(context.LinkedChildren
+ .Where(lc => matchingIds.Contains(lc.ChildId))
+ .Select(lc => lc.ParentId));
+
+ return query.Where(e =>
+ !matchingIds.Contains(e.Id)
+ && !foldersWithMatchingDescendants.Contains(e.Id));
+ }
+}