feat: add Herald API controller
This commit is contained in:
@@ -0,0 +1,23 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Jellyfin.Plugin.Herald.Notices;
|
||||||
|
|
||||||
|
namespace Jellyfin.Plugin.Herald.Api;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Body of an acknowledgement request.
|
||||||
|
/// </summary>
|
||||||
|
public class AckRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the revision the user saw.
|
||||||
|
/// </summary>
|
||||||
|
[Required]
|
||||||
|
public int? Revision { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the response.
|
||||||
|
/// </summary>
|
||||||
|
[Required]
|
||||||
|
[EnumDataType(typeof(AckAction))]
|
||||||
|
public AckAction? Action { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,282 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Mime;
|
||||||
|
using Jellyfin.Plugin.Herald.Notices;
|
||||||
|
using Jellyfin.Plugin.Herald.Stats;
|
||||||
|
using MediaBrowser.Common.Api;
|
||||||
|
using MediaBrowser.Controller.Library;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Jellyfin.Plugin.Herald.Api;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The Herald API.
|
||||||
|
/// </summary>
|
||||||
|
[ApiController]
|
||||||
|
[Route("Herald")]
|
||||||
|
[Authorize]
|
||||||
|
[Produces(MediaTypeNames.Application.Json)]
|
||||||
|
public class HeraldController : ControllerBase
|
||||||
|
{
|
||||||
|
private const string UserIdClaim = "Jellyfin-UserId";
|
||||||
|
private const string ClientScriptResource = "Jellyfin.Plugin.Herald.Web.client.js";
|
||||||
|
|
||||||
|
private readonly NoticeService _notices;
|
||||||
|
private readonly StatsService _stats;
|
||||||
|
private readonly IUserManager _userManager;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="HeraldController"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="notices">The notice service.</param>
|
||||||
|
/// <param name="stats">The stats service.</param>
|
||||||
|
/// <param name="userManager">The user manager.</param>
|
||||||
|
public HeraldController(NoticeService notices, StatsService stats, IUserManager userManager)
|
||||||
|
{
|
||||||
|
_notices = notices;
|
||||||
|
_stats = stats;
|
||||||
|
_userManager = userManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the script that shows notices in jellyfin-web.
|
||||||
|
/// </summary>
|
||||||
|
/// <response code="200">Script returned.</response>
|
||||||
|
/// <returns>The client script.</returns>
|
||||||
|
[HttpGet("client.js")]
|
||||||
|
[AllowAnonymous]
|
||||||
|
[Produces("application/javascript")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
public ActionResult GetClientScript()
|
||||||
|
{
|
||||||
|
var stream = typeof(HeraldController).Assembly.GetManifestResourceStream(ClientScriptResource);
|
||||||
|
if (stream is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
Response.Headers.CacheControl = "public, max-age=3600";
|
||||||
|
return File(stream, "application/javascript; charset=utf-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the notices the current user has not responded to yet, oldest first.
|
||||||
|
/// </summary>
|
||||||
|
/// <response code="200">Pending notices returned.</response>
|
||||||
|
/// <returns>The pending notices.</returns>
|
||||||
|
[HttpGet("Notices/Pending")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
public ActionResult<IEnumerable<PendingNotice>> GetPendingNotices()
|
||||||
|
{
|
||||||
|
if (!TryGetUserId(out var userId))
|
||||||
|
{
|
||||||
|
// API keys are not tied to a user, so nothing can be pending.
|
||||||
|
return Ok(Array.Empty<PendingNotice>());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(_notices.GetPending(userId).Select(n => new PendingNotice
|
||||||
|
{
|
||||||
|
Id = n.Id,
|
||||||
|
Title = n.Title,
|
||||||
|
Body = n.Body,
|
||||||
|
RequireAccept = n.RequireAccept,
|
||||||
|
Revision = n.Revision
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Records the current user's response to a notice.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The notice id.</param>
|
||||||
|
/// <param name="request">The response.</param>
|
||||||
|
/// <response code="204">Response stored.</response>
|
||||||
|
/// <response code="400">The request is invalid, or the notice has to be accepted and cannot be dismissed.</response>
|
||||||
|
/// <response code="404">The notice does not exist or is not addressed to the user.</response>
|
||||||
|
/// <response code="409">The notice has a newer revision.</response>
|
||||||
|
/// <returns>No content.</returns>
|
||||||
|
[HttpPost("Notices/{id:guid}/Ack")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status409Conflict)]
|
||||||
|
public ActionResult AcknowledgeNotice([FromRoute] Guid id, [FromBody] AckRequest request)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(request);
|
||||||
|
|
||||||
|
if (!TryGetUserId(out var userId))
|
||||||
|
{
|
||||||
|
return Problem("Acknowledging a notice needs a user session.", statusCode: StatusCodes.Status400BadRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.Revision is not { } revision || request.Action is not { } action || !Enum.IsDefined(action))
|
||||||
|
{
|
||||||
|
return Problem("Revision and action are required.", statusCode: StatusCodes.Status400BadRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
return _notices.Acknowledge(userId, id, revision, action) switch
|
||||||
|
{
|
||||||
|
AckResult.Ok => NoContent(),
|
||||||
|
AckResult.NotFound => NotFound(),
|
||||||
|
AckResult.RevisionMismatch => Problem("The notice has changed. Fetch the pending notices again.", statusCode: StatusCodes.Status409Conflict),
|
||||||
|
_ => Problem("This notice has to be accepted and cannot be dismissed.", statusCode: StatusCodes.Status400BadRequest)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all notices.
|
||||||
|
/// </summary>
|
||||||
|
/// <response code="200">Notices returned.</response>
|
||||||
|
/// <returns>The notices, oldest first.</returns>
|
||||||
|
[HttpGet("Notices")]
|
||||||
|
[Authorize(Policy = Policies.RequiresElevation)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
public ActionResult<IEnumerable<Notice>> GetNotices()
|
||||||
|
{
|
||||||
|
return Ok(_notices.GetAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a notice.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The notice id.</param>
|
||||||
|
/// <response code="200">Notice returned.</response>
|
||||||
|
/// <response code="404">Notice not found.</response>
|
||||||
|
/// <returns>The notice.</returns>
|
||||||
|
[HttpGet("Notices/{id:guid}")]
|
||||||
|
[Authorize(Policy = Policies.RequiresElevation)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public ActionResult<Notice> GetNotice([FromRoute] Guid id)
|
||||||
|
{
|
||||||
|
var notice = _notices.Get(id);
|
||||||
|
return notice is null ? NotFound() : Ok(notice);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a notice.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="draft">The notice.</param>
|
||||||
|
/// <response code="201">Notice created.</response>
|
||||||
|
/// <response code="400">The notice is invalid.</response>
|
||||||
|
/// <returns>The created notice.</returns>
|
||||||
|
[HttpPost("Notices")]
|
||||||
|
[Authorize(Policy = Policies.RequiresElevation)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
public ActionResult<Notice> CreateNotice([FromBody] NoticeDraft draft)
|
||||||
|
{
|
||||||
|
var error = NoticeRules.ValidateDraft(draft);
|
||||||
|
if (error is not null)
|
||||||
|
{
|
||||||
|
return Problem(error, statusCode: StatusCodes.Status400BadRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
var notice = _notices.Create(draft);
|
||||||
|
return CreatedAtAction(nameof(GetNotice), new { id = notice.Id }, notice);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates a notice. Users who already responded are not asked again; use the re-show action for that.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The notice id.</param>
|
||||||
|
/// <param name="draft">The notice.</param>
|
||||||
|
/// <response code="200">Notice updated.</response>
|
||||||
|
/// <response code="400">The notice is invalid.</response>
|
||||||
|
/// <response code="404">Notice not found.</response>
|
||||||
|
/// <returns>The updated notice.</returns>
|
||||||
|
[HttpPut("Notices/{id:guid}")]
|
||||||
|
[Authorize(Policy = Policies.RequiresElevation)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public ActionResult<Notice> UpdateNotice([FromRoute] Guid id, [FromBody] NoticeDraft draft)
|
||||||
|
{
|
||||||
|
var error = NoticeRules.ValidateDraft(draft);
|
||||||
|
if (error is not null)
|
||||||
|
{
|
||||||
|
return Problem(error, statusCode: StatusCodes.Status400BadRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
var notice = _notices.Update(id, draft);
|
||||||
|
return notice is null ? NotFound() : Ok(notice);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a notice and its acknowledgements.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The notice id.</param>
|
||||||
|
/// <response code="204">Notice deleted.</response>
|
||||||
|
/// <response code="404">Notice not found.</response>
|
||||||
|
/// <returns>No content.</returns>
|
||||||
|
[HttpDelete("Notices/{id:guid}")]
|
||||||
|
[Authorize(Policy = Policies.RequiresElevation)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public ActionResult DeleteNotice([FromRoute] Guid id)
|
||||||
|
{
|
||||||
|
return _notices.Delete(id) ? NoContent() : NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shows a notice to everyone again by bumping its revision.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The notice id.</param>
|
||||||
|
/// <response code="200">Revision bumped.</response>
|
||||||
|
/// <response code="404">Notice not found.</response>
|
||||||
|
/// <returns>The updated notice.</returns>
|
||||||
|
[HttpPost("Notices/{id:guid}/Reshow")]
|
||||||
|
[Authorize(Policy = Policies.RequiresElevation)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public ActionResult<Notice> ReshowNotice([FromRoute] Guid id)
|
||||||
|
{
|
||||||
|
var notice = _notices.Reshow(id);
|
||||||
|
return notice is null ? NotFound() : Ok(notice);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets who responded to the current revision of a notice, and which addressed users have not yet.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The notice id.</param>
|
||||||
|
/// <response code="200">Report returned.</response>
|
||||||
|
/// <response code="404">Notice not found.</response>
|
||||||
|
/// <returns>The acknowledgement report.</returns>
|
||||||
|
[HttpGet("Notices/{id:guid}/Acks")]
|
||||||
|
[Authorize(Policy = Policies.RequiresElevation)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
public ActionResult<AckReport> GetNoticeAcks([FromRoute] Guid id)
|
||||||
|
{
|
||||||
|
var report = _notices.GetAckReport(id, UserNames());
|
||||||
|
return report is null ? NotFound() : Ok(report);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets approximate watch time per user.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="days">The number of days to cover, today included (1 to 90).</param>
|
||||||
|
/// <response code="200">Stats returned.</response>
|
||||||
|
/// <returns>One entry per user.</returns>
|
||||||
|
[HttpGet("Stats")]
|
||||||
|
[Authorize(Policy = Policies.RequiresElevation)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
public ActionResult<IEnumerable<UserWatchSummary>> GetStats([FromQuery] int days = 30)
|
||||||
|
{
|
||||||
|
return Ok(_stats.GetSummary(days, UserNames()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Dictionary<Guid, string> UserNames()
|
||||||
|
{
|
||||||
|
return _userManager.GetUsers().ToDictionary(u => u.Id, u => u.Username);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool TryGetUserId(out Guid userId)
|
||||||
|
{
|
||||||
|
userId = Guid.Empty;
|
||||||
|
var claim = User.FindFirst(UserIdClaim)?.Value;
|
||||||
|
return !string.IsNullOrEmpty(claim) && Guid.TryParse(claim, out userId) && userId != Guid.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Jellyfin.Plugin.Herald.Api;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A notice as shown to the user it is pending for.
|
||||||
|
/// </summary>
|
||||||
|
public class PendingNotice
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the notice id.
|
||||||
|
/// </summary>
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the title.
|
||||||
|
/// </summary>
|
||||||
|
public string Title { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the body.
|
||||||
|
/// </summary>
|
||||||
|
public string Body { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether the notice has to be accepted.
|
||||||
|
/// </summary>
|
||||||
|
public bool RequireAccept { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the revision to send back with the response.
|
||||||
|
/// </summary>
|
||||||
|
public int Revision { get; set; }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user