Build / Build (push) Successful in 35s
All 384 routes, the Mastodon API, the client API and the frozen federation paths, are read from one class with one nested class per controller. Their values are unchanged, checked one by one against the old literals; decePubClient keeps its own APIs class. Checked against GoToSocial in the pasture: 64 passed, 0 failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
146 lines
5.3 KiB
C#
146 lines
5.3 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using MongoDB.Entities;
|
|
|
|
using PrivaPub.Models.Federation;
|
|
using PrivaPub.Models.Post;
|
|
using PrivaPub.Models.User;
|
|
using PrivaPub.StaticServices;
|
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
using PostEntity = PrivaPub.Models.Post.Post;
|
|
using PrivaPub.Domain.Privacy;
|
|
using PrivaPub.Federation.Actors;
|
|
using PrivaPub.Infrastructure;
|
|
|
|
namespace PrivaPub.Federation.Controllers
|
|
{
|
|
[ApiController]
|
|
public class WellKnownController(
|
|
ILocalActorService localActors,
|
|
IOptionsMonitor<RegistrationOptions> registrations,
|
|
Domain.Instance.IInstanceSettings settings) : ControllerBase
|
|
{
|
|
[HttpGet, Route(ApiPaths.WellKnown.GET_WebFinger)]
|
|
public async Task<IActionResult> WebFinger([FromQuery] string resource, CancellationToken token)
|
|
{
|
|
if (string.IsNullOrEmpty(resource))
|
|
return BadRequest();
|
|
// the server itself (FEP-d556): its instance actor
|
|
if (resource.TrimEnd('/') == localActors.BaseAddress)
|
|
{
|
|
var instance = await localActors.GetInstanceActor(token);
|
|
return Content(new JsonObject
|
|
{
|
|
["subject"] = resource,
|
|
["links"] = new JsonArray(new JsonObject
|
|
{
|
|
["rel"] = "https://www.w3.org/ns/activitystreams#Service", ["type"] = "application/activity+json", ["href"] = instance.Uri
|
|
})
|
|
}.ToJsonString(), "application/jrd+json; charset=utf-8");
|
|
}
|
|
|
|
var actor = default(LocalActor);
|
|
// Mastodon, GoToSocial and Pleroma also take a bare user@domain or @user@domain, so we do too.
|
|
var acct = resource.StartsWith("acct:", StringComparison.OrdinalIgnoreCase) ? resource[5..]
|
|
: !resource.Contains("://", StringComparison.Ordinal) && resource.Contains('@') ? resource
|
|
: default;
|
|
if (acct != default)
|
|
{
|
|
var parts = acct.TrimStart('@').Split('@');
|
|
var domain = new Uri(localActors.BaseAddress).Authority;
|
|
if (parts.Length != 2 || !parts[1].Equals(domain, StringComparison.OrdinalIgnoreCase))
|
|
return NotFound();
|
|
actor = await localActors.FindByUserName(parts[0], token);
|
|
if (actor == default && await localActors.Gone(parts[0], token) != default)
|
|
return StatusCode(StatusCodes.Status410Gone);
|
|
}
|
|
else
|
|
actor = await localActors.FindByUri(resource, token);
|
|
|
|
if (actor is not { IsFederated: true })
|
|
return NotFound();
|
|
|
|
var document = new JsonObject
|
|
{
|
|
["subject"] = $"acct:{actor.Handle}",
|
|
["aliases"] = actor.IsServerActor ? new JsonArray(actor.Uri) : new JsonArray(actor.HtmlUrl, actor.Uri),
|
|
["links"] = actor.IsServerActor
|
|
? new JsonArray(new JsonObject { ["rel"] = "self", ["type"] = "application/activity+json", ["href"] = actor.Uri })
|
|
: new JsonArray(
|
|
new JsonObject { ["rel"] = "http://webfinger.net/rel/profile-page", ["type"] = "text/html", ["href"] = actor.HtmlUrl },
|
|
new JsonObject { ["rel"] = "self", ["type"] = "application/activity+json", ["href"] = actor.Uri })
|
|
};
|
|
return Content(document.ToJsonString(), "application/jrd+json; charset=utf-8");
|
|
}
|
|
|
|
[HttpGet, Route(ApiPaths.WellKnown.GET_NodeInfoLinks)]
|
|
public async Task<IActionResult> NodeInfoLinks(CancellationToken token)
|
|
{
|
|
var document = new JsonObject
|
|
{
|
|
["links"] = new JsonArray(
|
|
new JsonObject
|
|
{
|
|
["rel"] = "http://nodeinfo.diaspora.software/ns/schema/2.1",
|
|
["href"] = $"{localActors.BaseAddress}/nodeinfo/2.1"
|
|
},
|
|
new JsonObject
|
|
{
|
|
["rel"] = "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
|
["href"] = $"{localActors.BaseAddress}/nodeinfo/2.0"
|
|
},
|
|
// the application actor (FEP-2677)
|
|
new JsonObject
|
|
{
|
|
["rel"] = "https://www.w3.org/ns/activitystreams#Application",
|
|
["href"] = (await localActors.GetInstanceActor(token)).Uri
|
|
})
|
|
};
|
|
return Content(document.ToJsonString(), "application/json; charset=utf-8");
|
|
}
|
|
|
|
[HttpGet, Route(ApiPaths.WellKnown.GET_M_NodeInfo)]
|
|
public async Task<IActionResult> NodeInfo(string version, CancellationToken token)
|
|
{
|
|
var users = await Counted.Users(token);
|
|
var posts = await Counted.LocalPosts(token);
|
|
var software = new JsonObject { ["name"] = "privapub", ["version"] = BuildInfo.Ref };
|
|
if (version == "2.1")
|
|
{
|
|
software["repository"] = "https://git.thepra.dev/thepra/SocialPub";
|
|
software["homepage"] = "https://git.thepra.dev/thepra/SocialPub";
|
|
}
|
|
var document = new JsonObject
|
|
{
|
|
["version"] = version,
|
|
["software"] = software,
|
|
["protocols"] = new JsonArray("activitypub"),
|
|
["services"] = new JsonObject { ["inbound"] = new JsonArray(), ["outbound"] = new JsonArray() },
|
|
["openRegistrations"] = registrations.CurrentValue.IsOpen,
|
|
["usage"] = new JsonObject
|
|
{
|
|
["users"] = new JsonObject
|
|
{
|
|
["total"] = users,
|
|
["activeMonth"] = await Counted.ActiveUsers(30, token),
|
|
["activeHalfyear"] = await Counted.ActiveUsers(180, token)
|
|
},
|
|
["localPosts"] = posts
|
|
},
|
|
["metadata"] = new JsonObject
|
|
{
|
|
// the server's name and short description, as its administrator set them (IInstanceSettings)
|
|
["nodeName"] = settings.Current.Title,
|
|
["nodeDescription"] = settings.Current.ShortDescription ?? string.Empty,
|
|
["federation"] = new JsonObject { ["document"] = "https://git.thepra.dev/thepra/SocialPub/src/branch/master/FEDERATION.md" }
|
|
}
|
|
};
|
|
return Content(document.ToJsonString(),
|
|
$"application/json; profile=\"http://nodeinfo.diaspora.software/ns/schema/{version}#\"; charset=utf-8");
|
|
}
|
|
}
|
|
}
|