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
@@ -26,41 +26,30 @@ namespace PrivaPub.Federation.Actors
|
||||
Task<string> ResolveHandle(string handle, CancellationToken token);
|
||||
}
|
||||
|
||||
public class RemoteActorService : IRemoteActorService
|
||||
public class RemoteActorService(
|
||||
IFederationHttp federationHttp,
|
||||
ILocalActorService localActors,
|
||||
IMemoryCache cache,
|
||||
DbEntities dbEntities,
|
||||
IOptionsMonitor<FederationOptions> options = default,
|
||||
Infrastructure.Jobs.IJobQueue jobs = default) : IRemoteActorService
|
||||
{
|
||||
public const string ActivityJson = "application/activity+json";
|
||||
const string Accept = "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
|
||||
static readonly TimeSpan CacheLifetime = TimeSpan.FromDays(1);
|
||||
static readonly TimeSpan RefetchInterval = TimeSpan.FromMinutes(5);
|
||||
|
||||
readonly IFederationHttp _http;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IMemoryCache _cache;
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly IOptionsMonitor<FederationOptions> _options;
|
||||
|
||||
readonly Infrastructure.Jobs.IJobQueue _jobs;
|
||||
|
||||
public RemoteActorService(IFederationHttp http, ILocalActorService localActors, IMemoryCache cache, DbEntities dbEntities,
|
||||
IOptionsMonitor<FederationOptions> options = default, Infrastructure.Jobs.IJobQueue jobs = default)
|
||||
{
|
||||
_jobs = jobs;
|
||||
_http = http;
|
||||
_localActors = localActors;
|
||||
_cache = cache;
|
||||
_dbEntities = dbEntities;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public Task<FetchedJson> FetchObject(string uri, CancellationToken token) => FetchObject(uri, default, token);
|
||||
|
||||
public async Task<FetchedJson> FetchObject(string uri, string etag, CancellationToken token)
|
||||
{
|
||||
using var scope = HttpScope.Default("object");
|
||||
var signer = await _localActors.GetInstanceActor(token);
|
||||
var signer = await localActors.GetInstanceActor(token);
|
||||
var fetched = string.IsNullOrEmpty(etag)
|
||||
? await Get(uri, signer, token)
|
||||
: await _http.GetJsonIfChanged(uri, Accept, etag, request => HttpSignatures.Sign(request, signer, body: null), token);
|
||||
: await federationHttp.GetJsonIfChanged(uri, Accept, etag, request => HttpSignatures.Sign(request, signer, body: null), token);
|
||||
if (fetched == default || fetched.NotModified)
|
||||
return fetched;
|
||||
|
||||
@@ -86,7 +75,7 @@ namespace PrivaPub.Federation.Actors
|
||||
return default;
|
||||
actorUri = StripFragment(actorUri);
|
||||
|
||||
var cached = await _dbEntities.ForeignAvatars.Match(a => a.ActorURI == actorUri).ExecuteFirstAsync(token);
|
||||
var cached = await dbEntities.ForeignAvatars.Match(a => a.ActorURI == actorUri).ExecuteFirstAsync(token);
|
||||
if (cached != default && !refresh && DateTime.UtcNow - cached.UpdatedAt < CacheLifetime)
|
||||
return cached;
|
||||
if (!MayFetch(actorUri))
|
||||
@@ -105,8 +94,8 @@ namespace PrivaPub.Federation.Actors
|
||||
// its counts are read once a day, after it was fetched (AccountCountsJob)
|
||||
async Task<ForeignAvatar> Counted(ForeignAvatar stored, CancellationToken token)
|
||||
{
|
||||
if (_jobs != default && stored is { DeletionAt: null } && Uri.TryCreate(stored.ActorURI, UriKind.Absolute, out var uri))
|
||||
await _jobs.Enqueue(Models.Jobs.JobKind.CountAccount, stored.ActorURI, uri.Host.ToLowerInvariant(),
|
||||
if (jobs != default && stored is { DeletionAt: null } && Uri.TryCreate(stored.ActorURI, UriKind.Absolute, out var uri))
|
||||
await jobs.Enqueue(Models.Jobs.JobKind.CountAccount, stored.ActorURI, uri.Host.ToLowerInvariant(),
|
||||
AccountCountsJob.DedupeKey(stored.ActorURI, DateTime.UtcNow), token);
|
||||
return stored;
|
||||
}
|
||||
@@ -116,7 +105,7 @@ namespace PrivaPub.Federation.Actors
|
||||
if (Origin.Of(keyId) == default)
|
||||
return default;
|
||||
|
||||
var cached = await _dbEntities.ForeignAvatars.Match(a => a.PublicKeyId == keyId).ExecuteFirstAsync(token);
|
||||
var cached = await dbEntities.ForeignAvatars.Match(a => a.PublicKeyId == keyId).ExecuteFirstAsync(token);
|
||||
if (cached != default && !string.IsNullOrEmpty(cached.PublicKey) && !refresh)
|
||||
return cached;
|
||||
if (!MayFetch(keyId))
|
||||
@@ -143,7 +132,7 @@ namespace PrivaPub.Federation.Actors
|
||||
return await Counted(await Upsert(actor, key, token), token);
|
||||
}
|
||||
|
||||
public bool KeyTemporarilyUnavailable(string keyId) => Origin.Of(keyId) != default && _http.FailedTemporarily(StripFragment(keyId));
|
||||
public bool KeyTemporarilyUnavailable(string keyId) => Origin.Of(keyId) != default && federationHttp.FailedTemporarily(StripFragment(keyId));
|
||||
|
||||
// whether an object's origin says it is gone: 404 or 410 to our instance actor, or a Tombstone in its place
|
||||
public async Task<bool> IsGone(string uri, CancellationToken token)
|
||||
@@ -151,8 +140,8 @@ namespace PrivaPub.Federation.Actors
|
||||
if (Origin.Of(uri) == default)
|
||||
return false;
|
||||
using var scope = HttpScope.Default("object");
|
||||
var signer = await _localActors.GetInstanceActor(token);
|
||||
var (status, fetched) = await _http.GetJsonStatus(uri, Accept, request => HttpSignatures.Sign(request, signer, body: null), token);
|
||||
var signer = await localActors.GetInstanceActor(token);
|
||||
var (status, fetched) = await federationHttp.GetJsonStatus(uri, Accept, request => HttpSignatures.Sign(request, signer, body: null), token);
|
||||
using (fetched)
|
||||
return status is StatusCodes.Status404NotFound or StatusCodes.Status410Gone || fetched != default && Text(fetched.Root, "type") == "Tombstone";
|
||||
}
|
||||
@@ -165,9 +154,9 @@ namespace PrivaPub.Federation.Actors
|
||||
|
||||
using var scope = HttpScope.For("webfinger");
|
||||
var query = $"/.well-known/webfinger?resource={Uri.EscapeDataString($"acct:{parts[0]}@{parts[1]}")}";
|
||||
using var fetched = await _http.GetJson($"https://{parts[1]}{query}", "application/jrd+json, application/json", sign: default, token)
|
||||
?? (_options?.CurrentValue.AllowPlainHttp == true
|
||||
? await _http.GetJson($"http://{parts[1]}{query}", "application/jrd+json, application/json", sign: default, token)
|
||||
using var fetched = await federationHttp.GetJson($"https://{parts[1]}{query}", "application/jrd+json, application/json", sign: default, token)
|
||||
?? (options?.CurrentValue.AllowPlainHttp == true
|
||||
? await federationHttp.GetJson($"http://{parts[1]}{query}", "application/jrd+json, application/json", sign: default, token)
|
||||
: default);
|
||||
if (fetched == default)
|
||||
return default;
|
||||
@@ -185,14 +174,14 @@ namespace PrivaPub.Federation.Actors
|
||||
}
|
||||
|
||||
async Task<FetchedJson> Get(string uri, LocalActor signer, CancellationToken token) =>
|
||||
await _http.GetJson(uri, Accept, request => HttpSignatures.Sign(request, signer, body: null), token);
|
||||
await federationHttp.GetJson(uri, Accept, request => HttpSignatures.Sign(request, signer, body: null), token);
|
||||
|
||||
bool MayFetch(string uri)
|
||||
{
|
||||
var key = "remote-actor:fetched:" + uri;
|
||||
if (_cache.TryGetValue(key, out _))
|
||||
if (cache.TryGetValue(key, out _))
|
||||
return false;
|
||||
_cache.Set(key, true, RefetchInterval);
|
||||
cache.Set(key, true, RefetchInterval);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -206,7 +195,7 @@ namespace PrivaPub.Federation.Actors
|
||||
if (named is not { Length: 2 } || named[0].Length == 0 || named[1].Length == 0 || named[1].Equals(host, StringComparison.OrdinalIgnoreCase))
|
||||
return host;
|
||||
var domain = named[1].ToLowerInvariant();
|
||||
var stored = await _dbEntities.ForeignAvatars.Match(a => a.ActorURI == actor.Id).ExecuteFirstAsync(token);
|
||||
var stored = await dbEntities.ForeignAvatars.Match(a => a.ActorURI == actor.Id).ExecuteFirstAsync(token);
|
||||
if (stored?.Domain == domain && stored.UserName == actor.PreferredUsername)
|
||||
return domain;
|
||||
return await ResolveHandle($"{named[0]}@{domain}", token) == actor.Id ? domain : host;
|
||||
|
||||
Reference in new issue
Block a user