aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations/Item/FolderAwareFilterExtensions.cs
blob: d7b2567f3708d70029b07d7e6073b807a0d1c8bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma warning disable RS0030 // Do not use banned APIs

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)
    {
        // Use correlated Any() subqueries instead of UNION + Contains for better index utilization
        var matchingIds = context.BaseItems.Where(condition).Select(b => b.Id);

        return query.Where(e =>
            matchingIds.Contains(e.Id)
            || context.AncestorIds.Any(a => a.ParentItemId == e.Id && matchingIds.Contains(a.ItemId))
            || context.LinkedChildren.Any(lc => lc.ParentId == e.Id && matchingIds.Contains(lc.ChildId)));
    }

    /// <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);

        return query.Where(e =>
            !matchingIds.Contains(e.Id)
            && !context.AncestorIds.Any(a => a.ParentItemId == e.Id && matchingIds.Contains(a.ItemId))
            && !context.LinkedChildren.Any(lc => lc.ParentId == e.Id && matchingIds.Contains(lc.ChildId)));
    }
}