This repository has been archived on 2023-05-27. You can view files and clone it, but cannot push or open issues or pull requests.
BirdsiteLIVE/src/BirdsiteLive.Domain/ActivityPubService.cs
2020-06-28 21:56:10 -04:00

66 lines
2.1 KiB
C#

using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using BirdsiteLive.ActivityPub;
using Newtonsoft.Json;
using Org.BouncyCastle.Bcpg;
namespace BirdsiteLive.Domain
{
public interface IActivityPubService
{
Task<Actor> GetUser(string objectId);
Task<HttpStatusCode> PostDataAsync<T>(T data, string targetHost, string actorUrl);
}
public class ActivityPubService : IActivityPubService
{
private readonly ICryptoService _cryptoService;
#region Ctor
public ActivityPubService(ICryptoService cryptoService)
{
_cryptoService = cryptoService;
}
#endregion
public async Task<Actor> GetUser(string objectId)
{
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
var result = await httpClient.GetAsync(objectId);
var content = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<Actor>(content);
}
}
public async Task<HttpStatusCode> PostDataAsync<T>(T data, string targetHost, string actorUrl)
{
var json = JsonConvert.SerializeObject(data);
var date = DateTime.UtcNow.ToUniversalTime();
var httpDate = date.ToString("r");
var signature = _cryptoService.SignAndGetSignatureHeader(date, actorUrl, targetHost);
var client = new HttpClient();
var httpRequestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri($"https://{targetHost}/inbox"),
Headers =
{
{"Host", targetHost},
{"Date", httpDate},
{"Signature", signature}
},
Content = new StringContent(json, Encoding.UTF8, "application/ld+json")
};
var response = await client.SendAsync(httpRequestMessage);
return response.StatusCode;
}
}
}