using System;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.JRay.Models;
using Jellyfin.Plugin.JRay.Services;
using Jellyfin.Plugin.JRay.Services.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Plugin.JRay.Controllers;
///
/// Accepts scene-actor-extraction "truth" data pushed directly by a remote
/// extraction worker, for servers that cannot run the extraction pipeline
/// locally. See SPEC.md ยง2.
///
///
/// The schema_version check refuses an unrecognised version rather than
/// guessing at its shape. It defers to rather than
/// holding its own constant: this used to be the only source that checked while
/// sidecar reads did not, so the version the plugin claimed to require and the
/// one it would actually parse could drift apart.
///
[ApiController]
[Route("Plugins/JRay/Items/{itemId}/Truth")]
[Authorize(Roles = "Administrator")]
// TRACES: JR-003, JR-009, JR-014 | SR-003
public class TruthController : ControllerBase
{
private readonly IManagedTruthStore _managedTruthStore;
private readonly ITruthDataService _truthDataService;
///
/// Initializes a new instance of the class.
///
/// The managed truth store.
/// The truth data service.
public TruthController(IManagedTruthStore managedTruthStore, ITruthDataService truthDataService)
{
_managedTruthStore = managedTruthStore;
_truthDataService = truthDataService;
}
///
/// Uploads (creates or replaces) the truth data for an item.
///
/// The Jellyfin item id.
/// The truth file contents.
/// Cancellation token.
/// 204 on success, or 400 if the schema version is unsupported.
[HttpPut]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task PutTruth(Guid itemId, [FromBody] TruthFile truth, CancellationToken cancellationToken)
{
if (!TruthSchema.IsSupported(truth))
{
return BadRequest(TruthSchema.DescribeRejection(truth.SchemaVersion));
}
var provenance = TruthProvenance.Local(TruthSource.Pushed, DateTime.UtcNow);
await _managedTruthStore.SaveAsync(itemId, truth, provenance, cancellationToken).ConfigureAwait(false);
_truthDataService.Invalidate(itemId);
return NoContent();
}
///
/// Removes any managed truth data for an item. The item falls back to
/// its sidecar truth file (if any) on subsequent reads.
///
/// The Jellyfin item id.
/// 204, whether or not managed data existed.
[HttpDelete]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public IActionResult DeleteTruth(Guid itemId)
{
_managedTruthStore.Delete(itemId);
_truthDataService.Invalidate(itemId);
return NoContent();
}
}