Skip to content

Commit

Permalink
feat: add filter series ids endpoints
Browse files Browse the repository at this point in the history
- Add new filter endpoints to get only the series ids matched by the filter, because I'll need them soon-ish in shokofin for client-side filtering of libraries.
  • Loading branch information
revam committed Oct 12, 2024
1 parent 66050d2 commit e369f2b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Shoko.Server/API/v3/Controllers/FilterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,28 @@ public ActionResult<ListResult<Series>> GetPreviewSeriesInFilteredGroup([FromBod
.ToListResult(series => new Series(series, User.JMMUserID, randomImages), page, pageSize);
}

/// <summary>
/// Get a raw list of all <see cref="Series"/> IDs for the live filter for
/// client-side filtering.
/// </summary>
/// <param name="filter">The filter to preview</param>
/// <returns></returns>
[HttpPost("Preview/Series/OnlyIDs")]
public ActionResult<List<int>> GetPreviewFilteredSeriesIDs([FromBody] Filter.Input.CreateOrUpdateFilterBody filter)
{
// Directories should only contain sub-filters, not groups and series.
if (filter.IsDirectory)
return new List<int>();

// Fast path when user is not in the filter.
var filterPreset = _factory.GetFilterPreset(filter, ModelState);
if (!ModelState.IsValid) return ValidationProblem(ModelState);

var results = _filterEvaluator.EvaluateFilter(filterPreset, User.JMMUserID);
return results.SelectMany(groupBy => groupBy)
.ToList();
}

/// <summary>
/// Get a list of all the sub-<see cref="Group"/>s belonging to the <see cref="Group"/> with the given <paramref name="groupID"/> and which are present within the live filter.
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions Shoko.Server/API/v3/Controllers/TreeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,42 @@ public ActionResult<ListResult<Series>> GetSeriesInFilteredGroup([FromRoute, Ran
.ToListResult(series => new Series(series, User.JMMUserID, randomImages), page, pageSize);
}

/// <summary>
/// Get a raw list of <see cref="Series"/> IDs for the <see cref="Filter"/>
/// with the given <paramref name="filterID"/> for client-side filtering.
/// </summary>
/// <remarks>
/// The <see cref="Filter"/> must have <see cref="Filter.IsDirectory"/> set to false to use
/// this endpoint.
/// </remarks>
/// <param name="filterID"><see cref="Filter"/> ID</param>
/// <returns></returns>
[HttpGet("Filter/{filterID}/Series/OnlyIDs")]
public ActionResult<List<int>> GetFilteredSeriesIDs([FromRoute, Range(0, int.MaxValue)] int filterID)
{
if (filterID == 0)
{
var user = User;
return RepoFactory.AnimeSeries.GetAll()
.Where(user.AllowedSeries)
.Select(group => group.AnimeSeriesID)
.ToList();
}

var filterPreset = RepoFactory.FilterPreset.GetByID(filterID);
if (filterPreset == null)
return NotFound(FilterController.FilterNotFound);

// Directories should only contain sub-filters, not groups and series.
if (filterPreset.IsDirectory())
return new List<int>();

// Gets Series and Series IDs in a filter, already sorted by the filter
var results = _filterEvaluator.EvaluateFilter(filterPreset, User.JMMUserID);
return results.SelectMany(groupBy => groupBy)
.ToList();
}

/// <summary>
/// Get a list of all the sub-<see cref="Group"/>s belonging to the <see cref="Group"/> with the given <paramref name="groupID"/> and which are present within the <see cref="Filter"/> with the given <paramref name="filterID"/>.
/// </summary>
Expand Down

0 comments on commit e369f2b

Please sign in to comment.