AP.NET/Controllers/OutboxController.cs
Gordon Pedersen 7c84fbc4c5 Initial commit way too late
There's a lot of rubbish in here, but I don't want to lose anything, so I'm going to commit it all before getting rid of some of the trash.
2024-04-05 15:26:57 +11:00

87 lines
3.1 KiB
C#

using ActivityPub.Utils;
using Microsoft.AspNetCore.Mvc;
using KristofferStrube;
using KristofferStrube.ActivityStreams;
using OneOf;
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="newData">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 KristofferStrube.ActivityStreams.Object))
return this.BadRequest($"No valid Activity or Object found in the request body");
OneOf<Activity, IntransitiveActiviy> newActivity =
(newObjectOrLink is Activity) ? (Activity)newObjectOrLink
: (newObjectOrLink is IntransitiveActiviy) ? (IntransitiveActiviy)newObjectOrLink
: Outbox.WrapObjectInCreate(newObjectOrLink as KristofferStrube.ActivityStreams.Object);
//// 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());
}
}