BirdsiteLive/src/BirdsiteLive/Controllers/InboxController.cs

65 lines
2.1 KiB
C#
Raw Normal View History

2020-03-21 08:29:36 +01:00
using System;
using System.Collections.Generic;
using System.IO;
2020-03-21 08:29:36 +01:00
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.ActivityPub;
using BirdsiteLive.ActivityPub.Models;
using BirdsiteLive.Domain;
using BirdsiteLive.Tools;
2020-03-21 08:29:36 +01:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2021-04-16 03:21:22 +02:00
using Microsoft.Extensions.Logging;
2020-03-21 08:29:36 +01:00
namespace BirdsiteLive.Controllers
{
[ApiController]
public class InboxController : ControllerBase
{
2021-04-16 03:21:22 +02:00
private readonly ILogger<InboxController> _logger;
private readonly IUserService _userService;
2021-04-16 03:21:22 +02:00
#region Ctor
public InboxController(ILogger<InboxController> logger, IUserService userService)
2021-04-16 03:21:22 +02:00
{
_logger = logger;
_userService = userService;
2021-04-16 03:21:22 +02:00
}
#endregion
2020-03-21 08:29:36 +01:00
[Route("/inbox")]
[HttpPost]
public async Task<IActionResult> Inbox()
{
2022-02-09 07:54:35 +01:00
try
{
2022-02-09 07:54:35 +01:00
var r = Request;
using (var reader = new StreamReader(Request.Body))
{
var body = await reader.ReadToEndAsync();
2021-04-16 03:21:22 +02:00
2022-02-09 07:54:35 +01:00
_logger.LogTrace("Inbox: {Body}", body);
//System.IO.File.WriteAllText($@"C:\apdebug\inbox\{Guid.NewGuid()}.json", body);
2022-02-09 07:54:35 +01:00
var activity = ApDeserializer.ProcessActivity(body);
var signature = r.Headers["Signature"].First();
2022-02-09 07:54:35 +01:00
switch (activity?.type)
{
2022-02-09 07:54:35 +01:00
case "Delete":
{
var succeeded = await _userService.DeleteRequestedAsync(signature, r.Method, r.Path,
2023-01-11 07:13:23 +01:00
r.QueryString.ToString(), HeaderHandler.RequestHeaders(r.Headers),
activity as ActivityDelete, body);
2022-02-09 07:54:35 +01:00
if (succeeded) return Accepted();
else return Unauthorized();
}
}
}
}
2023-01-11 07:13:23 +01:00
catch (FollowerIsGoneException) { }
2020-08-01 04:52:40 +02:00
return Accepted();
2020-03-21 08:29:36 +01:00
}
}
}