85 lines
2.7 KiB
C#
85 lines
2.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Jellyfin.Plugin.JRay.Models;
|
|
|
|
namespace Jellyfin.Plugin.JRay.Services;
|
|
|
|
/// <summary>
|
|
/// Resolves the effective prioritise/ignore action for an item from the
|
|
/// configured rules. More specific scopes win: an Item rule overrides a
|
|
/// Series rule, which overrides a Genre rule. Because a given scope+value can
|
|
/// hold only one action, the only conflicts possible are across scopes, and
|
|
/// specificity resolves those.
|
|
/// </summary>
|
|
public static class PolicyResolver
|
|
{
|
|
/// <summary>
|
|
/// Computes the effective action for an item, or <c>null</c> if no rule matches.
|
|
/// </summary>
|
|
/// <param name="rules">The configured rules.</param>
|
|
/// <param name="itemId">The item's id.</param>
|
|
/// <param name="seriesId">The item's series id, or <see cref="Guid.Empty"/> if it is not an episode.</param>
|
|
/// <param name="genres">The item's genres.</param>
|
|
/// <returns>The effective <see cref="PolicyAction"/>, or <c>null</c> when unruled.</returns>
|
|
public static PolicyAction? Resolve(
|
|
IReadOnlyList<MediaPolicyRule> rules,
|
|
Guid itemId,
|
|
Guid seriesId,
|
|
IReadOnlyList<string> genres)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(rules);
|
|
|
|
PolicyAction? itemAction = null;
|
|
PolicyAction? seriesAction = null;
|
|
PolicyAction? genreAction = null;
|
|
|
|
var itemIdStr = itemId.ToString("D");
|
|
var seriesIdStr = seriesId == Guid.Empty ? null : seriesId.ToString("D");
|
|
|
|
foreach (var rule in rules)
|
|
{
|
|
switch (rule.Scope)
|
|
{
|
|
case PolicyScope.Item:
|
|
if (string.Equals(rule.Value, itemIdStr, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
itemAction = rule.Action;
|
|
}
|
|
|
|
break;
|
|
|
|
case PolicyScope.Series:
|
|
if (seriesIdStr is not null && string.Equals(rule.Value, seriesIdStr, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
seriesAction = rule.Action;
|
|
}
|
|
|
|
break;
|
|
|
|
case PolicyScope.Genre:
|
|
if (genres is not null && MatchesGenre(genres, rule.Value))
|
|
{
|
|
genreAction = rule.Action;
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
return itemAction ?? seriesAction ?? genreAction;
|
|
}
|
|
|
|
private static bool MatchesGenre(IReadOnlyList<string> genres, string value)
|
|
{
|
|
for (var i = 0; i < genres.Count; i++)
|
|
{
|
|
if (string.Equals(genres[i], value, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|