using System.Net.Mime;
using Jellyfin.Plugin.RecentlyAddedGuard.Core;
using Jellyfin.Plugin.RecentlyAddedGuard.Ledger;
using Jellyfin.Plugin.RecentlyAddedGuard.Tasks;
using MediaBrowser.Common.Api;
using MediaBrowser.Model.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Plugin.RecentlyAddedGuard.Api;
///
/// Status and actions for the configuration page. Jellyfin adds plugin assemblies as MVC application parts,
/// so this controller is discovered automatically.
///
[ApiController]
[Route("RecentlyAddedGuard")]
[Authorize(Policy = Policies.RequiresElevation)]
[Produces(MediaTypeNames.Application.Json)]
public class GuardController : ControllerBase
{
private readonly LedgerStore _store;
private readonly CorrectionLog _corrections;
private readonly ITaskManager _taskManager;
///
/// Initializes a new instance of the class.
///
/// The ledger.
/// The correction log.
/// The task manager.
public GuardController(LedgerStore store, CorrectionLog corrections, ITaskManager taskManager)
{
_store = store;
_corrections = corrections;
_taskManager = taskManager;
}
///
/// Gets ledger statistics and the most recent corrections.
///
/// The status.
/// The status.
[HttpGet("Status")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult GetStatus()
{
var statistics = _store.GetStatistics();
return new GuardStatus
{
LedgerEntries = statistics.Entries,
LiveEntries = statistics.LiveEntries,
RemovedEntries = statistics.RemovedEntries,
CorrectionsSinceStart = _corrections.CorrectionsSinceStart,
DryRunDetectionsSinceStart = _corrections.DryRunDetectionsSinceStart,
SeedCompleted = _store.SeedCompleted,
LastSyncUtc = _store.LastSyncUtc,
DryRun = Plugin.CurrentConfiguration.DryRun,
RecentCorrections = _corrections.GetRecent()
};
}
///
/// Queues the scheduled task "Ledger synchronisieren".
///
/// The task was queued (or is already running).
/// No content.
[HttpPost("Sync")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult StartSync()
{
_taskManager.QueueIfNotRunning();
return NoContent();
}
}