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
@@ -33,7 +33,11 @@ namespace PrivaPub.Domain.Statuses
|
||||
Task<HashSet<int>> OwnVotes(string postId, string avatarId, CancellationToken token);
|
||||
}
|
||||
|
||||
public class PollService : IPollService
|
||||
public class PollService(
|
||||
DbEntities dbEntities,
|
||||
ILocalActorService localActors,
|
||||
IDeliveryService delivery,
|
||||
IJobQueue queue) : IPollService
|
||||
{
|
||||
public const int MaxOptions = 4;
|
||||
public const int MaxOptionLength = 50;
|
||||
@@ -41,18 +45,6 @@ namespace PrivaPub.Domain.Statuses
|
||||
public const int MaxExpiration = 2_629_746;
|
||||
static readonly TimeSpan RefreshEvery = TimeSpan.FromMinutes(3);
|
||||
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IDeliveryService _delivery;
|
||||
readonly IJobQueue _queue;
|
||||
|
||||
public PollService(DbEntities dbEntities, ILocalActorService localActors, IDeliveryService delivery, IJobQueue queue)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
_delivery = delivery;
|
||||
_queue = queue;
|
||||
}
|
||||
|
||||
public string Invalid(PollDraft draft)
|
||||
{
|
||||
@@ -81,7 +73,7 @@ namespace PrivaPub.Domain.Statuses
|
||||
{
|
||||
if (post.Poll?.ExpiresAt is not { } ends)
|
||||
return;
|
||||
await _queue.EnqueueMany(new[]
|
||||
await queue.EnqueueMany(new[]
|
||||
{
|
||||
new Job { Kind = JobKind.PollClose, Payload = post.ID, Host = LocalHost, DedupeKey = $"poll-close|{post.ID}", RunAt = ends }
|
||||
}, token);
|
||||
@@ -115,7 +107,7 @@ namespace PrivaPub.Domain.Statuses
|
||||
else
|
||||
await Refreshed(post, token);
|
||||
await Scheduled(post, token);
|
||||
return new StatusOutcome(await _dbEntities.Posts.MatchID(post.ID).ExecuteFirstAsync(token));
|
||||
return new StatusOutcome(await dbEntities.Posts.MatchID(post.ID).ExecuteFirstAsync(token));
|
||||
}
|
||||
|
||||
public async Task Receive(PostEntity post, ForeignAvatar voter, string choice, string activityId, CancellationToken token)
|
||||
@@ -164,13 +156,13 @@ namespace PrivaPub.Domain.Statuses
|
||||
|
||||
async Task SendVotes(LocalActor me, PostEntity post, IReadOnlyList<int> choices, CancellationToken token)
|
||||
{
|
||||
var owner = await _dbEntities.ForeignAvatars.Match(f => f.ActorURI == post.ActorURI).ExecuteFirstAsync(token);
|
||||
var owner = await dbEntities.ForeignAvatars.Match(f => f.ActorURI == post.ActorURI).ExecuteFirstAsync(token);
|
||||
if (string.IsNullOrEmpty(owner?.InboxURL))
|
||||
return;
|
||||
foreach (var choice in choices)
|
||||
{
|
||||
var voteId = me.Uri + $"#votes/{post.ID}/{choice}";
|
||||
await _delivery.Enqueue(me, new[] { owner.InboxURL }, new JsonObject
|
||||
await delivery.Enqueue(me, new[] { owner.InboxURL }, new JsonObject
|
||||
{
|
||||
["@context"] = ActivityPubRenderer.ActivityStreams,
|
||||
["id"] = voteId + "/activity",
|
||||
@@ -186,14 +178,14 @@ namespace PrivaPub.Domain.Statuses
|
||||
["to"] = new JsonArray(owner.ActorURI),
|
||||
["inReplyTo"] = post.ObjectURI
|
||||
}
|
||||
}, token);
|
||||
}, token: token);
|
||||
}
|
||||
}
|
||||
|
||||
async Task Refreshed(PostEntity post, CancellationToken token)
|
||||
{
|
||||
var bucket = DateTime.UtcNow.Ticks / RefreshEvery.Ticks;
|
||||
await _queue.EnqueueMany(new[]
|
||||
await queue.EnqueueMany(new[]
|
||||
{
|
||||
new Job
|
||||
{
|
||||
@@ -203,58 +195,48 @@ namespace PrivaPub.Domain.Statuses
|
||||
}, token);
|
||||
}
|
||||
|
||||
string LocalHost => new Uri(_localActors.BaseAddress).Host;
|
||||
string LocalHost => new Uri(localActors.BaseAddress).Host;
|
||||
}
|
||||
|
||||
public class PollRefreshJob : IJobHandler
|
||||
public class PollRefreshJob(
|
||||
DbEntities dbEntities,
|
||||
ILocalActorService localActors,
|
||||
IOutboxPublisher outbox) : IJobHandler
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IOutboxPublisher _outbox;
|
||||
|
||||
public PollRefreshJob(DbEntities dbEntities, ILocalActorService localActors, IOutboxPublisher outbox)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
_outbox = outbox;
|
||||
}
|
||||
|
||||
public virtual JobKind Kind => JobKind.PollRefresh;
|
||||
public int Concurrency => 1;
|
||||
public int MaxAttempts => 3;
|
||||
public int PerHostLimit => 1;
|
||||
|
||||
protected DbEntities Entities => dbEntities;
|
||||
|
||||
public virtual async Task<JobOutcome> Handle(Job job, CancellationToken token)
|
||||
{
|
||||
var post = await _dbEntities.Posts.MatchID(job.Payload).ExecuteFirstAsync(token);
|
||||
var post = await dbEntities.Posts.MatchID(job.Payload).ExecuteFirstAsync(token);
|
||||
if (post?.Poll == default || post.IsFederatedCopy || post.DeletedAt.HasValue)
|
||||
return JobOutcome.Done;
|
||||
var author = await _localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
|
||||
var author = await localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
|
||||
if (author == default)
|
||||
return JobOutcome.Done;
|
||||
await Closing(post, author, token);
|
||||
if (!post.IsLocalOnly)
|
||||
await _outbox.PublishUpdate(author, post, $"poll-{DateTime.UtcNow.Ticks}", token);
|
||||
await outbox.PublishUpdate(author, post, $"poll-{DateTime.UtcNow.Ticks}", token);
|
||||
return JobOutcome.Done;
|
||||
}
|
||||
|
||||
protected virtual Task Closing(PostEntity post, LocalActor author, CancellationToken token) => Task.CompletedTask;
|
||||
}
|
||||
|
||||
public class PollCloseJob : PollRefreshJob
|
||||
public class PollCloseJob(
|
||||
DbEntities dbEntities,
|
||||
ILocalActorService localActors,
|
||||
IOutboxPublisher outbox) : PollRefreshJob(dbEntities, localActors, outbox)
|
||||
{
|
||||
readonly DbEntities _posts;
|
||||
|
||||
public PollCloseJob(DbEntities dbEntities, ILocalActorService localActors, IOutboxPublisher outbox) : base(dbEntities, localActors, outbox)
|
||||
{
|
||||
_posts = dbEntities;
|
||||
}
|
||||
|
||||
public override JobKind Kind => JobKind.PollClose;
|
||||
|
||||
public override async Task<JobOutcome> Handle(Job job, CancellationToken token)
|
||||
{
|
||||
var post = await _posts.Posts.MatchID(job.Payload).ExecuteFirstAsync(token);
|
||||
var post = await Entities.Posts.MatchID(job.Payload).ExecuteFirstAsync(token);
|
||||
if (post?.Poll == default || post.DeletedAt.HasValue)
|
||||
return JobOutcome.Done;
|
||||
if (!post.IsFederatedCopy)
|
||||
@@ -274,9 +256,9 @@ namespace PrivaPub.Domain.Statuses
|
||||
{
|
||||
var voters = await DB.Default.Find<PollVote>().Match(v => v.PostId == post.ID && v.IsLocalVoter).ExecuteAsync(token);
|
||||
foreach (var voter in voters.Select(v => v.VoterAccountId).Distinct())
|
||||
await Notifications.Add(voter, NotificationType.Poll, authorAccountId, authorUri, post.ID, token);
|
||||
await Notifications.Add(voter, NotificationType.Poll, authorAccountId, authorUri, post.ID, token: token);
|
||||
if (!post.IsFederatedCopy)
|
||||
await Notifications.Add(post.GroupUserId, NotificationType.Poll, post.GroupUserId, authorUri, post.ID, token);
|
||||
await Notifications.Add(post.GroupUserId, NotificationType.Poll, post.GroupUserId, authorUri, post.ID, token: token);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user