aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations/Item/FolderAwareFilterExtensions.cs
blob: 888dacd16b66329691ab21dc82e48fa51b5bb9eb (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#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)
    {
        // Get IDs of items that directly match the condition
        var directMatchIds = context.BaseItems.Where(condition).Select(b => b.Id);

        // Get parent IDs where a descendant (via AncestorIds) matches
        var ancestorMatchIds = context.AncestorIds
            .Where(a => directMatchIds.Contains(a.ItemId))
            .Select(a => a.ParentItemId);

        // Get parent IDs where a linked child matches
        var linkedMatchIds = context.LinkedChildren
            .Where(lc => directMatchIds.Contains(lc.ChildId))
            .Select(lc => lc.ParentId);

        var allMatchingIds = directMatchIds
            .Concat(ancestorMatchIds)
            .Concat(linkedMatchIds)
            .Distinct();

        return query.Where(e => allMatchingIds.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)
    {
        // Get IDs of items that directly match the condition
        var directMatchIds = context.BaseItems.Where(condition).Select(b => b.Id);

        // Get parent IDs where a descendant (via AncestorIds) matches
        var ancestorMatchIds = context.AncestorIds
            .Where(a => directMatchIds.Contains(a.ItemId))
            .Select(a => a.ParentItemId);

        // Get parent IDs where a linked child matches
        var linkedMatchIds = context.LinkedChildren
            .Where(lc => directMatchIds.Contains(lc.ChildId))
            .Select(lc => lc.ParentId);

        var allMatchingIds = directMatchIds
            .Concat(ancestorMatchIds)
            .Concat(linkedMatchIds)
            .Distinct();

        return query.Where(e => !allMatchingIds.Contains(e.Id));
    }
}