using System.Collections.Generic;
using Jellyfin.Plugin.JRay.Models;
using Jellyfin.Plugin.JRay.Services.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Plugin.JRay.Controllers;
///
/// Manages the prioritise/ignore rules that shape the work-discovery API
/// (GET /Plugins/JRay/Tasks/Pending). Rules can target a genre, a
/// series, or a single item; setting a rule for a target that already has one
/// replaces it, so a target can never be both prioritised and ignored.
///
[ApiController]
[Route("Plugins/JRay/Policy")]
[Authorize(Roles = "Administrator")]
public class PolicyController : ControllerBase
{
private readonly IMediaPolicyStore _policyStore;
///
/// Initializes a new instance of the class.
///
/// The media policy store.
public PolicyController(IMediaPolicyStore policyStore)
{
_policyStore = policyStore;
}
///
/// Gets all configured prioritise/ignore rules.
///
/// The current rules.
[HttpGet("Rules")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult> GetRules()
{
return Ok(_policyStore.GetRules());
}
///
/// Adds or replaces a rule. If a rule already exists for the same scope
/// and value, its action is updated.
///
/// The rule to set.
/// 204 on success, or 400 if the rule has no value.
[HttpPut("Rules")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public IActionResult SetRule([FromBody] MediaPolicyRule rule)
{
if (rule is null || string.IsNullOrWhiteSpace(rule.Value))
{
return BadRequest("A rule must have a non-empty value.");
}
_policyStore.SetRule(rule);
return NoContent();
}
///
/// Removes the rule matching the given scope and value.
///
/// The scope of the rule to remove.
/// The value of the rule to remove.
/// 204 whether or not a matching rule existed.
[HttpDelete("Rules")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public IActionResult RemoveRule([FromQuery] PolicyScope scope, [FromQuery] string value)
{
_policyStore.RemoveRule(scope, value ?? string.Empty);
return NoContent();
}
}