2020-06-29 03:56:10 +02:00
|
|
|
|
using System;
|
2020-08-01 19:19:41 +02:00
|
|
|
|
using System.Linq;
|
2020-06-29 03:56:10 +02:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
2020-06-06 07:29:13 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BirdsiteLive.ActivityPub;
|
2020-08-01 04:13:52 +02:00
|
|
|
|
using BirdsiteLive.ActivityPub.Models;
|
2020-07-22 08:11:44 +02:00
|
|
|
|
using BirdsiteLive.Common.Settings;
|
2020-06-06 07:29:13 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2020-06-29 03:56:10 +02:00
|
|
|
|
using Org.BouncyCastle.Bcpg;
|
2020-06-06 07:29:13 +02:00
|
|
|
|
|
|
|
|
|
namespace BirdsiteLive.Domain
|
|
|
|
|
{
|
|
|
|
|
public interface IActivityPubService
|
|
|
|
|
{
|
|
|
|
|
Task<Actor> GetUser(string objectId);
|
2020-06-29 05:42:23 +02:00
|
|
|
|
Task<HttpStatusCode> PostDataAsync<T>(T data, string targetHost, string actorUrl, string inbox = null);
|
2020-07-22 08:11:44 +02:00
|
|
|
|
Task<HttpStatusCode> PostNewNoteActivity(Note note, string username, string noteId, string targetHost,
|
|
|
|
|
string targetInbox);
|
2020-06-06 07:29:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ActivityPubService : IActivityPubService
|
|
|
|
|
{
|
2020-07-22 08:11:44 +02:00
|
|
|
|
private readonly InstanceSettings _instanceSettings;
|
2020-06-29 03:56:10 +02:00
|
|
|
|
private readonly ICryptoService _cryptoService;
|
|
|
|
|
|
|
|
|
|
#region Ctor
|
2020-07-22 08:11:44 +02:00
|
|
|
|
public ActivityPubService(ICryptoService cryptoService, InstanceSettings instanceSettings)
|
2020-06-29 03:56:10 +02:00
|
|
|
|
{
|
|
|
|
|
_cryptoService = cryptoService;
|
2020-07-22 08:11:44 +02:00
|
|
|
|
_instanceSettings = instanceSettings;
|
2020-06-29 03:56:10 +02:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2020-06-06 07:29:13 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-29 03:56:10 +02:00
|
|
|
|
|
2020-07-22 08:11:44 +02:00
|
|
|
|
public async Task<HttpStatusCode> PostNewNoteActivity(Note note, string username, string noteId, string targetHost, string targetInbox)
|
|
|
|
|
{
|
|
|
|
|
//var username = "gra";
|
|
|
|
|
var actor = $"https://{_instanceSettings.Domain}/users/{username}";
|
|
|
|
|
//var targetHost = "mastodon.technology";
|
|
|
|
|
//var target = $"{targetHost}/users/testtest";
|
|
|
|
|
//var inbox = $"/users/testtest/inbox";
|
|
|
|
|
|
|
|
|
|
//var noteGuid = Guid.NewGuid();
|
|
|
|
|
var noteUri = $"https://{_instanceSettings.Domain}/users/{username}/statuses/{noteId}";
|
|
|
|
|
|
|
|
|
|
//var noteUrl = $"https://{_instanceSettings.Domain}/@{username}/{noteId}";
|
|
|
|
|
//var to = $"{actor}/followers";
|
|
|
|
|
//var apPublic = "https://www.w3.org/ns/activitystreams#Public";
|
|
|
|
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
var nowString = now.ToString("s") + "Z";
|
|
|
|
|
|
|
|
|
|
var noteActivity = new ActivityCreateNote()
|
|
|
|
|
{
|
|
|
|
|
context = "https://www.w3.org/ns/activitystreams",
|
|
|
|
|
id = $"{noteUri}/activity",
|
|
|
|
|
type = "Create",
|
|
|
|
|
actor = actor,
|
|
|
|
|
published = nowString,
|
|
|
|
|
|
|
|
|
|
to = note.to,
|
|
|
|
|
cc = note.cc,
|
|
|
|
|
apObject = note
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return await PostDataAsync(noteActivity, targetHost, actor, targetInbox);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 05:42:23 +02:00
|
|
|
|
public async Task<HttpStatusCode> PostDataAsync<T>(T data, string targetHost, string actorUrl, string inbox = null)
|
2020-06-29 03:56:10 +02:00
|
|
|
|
{
|
2020-06-29 05:42:23 +02:00
|
|
|
|
var usedInbox = $"/inbox";
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(inbox))
|
|
|
|
|
usedInbox = inbox;
|
|
|
|
|
|
2020-06-29 03:56:10 +02:00
|
|
|
|
var json = JsonConvert.SerializeObject(data);
|
|
|
|
|
|
|
|
|
|
var date = DateTime.UtcNow.ToUniversalTime();
|
|
|
|
|
var httpDate = date.ToString("r");
|
2020-06-29 05:42:23 +02:00
|
|
|
|
var signature = _cryptoService.SignAndGetSignatureHeader(date, actorUrl, targetHost, usedInbox);
|
|
|
|
|
|
|
|
|
|
|
2020-06-29 03:56:10 +02:00
|
|
|
|
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
var httpRequestMessage = new HttpRequestMessage
|
|
|
|
|
{
|
|
|
|
|
Method = HttpMethod.Post,
|
2020-06-29 05:42:23 +02:00
|
|
|
|
RequestUri = new Uri($"https://{targetHost}/{usedInbox}"),
|
2020-06-29 03:56:10 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
2020-06-06 07:29:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|