using ActivityPub.Utils; using Microsoft.AspNetCore.Mvc; using KristofferStrube.ActivityStreams; using Object = KristofferStrube.ActivityStreams.Object; namespace ActivityPub.Controllers; /// /// API Controller for the Outbox /// [Route("[controller]")] [ApiController] public class OutboxController : ControllerBase { private readonly ILogger _logger; /// /// Default Constructor /// /// The logger public OutboxController(ILogger logger) { _logger = logger; } /// /// Method to create and process a new object. /// See https://www.w3.org/TR/activitypub/#client-to-server-interactions /// /// The data for the Activity or Object to create /// /// 201 (Created) on success with the new id in the Location header /// TODO: Also currently returns the newly created Action to assist in debugging. /// /// The new Activity's id is returned in the Location header /// If the provided data is not a valid Activity or Object [HttpPost(Name = "PostOutboxObject")] [ProducesResponseType(StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task 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(newObjectOrLink as 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); } /// /// Retrieves a single Activity by id /// See: https://www.w3.org/TR/activitypub/#outbox /// /// The identifier of the object to retrieve. /// The Activity requested, or 404 (Not Found) [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); } /// /// Retrieves all Activities /// See: https://www.w3.org/TR/activitypub/#outbox /// /// A list of all activities [HttpGet(Name = "GetOutboxList")] public IActionResult Get() { ActivityStore Outbox = new(this.Url, ActivityStoreType.Outbox); return Ok(Outbox.GetAll()); } }