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
@@ -40,26 +40,14 @@ namespace PrivaPub.Domain.Statuses
|
||||
AskFirst
|
||||
}
|
||||
|
||||
public class QuoteService : IQuoteService
|
||||
public class QuoteService(
|
||||
DbEntities dbEntities,
|
||||
IRemoteActorService remoteActors,
|
||||
IRemotePosts remotePosts,
|
||||
ILocalActorService localActors,
|
||||
IDeliveryService delivery,
|
||||
IOutboxPublisher outbox) : IQuoteService
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly IRemoteActorService _remoteActors;
|
||||
readonly IRemotePosts _remotePosts;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IDeliveryService _delivery;
|
||||
readonly IOutboxPublisher _outbox;
|
||||
|
||||
public QuoteService(DbEntities dbEntities, IRemoteActorService remoteActors, IRemotePosts remotePosts, ILocalActorService localActors,
|
||||
IDeliveryService delivery, IOutboxPublisher outbox)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_remoteActors = remoteActors;
|
||||
_remotePosts = remotePosts;
|
||||
_localActors = localActors;
|
||||
_delivery = delivery;
|
||||
_outbox = outbox;
|
||||
}
|
||||
|
||||
const string RequestPrefix = "quote-request-";
|
||||
|
||||
const string LicencePath = "/parrot-licences/";
|
||||
@@ -68,7 +56,7 @@ namespace PrivaPub.Domain.Statuses
|
||||
|
||||
async Task<bool> MayQuote(PostEntity quoted, string quoterUri, CancellationToken token)
|
||||
{
|
||||
var author = await _localActors.FindById(LocalActorKind.Person, quoted.GroupUserId, token);
|
||||
var author = await localActors.FindById(LocalActorKind.Person, quoted.GroupUserId, token);
|
||||
if (author == default)
|
||||
return false;
|
||||
if (quoterUri == author.Uri)
|
||||
@@ -83,11 +71,11 @@ namespace PrivaPub.Domain.Statuses
|
||||
|
||||
async Task<bool> Follows(string followerUri, LocalActor author, CancellationToken token)
|
||||
{
|
||||
if (await _dbEntities.Followers.Match(f => f.LocalActorId == author.Id && f.ActorURI == followerUri && f.IsAccepted).ExecuteAnyAsync(token))
|
||||
if (await dbEntities.Followers.Match(f => f.LocalActorId == author.Id && f.ActorURI == followerUri && f.IsAccepted).ExecuteAnyAsync(token))
|
||||
return true;
|
||||
var local = await _localActors.FindByUri(followerUri, token);
|
||||
var local = await localActors.FindByUri(followerUri, token);
|
||||
return local != default
|
||||
&& await _dbEntities.Followings.Match(f => f.AvatarId == local.Id && f.TargetActorURI == author.Uri && f.State == FollowState.Accepted).ExecuteAnyAsync(token);
|
||||
&& await dbEntities.Followings.Match(f => f.AvatarId == local.Id && f.TargetActorURI == author.Uri && f.State == FollowState.Accepted).ExecuteAnyAsync(token);
|
||||
}
|
||||
|
||||
public async Task<string> Grant(LocalActor author, PostEntity quoted, string quotingUri, string quoterUri, CancellationToken token)
|
||||
@@ -111,8 +99,8 @@ namespace PrivaPub.Domain.Statuses
|
||||
Arrival.Drop("misattributed");
|
||||
return;
|
||||
}
|
||||
var quoted = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
var author = quoted == default ? default : await _localActors.FindById(LocalActorKind.Person, quoted.GroupUserId, token);
|
||||
var quoted = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
var author = quoted == default ? default : await localActors.FindById(LocalActorKind.Person, quoted.GroupUserId, token);
|
||||
if (author == default || string.IsNullOrEmpty(actor.InboxURL))
|
||||
{
|
||||
Arrival.Drop("unknown-object");
|
||||
@@ -139,22 +127,22 @@ namespace PrivaPub.Domain.Statuses
|
||||
answer["type"] = "Reject";
|
||||
answer["id"] = author.ActivityUri($"reject-quote-{Guid.NewGuid():N}");
|
||||
}
|
||||
await _delivery.Enqueue(author, new[] { actor.InboxURL }, answer, token);
|
||||
await delivery.Enqueue(author, new[] { actor.InboxURL }, answer, token: token);
|
||||
}
|
||||
|
||||
public async Task<bool> RevokeLicence(PostEntity quoted, PostEntity quoting, CancellationToken token)
|
||||
{
|
||||
var licence = await DB.Default.Find<QuoteLicence>().Match(l => l.PostId == quoted.ID && l.QuotingObjectURI == quoting.ObjectURI && l.RevokedAt == null).ExecuteFirstAsync(token);
|
||||
var author = licence == default ? default : await _localActors.FindById(LocalActorKind.Person, quoted.GroupUserId, token);
|
||||
var author = licence == default ? default : await localActors.FindById(LocalActorKind.Person, quoted.GroupUserId, token);
|
||||
if (author == default)
|
||||
return false;
|
||||
await DB.Default.Update<QuoteLicence>().MatchID(licence.ID).Modify(l => l.RevokedAt, DateTime.UtcNow).ExecuteAsync(token);
|
||||
var uri = LicenceUri(author, licence.ID);
|
||||
await Revoke(uri, token);
|
||||
var quoter = quoting.IsFederatedCopy ? await _dbEntities.ForeignAvatars.Match(f => f.ActorURI == quoting.ActorURI).ExecuteFirstAsync(token) : default;
|
||||
var inboxes = (await _delivery.FollowerInboxes(author, token)).Append(quoter?.InboxURL).Where(i => !string.IsNullOrEmpty(i)).Distinct().ToList();
|
||||
var quoter = quoting.IsFederatedCopy ? await dbEntities.ForeignAvatars.Match(f => f.ActorURI == quoting.ActorURI).ExecuteFirstAsync(token) : default;
|
||||
var inboxes = (await delivery.FollowerInboxes(author, token)).Append(quoter?.InboxURL).Where(i => !string.IsNullOrEmpty(i)).Distinct().ToList();
|
||||
if (inboxes.Count > 0)
|
||||
await _delivery.Enqueue(author, inboxes, new JsonObject
|
||||
await delivery.Enqueue(author, inboxes, new JsonObject
|
||||
{
|
||||
["@context"] = ActivityPubRenderer.Context(),
|
||||
["id"] = author.ActivityUri($"revoke-quote-{licence.ID}"),
|
||||
@@ -162,14 +150,14 @@ namespace PrivaPub.Domain.Statuses
|
||||
["actor"] = author.Uri,
|
||||
["object"] = uri,
|
||||
["to"] = new JsonArray(Addressing.Public)
|
||||
}, token);
|
||||
}, token: token);
|
||||
return true;
|
||||
}
|
||||
|
||||
async Task<bool> OurLicence(string stampUri, PostEntity quoting, PostEntity quoted, CancellationToken token)
|
||||
{
|
||||
var marker = stampUri?.LastIndexOf(LicencePath, StringComparison.Ordinal) ?? -1;
|
||||
if (marker < 0 || !stampUri.StartsWith(_localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|
||||
if (marker < 0 || !stampUri.StartsWith(localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
var id = stampUri[(marker + LicencePath.Length)..];
|
||||
return await DB.Default.Find<QuoteLicence>().Match(l => l.ID == id && l.PostId == quoted.ID && l.QuotingObjectURI == quoting.ObjectURI && l.RevokedAt == null)
|
||||
@@ -191,10 +179,10 @@ namespace PrivaPub.Domain.Statuses
|
||||
|
||||
public async Task Request(LocalActor author, PostEntity post, PostEntity quoted, JsonObject note, CancellationToken token)
|
||||
{
|
||||
var owner = await _dbEntities.ForeignAvatars.Match(f => f.ActorURI == quoted.ActorURI).ExecuteFirstAsync(token);
|
||||
var owner = await dbEntities.ForeignAvatars.Match(f => f.ActorURI == quoted.ActorURI).ExecuteFirstAsync(token);
|
||||
if (string.IsNullOrEmpty(owner?.InboxURL))
|
||||
return;
|
||||
await _delivery.Enqueue(author, new[] { owner.InboxURL }, new JsonObject
|
||||
await delivery.Enqueue(author, new[] { owner.InboxURL }, new JsonObject
|
||||
{
|
||||
["@context"] = ActivityPubRenderer.Context(),
|
||||
["id"] = author.ActivityUri(RequestPrefix + post.ID),
|
||||
@@ -203,7 +191,7 @@ namespace PrivaPub.Domain.Statuses
|
||||
["object"] = quoted.ObjectURI,
|
||||
["instrument"] = note.DeepClone(),
|
||||
["to"] = new JsonArray(quoted.ActorURI)
|
||||
}, token);
|
||||
}, token: token);
|
||||
}
|
||||
|
||||
public async Task<bool> Answered(JsonNode answer, ForeignAvatar actor, bool accepted, CancellationToken token)
|
||||
@@ -216,11 +204,11 @@ namespace PrivaPub.Domain.Statuses
|
||||
if (marker < 0)
|
||||
return true;
|
||||
var postId = requestId[(marker + "/grunts/".Length + RequestPrefix.Length)..];
|
||||
var post = await _dbEntities.Posts.Match(p => p.ID == postId && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
var author = post == default ? default : await _localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
|
||||
var post = await dbEntities.Posts.Match(p => p.ID == postId && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
var author = post == default ? default : await localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
|
||||
if (author == default || author.ActivityUri(RequestPrefix + post.ID) != requestId || post.QuoteState != QuoteState.Pending)
|
||||
return true;
|
||||
var quoted = await _dbEntities.Posts.MatchID(post.QuotedPostId).ExecuteFirstAsync(token);
|
||||
var quoted = await dbEntities.Posts.MatchID(post.QuotedPostId).ExecuteFirstAsync(token);
|
||||
if (quoted?.ActorURI != actor.ActorURI)
|
||||
return true;
|
||||
if (!accepted)
|
||||
@@ -239,7 +227,7 @@ namespace PrivaPub.Domain.Statuses
|
||||
.ExecuteAsync(token);
|
||||
await DB.Default.Update<PostEntity>().MatchID(quoted.ID).Modify(b => b.Inc(p => p.QuotesCount, 1)).ExecuteAsync(token);
|
||||
if (!post.IsLocalOnly)
|
||||
await _outbox.PublishUpdate(author, post, $"quote-approved-{DateTime.UtcNow.Ticks}", token);
|
||||
await outbox.PublishUpdate(author, post, $"quote-approved-{DateTime.UtcNow.Ticks}", token);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -249,8 +237,8 @@ namespace PrivaPub.Domain.Statuses
|
||||
return;
|
||||
var quoted = note.QuoteUri == default
|
||||
? default
|
||||
: await _dbEntities.Posts.Match(p => p.ObjectURI == note.QuoteUri && !p.DeletedAt.HasValue).ExecuteFirstAsync(token)
|
||||
?? await _remotePosts.StoreContext(note.QuoteUri, RemotePosts.MaxDepth, token);
|
||||
: await dbEntities.Posts.Match(p => p.ObjectURI == note.QuoteUri && !p.DeletedAt.HasValue).ExecuteFirstAsync(token)
|
||||
?? await remotePosts.StoreContext(note.QuoteUri, RemotePosts.MaxDepth, token);
|
||||
|
||||
var state = note.QuoteDeleted ? QuoteState.Deleted
|
||||
: quoted == default ? QuoteState.Pending
|
||||
@@ -281,13 +269,13 @@ namespace PrivaPub.Domain.Statuses
|
||||
{
|
||||
await DB.Default.Update<PostEntity>().MatchID(nowCounted).Modify(b => b.Inc(p => p.QuotesCount, 1)).ExecuteAsync(token);
|
||||
if (!quoted.IsFederatedCopy)
|
||||
await Notifications.Add(quoted.GroupUserId, NotificationType.Quote, post.AuthorAccountId, post.ActorURI, post.ID, token);
|
||||
await Notifications.Add(quoted.GroupUserId, NotificationType.Quote, post.AuthorAccountId, post.ActorURI, post.ID, token: token);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Revoke(string stampUri, CancellationToken token)
|
||||
{
|
||||
var revoked = await _dbEntities.Posts.Match(p => p.QuoteAuthorizationURI == stampUri && p.QuoteState == QuoteState.Accepted).ExecuteAsync(token);
|
||||
var revoked = await dbEntities.Posts.Match(p => p.QuoteAuthorizationURI == stampUri && p.QuoteState == QuoteState.Accepted).ExecuteAsync(token);
|
||||
foreach (var post in revoked)
|
||||
{
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(p => p.QuoteState, QuoteState.Revoked).ExecuteAsync(token);
|
||||
@@ -300,7 +288,7 @@ namespace PrivaPub.Domain.Statuses
|
||||
{
|
||||
if (!Origin.Same(stampUri, quoted.ActorURI))
|
||||
return false;
|
||||
using var fetched = await _remoteActors.FetchObject(stampUri, token);
|
||||
using var fetched = await remoteActors.FetchObject(stampUri, token);
|
||||
if (fetched == default)
|
||||
return false;
|
||||
var stamp = JsonNode.Parse(fetched.Root.GetRawText());
|
||||
|
||||
Reference in new issue
Block a user