House rules: primary constructors, a cancellation token on every async call, and the rules fail the build
Build / Build (push) Successful in 30s
Build / Build (push) Successful in 30s
- Services, controllers and jobs take their dependencies through primary constructors and use them directly; where a method already named a local after a dependency, the dependency takes its type's name. - Every async method takes a CancellationToken last and passes it down to MongoDB, HTTP and the job queue. The setup claim is still given back when a request is cancelled. - The federation and media options are read through IOptionsMonitor. - The Mastodon entities keep sending "" for empty strings, and every model field where null means something says so. - The inbox's 503 with Retry-After, which other servers retry, is the one server error left, and says why. - Arasaka.HouseRules.Analyzers checks all of this in every build, here and in PrivaPub.ClientModels as decePubClient builds it. 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
This commit is contained in:
1 parent
f0a14c737f
commit
0f73f69239
176 files changed
+2789
-3974
No files matched your search
@@ -11,31 +11,22 @@ using System.Text.Json;
|
||||
|
||||
namespace PrivaPub.Federation.Objects
|
||||
{
|
||||
public class InstanceDescriber : IJobHandler
|
||||
public class InstanceDescriber(
|
||||
IFederationHttp federationHttp,
|
||||
IGeoLocator geoLocator = default,
|
||||
IConnectedAddresses addresses = default,
|
||||
ICdnRanges cdnRanges = default,
|
||||
IEdgeHints edgeHints = default) : IJobHandler
|
||||
{
|
||||
const int MaxRawChars = 64 * 1024;
|
||||
|
||||
readonly IFederationHttp _http;
|
||||
readonly IGeoLocator _geo;
|
||||
readonly IConnectedAddresses _addresses;
|
||||
readonly ICdnRanges _cdnRanges;
|
||||
readonly IEdgeHints _edges;
|
||||
|
||||
public InstanceDescriber(IFederationHttp http, IGeoLocator geo = default, IConnectedAddresses addresses = default, ICdnRanges cdnRanges = default,
|
||||
IEdgeHints edges = default)
|
||||
{
|
||||
_http = http;
|
||||
_geo = geo;
|
||||
_addresses = addresses;
|
||||
_cdnRanges = cdnRanges;
|
||||
_edges = edges;
|
||||
}
|
||||
|
||||
public JobKind Kind => JobKind.DescribeInstance;
|
||||
public int Concurrency => 1;
|
||||
public int MaxAttempts => 2;
|
||||
public int PerHostLimit => 1;
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("HouseRules", "HR0011", Justification = "An ISO week key such as 2026-W05, not a number shown to a reader.")]
|
||||
public static string Week(DateTime at) => $"{ISOWeek.GetYear(at)}-W{ISOWeek.GetWeekOfYear(at):00}";
|
||||
|
||||
public static string DedupeKey(string host, DateTime at) => $"describe|{host}|{ISOWeek.GetYear(at)}-{ISOWeek.GetWeekOfYear(at)}";
|
||||
@@ -54,18 +45,19 @@ namespace PrivaPub.Federation.Objects
|
||||
public async Task<bool> Describe(string host, bool crawled, Func<string, bool> allowed, CancellationToken token)
|
||||
{
|
||||
allowed ??= _ => true;
|
||||
NodeInfoFacts facts = default;
|
||||
string rawNodeInfo = default, error;
|
||||
var facts = default(NodeInfoFacts);
|
||||
var rawNodeInfo = default(string);
|
||||
var error = default(string);
|
||||
using (HttpScope.For("nodeinfo"))
|
||||
{
|
||||
error = allowed("/.well-known/nodeinfo") ? "no NodeInfo" : "robots.txt disallows NodeInfo";
|
||||
using var links = allowed("/.well-known/nodeinfo")
|
||||
? await _http.GetJson(Address($"https://{host}/.well-known/nodeinfo"), "application/json", sign: default, token)
|
||||
? await federationHttp.GetJson(Address($"https://{host}/.well-known/nodeinfo"), "application/json", sign: default, token)
|
||||
: default;
|
||||
var (href, version) = links == default ? default : InstanceDocuments.NodeInfoLink(links.Root);
|
||||
if (href != default && allowed(new Uri(href).AbsolutePath))
|
||||
{
|
||||
using var nodeInfo = await _http.GetJson(Address(href), "application/json", sign: default, token);
|
||||
using var nodeInfo = await federationHttp.GetJson(Address(href), "application/json", sign: default, token);
|
||||
if (nodeInfo != default)
|
||||
{
|
||||
facts = InstanceDocuments.NodeInfo(nodeInfo.Root, version);
|
||||
@@ -78,7 +70,7 @@ namespace PrivaPub.Federation.Objects
|
||||
// no NodeInfo: NodeInfo2, Castopod's
|
||||
if (facts == default && allowed("/.well-known/x-nodeinfo2"))
|
||||
{
|
||||
using var nodeInfo2 = await _http.GetJson(Address($"https://{host}/.well-known/x-nodeinfo2"), "application/json", sign: default, token);
|
||||
using var nodeInfo2 = await federationHttp.GetJson(Address($"https://{host}/.well-known/x-nodeinfo2"), "application/json", sign: default, token);
|
||||
if (nodeInfo2 != default && InstanceDocuments.NodeInfo2(nodeInfo2.Root) is { } second)
|
||||
{
|
||||
facts = second;
|
||||
@@ -96,7 +88,7 @@ namespace PrivaPub.Federation.Objects
|
||||
{
|
||||
if (!allowed(path))
|
||||
continue;
|
||||
using var document = await _http.GetJson(Address($"https://{host}{path}"), "application/json", sign: default, token);
|
||||
using var document = await federationHttp.GetJson(Address($"https://{host}{path}"), "application/json", sign: default, token);
|
||||
if (document == default || document.Root.ValueKind != JsonValueKind.Object)
|
||||
continue;
|
||||
api = InstanceDocuments.InstanceApi(document.Root);
|
||||
@@ -182,9 +174,9 @@ namespace PrivaPub.Federation.Objects
|
||||
InstanceGeo Locate(string host)
|
||||
{
|
||||
var reachedHost = new Uri(Address($"https://{host}/")).Host;
|
||||
var reached = _addresses?.Of(reachedHost);
|
||||
var fix = reached == default ? default : _geo?.Locate(reached);
|
||||
var (cdn, cdnSource) = Cdn(reached, reachedHost, fix?.Asn, _cdnRanges, _edges);
|
||||
var reached = addresses?.Of(reachedHost);
|
||||
var fix = reached == default ? default : geoLocator?.Locate(reached);
|
||||
var (cdn, cdnSource) = Cdn(reached, reachedHost, fix?.Asn, cdnRanges, edgeHints);
|
||||
if (fix == default && cdn == default)
|
||||
return default;
|
||||
return new InstanceGeo
|
||||
@@ -199,7 +191,7 @@ namespace PrivaPub.Federation.Objects
|
||||
CdnDomain = cdn?.Domain,
|
||||
CdnSource = cdnSource,
|
||||
Ipv6 = reached?.AddressFamily == AddressFamily.InterNetworkV6,
|
||||
Source = _geo?.Source,
|
||||
Source = geoLocator?.Source,
|
||||
LocatedAt = DateTime.UtcNow
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user