AP.NET/Controllers/OutboxController.cs

86 lines
3 KiB
C#

using ActivityPub.Utils;
using Microsoft.AspNetCore.Mvc;
using KristofferStrube.ActivityStreams;
using Object = KristofferStrube.ActivityStreams.Object;
namespace ActivityPub.Controllers;
/// <summary>
/// API Controller for the Outbox
/// </summary>
[Route("[controller]")]
[ApiController]
public class OutboxController : ControllerBase {
private readonly ILogger<OutboxController> _logger;
/// <summary>
/// Default Constructor
/// </summary>
/// <param name="logger">The logger</param>
public OutboxController(ILogger<OutboxController> logger) {
_logger = logger;
}
/// <summary>
/// Method to create and process a new object.
/// See https://www.w3.org/TR/activitypub/#client-to-server-interactions
/// </summary>
/// <param name="newObjectOrLink">The data for the Activity or Object to create</param>
/// <returns>
/// 201 (Created) on success with the new id in the Location header
/// TODO: Also currently returns the newly created Action to assist in debugging.
/// </returns>
/// <response code="201">The new Activity's id is returned in the Location header</response>
/// <response code="400">If the provided data is not a valid Activity or Object</response>
[HttpPost(Name = "PostOutboxObject")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Post(IObjectOrLink newObjectOrLink) {
ActivityStore Outbox = new(this.Url, ActivityStoreType.Outbox);
if(newObjectOrLink == null || !(newObjectOrLink is Object))
return this.BadRequest($"No valid Activity or Object found in the request body");
Activity newActivity = (newObjectOrLink is Activity) ? (Activity)newObjectOrLink
: Outbox.WrapObjectInCreate((Object)newObjectOrLink);
//// Validate the activity
//try {
// if (!Outbox.ValidateActivity(newActivity)) throw new ArgumentException();
//}
//catch (ArgumentException err) {
// return this.BadRequest(err.Message);
//}
newActivity = Outbox.InsertActivity(newActivity);
return Created(newActivity.Id, newActivity);
}
/// <summary>
/// Retrieves a single Activity by id
/// See: https://www.w3.org/TR/activitypub/#outbox
/// </summary>
/// <param name="id">The identifier of the object to retrieve.</param>
/// <returns>The Activity requested, or 404 (Not Found)</returns>
[HttpGet("{id}", Name = "GetOutboxById")]
public IActionResult Get(string id) {
ActivityStore Outbox = new(this.Url, ActivityStoreType.Outbox);
Activity? activity = Outbox.GetById(id);
if (activity == null) return NotFound();
else return Ok(activity);
}
/// <summary>
/// Retrieves all Activities
/// See: https://www.w3.org/TR/activitypub/#outbox
/// </summary>
/// <returns>A list of all activities</returns>
[HttpGet(Name = "GetOutboxList")]
public IActionResult Get() {
ActivityStore Outbox = new(this.Url, ActivityStoreType.Outbox);
return Ok(Outbox.GetAll());
}
}