2024-04-05 04:26:57 +00:00
|
|
|
|
using ActivityPub.Utils;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using KristofferStrube.ActivityStreams;
|
2024-04-21 23:40:14 +00:00
|
|
|
|
|
|
|
|
|
using Object = KristofferStrube.ActivityStreams.Object;
|
2024-04-05 04:26:57 +00:00
|
|
|
|
|
|
|
|
|
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>
|
2024-05-02 04:54:14 +00:00
|
|
|
|
/// <param name="newObjectOrLink">The data for the Activity or Object to create</param>
|
2024-04-05 04:26:57 +00:00
|
|
|
|
/// <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);
|
|
|
|
|
|
2024-04-21 23:40:14 +00:00
|
|
|
|
if(newObjectOrLink == null || !(newObjectOrLink is Object))
|
2024-04-05 04:26:57 +00:00
|
|
|
|
return this.BadRequest($"No valid Activity or Object found in the request body");
|
|
|
|
|
|
2024-04-21 23:40:14 +00:00
|
|
|
|
Activity newActivity = (newObjectOrLink is Activity) ? (Activity)newObjectOrLink
|
2024-05-02 04:54:14 +00:00
|
|
|
|
: Outbox.WrapObjectInCreate((Object)newObjectOrLink);
|
2024-04-05 04:26:57 +00:00
|
|
|
|
|
|
|
|
|
//// Validate the activity
|
|
|
|
|
//try {
|
|
|
|
|
// if (!Outbox.ValidateActivity(newActivity)) throw new ArgumentException();
|
|
|
|
|
//}
|
|
|
|
|
//catch (ArgumentException err) {
|
|
|
|
|
// return this.BadRequest(err.Message);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
newActivity = Outbox.InsertActivity(newActivity);
|
2024-04-21 23:40:14 +00:00
|
|
|
|
|
2024-04-05 04:26:57 +00:00
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|