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
@@ -22,40 +22,23 @@ using PostEntity = PrivaPub.Models.Post.Post;
|
||||
|
||||
namespace PrivaPub.Federation.Inbox.Handlers
|
||||
{
|
||||
public class AnnounceHandler : IActivityHandler
|
||||
public class AnnounceHandler(
|
||||
DbEntities dbEntities,
|
||||
ILocalActorService localActors,
|
||||
IRemotePosts remotePosts,
|
||||
IFanout fanout,
|
||||
IRemoteActorService remoteActors,
|
||||
IObjectRecords records,
|
||||
IQuoteService quotes,
|
||||
IServiceProvider services = default,
|
||||
PrivaPub.Federation.Relays.IRelays relays = default,
|
||||
PrivaPub.Federation.Moderation.IDomainBlocks domainBlocks = default) : IActivityHandler
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly ILocalActorService _localActors;
|
||||
readonly IRemotePosts _remotePosts;
|
||||
readonly IFanout _fanout;
|
||||
readonly IRemoteActorService _remoteActors;
|
||||
|
||||
readonly IObjectRecords _records;
|
||||
readonly IQuoteService _quotes;
|
||||
readonly IServiceProvider _services;
|
||||
|
||||
// the handlers a community's relayed votes go to (Like, Dislike, Undo); resolved when first needed, since this
|
||||
// handler is one of them; tests set it
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("HouseRules", "HR0006", Justification = "Null until first needed: this handler is one of the relayed-vote handlers, so they cannot be injected into it.")]
|
||||
public IEnumerable<IActivityHandler> Relays { get; set; }
|
||||
|
||||
readonly PrivaPub.Federation.Relays.IRelays _relayService;
|
||||
readonly PrivaPub.Federation.Moderation.IDomainBlocks _domainBlocks;
|
||||
|
||||
public AnnounceHandler(DbEntities dbEntities, ILocalActorService localActors, IRemotePosts remotePosts, IFanout fanout, IRemoteActorService remoteActors,
|
||||
IObjectRecords records, IQuoteService quotes, IServiceProvider services = default, PrivaPub.Federation.Relays.IRelays relays = default,
|
||||
PrivaPub.Federation.Moderation.IDomainBlocks domainBlocks = default)
|
||||
{
|
||||
_domainBlocks = domainBlocks;
|
||||
_relayService = relays;
|
||||
_services = services;
|
||||
_records = records;
|
||||
_quotes = quotes;
|
||||
_remoteActors = remoteActors;
|
||||
_dbEntities = dbEntities;
|
||||
_localActors = localActors;
|
||||
_remotePosts = remotePosts;
|
||||
_fanout = fanout;
|
||||
}
|
||||
|
||||
public string Type => "Announce";
|
||||
|
||||
@@ -74,27 +57,27 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
Arrival.Drop("unparseable");
|
||||
return;
|
||||
}
|
||||
if (await _dbEntities.Posts.Match(p => p.ObjectURI == announceId).ExecuteAnyAsync(token))
|
||||
if (await dbEntities.Posts.Match(p => p.ObjectURI == announceId).ExecuteAnyAsync(token))
|
||||
{
|
||||
Arrival.Drop("duplicate");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_relayService != default && await _relayService.Passes(actor.ActorURI, token))
|
||||
if (relays != default && await relays.Passes(actor.ActorURI, token))
|
||||
{
|
||||
await FromRelay(objectUri, token);
|
||||
return;
|
||||
}
|
||||
var isLocal = objectUri.StartsWith(_localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase);
|
||||
var original = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
|
||||
var followed = await _dbEntities.Followings.Match(f => f.TargetActorURI == actor.ActorURI && f.State == FollowState.Accepted).ExecuteAnyAsync(token);
|
||||
var isLocal = objectUri.StartsWith(localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase);
|
||||
var original = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
|
||||
var followed = await dbEntities.Followings.Match(f => f.TargetActorURI == actor.ActorURI && f.State == FollowState.Accepted).ExecuteAnyAsync(token);
|
||||
if (original is not { IsFederatedCopy: false } && !followed)
|
||||
{
|
||||
Arrival.Drop("not-followed");
|
||||
return;
|
||||
}
|
||||
if (original == default && !isLocal)
|
||||
original = await _remotePosts.StoreContext(objectUri, 0, token);
|
||||
original = await remotePosts.StoreContext(objectUri, 0, token);
|
||||
if (original == default)
|
||||
{
|
||||
Arrival.Drop("fetch-failed");
|
||||
@@ -125,7 +108,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
Cc = cc,
|
||||
IsFederatedCopy = true,
|
||||
AuthorSuspended = actor.SuspendedAt.HasValue,
|
||||
AuthorLimited = RemotePosts.Limited(actor, _domainBlocks),
|
||||
AuthorLimited = RemotePosts.Limited(actor, domainBlocks),
|
||||
CreationDate = published,
|
||||
UpdateDate = published
|
||||
};
|
||||
@@ -144,20 +127,20 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
return;
|
||||
}
|
||||
Arrival.Accept("stored");
|
||||
Arrival.About(local: original.IsFederatedCopy ? default : await _localActors.FindById(Models.Federation.LocalActorKind.Person, original.GroupUserId, token));
|
||||
Arrival.About(local: original.IsFederatedCopy ? default : await localActors.FindById(Models.Federation.LocalActorKind.Person, original.GroupUserId, token));
|
||||
|
||||
await DB.Default.Update<PostEntity>().MatchID(original.ID).Modify(b => b.Inc(p => p.ReblogsCount, 1)).ExecuteAsync(token);
|
||||
if (!original.IsFederatedCopy)
|
||||
await Notifications.Add(original.GroupUserId, NotificationType.Reblog, actor.ID, actor.ActorURI, original.ID, token);
|
||||
await _fanout.Distribute(reblog, token);
|
||||
await Notifications.Add(original.GroupUserId, NotificationType.Reblog, actor.ID, actor.ActorURI, original.ID, token: token);
|
||||
await fanout.Distribute(reblog, token);
|
||||
}
|
||||
|
||||
// a post a relay we subscribe to announces: kept as its author's, for the federated timeline, never as the relay's
|
||||
// boost (Mastodon unwraps them the same way)
|
||||
async Task FromRelay(string objectUri, CancellationToken token)
|
||||
{
|
||||
var held = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
|
||||
var post = held ?? await _remotePosts.StoreContext(objectUri, 0, token);
|
||||
var held = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
|
||||
var post = held ?? await remotePosts.StoreContext(objectUri, 0, token);
|
||||
if (post == default)
|
||||
{
|
||||
Arrival.Drop("fetch-failed");
|
||||
@@ -170,14 +153,14 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
return;
|
||||
}
|
||||
Arrival.Accept("relayed");
|
||||
await _fanout.Distribute(post, token);
|
||||
await fanout.Distribute(post, token);
|
||||
}
|
||||
|
||||
async Task GroupActivity(JsonNode inner, ForeignAvatar group, CancellationToken token)
|
||||
{
|
||||
Arrival.About(Value(inner, "type"));
|
||||
if (group.AvatarType != AvatarType.Group
|
||||
|| !await _dbEntities.Followings.Match(f => f.TargetActorURI == group.ActorURI && f.State == FollowState.Accepted).ExecuteAnyAsync(token)
|
||||
|| !await dbEntities.Followings.Match(f => f.TargetActorURI == group.ActorURI && f.State == FollowState.Accepted).ExecuteAnyAsync(token)
|
||||
&& !await OurPostInItsThread(inner, group, token))
|
||||
{
|
||||
Arrival.Drop("not-followed");
|
||||
@@ -187,12 +170,12 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
switch (Value(inner, "type"))
|
||||
{
|
||||
case "Create" when objectUri != default:
|
||||
if (await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteAnyAsync(token))
|
||||
if (await dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteAnyAsync(token))
|
||||
{
|
||||
Arrival.Drop("duplicate");
|
||||
return;
|
||||
}
|
||||
var post = await _remotePosts.StoreContext(objectUri, 0, token);
|
||||
var post = await remotePosts.StoreContext(objectUri, 0, token);
|
||||
if (post == default)
|
||||
{
|
||||
Arrival.Drop("fetch-failed");
|
||||
@@ -202,10 +185,10 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
Arrival.About(visibility: post.Visibility);
|
||||
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(p => p.AudienceURI, group.ActorURI).ExecuteAsync(token);
|
||||
post.AudienceURI = group.ActorURI;
|
||||
await _fanout.Distribute(post, token);
|
||||
await fanout.Distribute(post, token);
|
||||
break;
|
||||
case "Update" when objectUri != default:
|
||||
var stored = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.AudienceURI == group.ActorURI && !p.DeletedAt.HasValue)
|
||||
var stored = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.AudienceURI == group.ActorURI && !p.DeletedAt.HasValue)
|
||||
.ExecuteFirstAsync(token);
|
||||
if (stored == default)
|
||||
{
|
||||
@@ -213,7 +196,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
return;
|
||||
}
|
||||
Arrival.About(visibility: stored.Visibility, created: stored.CreationDate);
|
||||
using (var fetched = await _remoteActors.FetchObject(objectUri, token))
|
||||
using (var fetched = await remoteActors.FetchObject(objectUri, token))
|
||||
{
|
||||
var note = fetched == default ? default : NoteParser.Parse(System.Text.Json.Nodes.JsonNode.Parse(fetched.Root.GetRawText()));
|
||||
if (note == default || note.AttributedTo != stored.ActorURI)
|
||||
@@ -221,11 +204,11 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
Arrival.Drop(note == default ? "fetch-failed" : "misattributed");
|
||||
return;
|
||||
}
|
||||
Arrival.Accept(await RemoteEdits.Apply(stored, note, Id(inner), _localActors, _records, _quotes, token) ? "edit" : "refresh");
|
||||
Arrival.Accept(await RemoteEdits.Apply(stored, note, Id(inner), localActors, records, quotes, token) ? "edit" : "refresh");
|
||||
}
|
||||
break;
|
||||
case "Delete" when objectUri != default:
|
||||
var deleted = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.AudienceURI == group.ActorURI).ExecuteFirstAsync(token);
|
||||
var deleted = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.AudienceURI == group.ActorURI).ExecuteFirstAsync(token);
|
||||
if (deleted == default)
|
||||
{
|
||||
Arrival.Drop("unknown-object");
|
||||
@@ -235,7 +218,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
// a community on the post's own server speaks for it: PieFed keeps serving a thread its moderator removed.
|
||||
// One elsewhere is believed once the post's server says it is gone
|
||||
if (!Origin.Same(objectUri, group.ActorURI))
|
||||
using (var check = await _remoteActors.FetchObject(objectUri, token))
|
||||
using (var check = await remoteActors.FetchObject(objectUri, token))
|
||||
if (check != default && Value(System.Text.Json.Nodes.JsonNode.Parse(check.Root.GetRawText()), "type") != "Tombstone")
|
||||
{
|
||||
Arrival.Drop("not-deleted");
|
||||
@@ -257,7 +240,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
await Ban(inner, group, token);
|
||||
break;
|
||||
case "Undo" when Value(inner["object"], "type") == "Block" && Id(inner["object"]?["target"]) == group.ActorURI:
|
||||
if (await BlockHandler.Undo(inner["object"], Id(inner["object"]), group, _localActors, token))
|
||||
if (await BlockHandler.Undo(inner["object"], Id(inner["object"]), group, localActors, token))
|
||||
Arrival.Accept("unbanned");
|
||||
else
|
||||
Arrival.Drop("unknown-object");
|
||||
@@ -267,7 +250,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
break;
|
||||
// a moderator features a post in the community, or no longer: the community's own pins
|
||||
case "Add" or "Remove" when Id(inner["target"]) is { } target && target == group.FeaturedURL:
|
||||
var featured = (Relays ?? _services?.GetService(typeof(IEnumerable<IActivityHandler>)) as IEnumerable<IActivityHandler>)
|
||||
var featured = (Relays ?? services?.GetService(typeof(IEnumerable<IActivityHandler>)) as IEnumerable<IActivityHandler>)
|
||||
?.FirstOrDefault(h => h.Type == Value(inner, "type"));
|
||||
if (featured == default)
|
||||
Arrival.Drop("unsupported");
|
||||
@@ -292,8 +275,8 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
};
|
||||
var post = target == default
|
||||
? default
|
||||
: await _dbEntities.Posts.Match(p => p.ObjectURI == target && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
return post != default && await Communities.Of(post, _dbEntities, token) == group.ActorURI;
|
||||
: await dbEntities.Posts.Match(p => p.ObjectURI == target && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
|
||||
return post != default && await Communities.Of(post, dbEntities, token) == group.ActorURI;
|
||||
}
|
||||
|
||||
// A community's moderators lock one of its posts, or unlock it: no more replies, ours included. The community vouches
|
||||
@@ -302,7 +285,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
{
|
||||
var post = objectUri == default
|
||||
? default
|
||||
: await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.AudienceURI == group.ActorURI).ExecuteFirstAsync(token);
|
||||
: await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.AudienceURI == group.ActorURI).ExecuteFirstAsync(token);
|
||||
if (post == default)
|
||||
{
|
||||
Arrival.Drop("unknown-object");
|
||||
@@ -318,7 +301,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
// host is not taken: it would be ours to announce.
|
||||
async Task Move(string objectUri, string targetUri, ForeignAvatar group, CancellationToken token)
|
||||
{
|
||||
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.AudienceURI == group.ActorURI && !p.DeletedAt.HasValue)
|
||||
var post = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.AudienceURI == group.ActorURI && !p.DeletedAt.HasValue)
|
||||
.ExecuteFirstAsync(token);
|
||||
if (post == default)
|
||||
{
|
||||
@@ -326,9 +309,9 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
return;
|
||||
}
|
||||
Arrival.About(visibility: post.Visibility, created: post.CreationDate);
|
||||
var target = targetUri == default || targetUri == group.ActorURI || await _localActors.FindByUri(targetUri, token) != default
|
||||
var target = targetUri == default || targetUri == group.ActorURI || await localActors.FindByUri(targetUri, token) != default
|
||||
? default
|
||||
: await _remoteActors.GetActor(targetUri, refresh: false, token);
|
||||
: await remoteActors.GetActor(targetUri, refresh: false, token);
|
||||
if (target is not { AvatarType: AvatarType.Group })
|
||||
{
|
||||
Arrival.Drop("unknown-target");
|
||||
@@ -339,7 +322,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
while (level.Count > 0 && thread.Count < MaxMoved)
|
||||
{
|
||||
var parents = level;
|
||||
level = (await _dbEntities.Posts.Match(p => parents.Contains(p.AnsweringToPostId) && p.AudienceURI == group.ActorURI)
|
||||
level = (await dbEntities.Posts.Match(p => parents.Contains(p.AnsweringToPostId) && p.AudienceURI == group.ActorURI)
|
||||
.Project(p => new PostEntity { ID = p.ID }).ExecuteAsync(token)).Select(p => p.ID).ToList();
|
||||
thread.AddRange(level);
|
||||
}
|
||||
@@ -354,7 +337,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
// Undo. A ban of someone from another server is no business of ours.
|
||||
async Task Ban(JsonNode inner, ForeignAvatar group, CancellationToken token)
|
||||
{
|
||||
if (Id(inner["object"]) is not { } uri || await _localActors.FindByUri(uri, token) is not { Kind: LocalActorKind.Person } persona)
|
||||
if (Id(inner["object"]) is not { } uri || await localActors.FindByUri(uri, token) is not { Kind: LocalActorKind.Person } persona)
|
||||
{
|
||||
Arrival.Drop("not-ours");
|
||||
return;
|
||||
@@ -378,7 +361,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
{
|
||||
var actorUri = Id(inner["actor"]);
|
||||
var type = Value(inner, "type");
|
||||
var handlers = Relays ?? _services?.GetService(typeof(IEnumerable<IActivityHandler>)) as IEnumerable<IActivityHandler>;
|
||||
var handlers = Relays ?? services?.GetService(typeof(IEnumerable<IActivityHandler>)) as IEnumerable<IActivityHandler>;
|
||||
var handler = handlers?.FirstOrDefault(h => h.Type == type);
|
||||
if (actorUri == default || handler == default)
|
||||
{
|
||||
@@ -388,7 +371,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
var activity = inner;
|
||||
var target = Id(Value(inner, "type") == "Undo" ? (inner["object"] as JsonObject)?["object"] : inner["object"]);
|
||||
var ownPost = target != default
|
||||
&& await _dbEntities.Posts.Match(p => p.ObjectURI == target && p.AudienceURI == group.ActorURI).ExecuteAnyAsync(token);
|
||||
&& await dbEntities.Posts.Match(p => p.ObjectURI == target && p.AudienceURI == group.ActorURI).ExecuteAnyAsync(token);
|
||||
if (!Origin.Same(actorUri, group.ActorURI) && !ownPost)
|
||||
{
|
||||
var id = Id(inner);
|
||||
@@ -397,7 +380,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
Arrival.Drop("misattributed");
|
||||
return;
|
||||
}
|
||||
using var fetched = await _remoteActors.FetchObject(id, token);
|
||||
using var fetched = await remoteActors.FetchObject(id, token);
|
||||
activity = fetched == default ? default : JsonNode.Parse(fetched.Root.GetRawText());
|
||||
if (activity == default || Value(activity, "type") != type || Id(activity["actor"]) != actorUri)
|
||||
{
|
||||
@@ -405,7 +388,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
|
||||
return;
|
||||
}
|
||||
}
|
||||
var actor = await _remoteActors.GetActor(actorUri, refresh: false, token);
|
||||
var actor = await remoteActors.GetActor(actorUri, refresh: false, token);
|
||||
if (actor == default)
|
||||
{
|
||||
Arrival.Drop("unknown-actor");
|
||||
|
||||
Reference in new issue
Block a user