Files
SocialPub/PrivaPub.Tests/Support/FederatedSeeds.cs
T

55 lines
2.3 KiB
C#
Raw Normal View History

using MongoDB.Entities;
using PrivaPub.Federation.Objects;
using PrivaPub.Models.Social;
using PrivaPub.Tests.Support.Host;
using System.Globalization;
using System.Net;
using System.Numerics;
using System.Text.Json.Nodes;
namespace PrivaPub.Tests.Support
{
public static class FederatedSeeds
{
public static string Origin(RemoteActor actor) => new Uri(actor.Id).GetLeftPart(UriPartial.Authority);
public static string NewId(RemoteActor actor, string kind) => $"{Origin(actor)}/{kind}/{Guid.NewGuid():N}";
public static JsonObject PublicNote(RemoteActor author, string content = "<p>hello</p>", params string[] cc) => new()
{
["id"] = NewId(author, "notes"),
["type"] = "Note",
["attributedTo"] = author.Id,
["content"] = content,
["published"] = DateTime.UtcNow.ToString("O"),
["to"] = new JsonArray(Addressing.Public),
["cc"] = new JsonArray(cc.Prepend(author.Id + "/followers").Select(c => (JsonNode)c).ToArray())
};
public static JsonObject Create(RemoteActor author, JsonObject note) =>
new() { ["id"] = NewId(author, "activities"), ["type"] = "Create", ["actor"] = author.Id, ["object"] = note.DeepClone() };
public static JsonObject Activity(RemoteActor actor, string type, JsonNode inner) =>
new() { ["id"] = NewId(actor, "activities"), ["type"] = type, ["actor"] = actor.Id, ["object"] = inner.DeepClone() };
public static string IdOf(JsonNode node) => node["id"]!.GetValue<string>();
//an accepted follow made here, without the round trip
public static Task Follows(string avatarId, RemoteActor target) =>
DB.Default.SaveAsync(new Following { AvatarId = avatarId, TargetActorURI = target.Id, TargetInboxURL = target.Id + "/inbox", State = FollowState.Accepted });
public static async Task<HttpStatusCode> DeliverSigned(PrivaPubHost host, RemoteActor sender, JsonObject activity, string path = "/human-centipede")
{
using var client = host.Client();
var response = await client.SendAsync(sender.SignedPost(path, activity), TestContext.Current.CancellationToken);
await host.RunInbox(IdOf(activity), TestContext.Current.CancellationToken);
return response.StatusCode;
}
//the smallest id above this one, for a max_id that starts a page exactly at it
public static string IdAbove(string id) =>
(BigInteger.Parse("0" + id, NumberStyles.HexNumber) + 1).ToString("x24")[^24..];
}
}