House rules: primary constructors, a cancellation token on every async call, and the rules fail the build
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:
thepraandClaude Opus 5.5 committed 2026-11-10 01:24:43 +01:00
1 parent f0a14c737f
commit 0f73f69239
176 files changed
+2789 -3974

No files matched your search

+29 -46
View File
@@ -25,24 +25,17 @@ namespace PrivaPub.Federation.Outbox
{
// again: a delivery made once more on purpose, which an earlier one of the same activity does not stop; deliveries
// with the same again are made once
Task Enqueue(LocalActor signer, IEnumerable<string> inboxes, JsonObject activity, CancellationToken token, string again = default);
Task EnqueueToFollowers(LocalActor signer, JsonObject activity, CancellationToken token, IEnumerable<string> extraInboxes = default);
Task Enqueue(LocalActor signer, IEnumerable<string> inboxes, JsonObject activity, string again = default, CancellationToken token = default);
Task EnqueueToFollowers(LocalActor signer, JsonObject activity, IEnumerable<string> extraInboxes = default, CancellationToken token = default);
Task<IReadOnlyList<string>> FollowerInboxes(LocalActor actor, CancellationToken token);
}
public sealed record DeliveryPayload(string SignerId, LocalActorKind SignerKind, string Inbox, string Body);
public class DeliveryService : IDeliveryService
public class DeliveryService(
DbEntities dbEntities,
IJobQueue queue) : IDeliveryService
{
readonly DbEntities _dbEntities;
readonly IJobQueue _queue;
public DeliveryService(DbEntities dbEntities, IJobQueue queue)
{
_dbEntities = dbEntities;
_queue = queue;
}
// what the staff's moderation holds back (completion program R6): a suspended persona (by itself or with its banned
// login) delivers nothing, but the Delete of itself, which its deletion sends; and nothing goes to the own inbox of an
// account elsewhere the staff suspended (its server's shared inbox still serves the others there)
@@ -52,7 +45,7 @@ namespace PrivaPub.Federation.Outbox
static bool DeletesItself(LocalActor signer, JsonNode activity) =>
ActivityJson.Value(activity, "type") == "Delete" && ActivityJson.Id(activity?["object"]) == signer.Uri;
public async Task Enqueue(LocalActor signer, IEnumerable<string> inboxes, JsonObject activity, CancellationToken token, string again = default)
public async Task Enqueue(LocalActor signer, IEnumerable<string> inboxes, JsonObject activity, string again = default, CancellationToken token = default)
{
if (HeldBack(signer, activity))
return;
@@ -90,18 +83,18 @@ namespace PrivaPub.Federation.Outbox
})
.ToList();
if (jobs.Count > 0)
await _queue.EnqueueMany(jobs, token);
await queue.EnqueueMany(jobs, token);
}
public async Task EnqueueToFollowers(LocalActor signer, JsonObject activity, CancellationToken token, IEnumerable<string> extraInboxes = default)
public async Task EnqueueToFollowers(LocalActor signer, JsonObject activity, IEnumerable<string> extraInboxes = default, CancellationToken token = default)
{
var inboxes = (await FollowerInboxes(signer, token)).Concat(extraInboxes ?? Enumerable.Empty<string>());
await Enqueue(signer, inboxes, activity, token);
await Enqueue(signer, inboxes, activity, token: token);
}
public async Task<IReadOnlyList<string>> FollowerInboxes(LocalActor actor, CancellationToken token)
{
var followers = await _dbEntities.Followers
var followers = await dbEntities.Followers
.Match(f => f.LocalActorId == actor.Id && f.LocalActorKind == actor.Kind && f.IsAccepted)
.ExecuteAsync(token);
return followers.Select(f => string.IsNullOrEmpty(f.SharedInboxURL) ? f.InboxURL : f.SharedInboxURL)
@@ -110,26 +103,16 @@ namespace PrivaPub.Federation.Outbox
}
}
public class DeliveryJobHandler : IJobHandler
public class DeliveryJobHandler(
ILocalActorService actors,
IFederationHttp http,
IHostCircuitBreaker breaker,
ILogger<DeliveryJobHandler> logger,
IInteractionLedger ledger = default,
Microsoft.Extensions.Options.IOptionsMonitor<Infrastructure.Http.FederationOptions> federation = default) : IJobHandler
{
readonly ILocalActorService _actors;
readonly IFederationHttp _http;
readonly IHostCircuitBreaker _breaker;
readonly ILogger<DeliveryJobHandler> _logger;
readonly IInteractionLedger _ledger;
readonly int _concurrency = Math.Max(1, federation?.CurrentValue.DeliveryConcurrency ?? 8);
readonly int _concurrency;
public DeliveryJobHandler(ILocalActorService actors, IFederationHttp http, IHostCircuitBreaker breaker, ILogger<DeliveryJobHandler> logger,
IInteractionLedger ledger = default, Microsoft.Extensions.Options.IOptions<Infrastructure.Http.FederationOptions> federation = default)
{
_concurrency = Math.Max(1, federation?.Value.DeliveryConcurrency ?? 8);
_actors = actors;
_http = http;
_breaker = breaker;
_logger = logger;
_ledger = ledger;
}
sealed class Attempt
{
@@ -162,10 +145,10 @@ namespace PrivaPub.Federation.Outbox
void Record(Job job, Attempt attempt, JobOutcome outcome)
{
if (_ledger == default)
if (ledger == default)
return;
var shape = attempt.Payload == default ? default : ActivityShape.Of(attempt.Payload.Body);
_ledger.Record(new InteractionEvent
ledger.Record(new InteractionEvent
{
Channel = Interactions.Out,
Host = job.Host,
@@ -215,20 +198,20 @@ namespace PrivaPub.Federation.Outbox
{
var payload = JsonSerializer.Deserialize<DeliveryPayload>(job.Payload);
attempt.Payload = payload;
if (payload == default || !Uri.TryCreate(payload.Inbox, UriKind.Absolute, out var inbox) || !_http.IsAllowed(inbox))
if (payload == default || !Uri.TryCreate(payload.Inbox, UriKind.Absolute, out var inbox) || !http.IsAllowed(inbox))
{
attempt.Reason = "not-deliverable";
return JobOutcome.Dead("not a deliverable inbox");
}
var unavailableUntil = await _breaker.UnavailableUntil(job.Host, token);
var unavailableUntil = await breaker.UnavailableUntil(job.Host, token);
if (unavailableUntil.HasValue)
{
attempt.Reason = "host-unavailable";
return JobOutcome.Defer(unavailableUntil.Value, "the host is unavailable");
}
var signer = await _actors.FindById(payload.SignerKind, payload.SignerId, token);
var signer = await actors.FindById(payload.SignerKind, payload.SignerId, token);
attempt.Signer = signer;
if (signer == default)
{
@@ -255,14 +238,14 @@ namespace PrivaPub.Federation.Outbox
var started = Stopwatch.GetTimestamp();
try
{
using var response = await _http.Send(request, token);
using var response = await http.Send(request, token);
attempt.LatencyMs = (int)Stopwatch.GetElapsedTime(started).TotalMilliseconds;
var status = (int)response.StatusCode;
attempt.Status = status;
attempt.Reason = response.IsSuccessStatusCode ? default : status.ToString(System.Globalization.CultureInfo.InvariantCulture);
if (response.IsSuccessStatusCode)
{
await _breaker.Succeeded(job.Host, token);
await breaker.Succeeded(job.Host, token);
return JobOutcome.Done;
}
if (response.StatusCode == HttpStatusCode.TooManyRequests
@@ -280,15 +263,15 @@ namespace PrivaPub.Federation.Outbox
return JobOutcome.Retry($"{status} {response.ReasonPhrase}");
if (status is >= 400 and < 500 && status != 408)
{
await _breaker.Succeeded(job.Host, token);
_logger.LogInformation("Delivery to {Inbox} refused with {Status}", payload.Inbox, status);
await breaker.Succeeded(job.Host, token);
logger.LogInformation("Delivery to {Inbox} refused with {Status}", payload.Inbox, status);
return JobOutcome.Dead($"{status} {response.ReasonPhrase}");
}
// a gateway's 502, 503 or 504 says the server behind it is down, which counts against the host; any other 5xx
// says the server is up and failed on this activity (Smithereen's 500 on a document its JSON-LD reader
// refused), retried on its own while the host's other deliveries go on
if (status is 502 or 503 or 504)
await _breaker.Failed(job.Host, $"{status}", token);
await breaker.Failed(job.Host, $"{status}", token);
return JobOutcome.Retry($"{status} {response.ReasonPhrase}");
}
catch (BlockedDestinationException ex)
@@ -300,7 +283,7 @@ namespace PrivaPub.Federation.Outbox
{
attempt.LatencyMs = (int)Stopwatch.GetElapsedTime(started).TotalMilliseconds;
attempt.Reason = ex is TaskCanceledException ? "timeout" : "network";
await _breaker.Failed(job.Host, ex.GetType().Name, token);
await breaker.Failed(job.Host, ex.GetType().Name, token);
return JobOutcome.Retry(ex.Message);
}
}
@@ -16,7 +16,8 @@ namespace PrivaPub.Federation.Outbox
Task Announce(LocalActor group, JsonObject activity, string objectUri, bool isNewPost, CancellationToken token);
}
public class GroupDistributor : IGroupDistributor
public class GroupDistributor(
IDeliveryService delivery) : IGroupDistributor
{
// so the announce's id resolves (PeasantsController.Grunt); a repeat of the same announce keeps the first
// a remote author's server's inbox (its shared one when it has one); none for our own personas
@@ -48,12 +49,6 @@ namespace PrivaPub.Federation.Outbox
}
}
readonly IDeliveryService _delivery;
public GroupDistributor(IDeliveryService delivery)
{
_delivery = delivery;
}
public async Task Announce(LocalActor group, JsonObject activity, string objectUri, bool isNewPost, CancellationToken token)
{
@@ -78,14 +73,14 @@ namespace PrivaPub.Federation.Outbox
// its user's post pending until the community announces it back, and clears that before it answers the echo 400
// ("Object is not remote").
var authorsServer = await AuthorsInbox(activity, token);
await _delivery.EnqueueToFollowers(group, announce, token, authorsServer);
await delivery.EnqueueToFollowers(group, announce, authorsServer, token);
await Keep(group, announce, objectUri, token);
// a new post or comment is also boosted as itself: microblogs (Mastodon, Akkoma, Misskey) drop an Announce of an
// activity, so without it their members never see what is said in the community; Lemmy answers it 400, harmlessly
if (isNewPost && !string.IsNullOrEmpty(objectUri))
{
var boost = ActivityPubRenderer.Announce(group, objectUri, $"announce-object-{key}");
await _delivery.EnqueueToFollowers(group, boost, token, authorsServer);
await delivery.EnqueueToFollowers(group, boost, authorsServer, token);
await Keep(group, boost, objectUri, token);
}
}
+19 -28
View File
@@ -23,21 +23,12 @@ namespace PrivaPub.Federation.Outbox
Task PublishFeatured(string personaId, PostEntity post, bool featured, CancellationToken token);
}
public class OutboxPublisher : IOutboxPublisher
public class OutboxPublisher(
DbEntities dbEntities,
ILocalActorService localActors,
IDeliveryService delivery,
Relays.IRelays relays = default) : IOutboxPublisher
{
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly IDeliveryService _delivery;
readonly Relays.IRelays _relays;
public OutboxPublisher(DbEntities dbEntities, ILocalActorService localActors, IDeliveryService delivery, Relays.IRelays relays = default)
{
_relays = relays;
_dbEntities = dbEntities;
_localActors = localActors;
_delivery = delivery;
}
public async Task<IReadOnlyList<string>> Audience(LocalActor author, PostEntity post, CancellationToken token)
{
if (post.Visibility == PostVisibility.LocalGeo || post.IsLocalOnly)
@@ -47,7 +38,7 @@ namespace PrivaPub.Federation.Outbox
var inboxes = new List<string>();
if (post.Visibility is PostVisibility.Public or PostVisibility.Unlisted or PostVisibility.FollowersOnly)
inboxes.AddRange(await _delivery.FollowerInboxes(author, token));
inboxes.AddRange(await delivery.FollowerInboxes(author, token));
//nothing is addressed to an account that blocked the author (it would refuse it, and Mastodon counts on that)
var blockers = (await DB.Default.Find<BlockedBy>().Match(b => b.AvatarId == author.Id).ExecuteAsync(token))
@@ -61,23 +52,23 @@ namespace PrivaPub.Federation.Outbox
.ToList();
foreach (var uri in addressed)
{
var actor = await _dbEntities.ForeignAvatars.Match(a => a.ActorURI == uri).ExecuteFirstAsync(token);
var actor = await dbEntities.ForeignAvatars.Match(a => a.ActorURI == uri).ExecuteFirstAsync(token);
if (Preferred(actor) is { } inbox)
inboxes.Add(inbox);
}
if (post.Visibility != PostVisibility.Direct && !string.IsNullOrEmpty(post.InReplyToAccountId))
{
var parentAuthor = await _dbEntities.ForeignAvatars.MatchID(post.InReplyToAccountId).ExecuteFirstAsync(token);
var parentAuthor = await dbEntities.ForeignAvatars.MatchID(post.InReplyToAccountId).ExecuteFirstAsync(token);
if (Preferred(parentAuthor) is { } inbox && !blockers.Contains(parentAuthor.ActorURI) && !parentAuthor.SuspendedAt.HasValue)
inboxes.Add(inbox);
}
if (post.Visibility is PostVisibility.Public or PostVisibility.Unlisted && !string.IsNullOrEmpty(post.QuotedPostId))
{
var quoted = await _dbEntities.Posts.MatchID(post.QuotedPostId).ExecuteFirstAsync(token);
var quoted = await dbEntities.Posts.MatchID(post.QuotedPostId).ExecuteFirstAsync(token);
var quotedAuthor = quoted is { IsFederatedCopy: true }
? await _dbEntities.ForeignAvatars.Match(a => a.ActorURI == quoted.ActorURI).ExecuteFirstAsync(token)
? await dbEntities.ForeignAvatars.Match(a => a.ActorURI == quoted.ActorURI).ExecuteFirstAsync(token)
: default;
if (Preferred(quotedAuthor) is { } inbox && !quotedAuthor.SuspendedAt.HasValue)
inboxes.Add(inbox);
@@ -86,9 +77,9 @@ namespace PrivaPub.Federation.Outbox
// a persona's own public post, outside any group, also goes to the relays we subscribe to (owner decision
// 2026-10-06), its edits and deletion with it; a silenced persona's never does (R6): the relays are where strangers
// read
if (_relays != default && post.Visibility == PostVisibility.Public && author.Kind == LocalActorKind.Person
if (relays != default && post.Visibility == PostVisibility.Public && author.Kind == LocalActorKind.Person
&& string.IsNullOrEmpty(post.ReblogOfPostId) && string.IsNullOrEmpty(post.GroupId) && !post.AuthorLimited && !author.Silenced)
inboxes.AddRange(await _relays.Inboxes(token));
inboxes.AddRange(await relays.Inboxes(token));
return inboxes.Where(i => !string.IsNullOrEmpty(i)).Distinct(StringComparer.Ordinal).ToList();
}
@@ -104,13 +95,13 @@ namespace PrivaPub.Federation.Outbox
async Task<IReadOnlyList<ForeignAvatar>> CircleRecipients(string groupId, CancellationToken token)
{
var circle = string.IsNullOrEmpty(groupId) ? default : await _dbEntities.Groups.MatchID(groupId).ExecuteFirstAsync(token);
var circle = string.IsNullOrEmpty(groupId) ? default : await dbEntities.Groups.MatchID(groupId).ExecuteFirstAsync(token);
if (circle == default)
return Array.Empty<ForeignAvatar>();
var remote = circle.Members.Where(m => m.IsForeign).Select(m => m.AvatarId).ToList();
if (remote.Count == 0)
return Array.Empty<ForeignAvatar>();
return (await _dbEntities.ForeignAvatars.Match(a => remote.Contains(a.ActorURI)).ExecuteAsync(token))
return (await dbEntities.ForeignAvatars.Match(a => remote.Contains(a.ActorURI)).ExecuteAsync(token))
.Where(a => !string.IsNullOrEmpty(a.InboxURL))
.ToList();
}
@@ -120,19 +111,19 @@ namespace PrivaPub.Federation.Outbox
if (post.Visibility == PostVisibility.Circle)
{
foreach (var member in await CircleRecipients(post.GroupId, token))
await _delivery.Enqueue(author, new[] { member.InboxURL }, Naming(activity, new[] { member }), token);
await delivery.Enqueue(author, new[] { member.InboxURL }, Naming(activity, new[] { member }), token: token);
return;
}
var inboxes = await Audience(author, post, token);
if (inboxes.Count > 0)
await _delivery.Enqueue(author, inboxes, activity, token);
await delivery.Enqueue(author, inboxes, activity, token: token);
}
// a persona pins its post or unpins it: Add or Remove on its featured collection (/trophies, which serves the same
// pins), told to the post's audience as Mastodon tells it
public async Task PublishFeatured(string personaId, PostEntity post, bool featured, CancellationToken token)
{
var author = await _localActors.FindById(LocalActorKind.Person, personaId, token);
var author = await localActors.FindById(LocalActorKind.Person, personaId, token);
if (author is not { IsFederated: true } || post.IsLocalOnly)
return;
await Publish(author, post, new JsonObject
@@ -150,7 +141,7 @@ namespace PrivaPub.Federation.Outbox
public async Task PublishUpdate(LocalActor author, PostEntity post, string reason, CancellationToken token)
{
var group = string.IsNullOrEmpty(post.GroupId) ? default : await _localActors.FindById(LocalActorKind.Group, post.GroupId, token);
var group = string.IsNullOrEmpty(post.GroupId) ? default : await localActors.FindById(LocalActorKind.Group, post.GroupId, token);
await Publish(author, post, ActivityPubRenderer.UpdateOf(post, author, group, reason), token);
}
@@ -206,7 +197,7 @@ namespace PrivaPub.Federation.Outbox
["cc"] = new JsonArray(actor.Followers),
["object"] = document
};
await _delivery.EnqueueToFollowers(actor, update, token);
await delivery.EnqueueToFollowers(actor, update, token: token);
}
}
}