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

+18 -30
View File
@@ -14,28 +14,16 @@ namespace PrivaPub.Federation.Actors
// once a day, never when someone looks at it; a hidden or missing collection leaves its count unknown. Mastodon and
// GoToSocial do the same. A feed's following is its communities: they are kept, and read in turn, so its list shows.
// Its featured hashtags (its featuredTags collection, R8) are read with them.
public class AccountCountsJob : IJobHandler
public class AccountCountsJob(
IRemoteActorService remoteActors,
DbEntities dbEntities,
IFeaturedPosts featured = default,
ILocalActorService localActors = default,
IJobQueue jobs = default,
Domain.Social.IFeaturedTags featuredTags = default) : IJobHandler
{
readonly IRemoteActorService _remoteActors;
readonly DbEntities _dbEntities;
readonly IFeaturedPosts _featured;
readonly ILocalActorService _localActors;
readonly IJobQueue _jobs;
readonly Domain.Social.IFeaturedTags _featuredTags;
public const int MaxFeedCommunities = 50;
public AccountCountsJob(IRemoteActorService remoteActors, DbEntities dbEntities, IFeaturedPosts featured = default,
ILocalActorService localActors = default, IJobQueue jobs = default, Domain.Social.IFeaturedTags featuredTags = default)
{
_featuredTags = featuredTags;
_remoteActors = remoteActors;
_dbEntities = dbEntities;
_featured = featured;
_localActors = localActors;
_jobs = jobs;
}
public JobKind Kind => JobKind.CountAccount;
public int Concurrency => 2;
public int MaxAttempts => 2;
@@ -45,7 +33,7 @@ namespace PrivaPub.Federation.Actors
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
{
var actor = await _dbEntities.ForeignAvatars.Match(a => a.ActorURI == job.Payload && !a.DeletionAt.HasValue).ExecuteFirstAsync(token);
var actor = await dbEntities.ForeignAvatars.Match(a => a.ActorURI == job.Payload && !a.DeletionAt.HasValue).ExecuteFirstAsync(token);
if (actor == default)
return JobOutcome.Done;
var feed = actor.AvatarType == AvatarType.Feed;
@@ -62,15 +50,15 @@ namespace PrivaPub.Federation.Actors
if (feed && listed)
{
foreach (var community in communities)
if (_localActors == default || !community.StartsWith(_localActors.BaseAddress + "/", StringComparison.Ordinal))
await _remoteActors.GetActor(community, refresh: false, token);
if (_jobs != default && await DB.Default.Find<Models.Social.FeedSubscription>().Match(s => s.FeedActorURI == actor.ActorURI).ExecuteAnyAsync(token))
await Domain.Social.FeedFollows.Sync(_jobs, token);
if (localActors == default || !community.StartsWith(localActors.BaseAddress + "/", StringComparison.Ordinal))
await remoteActors.GetActor(community, refresh: false, token);
if (jobs != default && await DB.Default.Find<Models.Social.FeedSubscription>().Match(s => s.FeedActorURI == actor.ActorURI).ExecuteAnyAsync(token))
await Domain.Social.FeedFollows.Sync(jobs, token);
}
if (_featured != default)
await _featured.Sync(actor, token);
if (_featuredTags != default)
await _featuredTags.Sync(actor, token);
if (featured != default)
await featured.Sync(actor, token);
if (featuredTags != default)
await featuredTags.Sync(actor, token);
return JobOutcome.Done;
}
@@ -82,7 +70,7 @@ namespace PrivaPub.Federation.Actors
if (string.IsNullOrEmpty(collection) || !Origin.Same(collection, actor.ActorURI))
return (default, listed, false);
using var scope = HttpScope.For("collection");
using var fetched = await _remoteActors.FetchObject(collection, token);
using var fetched = await remoteActors.FetchObject(collection, token);
if (fetched == default || fetched.Root.ValueKind != System.Text.Json.JsonValueKind.Object)
return (default, listed, false);
int? total = fetched.Root.TryGetProperty("totalItems", out var counted) && counted.TryGetInt32(out var count) && count >= 0 ? count : default;
@@ -92,7 +80,7 @@ namespace PrivaPub.Federation.Actors
var page = document["orderedItems"] ?? document["items"] ?? document["first"]?["orderedItems"] ?? document["first"]?["items"];
if (page == default && ActivityJson.Id(document["first"]) is { } first && Origin.Same(first, actor.ActorURI))
{
using var next = await _remoteActors.FetchObject(first, token);
using var next = await remoteActors.FetchObject(first, token);
if (next == default)
return (total, listed, false);
var nextPage = System.Text.Json.Nodes.JsonNode.Parse(next.Root.GetRawText());
@@ -39,6 +39,7 @@ namespace PrivaPub.Federation.Actors
public string MovedTo { get; init; }
public string Moderators { get; init; }
public List<PostFlair> Flairs { get; init; } = new();
[System.Diagnostics.CodeAnalysis.SuppressMessage("HouseRules", "HR0006", Justification = "Null when the document does not state searchableBy (FEP-268d), which is not the same as an empty list.")]
public List<string> SearchableBy { get; init; }
public List<string> AlsoKnownAs { get; init; } = new();//the accounts it says it also is (a Move's target names the moved one)
public DateTime? Published { get; init; }
+8 -15
View File
@@ -26,20 +26,13 @@ namespace PrivaPub.Federation.Actors
// What an account elsewhere features (pins): its `featured` collection, read with its counts at most once a day, and
// the Add and Remove it sends when it pins or unpins. An account features only its own posts; a community (Lemmy's,
// inside its announce) features posts made in it. Shown first on its profile here, as Mastodon shows them.
public class FeaturedPosts : IFeaturedPosts
public class FeaturedPosts(
DbEntities dbEntities,
IRemoteActorService remoteActors,
IRemotePosts remotePosts) : IFeaturedPosts
{
public const int MaxFeatured = 20;
readonly DbEntities _dbEntities;
readonly IRemoteActorService _remoteActors;
readonly IRemotePosts _remotePosts;
public FeaturedPosts(DbEntities dbEntities, IRemoteActorService remoteActors, IRemotePosts remotePosts)
{
_dbEntities = dbEntities;
_remoteActors = remoteActors;
_remotePosts = remotePosts;
}
public async Task Receive(JsonNode activity, ForeignAvatar owner, bool add, CancellationToken token)
{
@@ -110,8 +103,8 @@ namespace PrivaPub.Federation.Actors
var isGroup = owner.AvatarType is AvatarType.Group;
if (!isGroup && !Origin.Same(objectUri, owner.ActorURI))
return default;
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue).ExecuteFirstAsync(token)
?? await _remotePosts.StoreContext(objectUri, 0, token);
var post = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue).ExecuteFirstAsync(token)
?? await remotePosts.StoreContext(objectUri, 0, token);
return post switch
{
null => default,
@@ -125,7 +118,7 @@ namespace PrivaPub.Federation.Actors
async Task<List<string>> Items(ForeignAvatar owner, CancellationToken token)
{
using var scope = HttpScope.For("collection");
using var collection = await _remoteActors.FetchObject(owner.FeaturedURL, token);
using var collection = await remoteActors.FetchObject(owner.FeaturedURL, token);
if (collection == default || collection.Root.ValueKind != JsonValueKind.Object)
return default;
var items = ItemsOf(collection.Root);
@@ -136,7 +129,7 @@ namespace PrivaPub.Federation.Actors
var page = first.ValueKind == JsonValueKind.String ? first.GetString() : default;
if (page == default || !Origin.Same(page, owner.ActorURI))
return items;
using var fetched = await _remoteActors.FetchObject(page, token);
using var fetched = await remoteActors.FetchObject(page, token);
return fetched == default || fetched.Root.ValueKind != JsonValueKind.Object ? items : ItemsOf(fetched.Root);
}
@@ -23,25 +23,15 @@ namespace PrivaPub.Federation.Actors
// only PrivaPub remembers ends, a request it answered without our knowing is accepted, and a follow only it remembers
// is undone by the persona, as Mastodon does, or kept within RestoreRecord.FollowersGrace of a restore, which may have
// lost it. A follow ends only when the list is the one the digest describes.
public class FollowingSynchronization : IJobHandler
public class FollowingSynchronization(
DbEntities dbEntities,
ILocalActorService localActors,
IRemoteActorService remoteActors,
IFollowService follows,
IDeliveryService deliveryService) : IJobHandler
{
const int MaxPages = 10;
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly IRemoteActorService _remoteActors;
readonly IFollowService _follows;
readonly IDeliveryService _delivery;
public FollowingSynchronization(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IFollowService follows,
IDeliveryService delivery)
{
_dbEntities = dbEntities;
_localActors = localActors;
_remoteActors = remoteActors;
_follows = follows;
_delivery = delivery;
}
public JobKind Kind => JobKind.SynchronizeFollowing;
public int Concurrency => 1;
@@ -55,14 +45,14 @@ namespace PrivaPub.Federation.Actors
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
{
var claim = JsonSerializer.Deserialize<SynchronizationClaim>(job.Payload);
var actor = claim == default ? default : await _dbEntities.ForeignAvatars.Match(a => a.ActorURI == claim.Actor && !a.DeletionAt.HasValue).ExecuteFirstAsync(token);
var actor = claim == default ? default : await dbEntities.ForeignAvatars.Match(a => a.ActorURI == claim.Actor && !a.DeletionAt.HasValue).ExecuteFirstAsync(token);
if (actor == default || string.IsNullOrEmpty(actor.FollowersURL) || claim.CollectionId != actor.FollowersURL || !Origin.Same(claim.Url, actor.ActorURI))
return JobOutcome.Done;
var followings = await _dbEntities.Followings.Match(f => f.TargetActorURI == actor.ActorURI && !f.TargetIsLocal).ExecuteAsync(token);
var followings = await dbEntities.Followings.Match(f => f.TargetActorURI == actor.ActorURI && !f.TargetIsLocal).ExecuteAsync(token);
var personas = new Dictionary<string, (LocalActor Persona, Following Following)>(StringComparer.Ordinal);
foreach (var following in followings)
if (await _localActors.FindById(LocalActorKind.Person, following.AvatarId, token) is { } persona)
if (await localActors.FindById(LocalActorKind.Person, following.AvatarId, token) is { } persona)
personas[persona.Uri] = (persona, following);
var accepted = personas.Values.Where(p => p.Following.State == FollowState.Accepted).Select(p => p.Persona.Uri).ToList();
if (FollowersSynchronization.Digest(accepted) == claim.Digest)
@@ -76,7 +66,7 @@ namespace PrivaPub.Federation.Actors
var named = listed.ToHashSet(StringComparer.Ordinal);
if (complete)
foreach (var uri in accepted.Where(uri => !named.Contains(uri)))
await _follows.UnfollowAs(personas[uri].Persona, actor.ActorURI, token);
await follows.UnfollowAs(personas[uri].Persona, actor.ActorURI, token);
foreach (var uri in named)
{
@@ -86,7 +76,7 @@ namespace PrivaPub.Federation.Actors
await DB.Default.Update<Following>().MatchID(known.Following.ID).Modify(f => f.State, FollowState.Accepted).ExecuteAsync(token);
continue;
}
if (await _localActors.FindByUri(uri, token) is not { Kind: LocalActorKind.Person } stranger)
if (await localActors.FindByUri(uri, token) is not { Kind: LocalActorKind.Person } stranger)
continue;
// a follow the restore lost: kept, not undone
if (await Infrastructure.Backup.RestoreRecord.InFollowersGrace(token))
@@ -110,7 +100,7 @@ namespace PrivaPub.Federation.Actors
["actor"] = stranger.Uri,
["object"] = new JsonObject { ["type"] = "Follow", ["actor"] = stranger.Uri, ["object"] = actor.ActorURI }
};
await _delivery.Enqueue(stranger, new[] { string.IsNullOrEmpty(actor.InboxURL) ? actor.SharedInboxURL : actor.InboxURL }, undo, token);
await deliveryService.Enqueue(stranger, new[] { string.IsNullOrEmpty(actor.InboxURL) ? actor.SharedInboxURL : actor.InboxURL }, undo, token: token);
}
return JobOutcome.Done;
}
@@ -123,7 +113,7 @@ namespace PrivaPub.Federation.Actors
var next = url;
for (var page = 0; page < MaxPages && next != default; page++)
{
using var fetched = await _remoteActors.FetchObject(next, token);
using var fetched = await remoteActors.FetchObject(next, token);
if (fetched == default || fetched.Root.ValueKind != JsonValueKind.Object)
return page == 0 ? default : items;
var root = fetched.Root;
+15 -22
View File
@@ -91,7 +91,10 @@ namespace PrivaPub.Federation.Actors
// a persona or group that was deleted: its name stays reserved, and its documents answer 410 with this
public sealed record GoneActor(string Uri, string FormerType, DateTime DeletedAt);
public class LocalActorService : ILocalActorService
public class LocalActorService(
DbEntities dbEntities,
IOptionsMonitor<AppConfiguration> appConfiguration,
Domain.Instance.IInstanceSettings instanceSettings) : ILocalActorService
{
public const string InstanceUserName = "privapub";
// the anonymous Service that carries this server's reports to Lemmy, which takes no report from an Application
@@ -114,21 +117,11 @@ namespace PrivaPub.Federation.Actors
// what the instance actor says of itself until the administrator describes the server (ServerSettings)
const string InstanceSummary = "The instance actor of this PrivaPub server; it signs the requests no other actor speaks for.";
readonly DbEntities _dbEntities;
readonly IOptionsMonitor<AppConfiguration> _appConfiguration;
readonly Domain.Instance.IInstanceSettings _settings;
InstanceActor _instanceActor;
ReporterActor _reporterActor;
ReaderActor _readerActor;
public LocalActorService(DbEntities dbEntities, IOptionsMonitor<AppConfiguration> appConfiguration, Domain.Instance.IInstanceSettings settings)
{
_dbEntities = dbEntities;
_appConfiguration = appConfiguration;
_settings = settings;
}
public string BaseAddress => _appConfiguration.CurrentValue.BackendBaseAddress?.TrimEnd('/');
public string BaseAddress => appConfiguration.CurrentValue.BackendBaseAddress?.TrimEnd('/');
public async Task<LocalActor> FindByUserName(string userName, CancellationToken token)
{
@@ -142,13 +135,13 @@ namespace PrivaPub.Federation.Actors
if (userName == ReaderUserName)
return await GetReaderActor(token);
var avatar = await _dbEntities.Avatars
var avatar = await dbEntities.Avatars
.Match(a => a.UserName == userName && !a.DeletionAt.HasValue)
.ExecuteFirstAsync(token);
if (avatar != default)
return FromAvatar(avatar);
var group = await _dbEntities.Groups
var group = await dbEntities.Groups
.Match(g => g.UserName == userName && !g.DeletionAt.HasValue)
.ExecuteFirstAsync(token);
return group == default ? default : FromGroup(group);
@@ -159,10 +152,10 @@ namespace PrivaPub.Federation.Actors
if (string.IsNullOrEmpty(userName))
return default;
userName = userName.ToLowerInvariant();
var avatar = await _dbEntities.Avatars.Match(a => a.UserName == userName && a.DeletionAt.HasValue).ExecuteFirstAsync(token);
var avatar = await dbEntities.Avatars.Match(a => a.UserName == userName && a.DeletionAt.HasValue).ExecuteFirstAsync(token);
if (avatar != default)
return new GoneActor($"{BaseAddress}/peasants/{avatar.UserName}", "Person", avatar.DeletionAt.Value);
var group = await _dbEntities.Groups.Match(g => g.UserName == userName && g.DeletionAt.HasValue).ExecuteFirstAsync(token);
var group = await dbEntities.Groups.Match(g => g.UserName == userName && g.DeletionAt.HasValue).ExecuteFirstAsync(token);
return group == default ? default : new GoneActor($"{BaseAddress}/peasants/{group.UserName}", "Group", group.DeletionAt.Value);
}
@@ -171,10 +164,10 @@ namespace PrivaPub.Federation.Actors
switch (kind)
{
case LocalActorKind.Person:
var avatar = await _dbEntities.Avatars.MatchID(id).ExecuteFirstAsync(token);
var avatar = await dbEntities.Avatars.MatchID(id).ExecuteFirstAsync(token);
return avatar == default ? default : FromAvatar(avatar);
case LocalActorKind.Group:
var group = await _dbEntities.Groups.MatchID(id).ExecuteFirstAsync(token);
var group = await dbEntities.Groups.MatchID(id).ExecuteFirstAsync(token);
return group == default ? default : FromGroup(group);
case LocalActorKind.Reporter:
return await GetReporterActor(token);
@@ -208,7 +201,7 @@ namespace PrivaPub.Federation.Actors
{
var instance = _instanceActor ??= await LoadInstanceActor(token);
// the server's name, and its short description once the administrator wrote one of their own
var settings = _settings.Current;
var settings = instanceSettings.Current;
return new LocalActor
{
@@ -268,7 +261,7 @@ namespace PrivaPub.Federation.Actors
async Task<ReaderActor> LoadReaderActor(CancellationToken token)
{
var reader = await _dbEntities.ReaderActors.Sort(r => r.CreationDate, Order.Ascending).ExecuteFirstAsync(token);
var reader = await dbEntities.ReaderActors.Sort(r => r.CreationDate, Order.Ascending).ExecuteFirstAsync(token);
if (reader != default)
return reader;
var (privateKey, publicKey) = Keys.NewKeyPair();
@@ -279,7 +272,7 @@ namespace PrivaPub.Federation.Actors
async Task<ReporterActor> LoadReporterActor(CancellationToken token)
{
var reporter = await _dbEntities.ReporterActors.Sort(r => r.CreationDate, Order.Ascending).ExecuteFirstAsync(token);
var reporter = await dbEntities.ReporterActors.Sort(r => r.CreationDate, Order.Ascending).ExecuteFirstAsync(token);
if (reporter != default)
return reporter;
var (privateKey, publicKey) = Keys.NewKeyPair();
@@ -290,7 +283,7 @@ namespace PrivaPub.Federation.Actors
async Task<InstanceActor> LoadInstanceActor(CancellationToken token)
{
var instance = await _dbEntities.InstanceActors.Sort(i => i.CreationDate, Order.Ascending).ExecuteFirstAsync(token);
var instance = await dbEntities.InstanceActors.Sort(i => i.CreationDate, Order.Ascending).ExecuteFirstAsync(token);
if (instance != default)
return instance;
var (privateKey, publicKey) = Keys.NewKeyPair();
+8 -15
View File
@@ -14,20 +14,13 @@ namespace PrivaPub.Federation.Actors
// The latest public posts of an account a persona has just followed, read from the first page of its outbox (at most
// twenty, once a day) and kept as any fetched post: its profile shows them and threads find them at once, instead of
// only what it posts from then on. Homes still get only what arrives from now on, as on Mastodon.
public class OutboxBackfill : IJobHandler
public class OutboxBackfill(
DbEntities dbEntities,
IRemoteActorService remoteActors,
IRemotePosts remotePosts) : IJobHandler
{
public const int MaxPosts = 20;
readonly DbEntities _dbEntities;
readonly IRemoteActorService _remoteActors;
readonly IRemotePosts _remotePosts;
public OutboxBackfill(DbEntities dbEntities, IRemoteActorService remoteActors, IRemotePosts remotePosts)
{
_dbEntities = dbEntities;
_remoteActors = remoteActors;
_remotePosts = remotePosts;
}
public JobKind Kind => JobKind.BackfillOutbox;
public int Concurrency => 1;
@@ -38,11 +31,11 @@ namespace PrivaPub.Federation.Actors
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
{
var actor = await _dbEntities.ForeignAvatars.Match(a => a.ActorURI == job.Payload && !a.DeletionAt.HasValue).ExecuteFirstAsync(token);
var actor = await dbEntities.ForeignAvatars.Match(a => a.ActorURI == job.Payload && !a.DeletionAt.HasValue).ExecuteFirstAsync(token);
if (actor == default || string.IsNullOrEmpty(actor.OutboxURL) || !Origin.Same(actor.OutboxURL, actor.ActorURI))
return JobOutcome.Done;
foreach (var objectUri in (await Items(actor, token)).Take(MaxPosts))
await _remotePosts.StoreContext(objectUri, 0, token);
await remotePosts.StoreContext(objectUri, 0, token);
return JobOutcome.Done;
}
@@ -51,7 +44,7 @@ namespace PrivaPub.Federation.Actors
async Task<List<string>> Items(Models.User.ForeignAvatar actor, CancellationToken token)
{
using var scope = HttpScope.For("outbox");
using var outbox = await _remoteActors.FetchObject(actor.OutboxURL, token);
using var outbox = await remoteActors.FetchObject(actor.OutboxURL, token);
if (outbox == default || outbox.Root.ValueKind != JsonValueKind.Object)
return new();
var page = ItemsOf(outbox.Root, actor);
@@ -62,7 +55,7 @@ namespace PrivaPub.Federation.Actors
var pageUri = first.ValueKind == JsonValueKind.String ? first.GetString() : default;
if (pageUri == default || !Origin.Same(pageUri, actor.ActorURI))
return page;
using var fetched = await _remoteActors.FetchObject(pageUri, token);
using var fetched = await remoteActors.FetchObject(pageUri, token);
return fetched == default || fetched.Root.ValueKind != JsonValueKind.Object ? page : ItemsOf(fetched.Root, actor);
}
@@ -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;
+16 -25
View File
@@ -32,42 +32,33 @@ namespace PrivaPub.Federation.Actors
// the persona and its followers, and told to its followers' servers with Add{Note}. The persona may take it off again
// (Remove, which Smithereen takes for a deletion, as it is on its walls). Another server's account that tells its
// followers here of a post on its wall (Add) has it shown to them as on its wall.
public class Walls : IWalls
public class Walls(
DbEntities dbEntities,
ILocalActorService localActors,
IRemotePosts remotePosts,
IDeliveryService delivery,
IFanout fanout) : IWalls
{
public const string Path = "graffiti";
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly IRemotePosts _remotePosts;
readonly IDeliveryService _delivery;
readonly IFanout _fanout;
public Walls(DbEntities dbEntities, ILocalActorService localActors, IRemotePosts remotePosts, IDeliveryService delivery, IFanout fanout)
{
_dbEntities = dbEntities;
_localActors = localActors;
_remotePosts = remotePosts;
_delivery = delivery;
_fanout = fanout;
}
public async Task<LocalActor> OwnerOf(string wallUri, CancellationToken token)
{
if (string.IsNullOrEmpty(wallUri) || !wallUri.EndsWith("/" + Path, StringComparison.Ordinal))
return default;
var owner = await _localActors.FindByUri(wallUri[..^(Path.Length + 1)], token);
var owner = await localActors.FindByUri(wallUri[..^(Path.Length + 1)], token);
return owner is { HasWall: true } && owner.Wall == wallUri ? owner : default;
}
// its followers, as the actor says to Smithereen (wallPosting), unless the persona hides them
public async Task<bool> MayWrite(LocalActor owner, ForeignAvatar author, CancellationToken token) =>
await _dbEntities.Followers.Match(f => f.LocalActorId == owner.Id && f.LocalActorKind == owner.Kind && f.ActorURI == author.ActorURI && f.IsAccepted)
await dbEntities.Followers.Match(f => f.LocalActorId == owner.Id && f.LocalActorKind == owner.Kind && f.ActorURI == author.ActorURI && f.IsAccepted)
.ExecuteAnyAsync(token)
&& !(await Domain.Relationships.Hidden.RecipientsHiding(new[] { owner.Id }, author.ActorURI, token)).Contains(owner.Id);
public async Task Hosted(PostEntity post, LocalActor owner, CancellationToken token)
{
await Notifications.Add(owner.Id, NotificationType.Mention, post.AuthorAccountId, post.ActorURI, post.ID, token);
await Notifications.Add(owner.Id, NotificationType.Mention, post.AuthorAccountId, post.ActorURI, post.ID, token: token);
var add = new JsonObject
{
["@context"] = ActivityPubRenderer.Context(),
@@ -78,7 +69,7 @@ namespace PrivaPub.Federation.Actors
["target"] = new JsonObject { ["type"] = "OrderedCollection", ["id"] = owner.Wall, ["attributedTo"] = owner.Uri },
["to"] = new JsonArray(ActivityPubRenderer.Public, owner.Followers, post.ActorURI)
};
await _delivery.EnqueueToFollowers(owner, add, token, await AuthorInbox(post, token));
await delivery.EnqueueToFollowers(owner, add, await AuthorInbox(post, token), token);
}
public async Task Receive(JsonNode activity, ForeignAvatar owner, bool add, CancellationToken token)
@@ -91,7 +82,7 @@ namespace PrivaPub.Federation.Actors
}
if (!add)
{
var walled = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.WallOwnerURI == owner.ActorURI).ExecuteFirstAsync(token);
var walled = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.WallOwnerURI == owner.ActorURI).ExecuteFirstAsync(token);
if (walled == default)
{
Arrival.Drop("unknown-object");
@@ -102,8 +93,8 @@ namespace PrivaPub.Federation.Actors
return;
}
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue).ExecuteFirstAsync(token)
?? await _remotePosts.StoreContext(objectUri, 0, token);
var post = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue).ExecuteFirstAsync(token)
?? await remotePosts.StoreContext(objectUri, 0, token);
if (post is not { IsFederatedCopy: true, ReblogOfPostId: null, Visibility: PostVisibility.Public or PostVisibility.Unlisted } || !string.IsNullOrEmpty(post.InReplyToURI))
{
Arrival.Drop("unknown-object");
@@ -123,7 +114,7 @@ namespace PrivaPub.Federation.Actors
.Modify(p => p.WallOwnerURI, post.WallOwnerURI)
.Modify(p => p.WallOwnerAccountId, post.WallOwnerAccountId)
.ExecuteAsync(token);
await _fanout.Distribute(post, token);
await fanout.Distribute(post, token);
Arrival.Accept("walled");
}
@@ -142,14 +133,14 @@ namespace PrivaPub.Federation.Actors
["target"] = owner.Wall,
["to"] = new JsonArray(ActivityPubRenderer.Public, owner.Followers, post.ActorURI)
};
await _delivery.EnqueueToFollowers(owner, remove, token, await AuthorInbox(post, token));
await delivery.EnqueueToFollowers(owner, remove, await AuthorInbox(post, token), token);
await RemoteDeletes.Remove(post, post.ObjectURI, token);
return true;
}
async Task<IEnumerable<string>> AuthorInbox(PostEntity post, CancellationToken token)
{
var author = await _dbEntities.ForeignAvatars.Match(a => a.ActorURI == post.ActorURI).ExecuteFirstAsync(token);
var author = await dbEntities.ForeignAvatars.Match(a => a.ActorURI == post.ActorURI).ExecuteFirstAsync(token);
var inbox = string.IsNullOrEmpty(author?.SharedInboxURL) ? author?.InboxURL : author.SharedInboxURL;
return inbox == default ? Array.Empty<string>() : new[] { inbox };
}
@@ -17,16 +17,11 @@ namespace PrivaPub.Federation.Controllers
// answers 410 with a Tombstone. An emoji tells nothing about anyone, so it is served unsigned under SecureMode too, as the
// server's own actors are; a browser is sent to the picture.
[ApiController]
public class EmojisController : ControllerBase
public class EmojisController(
ILocalActorService localActors) : ControllerBase
{
const string ActivityContentType = "application/activity+json; charset=utf-8";
readonly ILocalActorService _localActors;
public EmojisController(ILocalActorService localActors)
{
_localActors = localActors;
}
[HttpGet, Route("/" + LocalEmojis.Path + "/{shortcode}")]
public async Task<IActionResult> Get(string shortcode, CancellationToken token)
@@ -44,7 +39,7 @@ namespace PrivaPub.Federation.Controllers
Content = new JsonObject
{
["@context"] = ActivityPubRenderer.ActivityStreams,
["id"] = LocalEmojis.Uri(_localActors.BaseAddress, emoji.Shortcode),
["id"] = LocalEmojis.Uri(localActors.BaseAddress, emoji.Shortcode),
["type"] = "Tombstone",
["formerType"] = "Emoji",
["deleted"] = ActivityPubRenderer.Timestamp(deleted)
@@ -60,7 +55,7 @@ namespace PrivaPub.Federation.Controllers
emoji.ImageUpdatedAt, emoji.Category, emoji.VisibleInPicker);
return new ContentResult
{
Content = LocalEmojis.Document(live, _localActors.BaseAddress).ToJsonString(),
Content = LocalEmojis.Document(live, localActors.BaseAddress).ToJsonString(),
ContentType = ActivityContentType,
StatusCode = StatusCodes.Status200OK
};
@@ -26,34 +26,23 @@ using PrivaPub.Infrastructure;
namespace PrivaPub.Federation.Controllers
{
[ApiController, Route("peasants")]
public class PeasantsController : ControllerBase, IAsyncActionFilter
public class PeasantsController(
ILocalActorService localActors,
IInboxReceiver inboxReceiver,
DbEntities dbEntities,
ILogger<PeasantsController> logger,
ISignedFetchAuthorizer fetches,
IOptionsMonitor<FederationOptions> federation) : ControllerBase, IAsyncActionFilter
{
const string ActivityContentType = "application/activity+json; charset=utf-8";
const int OutboxSize = 20;
readonly ILocalActorService _localActors;
readonly IInboxReceiver _inbox;
readonly DbEntities _dbEntities;
readonly ILogger<PeasantsController> _logger;
readonly ISignedFetchAuthorizer _fetches;
readonly IOptionsMonitor<FederationOptions> _federation;
public PeasantsController(ILocalActorService localActors, IInboxReceiver inbox, DbEntities dbEntities,
ILogger<PeasantsController> logger, ISignedFetchAuthorizer fetches, IOptionsMonitor<FederationOptions> federation)
{
_fetches = fetches;
_federation = federation;
_localActors = localActors;
_inbox = inbox;
_dbEntities = dbEntities;
_logger = logger;
}
[HttpGet, Route("{actor}")]
public async Task<IActionResult> GetActor(string actor, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
if (local == default && await _localActors.Gone(actor, token) is { } gone)
var local = await localActors.FindByUserName(actor, token);
if (local == default && await localActors.Gone(actor, token) is { } gone)
return new ContentResult
{
Content = new JsonObject
@@ -82,14 +71,14 @@ namespace PrivaPub.Federation.Controllers
var accept = Request.Headers.Accept.ToString();
if (!accept.Contains("activity+json", StringComparison.OrdinalIgnoreCase) && !accept.Contains("ld+json", StringComparison.OrdinalIgnoreCase))
return NotFound();
return Activity(ActivityPubRenderer.Actor(await _localActors.GetInstanceActor(token)));
return Activity(ActivityPubRenderer.Actor(await localActors.GetInstanceActor(token)));
}
[HttpGet, Route("{actor}/anus")]
public async Task<IActionResult> Outbox(string actor, [FromQuery] bool page, [FromQuery(Name = "max_id")] string maxId,
CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { IsFederated: true })
return NotFound();
@@ -101,8 +90,8 @@ namespace PrivaPub.Federation.Controllers
}
var query = local.Kind == LocalActorKind.Group
? _dbEntities.Posts.Match(p => p.GroupId == local.Id).Match(VisibilityPolicy.IsPublic)
: _dbEntities.Posts.Match(p => p.GroupUserId == local.Id && !p.IsFederatedCopy).Match(VisibilityPolicy.IsPublic);
? dbEntities.Posts.Match(p => p.GroupId == local.Id).Match(VisibilityPolicy.IsPublic)
: dbEntities.Posts.Match(p => p.GroupUserId == local.Id && !p.IsFederatedCopy).Match(VisibilityPolicy.IsPublic);
if (!string.IsNullOrEmpty(maxId))
query.Match(f => f.Lt(p => p.ID, maxId));
var latest = await query.Sort(p => p.ID, Order.Descending).Limit(OutboxSize).ExecuteAsync(token);
@@ -134,11 +123,11 @@ namespace PrivaPub.Federation.Controllers
[HttpGet, Route("{actor}/groupies")]
public async Task<IActionResult> Followers(string actor, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { IsFederated: true })
return NotFound();
if (local.IsCircle && !SignedFetchAuthorizer.MayReadCircle(await _dbEntities.Groups.MatchID(local.Id).ExecuteFirstAsync(token),
await _fetches.Requester(Request, token)))
if (local.IsCircle && !SignedFetchAuthorizer.MayReadCircle(await dbEntities.Groups.MatchID(local.Id).ExecuteFirstAsync(token),
await fetches.Requester(Request, token)))
return NotFound();
var count = await DB.Default.CountAsync<Follower>(
f => f.LocalActorId == local.Id && f.LocalActorKind == local.Kind && f.IsAccepted, token);
@@ -150,7 +139,7 @@ namespace PrivaPub.Federation.Controllers
[HttpGet, Route("{actor}/" + Walls.Path)]
public async Task<IActionResult> Wall(string actor, [FromQuery] bool page, [FromQuery(Name = "max_id")] string maxId, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { HasWall: true })
return NotFound();
var wall = local.Wall;
@@ -161,7 +150,7 @@ namespace PrivaPub.Federation.Controllers
|| p.GroupUserId == local.Id && !p.IsFederatedCopy && p.ReblogOfPostId == null && p.AnsweringToPostId == null && p.InReplyToURI == null && p.GroupId == null);
if (!page)
return Activity(ActivityPubRenderer.OrderedCollection(wall, (int)await DB.Default.CountAsync(onWall, token), default, $"{wall}?page=true"));
var query = _dbEntities.Posts.Match(onWall);
var query = dbEntities.Posts.Match(onWall);
if (!string.IsNullOrEmpty(maxId))
query.Match(f => f.Lt(p => p.ID, maxId));
var latest = await query.Sort(p => p.ID, Order.Descending).Limit(OutboxSize).ExecuteAsync(token);
@@ -174,10 +163,10 @@ namespace PrivaPub.Federation.Controllers
[HttpGet, Route("{actor}/groupies/roll-call")]
public async Task<IActionResult> RollCall(string actor, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { IsFederated: true } || !FollowersSynchronization.Synchronizes(local))
return NotFound();
var requester = await _fetches.Requester(Request, token);
var requester = await fetches.Requester(Request, token);
if (requester == default)
return StatusCode(StatusCodes.Status401Unauthorized);
var there = await FollowersSynchronization.On(local, FollowersSynchronization.Origin(requester.ActorURI), token);
@@ -187,7 +176,7 @@ namespace PrivaPub.Federation.Controllers
[HttpGet, Route("{actor}/stalking")]
public async Task<IActionResult> Following(string actor, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { IsFederated: true })
return NotFound();
// the count, as following_count says it; who is followed is never listed (FEDERATION.md)
@@ -200,11 +189,11 @@ namespace PrivaPub.Federation.Controllers
[HttpGet, Route("{actor}/flock")]
public async Task<IActionResult> Members(string actor, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { IsFederated: true, Kind: LocalActorKind.Group })
return NotFound();
var group = await _dbEntities.Groups.MatchID(local.Id).ExecuteFirstAsync(token);
if (local.IsCircle && !SignedFetchAuthorizer.MayReadCircle(group, await _fetches.Requester(Request, token)))
var group = await dbEntities.Groups.MatchID(local.Id).ExecuteFirstAsync(token);
if (local.IsCircle && !SignedFetchAuthorizer.MayReadCircle(group, await fetches.Requester(Request, token)))
return NotFound();
return Activity(ActivityPubRenderer.OrderedCollection(local.Flock, group.Members.Count, default));
}
@@ -212,15 +201,15 @@ namespace PrivaPub.Federation.Controllers
[HttpGet, Route("{actor}/wardens")]
public async Task<IActionResult> Moderators(string actor, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { IsFederated: true, Kind: LocalActorKind.Group })
return NotFound();
var group = await _dbEntities.Groups.MatchID(local.Id).ExecuteFirstAsync(token);
if (local.IsCircle && !SignedFetchAuthorizer.MayReadCircle(group, await _fetches.Requester(Request, token)))
var group = await dbEntities.Groups.MatchID(local.Id).ExecuteFirstAsync(token);
if (local.IsCircle && !SignedFetchAuthorizer.MayReadCircle(group, await fetches.Requester(Request, token)))
return NotFound();
var moderators = new List<JsonNode>();
foreach (var member in group.Members.Where(m => !m.IsForeign && m.Role is GroupRole.Owner or GroupRole.Moderator))
if (await _localActors.FindById(LocalActorKind.Person, member.AvatarId, token) is { } moderator)
if (await localActors.FindById(LocalActorKind.Person, member.AvatarId, token) is { } moderator)
moderators.Add(moderator.Uri);
return Activity(ActivityPubRenderer.OrderedCollection(local.Wardens, moderators.Count, moderators));
}
@@ -228,14 +217,14 @@ namespace PrivaPub.Federation.Controllers
[HttpGet, Route("{actor}/trophies")]
public async Task<IActionResult> Featured(string actor, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { IsFederated: true })
return NotFound();
var pinIds = (await DB.Default.Find<Pin>().Match(p => p.AvatarId == local.Id).Sort(p => p.ID, Order.Descending).ExecuteAsync(token))
.Select(p => p.PostId).ToList();
var posts = pinIds.Count == 0
? new List<PostEntity>()
: await _dbEntities.Posts.Match(p => pinIds.Contains(p.ID) && p.GroupUserId == local.Id && p.ReblogOfPostId == null)
: await dbEntities.Posts.Match(p => pinIds.Contains(p.ID) && p.GroupUserId == local.Id && p.ReblogOfPostId == null)
.Match(VisibilityPolicy.IsPublic).ExecuteAsync(token);
var notes = new List<JsonNode>();
foreach (var id in pinIds)
@@ -249,7 +238,7 @@ namespace PrivaPub.Federation.Controllers
[HttpGet, Route("{actor}/tattoos")]
public async Task<IActionResult> FeaturedTags(string actor, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { IsFederated: true })
return NotFound();
var tags = local.Kind == LocalActorKind.Person
@@ -278,7 +267,7 @@ namespace PrivaPub.Federation.Controllers
if (WantsHtml())
return Redirect(local.PostHtmlUrl(post.ID));
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);
var note = ActivityPubRenderer.Note(post, local, group, post.InReplyToURI);
note["@context"] = ActivityPubRenderer.Context();
return Activity(note);
@@ -291,7 +280,7 @@ namespace PrivaPub.Federation.Controllers
var (local, post) = await PublicPost(actor, postId, token);
if (post == default || post.IsLocalOnly || !string.IsNullOrEmpty(post.GroupId))
return NotFound();
var replies = await _dbEntities.Posts.Match(p => p.AnsweringToPostId == post.ID && !p.IsLocalOnly && p.ReblogOfPostId == null)
var replies = await dbEntities.Posts.Match(p => p.AnsweringToPostId == post.ID && !p.IsLocalOnly && p.ReblogOfPostId == null)
.Match(VisibilityPolicy.IsPublic).Sort(p => p.CreationDate, Order.Ascending).Limit(MaxThread).ExecuteAsync(token);
replies.RemoveAll(VisibilityPolicy.Barred);
return Activity(ActivityPubRenderer.OrderedCollection(local.PostUri(post.ID) + "/replies", replies.Count,
@@ -309,7 +298,7 @@ namespace PrivaPub.Federation.Controllers
var level = new List<string> { root.ID };
for (var depth = 0; depth < PrivaPub.Federation.Inbox.RemotePosts.MaxDepth && level.Count > 0 && thread.Count < MaxThread; depth++)
{
var next = await _dbEntities.Posts.Match(p => level.Contains(p.AnsweringToPostId) && !p.IsLocalOnly && p.ReblogOfPostId == null)
var next = await dbEntities.Posts.Match(p => level.Contains(p.AnsweringToPostId) && !p.IsLocalOnly && p.ReblogOfPostId == null)
.Match(VisibilityPolicy.IsPublic).Sort(p => p.CreationDate, Order.Ascending).Limit(MaxThread - thread.Count).ExecuteAsync(token);
thread.AddRange(next.Where(p => !VisibilityPolicy.Barred(p)));
level = next.Select(p => p.ID).ToList();
@@ -324,9 +313,9 @@ namespace PrivaPub.Federation.Controllers
[HttpGet, Route("{actor}/parrot-licences/{licenceId}")]
public async Task<IActionResult> ParrotLicence(string actor, string licenceId, CancellationToken token)
{
var author = await _localActors.FindByUserName(actor, token);
var author = await localActors.FindByUserName(actor, token);
var licence = author == default ? default : await DB.Default.Find<QuoteLicence>().Match(l => l.ID == licenceId && l.AuthorAvatarId == author.Id).ExecuteFirstAsync(token);
var quoted = licence == default ? default : await _dbEntities.Posts.MatchID(licence.PostId).ExecuteFirstAsync(token);
var quoted = licence == default ? default : await dbEntities.Posts.MatchID(licence.PostId).ExecuteFirstAsync(token);
if (quoted == default)
return NotFound();
var uri = Domain.Statuses.QuoteService.LicenceUri(author, licence.ID);
@@ -353,13 +342,13 @@ namespace PrivaPub.Federation.Controllers
{
if (activityId.StartsWith("announce-", StringComparison.Ordinal))
{
var booster = await _localActors.FindByUserName(actor, token);
var booster = await localActors.FindByUserName(actor, token);
var uri = booster?.ActivityUri(activityId);
if (booster is { Kind: LocalActorKind.Group, IsCircle: false })
return await GroupAnnounce(booster, activityId, uri, token);
var reblog = uri == default ? default : await _dbEntities.Posts.Match(p => p.ObjectURI == uri && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
var reblog = uri == default ? default : await dbEntities.Posts.Match(p => p.ObjectURI == uri && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
var rendered = reblog == default ? default : await AnnounceFor(reblog, token);
if (rendered != default && WantsHtml() && await _dbEntities.Posts.MatchID(reblog.ReblogOfPostId).ExecuteFirstAsync(token) is { } boosted)
if (rendered != default && WantsHtml() && await dbEntities.Posts.MatchID(reblog.ReblogOfPostId).ExecuteFirstAsync(token) is { } boosted)
return Redirect(boosted.Url ?? boosted.ObjectURI);//a boost has no page of its own: the boosted post's
return rendered == default ? NotFound() : Activity(rendered);
}
@@ -377,14 +366,14 @@ namespace PrivaPub.Federation.Controllers
[HttpGet, Route("{actor}/whispers/{conversationId}")]
public async Task<IActionResult> Whispers(string actor, string conversationId, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { IsFederated: true, Kind: LocalActorKind.Person })
return NotFound();
var uri = local.ConversationUri(conversationId);
var conversation = await _dbEntities.DmGroups.Match(g => g.ID == conversationId && g.ConversationURI == uri && !g.DeletionAt.HasValue).ExecuteFirstAsync(token);
if (conversation == default || !SignedFetchAuthorizer.MayReadConversation(conversation, await _fetches.Requester(Request, token)))
var conversation = await dbEntities.DmGroups.Match(g => g.ID == conversationId && g.ConversationURI == uri && !g.DeletionAt.HasValue).ExecuteFirstAsync(token);
if (conversation == default || !SignedFetchAuthorizer.MayReadConversation(conversation, await fetches.Requester(Request, token)))
return NotFound();
var posts = await _dbEntities.Posts
var posts = await dbEntities.Posts
.Match(p => p.ConversationId == conversationId && p.Visibility == PostVisibility.Direct && !p.DeletedAt.HasValue)
.Sort(p => p.CreationDate, Order.Ascending)
.ExecuteAsync(token);
@@ -401,28 +390,28 @@ namespace PrivaPub.Federation.Controllers
[HttpPost, Route("{actor}/mouth"), EnableRateLimiting(RateLimiting.Inbox)]
public async Task<IActionResult> Inbox(string actor, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
if (local == default && await _localActors.Gone(actor, token) != default)
var local = await localActors.FindByUserName(actor, token);
if (local == default && await localActors.Gone(actor, token) != default)
return StatusCode(StatusCodes.Status410Gone);
if (local is not { IsFederated: true })
return Answer(_inbox.NoSuchRecipient(Request));
return Answer(await _inbox.Receive(Request, local, token));
return Answer(inboxReceiver.NoSuchRecipient(Request));
return Answer(await inboxReceiver.Receive(Request, local, token));
}
[HttpPost, Route("{actor}/human-centipede"), EnableRateLimiting(RateLimiting.Inbox)]
public async Task<IActionResult> ActorSharedInbox(string actor, CancellationToken token) =>
Answer(await _inbox.Receive(Request, default, token));
Answer(await inboxReceiver.Receive(Request, default, token));
[HttpPost, Route("/human-centipede"), EnableRateLimiting(RateLimiting.Inbox)]
public async Task<IActionResult> SharedInbox(CancellationToken token) =>
Answer(await _inbox.Receive(Request, default, token));
Answer(await inboxReceiver.Receive(Request, default, token));
async Task<(LocalActor Actor, PostEntity Post)> PublicPost(string actor, string postId, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { IsFederated: true, Kind: LocalActorKind.Person })
return default;
var post = await _dbEntities.Posts
var post = await dbEntities.Posts
.Match(p => p.ID == postId && p.GroupUserId == local.Id && !p.IsFederatedCopy && p.ReblogOfPostId == null)
.Match(VisibilityPolicy.IsPublic)
.ExecuteFirstAsync(token);
@@ -434,24 +423,24 @@ namespace PrivaPub.Federation.Controllers
// member, or the members on the requesting instance actor's server.
async Task<(LocalActor Author, PostEntity Post, JsonObject Note)?> SignedPost(string actor, string postId, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { IsFederated: true, Kind: LocalActorKind.Person })
return default;
var post = await _dbEntities.Posts
var post = await dbEntities.Posts
.Match(p => p.ID == postId && p.GroupUserId == local.Id && !p.IsFederatedCopy && p.ReblogOfPostId == null
&& (p.Visibility == PostVisibility.FollowersOnly || p.Visibility == PostVisibility.Direct || p.Visibility == PostVisibility.Circle))
.ExecuteFirstAsync(token);
if (post == default)
return default;
var requester = await _fetches.Requester(Request, token);
if (!await _fetches.MayRead(post, requester, token))
var requester = await fetches.Requester(Request, token);
if (!await fetches.MayRead(post, requester, token))
return default;
if (post.Visibility == PostVisibility.Circle)
{
var circle = await _dbEntities.Groups.MatchID(post.GroupId).ExecuteFirstAsync(token);
var note = ActivityPubRenderer.Note(post, local, _localActors.FromGroup(circle), post.InReplyToURI);
var circle = await dbEntities.Groups.MatchID(post.GroupId).ExecuteFirstAsync(token);
var note = ActivityPubRenderer.Note(post, local, localActors.FromGroup(circle), post.InReplyToURI);
var readers = SignedFetchAuthorizer.CircleReaders(circle, requester);
var named = readers.Count == 0 ? new List<Models.User.ForeignAvatar>() : await _dbEntities.ForeignAvatars.Match(a => readers.Contains(a.ActorURI)).ExecuteAsync(token);
var named = readers.Count == 0 ? new List<Models.User.ForeignAvatar>() : await dbEntities.ForeignAvatars.Match(a => readers.Contains(a.ActorURI)).ExecuteAsync(token);
return (local, post, OutboxPublisher.Naming(note, named));
}
var rendered = post.Visibility == PostVisibility.Direct
@@ -464,10 +453,10 @@ namespace PrivaPub.Federation.Controllers
async Task<IActionResult> Tombstone(string actor, string postId, CancellationToken token)
{
var local = await _localActors.FindByUserName(actor, token);
var local = await localActors.FindByUserName(actor, token);
if (local is not { IsFederated: true, Kind: LocalActorKind.Person })
return default;
var deleted = await _dbEntities.Posts
var deleted = await dbEntities.Posts
.Match(p => p.ID == postId && p.GroupUserId == local.Id && !p.IsFederatedCopy && p.DeletedAt.HasValue
&& (p.Visibility == PostVisibility.Public || p.Visibility == PostVisibility.Unlisted))
.ExecuteFirstAsync(token);
@@ -493,7 +482,7 @@ namespace PrivaPub.Federation.Controllers
{
if (await DB.Default.Find<GroupAnnouncement>().Match(a => a.ActivityURI == uri && a.GroupId == group.Id).ExecuteFirstAsync(token) is { } kept)
{
var about = await _dbEntities.Posts.Match(p => p.ObjectURI == kept.ObjectURI).ExecuteFirstAsync(token);
var about = await dbEntities.Posts.Match(p => p.ObjectURI == kept.ObjectURI).ExecuteFirstAsync(token);
if (!VisibilityPolicy.Shown(about) || about.Visibility is not (PostVisibility.Public or PostVisibility.Unlisted))
return StatusCode(StatusCodes.Status410Gone);
var body = JsonNode.Parse(kept.Body)!.AsObject();
@@ -501,13 +490,13 @@ namespace PrivaPub.Federation.Controllers
return Activity(body);
}
var postId = activityId["announce-".Length..];
var post = await _dbEntities.Posts.Match(p => p.ID == postId && p.GroupId == group.Id).Match(VisibilityPolicy.IsPublic).ExecuteFirstAsync(token);
var post = await dbEntities.Posts.Match(p => p.ID == postId && p.GroupId == group.Id).Match(VisibilityPolicy.IsPublic).ExecuteFirstAsync(token);
return post == default ? NotFound() : Activity(ActivityPubRenderer.Announce(group, post.ObjectURI, activityId, post.CreationDate));
}
async Task<JsonObject> AnnounceFor(PostEntity reblog, CancellationToken token)
{
var original = await _dbEntities.Posts.MatchID(reblog.ReblogOfPostId).ExecuteFirstAsync(token);
var original = await dbEntities.Posts.MatchID(reblog.ReblogOfPostId).ExecuteFirstAsync(token);
if (!VisibilityPolicy.Shown(original))
return default;
return new JsonObject
@@ -525,7 +514,7 @@ namespace PrivaPub.Federation.Controllers
async Task<JsonObject> CreateFor(PostEntity post, LocalActor author, 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);
var note = ActivityPubRenderer.Note(post, author, group, post.InReplyToURI);
return ActivityPubRenderer.Create(author, note, $"create-{post.ID}");
}
@@ -546,9 +535,9 @@ namespace PrivaPub.Federation.Controllers
// SecureMode asks every reader of ActivityPub documents for a signature, except for the server's own actors (the
// instance actor, the reporter, the reader of feeds), whose keys peers need first, and except for browsers, which
// only get redirected to the public pages
if (_federation.CurrentValue.SecureMode && HttpMethods.IsGet(Request.Method) && !WantsHtml() && Request.Path.Value != "/"
if (federation.CurrentValue.SecureMode && HttpMethods.IsGet(Request.Method) && !WantsHtml() && Request.Path.Value != "/"
&& !LocalActorService.IsServerActorName(context.RouteData.Values["actor"] as string)
&& await _fetches.Requester(Request, HttpContext.RequestAborted) == default)
&& await fetches.Requester(Request, HttpContext.RequestAborted) == default)
{
context.Result = StatusCode(StatusCodes.Status401Unauthorized);
return;
@@ -557,7 +546,7 @@ namespace PrivaPub.Federation.Controllers
// nothing new is told of it, and lifting the suspension serves it all again (owner decision 2026-10-17)
if (HttpMethods.IsGet(Request.Method) && context.RouteData.Values["actor"] is string name
&& context.ActionDescriptor is Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor { ActionName: not nameof(GetActor) }
&& await _localActors.FindByUserName(name, HttpContext.RequestAborted) is { Kind: LocalActorKind.Person, Suspended: true })
&& await localActors.FindByUserName(name, HttpContext.RequestAborted) is { Kind: LocalActorKind.Person, Suspended: true })
{
context.Result = NotFound();
return;
@@ -568,7 +557,7 @@ namespace PrivaPub.Federation.Controllers
IActionResult Answer(InboxResult result)
{
if (result.Error != default)
_logger.LogInformation("Inbox refused with {Status} ({Reason}): {Error}", result.StatusCode, result.Reason, result.Error);
logger.LogInformation("Inbox refused with {Status} ({Reason}): {Error}", result.StatusCode, result.Reason, result.Error);
if (result.RetryAfterSeconds is { } seconds)
Response.Headers.RetryAfter = seconds.ToString(System.Globalization.CultureInfo.InvariantCulture);
return result.Error == default ? StatusCode(result.StatusCode) : StatusCode(result.StatusCode, result.Error);
@@ -18,31 +18,20 @@ using PrivaPub.Infrastructure;
namespace PrivaPub.Federation.Controllers
{
[ApiController]
public class WellKnownController : ControllerBase
public class WellKnownController(
ILocalActorService localActors,
IOptionsMonitor<RegistrationOptions> registrations,
Domain.Instance.IInstanceSettings settings) : ControllerBase
{
readonly ILocalActorService _localActors;
readonly DbEntities _dbEntities;
readonly IOptionsMonitor<RegistrationOptions> _registrations;
readonly Domain.Instance.IInstanceSettings _settings;
public WellKnownController(ILocalActorService localActors, DbEntities dbEntities, IOptionsMonitor<RegistrationOptions> registrations,
Domain.Instance.IInstanceSettings settings)
{
_registrations = registrations;
_settings = settings;
_localActors = localActors;
_dbEntities = dbEntities;
}
[HttpGet, Route("/.well-known/webfinger")]
public async Task<IActionResult> WebFinger([FromQuery] string resource, CancellationToken token)
{
if (string.IsNullOrEmpty(resource))
return BadRequest();
// the server itself (FEP-d556): its instance actor
if (resource.TrimEnd('/') == _localActors.BaseAddress)
if (resource.TrimEnd('/') == localActors.BaseAddress)
{
var instance = await _localActors.GetInstanceActor(token);
var instance = await localActors.GetInstanceActor(token);
return Content(new JsonObject
{
["subject"] = resource,
@@ -53,7 +42,7 @@ namespace PrivaPub.Federation.Controllers
}.ToJsonString(), "application/jrd+json; charset=utf-8");
}
LocalActor actor;
var actor = default(LocalActor);
// Mastodon, GoToSocial and Pleroma also take a bare user@domain or @user@domain, so we do too.
var acct = resource.StartsWith("acct:", StringComparison.OrdinalIgnoreCase) ? resource[5..]
: !resource.Contains("://", StringComparison.Ordinal) && resource.Contains('@') ? resource
@@ -61,15 +50,15 @@ namespace PrivaPub.Federation.Controllers
if (acct != default)
{
var parts = acct.TrimStart('@').Split('@');
var domain = new Uri(_localActors.BaseAddress).Authority;
var domain = new Uri(localActors.BaseAddress).Authority;
if (parts.Length != 2 || !parts[1].Equals(domain, StringComparison.OrdinalIgnoreCase))
return NotFound();
actor = await _localActors.FindByUserName(parts[0], token);
if (actor == default && await _localActors.Gone(parts[0], token) != default)
actor = await localActors.FindByUserName(parts[0], token);
if (actor == default && await localActors.Gone(parts[0], token) != default)
return StatusCode(StatusCodes.Status410Gone);
}
else
actor = await _localActors.FindByUri(resource, token);
actor = await localActors.FindByUri(resource, token);
if (actor is not { IsFederated: true })
return NotFound();
@@ -96,18 +85,18 @@ namespace PrivaPub.Federation.Controllers
new JsonObject
{
["rel"] = "http://nodeinfo.diaspora.software/ns/schema/2.1",
["href"] = $"{_localActors.BaseAddress}/nodeinfo/2.1"
["href"] = $"{localActors.BaseAddress}/nodeinfo/2.1"
},
new JsonObject
{
["rel"] = "http://nodeinfo.diaspora.software/ns/schema/2.0",
["href"] = $"{_localActors.BaseAddress}/nodeinfo/2.0"
["href"] = $"{localActors.BaseAddress}/nodeinfo/2.0"
},
// the application actor (FEP-2677)
new JsonObject
{
["rel"] = "https://www.w3.org/ns/activitystreams#Application",
["href"] = (await _localActors.GetInstanceActor(token)).Uri
["href"] = (await localActors.GetInstanceActor(token)).Uri
})
};
return Content(document.ToJsonString(), "application/json; charset=utf-8");
@@ -130,7 +119,7 @@ namespace PrivaPub.Federation.Controllers
["software"] = software,
["protocols"] = new JsonArray("activitypub"),
["services"] = new JsonObject { ["inbound"] = new JsonArray(), ["outbound"] = new JsonArray() },
["openRegistrations"] = _registrations.CurrentValue.IsOpen,
["openRegistrations"] = registrations.CurrentValue.IsOpen,
["usage"] = new JsonObject
{
["users"] = new JsonObject
@@ -144,8 +133,8 @@ namespace PrivaPub.Federation.Controllers
["metadata"] = new JsonObject
{
// the server's name and short description, as its administrator set them (IInstanceSettings)
["nodeName"] = _settings.Current.Title,
["nodeDescription"] = _settings.Current.ShortDescription ?? string.Empty,
["nodeName"] = settings.Current.Title,
["nodeDescription"] = settings.Current.ShortDescription ?? string.Empty,
["federation"] = new JsonObject { ["document"] = "https://git.thepra.dev/thepra/SocialPub/src/branch/master/FEDERATION.md" }
}
};
+28 -49
View File
@@ -23,21 +23,12 @@ namespace PrivaPub.Federation.Crawler
}
//each hour, the servers due for a visit, spread across the hour so the crawler reaches one at a time
public class CrawlPlanner : IJobHandler
public class CrawlPlanner(
IJobQueue queue,
IDomainBlocks blocks,
IOptionsMonitor<StatisticsOptions> options,
IOptionsMonitor<FederationOptions> federation = default) : IJobHandler
{
readonly IJobQueue _queue;
readonly IDomainBlocks _blocks;
readonly IOptionsMonitor<StatisticsOptions> _options;
readonly IOptionsMonitor<FederationOptions> _federation;
public CrawlPlanner(IJobQueue queue, IDomainBlocks blocks, IOptionsMonitor<StatisticsOptions> options, IOptionsMonitor<FederationOptions> federation = default)
{
_queue = queue;
_blocks = blocks;
_options = options;
_federation = federation;
}
public JobKind Kind => JobKind.CrawlPlan;
public int Concurrency => 1;
public int MaxAttempts => 1;
@@ -53,11 +44,11 @@ namespace PrivaPub.Federation.Crawler
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
{
var crawler = _options.CurrentValue.Crawler;
var crawler = options.CurrentValue.Crawler;
if (!crawler.Enabled)
return JobOutcome.Done;
var now = DateTime.UtcNow;
foreach (var seed in PeerHosts.Filter(crawler.Seeds ?? new(), host => !_blocks.IsSuspended(host), _federation?.CurrentValue.AllowPrivateNetworks == true))
foreach (var seed in PeerHosts.Filter(crawler.Seeds ?? new(), host => !blocks.IsSuspended(host), federation?.CurrentValue.AllowPrivateNetworks == true))
await DB.Default.Update<RemoteInstance>()
.Match(i => i.Host == seed)
.Modify(b => b.SetOnInsert(i => i.Seen, "crawled"))
@@ -73,7 +64,7 @@ namespace PrivaPub.Federation.Crawler
.Limit(perHour * 3)
.ExecuteAsync(token);
var week = InstanceDescriber.Week(now);
var jobs = candidates.Where(i => _blocks.Find(i.Host) == default && PeerHosts.Normalise(i.Host) != default)
var jobs = candidates.Where(i => blocks.Find(i.Host) == default && PeerHosts.Normalise(i.Host) != default)
.Take(perHour)
.Select((instance, index) => new Job
{
@@ -83,36 +74,24 @@ namespace PrivaPub.Federation.Crawler
DedupeKey = $"crawl|{instance.Host}|{week}",
RunAt = now.AddSeconds(index * 3600.0 / perHour)
});
await _queue.EnqueueMany(jobs, token);
await queue.EnqueueMany(jobs, token);
return JobOutcome.Done;
}
}
//one server: its robots.txt, a description if only crawling ever found it, then the servers its peers list names
public class InstanceCrawler : IJobHandler
public class InstanceCrawler(
IFederationHttp http,
InstanceDescriber describer,
IDomainBlocks blocks,
IOptionsMonitor<StatisticsOptions> options,
IOptionsMonitor<AppConfiguration> app,
IOptionsMonitor<FederationOptions> federation,
IInteractionLedger ledger = default) : IJobHandler
{
const int MaxPeers = 20_000;
const int MaxRobotsBytes = 64 * 1024;
readonly IFederationHttp _http;
readonly InstanceDescriber _describer;
readonly IDomainBlocks _blocks;
readonly IOptionsMonitor<StatisticsOptions> _options;
readonly IOptionsMonitor<AppConfiguration> _app;
readonly IOptionsMonitor<FederationOptions> _federation;
readonly IInteractionLedger _ledger;
public InstanceCrawler(IFederationHttp http, InstanceDescriber describer, IDomainBlocks blocks, IOptionsMonitor<StatisticsOptions> options,
IOptionsMonitor<AppConfiguration> app, IOptionsMonitor<FederationOptions> federation, IInteractionLedger ledger = default)
{
_http = http;
_describer = describer;
_blocks = blocks;
_options = options;
_app = app;
_federation = federation;
_ledger = ledger;
}
public JobKind Kind => JobKind.CrawlInstance;
public int Concurrency => 1;
@@ -123,16 +102,16 @@ namespace PrivaPub.Federation.Crawler
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
{
var crawler = _options.CurrentValue.Crawler;
var crawler = options.CurrentValue.Crawler;
var host = job.Payload;
if (!crawler.Enabled || _blocks.Find(host) != default)
if (!crawler.Enabled || blocks.Find(host) != default)
return JobOutcome.Done;
using var crawl = HttpScope.Crawl(Stargazer.UserAgent(_app.CurrentValue.BackendBaseAddress));
using var crawl = HttpScope.Crawl(Stargazer.UserAgent(app.CurrentValue.BackendBaseAddress));
RobotsTxt robots;
var robots = default(RobotsTxt);
using (HttpScope.For("robots"))
{
var (status, text) = await _http.GetText(Address($"https://{host}/robots.txt"), MaxRobotsBytes, token);
var (status, text) = await http.GetText(Address($"https://{host}/robots.txt"), MaxRobotsBytes, token);
robots = RobotsTxt.From(status, text, Stargazer.Token, "PrivaPub");
}
var instance = await DB.Default.Find<RemoteInstance>().Match(i => i.Host == host).ExecuteFirstAsync(token);
@@ -146,14 +125,14 @@ namespace PrivaPub.Federation.Crawler
var reachable = true;
if (instance?.Seen != "touched")
reachable = await _describer.Describe(host, crawled: true, robots.Allows, token);
reachable = await describer.Describe(host, crawled: true, robots.Allows, token);
int? peersCount = default;
if (robots.Allows("/api/v1/instance/peers"))
{
IReadOnlyList<string> peers;
var peers = default(IReadOnlyList<string>);
using (HttpScope.For("peers"))
peers = await _http.GetStringArray(Address($"https://{host}/api/v1/instance/peers"), MaxPeers, token);
peers = await http.GetStringArray(Address($"https://{host}/api/v1/instance/peers"), MaxPeers, token);
peersCount = peers.Count;
await Discover(peers, crawler, token);
}
@@ -167,8 +146,8 @@ namespace PrivaPub.Federation.Crawler
var room = Math.Min(crawler.MaxNewHostsPerCrawl, crawler.MaxHosts - (int)await DB.Default.CountAsync<RemoteInstance>(i => i.Seen == "crawled", token));
if (room <= 0)
return;
var candidates = PeerHosts.Filter(peers, host => _http.IsAllowed(new Uri($"https://{host}/")) && _blocks.Find(host) == default,
_federation.CurrentValue.AllowPrivateNetworks).ToList();
var candidates = PeerHosts.Filter(peers, host => http.IsAllowed(new Uri($"https://{host}/")) && blocks.Find(host) == default,
federation.CurrentValue.AllowPrivateNetworks).ToList();
var now = DateTime.UtcNow;
foreach (var batch in candidates.Chunk(500))
{
@@ -203,6 +182,6 @@ namespace PrivaPub.Federation.Crawler
}
void Record(string host, string outcome, string reason) =>
_ledger?.Record(new InteractionEvent { Channel = Interactions.Crawl, Host = host, Outcome = outcome, Reason = reason, Crawl = true });
ledger?.Record(new InteractionEvent { Channel = Interactions.Crawl, Host = host, Outcome = outcome, Reason = reason, Crawl = true });
}
}
@@ -12,53 +12,40 @@ using static PrivaPub.Federation.Objects.ActivityJson;
namespace PrivaPub.Federation.Inbox.Handlers
{
public class AcceptHandler : IActivityHandler
public class AcceptHandler(
DbEntities db,
ILocalActorService localActorService,
IQuoteService quotes,
IInteractionApprovals approvals = default,
IParticipations participations = default,
Relays.IRelays relays = default,
Infrastructure.Jobs.IJobQueue queue = default) : IActivityHandler
{
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly IQuoteService _quotes;
readonly IInteractionApprovals _approvals;
readonly IParticipations _participations;
readonly Relays.IRelays _relays;
readonly Infrastructure.Jobs.IJobQueue _queue;
public AcceptHandler(DbEntities dbEntities, ILocalActorService localActors, IQuoteService quotes, IInteractionApprovals approvals = default,
IParticipations participations = default, Relays.IRelays relays = default, Infrastructure.Jobs.IJobQueue queue = default)
{
_queue = queue;
_relays = relays;
_participations = participations;
_dbEntities = dbEntities;
_localActors = localActors;
_quotes = quotes;
_approvals = approvals;
}
public virtual string Type => "Accept";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
{
if (await _quotes.Answered(activity, actor, accepted: Type == "Accept", token))
if (await quotes.Answered(activity, actor, accepted: Type == "Accept", token))
{
Arrival.Accept("quote-answer");
return;
}
if (_approvals != default && await _approvals.Answered(activity, actor, accepted: Type == "Accept", token))
if (approvals != default && await approvals.Answered(activity, actor, accepted: Type == "Accept", token))
{
Arrival.Accept("interaction-answer");
return;
}
if (_participations != default && await _participations.Answered(activity, actor, accepted: Type == "Accept", token))
if (participations != default && await participations.Answered(activity, actor, accepted: Type == "Accept", token))
{
Arrival.Accept("participation-answer");
return;
}
if (_relays != default && await _relays.Answered(activity, actor, accepted: Type == "Accept", token))
if (relays != default && await relays.Answered(activity, actor, accepted: Type == "Accept", token))
{
Arrival.Accept("relay-answer");
return;
}
var following = await FindFollowing(activity["object"], actor, _dbEntities, _localActors, token);
var following = await FindFollowing(activity["object"], actor, db, localActorService, token);
if (following == default)
{
Arrival.Drop("unknown-object");
@@ -73,8 +60,8 @@ namespace PrivaPub.Federation.Inbox.Handlers
protected virtual async Task Apply(Following following, CancellationToken token)
{
await DB.Default.Update<Following>().MatchID(following.ID).Modify(f => f.State, FollowState.Accepted).ExecuteAsync(token);
if (_queue != default)
await _queue.Enqueue(Models.Jobs.JobKind.BackfillOutbox, following.TargetActorURI, new Uri(following.TargetActorURI).Host,
if (queue != default)
await queue.Enqueue(Models.Jobs.JobKind.BackfillOutbox, following.TargetActorURI, new Uri(following.TargetActorURI).Host,
Actors.OutboxBackfill.DedupeKey(following.TargetActorURI, DateTime.UtcNow), token);
}
@@ -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");
@@ -17,22 +17,15 @@ namespace PrivaPub.Federation.Inbox.Handlers
// A remote account blocks one of our personas, as Mastodon does it: the follows between them end here too (the blocker
// already ended its side, so nothing is sent back), the persona stops seeing the blocker's posts and notifications, the
// relationship says blocked_by, and nothing of the persona's is addressed to the blocker any more. Undo{Block} lifts it.
public class BlockHandler : IActivityHandler
public class BlockHandler(
DbEntities dbEntities,
ILocalActorService localActorService) : IActivityHandler
{
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
public BlockHandler(DbEntities dbEntities, ILocalActorService localActors)
{
_dbEntities = dbEntities;
_localActors = localActors;
}
public string Type => "Block";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
{
var target = Id(activity["object"]) is { } uri ? await _localActors.FindByUri(uri, token) : default;
var target = Id(activity["object"]) is { } uri ? await localActorService.FindByUri(uri, token) : default;
if (target is not { Kind: LocalActorKind.Person })
{
Arrival.Drop("unknown-object");
@@ -70,7 +63,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
async Task Ban(JsonNode activity, LocalActor persona, string communityUri, CancellationToken token)
{
var community = await _dbEntities.ForeignAvatars.Match(f => f.ActorURI == communityUri && f.AvatarType == AvatarType.Group).ExecuteFirstAsync(token);
var community = await dbEntities.ForeignAvatars.Match(f => f.ActorURI == communityUri && f.AvatarType == AvatarType.Group).ExecuteFirstAsync(token);
if (community == default)
{
Arrival.Drop("unknown-community");
@@ -26,45 +26,22 @@ using PostEntity = PrivaPub.Models.Post.Post;
namespace PrivaPub.Federation.Inbox.Handlers
{
public class CreateHandler : IActivityHandler
public class CreateHandler(
DbEntities dbEntities,
ILocalActorService localActors,
IRemoteActorService remoteActors,
IDeliveryService delivery,
IFanout fanout,
IRemotePosts remotePosts,
IGroupDistributor groups,
IObjectRecords records,
IPollService polls,
ILinkPreviews previews,
IQuoteService quotes,
IInteractionApprovals approvals = default,
Relays.IRelays relays = default,
IWalls walls = default) : IActivityHandler
{
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly IRemoteActorService _remoteActors;
readonly IDeliveryService _delivery;
readonly IDomainBlocks _domainBlocks;
readonly IFanout _fanout;
readonly IRemotePosts _remotePosts;
readonly IGroupDistributor _groups;
readonly IObjectRecords _records;
readonly IPollService _polls;
readonly ILinkPreviews _previews;
readonly IQuoteService _quotes;
readonly IInteractionApprovals _approvals;
readonly Relays.IRelays _relays;
readonly IWalls _walls;
public CreateHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery,
IDomainBlocks domainBlocks, IFanout fanout, IRemotePosts remotePosts, IGroupDistributor groups, IObjectRecords records, IPollService polls, ILinkPreviews previews, IQuoteService quotes,
IInteractionApprovals approvals = default, Relays.IRelays relays = default, IWalls walls = default)
{
_walls = walls;
_relays = relays;
_approvals = approvals;
_quotes = quotes;
_previews = previews;
_records = records;
_polls = polls;
_groups = groups;
_fanout = fanout;
_remotePosts = remotePosts;
_dbEntities = dbEntities;
_localActors = localActors;
_remoteActors = remoteActors;
_delivery = delivery;
_domainBlocks = domainBlocks;
}
public string Type => "Create";
public async Task Handle(JsonNode activity, ForeignAvatar author, CancellationToken token)
@@ -73,7 +50,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
var refetched = node is not JsonObject || !Origin.Same(Id(node), author.ActorURI);
if (refetched)
{
using var fetched = await _remoteActors.FetchObject(Id(node), token);
using var fetched = await remoteActors.FetchObject(Id(node), token);
node = fetched == default ? default : JsonNode.Parse(fetched.Root.GetRawText());
if (node == default)
Arrival.Drop("fetch-failed");
@@ -85,11 +62,11 @@ namespace PrivaPub.Federation.Inbox.Handlers
{
if (!refetched)
{
using var fetched = await _remoteActors.FetchObject(note.Id, token);
using var fetched = await remoteActors.FetchObject(note.Id, token);
note = fetched == default ? default : NoteParser.Parse(JsonNode.Parse(fetched.Root.GetRawText()));
refetched = true;
}
author = note == default ? default : await _remoteActors.GetActor(note.AttributedTo, refresh: false, token);
author = note == default ? default : await remoteActors.GetActor(note.AttributedTo, refresh: false, token);
if (author == default)
{
Arrival.Drop(note == default ? "fetch-failed" : "author-unavailable");
@@ -104,16 +81,16 @@ namespace PrivaPub.Federation.Inbox.Handlers
Arrival.About(note.Type);
if (note.Title != default && note.InReplyTo != default && ObjectShapes.Text(note.ContentHtml, 1) == default)
{
var question = await _dbEntities.Posts.Match(p => p.ObjectURI == note.InReplyTo && p.Poll != null).ExecuteFirstAsync(token);
var question = await dbEntities.Posts.Match(p => p.ObjectURI == note.InReplyTo && p.Poll != null).ExecuteFirstAsync(token);
if (question != default)
{
await _polls.Receive(question, author, note.Title, Id(activity), token);
await polls.Receive(question, author, note.Title, Id(activity), token);
Arrival.Accept("poll-vote");
Arrival.About(visibility: question.Visibility);
return;
}
}
if (await _dbEntities.Posts.Match(p => p.ObjectURI == note.Id).ExecuteAnyAsync(token))
if (await dbEntities.Posts.Match(p => p.ObjectURI == note.Id).ExecuteAnyAsync(token))
{
Arrival.Drop("duplicate");
return;
@@ -146,7 +123,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
var localTargets = new List<LocalActor>();
for (var i = 0; i < addressed.Count; i++)
{
var local = await _localActors.FindByAddress(addressed[i], token);
var local = await localActors.FindByAddress(addressed[i], token);
if (local == default)
continue;
addressed[i] = local.Uri;
@@ -158,9 +135,9 @@ namespace PrivaPub.Federation.Inbox.Handlers
var parent = string.IsNullOrEmpty(note.InReplyTo)
? default
: await _dbEntities.Posts.Match(p => p.ObjectURI == note.InReplyTo && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
: await dbEntities.Posts.Match(p => p.ObjectURI == note.InReplyTo && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
// a reply its parent's author has not let in (GoToSocial's canReply), which a third party must not show
if (_approvals != default && parent != default && !await _approvals.MayReply(note, author, parent, token))
if (approvals != default && parent != default && !await approvals.MayReply(note, author, parent, token))
{
Arrival.Drop("reply-not-authorized");
return;
@@ -183,26 +160,26 @@ namespace PrivaPub.Federation.Inbox.Handlers
group = default;
var repliesToLocal = parent is { IsFederatedCopy: false } && visibility != PostVisibility.Direct;
var followed = visibility != PostVisibility.Direct && await _dbEntities.Followings
var followed = visibility != PostVisibility.Direct && await dbEntities.Followings
.Match(f => f.TargetActorURI == author.ActorURI && f.State == FollowState.Accepted)
.ExecuteAnyAsync(token);
// a reply in the thread of someone followed here, as Mastodon keeps them: what the thread's server forwards
var repliesToFollowed = !followed && parent is { IsFederatedCopy: true } && visibility is PostVisibility.Public or PostVisibility.Unlisted
&& await _dbEntities.Followings.Match(f => f.TargetActorURI == parent.ActorURI && f.State == FollowState.Accepted).ExecuteAnyAsync(token);
&& await dbEntities.Followings.Match(f => f.TargetActorURI == parent.ActorURI && f.State == FollowState.Accepted).ExecuteAnyAsync(token);
// a post in a remote group followed here that the group's own server sends itself rather than announcing it (Mbin
// delivers a magazine's threads to its subscribers as their author's Create): the group's server speaks for it
var inFollowedGroup = !followed && visibility is PostVisibility.Public or PostVisibility.Unlisted && note.Audience != default
&& Origin.Same(note.Audience, author.ActorURI) && Origin.Same(note.Id, note.Audience)
&& await _dbEntities.Followings.Match(f => f.TargetActorURI == note.Audience && f.State == FollowState.Accepted).ExecuteAnyAsync(token);
&& await dbEntities.Followings.Match(f => f.TargetActorURI == note.Audience && f.State == FollowState.Accepted).ExecuteAnyAsync(token);
// a public post a relay we subscribe to passes on, for the federated timeline (read again from its origin, as any
// forwarded post)
var relayed = !followed && visibility == PostVisibility.Public && _relays != default
&& await _relays.Passes(Arrival.Current?.ForwardedBy, token);
var relayed = !followed && visibility == PostVisibility.Public && relays != default
&& await relays.Passes(Arrival.Current?.ForwardedBy, token);
// written on a persona's wall (FEP-400e): public, not a reply, by an account following the persona, as its actor
// tells Smithereen
var wall = _walls == default || note.Wall == default ? default : await _walls.OwnerOf(note.Wall, token);
var wall = walls == default || note.Wall == default ? default : await walls.OwnerOf(note.Wall, token);
if (wall != default && (visibility is not (PostVisibility.Public or PostVisibility.Unlisted) || !string.IsNullOrEmpty(note.InReplyTo)
|| note.WallOwner != default && note.WallOwner != wall.Uri || !await _walls.MayWrite(wall, author, token)))
|| note.WallOwner != default && note.WallOwner != wall.Uri || !await walls.MayWrite(wall, author, token)))
{
Arrival.Drop("wall-refused");
return;
@@ -214,7 +191,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
return;
}
if (parent == default && visibility != PostVisibility.Direct)
parent = await _remotePosts.Parent(note.InReplyTo, 1, token);
parent = await remotePosts.Parent(note.InReplyTo, 1, token);
DmGroup conversation = default;
if (visibility == PostVisibility.Direct)
@@ -226,7 +203,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
conversation = await FindOrCreateConversation(participants, Origin.Same(note.Context, author.ActorURI) ? note.Context : default, token);
}
var post = await _remotePosts.Build(note, author, visibility, to, cc, parent, token);
var post = await remotePosts.Build(note, author, visibility, to, cc, parent, token);
post.ActivityURI = Id(activity);
post.GroupId = group?.Id;
post.ConversationId = conversation?.ID;
@@ -247,31 +224,31 @@ namespace PrivaPub.Federation.Inbox.Handlers
}
Arrival.Accept("stored");
Arrival.About(local: group ?? persons.FirstOrDefault());
await _records.Record(note, post, ObjectPath.Delivered, refetched, token);
await _previews.Wanted(post, token);
await _quotes.Resolve(post, note, token);
await records.Record(note, post, ObjectPath.Delivered, refetched, token);
await previews.Wanted(post, token);
await quotes.Resolve(post, note, token);
if (parent != default && Domain.Privacy.Counted.Reply(post))
await DB.Default.Update<PostEntity>().MatchID(parent.ID).Modify(b => b.Inc(p => p.RepliesCount, 1)).ExecuteAsync(token);
await _fanout.Distribute(post, token);
await ReplyRelay.Pass(post, _dbEntities, _localActors, _delivery, token);
await fanout.Distribute(post, token);
await ReplyRelay.Pass(post, dbEntities, localActors, delivery, token);
if (conversation != default)
{
await Domain.Statuses.ConversationStates.Posted(conversation.ID, post.ID, default, token);
await Domain.Timelines.Fanout.Conversation(conversation.ID, token);//the `direct` streams (R8), once it is the newest
}
if (group is { IsCircle: false })
await _groups.Announce(group, activity.AsObject(), note.Id, isNewPost: true, token);
await groups.Announce(group, activity.AsObject(), note.Id, isNewPost: true, token);
if (wall != default)
await _walls.Hosted(post, wall, token);
await walls.Hosted(post, wall, token);
}
async Task<bool> IsCircleMember(LocalActor circle, string actorUri, CancellationToken token) =>
await _dbEntities.Groups.Match(g => g.ID == circle.Id && g.Members.Any(m => m.IsForeign && m.AvatarId == actorUri)).ExecuteAnyAsync(token);
await dbEntities.Groups.Match(g => g.ID == circle.Id && g.Members.Any(m => m.IsForeign && m.AvatarId == actorUri)).ExecuteAnyAsync(token);
async Task<bool> MayPost(LocalActor community, string actorUri, CancellationToken token)
{
var entity = await _dbEntities.Groups.MatchID(community.Id).ExecuteFirstAsync(token);
var entity = await dbEntities.Groups.MatchID(community.Id).ExecuteFirstAsync(token);
return entity?.PostingPolicy switch
{
PostingPolicy.Anyone => true,
@@ -285,7 +262,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
var members = new List<GroupMember>();
foreach (var uri in participantUris.Distinct(StringComparer.Ordinal))
{
var local = await _localActors.FindByUri(uri, token);
var local = await localActors.FindByUri(uri, token);
var member = local != default && local.Kind == LocalActorKind.Person
? new GroupMember { AvatarId = local.Id }
: new GroupMember { AvatarId = uri, IsForeign = true };
@@ -294,7 +271,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
}
var key = DmGroup.KeyOf(members);
var match = await _dbEntities.DmGroups.Match(g => g.ParticipantsKey == key && !g.DeletionAt.HasValue).ExecuteFirstAsync(token);
var match = await dbEntities.DmGroups.Match(g => g.ParticipantsKey == key && !g.DeletionAt.HasValue).ExecuteFirstAsync(token);
if (match != default)
return match;
@@ -21,26 +21,14 @@ using PostEntity = PrivaPub.Models.Post.Post;
namespace PrivaPub.Federation.Inbox.Handlers
{
public class DeleteHandler : IActivityHandler
public class DeleteHandler(
DbEntities dbEntities,
ILocalActorService localActors,
IRemoteActorService remoteActors,
IDeliveryService delivery,
IGroupDistributor groups,
IQuoteService quotes) : IActivityHandler
{
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly IRemoteActorService _remoteActors;
readonly IDeliveryService _delivery;
readonly IGroupDistributor _groups;
readonly IQuoteService _quotes;
public DeleteHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery,
IGroupDistributor groups, IQuoteService quotes)
{
_quotes = quotes;
_groups = groups;
_dbEntities = dbEntities;
_localActors = localActors;
_remoteActors = remoteActors;
_delivery = delivery;
}
public string Type => "Delete";
const int MaxObjects = 50;
@@ -70,7 +58,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
actor.DeletionAt = DateTime.UtcNow;
actor.AccountState = AvatarAccountState.Deleted;
await DB.Default.SaveAsync(actor, token);
var followers = await _dbEntities.Followers.Match(f => f.ActorURI == actor.ActorURI).ExecuteAsync(token);
var followers = await dbEntities.Followers.Match(f => f.ActorURI == actor.ActorURI).ExecuteAsync(token);
foreach (var follower in followers)
{
await DB.Default.DeleteAsync<Follower>(follower.ID);
@@ -85,24 +73,24 @@ namespace PrivaPub.Federation.Inbox.Handlers
}
await RemoteDeletes.Tombstone(objectUri, token);
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
var post = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
// deleted by another account of its author's server (Mobilizon's organiser deletes the group's event): once
// that server says it is gone
if (post == default && await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token) is { } held
&& held.ActorURI != actor.ActorURI && Origin.Same(held.ActorURI, actor.ActorURI) && await _remoteActors.IsGone(objectUri, token))
if (post == default && await dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token) is { } held
&& held.ActorURI != actor.ActorURI && Origin.Same(held.ActorURI, actor.ActorURI) && await remoteActors.IsGone(objectUri, token))
post = held;
if (post == default)
{
Arrival.Accept("tombstone-only");
await _quotes.Revoke(objectUri, token);
await quotes.Revoke(objectUri, token);
return;
}
Arrival.Accept("removed");
Arrival.About(post.ObjectType, post.Visibility, post.CreationDate);
await ReplyRelay.Pass(post, _dbEntities, _localActors, _delivery, token);
await ReplyRelay.Pass(post, dbEntities, localActors, delivery, token);
await RemoteDeletes.Remove(post, objectUri, token);
if (!string.IsNullOrEmpty(post.GroupId) && await _localActors.FindById(LocalActorKind.Group, post.GroupId, token) is { IsCircle: false } community)
await _groups.Announce(community, activity.AsObject(), post.ObjectURI, isNewPost: false, token);
if (!string.IsNullOrEmpty(post.GroupId) && await localActors.FindById(LocalActorKind.Group, post.GroupId, token) is { IsCircle: false } community)
await groups.Announce(community, activity.AsObject(), post.ObjectURI, isNewPost: false, token);
}
}
}
@@ -14,15 +14,9 @@ using PostEntity = PrivaPub.Models.Post.Post;
namespace PrivaPub.Federation.Inbox.Handlers
{
public class DislikeHandler : IActivityHandler
public class DislikeHandler(
DbEntities dbEntities) : IActivityHandler
{
readonly DbEntities _dbEntities;
public DislikeHandler(DbEntities dbEntities)
{
_dbEntities = dbEntities;
}
public string Type => "Dislike";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
@@ -30,7 +24,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
var objectUri = Id(activity["object"]);
var post = objectUri == default
? default
: await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
: await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
if (post == default || !await VisibilityPolicy.RemoteCanSee(post, actor, token))
{
Arrival.Drop(post == default ? "unknown-object" : "not-visible");
@@ -8,17 +8,10 @@ using static PrivaPub.Federation.Objects.ActivityJson;
namespace PrivaPub.Federation.Inbox.Handlers
{
public class EmojiReactHandler : IActivityHandler
public class EmojiReactHandler(
DbEntities dbEntities,
IReactions reactions) : IActivityHandler
{
readonly DbEntities _dbEntities;
readonly IReactions _reactions;
public EmojiReactHandler(DbEntities dbEntities, IReactions reactions)
{
_dbEntities = dbEntities;
_reactions = reactions;
}
public string Type => "EmojiReact";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
@@ -26,14 +19,14 @@ namespace PrivaPub.Federation.Inbox.Handlers
var objectUri = Id(activity["object"]);
var post = objectUri == default
? default
: await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
: await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
if (post == default)
{
Arrival.Drop("unknown-object");
return;
}
Arrival.About(post.ObjectType ?? "Note", post.Visibility, post.CreationDate);
await _reactions.Receive(post, actor, Value(activity, "content"), activity["tag"], Id(activity), token);
await reactions.Receive(post, actor, Value(activity, "content"), activity["tag"], Id(activity), token);
}
}
}
@@ -10,51 +10,39 @@ namespace PrivaPub.Federation.Inbox.Handlers
// its `featured` collection (as Mastodon sends it) or its `featuredTags` is a featured tag (R8). On its wall
// (Smithereen's, FEP-400e) it tells of a post someone wrote there, or takes it off. Any other target (a community's
// moderators) is not ours to keep.
public class FeaturedHandler : IActivityHandler
public class FeaturedHandler(
IFeaturedPosts featuredPosts,
bool add,
IWalls walls = default,
IRemoteActorService remoteActors = default,
Infrastructure.Jobs.IJobQueue queue = default,
Domain.Social.IFeaturedTags tags = default) : IActivityHandler
{
readonly IFeaturedPosts _featured;
readonly IWalls _walls;
readonly IRemoteActorService _remoteActors;
readonly Infrastructure.Jobs.IJobQueue _queue;
readonly Domain.Social.IFeaturedTags _tags;
readonly bool _add;
public FeaturedHandler(IFeaturedPosts featured, bool add, IWalls walls = default, IRemoteActorService remoteActors = default,
Infrastructure.Jobs.IJobQueue queue = default, Domain.Social.IFeaturedTags tags = default)
{
_tags = tags;
_featured = featured;
_walls = walls;
_remoteActors = remoteActors;
_queue = queue;
_add = add;
}
public string Type => _add ? "Add" : "Remove";
public string Type => add ? "Add" : "Remove";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
{
if (_add && _queue != default && await ConversationContainers.Unwrap(activity, actor, _queue, token))
if (add && queue != default && await ConversationContainers.Unwrap(activity, actor, queue, token))
return;
var target = Objects.ActivityJson.Id(activity["target"]);
// a collection of its own we do not know of: its document read again, at most hourly, in case it was kept before
// we read that collection (a wall)
if (_remoteActors != default && target != default && target != actor.FeaturedURL && target != actor.WallURL && target != actor.FeaturedTagsURL
if (remoteActors != default && target != default && target != actor.FeaturedURL && target != actor.WallURL && target != actor.FeaturedTagsURL
&& Objects.Origin.Same(target, actor.ActorURI) && actor.UpdatedAt < DateTime.UtcNow.AddHours(-1))
actor = await _remoteActors.GetActor(actor.ActorURI, refresh: true, token) ?? actor;
actor = await remoteActors.GetActor(actor.ActorURI, refresh: true, token) ?? actor;
// a featured hashtag: an object of its own, with no id (its href is no post), so before anything reads it as one
if (Objects.ActivityJson.Value(activity["object"], "type") == "Hashtag")
{
if (_tags != default && target != default && (target == actor.FeaturedURL || target == actor.FeaturedTagsURL))
await _tags.Receive(activity, actor, _add, token);
if (tags != default && target != default && (target == actor.FeaturedURL || target == actor.FeaturedTagsURL))
await tags.Receive(activity, actor, add, token);
else
Arrival.Drop("unknown-target");
return;
}
if (_walls != default && !string.IsNullOrEmpty(actor.WallURL) && target == actor.WallURL)
await _walls.Receive(activity, actor, _add, token);
if (walls != default && !string.IsNullOrEmpty(actor.WallURL) && target == actor.WallURL)
await walls.Receive(activity, actor, add, token);
else
await _featured.Receive(activity, actor, _add, token);
await featuredPosts.Receive(activity, actor, add, token);
}
}
}
@@ -12,17 +12,10 @@ using static PrivaPub.Federation.Objects.ActivityJson;
namespace PrivaPub.Federation.Inbox.Handlers
{
public class FlagHandler : IActivityHandler
public class FlagHandler(
DbEntities dbEntities,
ILocalActorService localActors) : IActivityHandler
{
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
public FlagHandler(DbEntities dbEntities, ILocalActorService localActors)
{
_dbEntities = dbEntities;
_localActors = localActors;
}
public string Type => "Flag";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
@@ -38,7 +31,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
var postUris = new List<string>();
foreach (var uri in objects.Take(50))
{
var local = await _localActors.FindByUri(uri, token);
var local = await localActors.FindByUri(uri, token);
if (local is { Kind: LocalActorKind.Person } && uri.TrimEnd('/') == local.Uri)
target ??= local;
else
@@ -46,9 +39,9 @@ namespace PrivaPub.Federation.Inbox.Handlers
}
var posts = postUris.Count == 0
? new List<Models.Post.Post>()
: await _dbEntities.Posts.Match(p => postUris.Contains(p.ObjectURI) && !p.IsFederatedCopy).ExecuteAsync(token);
: await dbEntities.Posts.Match(p => postUris.Contains(p.ObjectURI) && !p.IsFederatedCopy).ExecuteAsync(token);
if (target == default && posts.Count > 0)
target = await _localActors.FindById(LocalActorKind.Person, posts[0].GroupUserId, token);
target = await localActors.FindById(LocalActorKind.Person, posts[0].GroupUserId, token);
if (target == default)
{
Arrival.Drop("unknown-object");
@@ -21,21 +21,11 @@ using PostEntity = PrivaPub.Models.Post.Post;
namespace PrivaPub.Federation.Inbox.Handlers
{
public class FollowHandler : IActivityHandler
public class FollowHandler(
DbEntities dbEntities,
ILocalActorService localActors,
IDeliveryService delivery) : IActivityHandler
{
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly IRemoteActorService _remoteActors;
readonly IDeliveryService _delivery;
public FollowHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDeliveryService delivery)
{
_dbEntities = dbEntities;
_localActors = localActors;
_remoteActors = remoteActors;
_delivery = delivery;
}
public string Type => "Follow";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
@@ -43,7 +33,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
var follow = activity;
var follower = actor;
var target = await _localActors.FindByAddress(Id(follow["object"]), token);
var target = await localActors.FindByAddress(Id(follow["object"]), token);
if (target is not { IsFederated: true } || target.IsServerActor)
{
Arrival.Drop("unknown-recipient");
@@ -67,12 +57,12 @@ namespace PrivaPub.Federation.Inbox.Handlers
["actor"] = target.Uri,
["object"] = follow.DeepClone()
};
await _delivery.Enqueue(target, new[] { follower.InboxURL }, reject, token);
await delivery.Enqueue(target, new[] { follower.InboxURL }, reject, token: token);
Arrival.Reject("blocked");
return;
}
var existing = await _dbEntities.Followers
var existing = await dbEntities.Followers
.Match(f => f.LocalActorId == target.Id && f.LocalActorKind == target.Kind && f.ActorURI == follower.ActorURI)
.ExecuteFirstAsync(token);
var record = existing ?? new Follower
@@ -90,7 +80,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
if (target.Kind == LocalActorKind.Person)
await Notifications.Add(target.Id, record.IsAccepted ? NotificationType.Follow : NotificationType.FollowRequest,
follower.ID, follower.ActorURI, default, token);
follower.ID, follower.ActorURI, default, token: token);
if (!record.IsAccepted)
return;
@@ -98,7 +88,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
await AddForeignMember(target.Id, follower.ActorURI, token);
var accept = ActivityPubRenderer.Accept(target, follow, $"accept-{record.ID}-{DateTime.UtcNow.Ticks}");
await _delivery.Enqueue(target, new[] { follower.InboxURL }, accept, token);
await delivery.Enqueue(target, new[] { follower.InboxURL }, accept, token: token);
return;
}
}
@@ -11,31 +11,23 @@ using static PrivaPub.Federation.Objects.ActivityJson;
namespace PrivaPub.Federation.Inbox.Handlers
{
public class JoinHandler : IActivityHandler
public class JoinHandler(
DbEntities dbEntities,
ILocalActorService localActors,
IDeliveryService delivery) : IActivityHandler
{
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly IDeliveryService _delivery;
public JoinHandler(DbEntities dbEntities, ILocalActorService localActors, IDeliveryService delivery)
{
_dbEntities = dbEntities;
_localActors = localActors;
_delivery = delivery;
}
public string Type => "Join";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
{
var objectUri = Id(activity["object"]);
if (objectUri == default || !objectUri.StartsWith(_localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
if (objectUri == default || !objectUri.StartsWith(localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
{
Arrival.Drop("unknown-object");
return;
}
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
var owner = post == default ? default : await _localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
var post = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.IsFederatedCopy && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
var owner = post == default ? default : await localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
if (owner == default || string.IsNullOrEmpty(actor.InboxURL))
{
Arrival.Drop("unknown-object");
@@ -43,14 +35,14 @@ namespace PrivaPub.Federation.Inbox.Handlers
}
Arrival.About(post.ObjectType ?? "Note", post.Visibility, post.CreationDate);
Arrival.Reject("ignored");
await _delivery.Enqueue(owner, new[] { actor.InboxURL }, new JsonObject
await delivery.Enqueue(owner, new[] { actor.InboxURL }, new JsonObject
{
["@context"] = ActivityPubRenderer.ActivityStreams,
["id"] = owner.ActivityUri($"ignore-{Guid.NewGuid():N}"),
["type"] = "Ignore",
["actor"] = owner.Uri,
["object"] = activity.DeepClone()
}, token);
}, token: token);
}
}
}
@@ -16,17 +16,10 @@ using PostEntity = PrivaPub.Models.Post.Post;
namespace PrivaPub.Federation.Inbox.Handlers
{
public class LikeHandler : IActivityHandler
public class LikeHandler(
DbEntities dbEntities,
IReactions reactions) : IActivityHandler
{
readonly DbEntities _dbEntities;
readonly IReactions _reactions;
public LikeHandler(DbEntities dbEntities, IReactions reactions)
{
_dbEntities = dbEntities;
_reactions = reactions;
}
public string Type => "Like";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
@@ -34,7 +27,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
var objectUri = Id(activity["object"]);
var post = objectUri == default
? default
: await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
: await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && !p.DeletedAt.HasValue && p.ReblogOfPostId == null).ExecuteFirstAsync(token);
if (post == default || !await VisibilityPolicy.RemoteCanSee(post, actor, token))
{
Arrival.Drop(post == default ? "unknown-object" : "not-visible");
@@ -44,7 +37,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
var content = Value(activity, "_misskey_reaction") ?? Value(activity, "content");
if (!Reactions.IsHeart(content))
{
await _reactions.Receive(post, actor, content, activity["tag"], Id(activity), token);
await reactions.Receive(post, actor, content, activity["tag"], Id(activity), token);
return;
}
@@ -64,7 +57,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
&& (await DB.Default.DeleteAsync<Downvote>(downvoted.ID)).DeletedCount > 0)
await DB.Default.Update<PostEntity>().MatchID(post.ID).Modify(b => b.Inc(p => p.DownvotesCount, -1)).ExecuteAsync(token);
if (!post.IsFederatedCopy)
await Notifications.Add(post.GroupUserId, NotificationType.Favourite, actor.ID, actor.ActorURI, post.ID, token);
await Notifications.Add(post.GroupUserId, NotificationType.Favourite, actor.ID, actor.ActorURI, post.ID, token: token);
}
}
}
@@ -15,20 +15,14 @@ namespace PrivaPub.Federation.Inbox.Handlers
// A moderator's lock of a thread sent as it is, not inside its community's announce (Mbin sends it to the magazine's
// subscribers itself): taken only from the post's own server, which speaks for it. Replies are refused until the
// Undo{Lock}, as for a lock a community announces.
public class LockHandler : IActivityHandler
public class LockHandler(
DbEntities db) : IActivityHandler
{
readonly DbEntities _dbEntities;
public LockHandler(DbEntities dbEntities)
{
_dbEntities = dbEntities;
}
public string Type => "Lock";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
{
if (await Set(_dbEntities, Id(activity["object"]), actor, locked: true, token))
if (await Set(db, Id(activity["object"]), actor, locked: true, token))
Arrival.Accept("locked");
else
Arrival.Drop("unknown-object");
@@ -20,24 +20,13 @@ namespace PrivaPub.Federation.Inbox.Handlers
// it among its alsoKnownAs. The old account then shows where it went (`moved`), and, as Mastodon does it (owner
// decision 2026-10-05), the personas following it follow the new one instead, in the same lists, and those that
// muted or blocked it mute or block the new one too.
public class MoveHandler : IActivityHandler
public class MoveHandler(
IRemoteActorService remoteActors,
DbEntities dbEntities,
ILocalActorService localActors,
IFollowService follows,
IRelationshipService relationships) : IActivityHandler
{
readonly IRemoteActorService _remoteActors;
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly IFollowService _follows;
readonly IRelationshipService _relationships;
public MoveHandler(IRemoteActorService remoteActors, DbEntities dbEntities, ILocalActorService localActors, IFollowService follows,
IRelationshipService relationships)
{
_remoteActors = remoteActors;
_dbEntities = dbEntities;
_localActors = localActors;
_follows = follows;
_relationships = relationships;
}
public string Type => "Move";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
@@ -48,7 +37,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
Arrival.Drop("not-self");
return;
}
var moved = await _remoteActors.GetActor(target, refresh: true, token);
var moved = await remoteActors.GetActor(target, refresh: true, token);
if (moved == default)
{
Arrival.Drop("target-unavailable");
@@ -67,15 +56,15 @@ namespace PrivaPub.Federation.Inbox.Handlers
async Task MoveFollows(ForeignAvatar old, ForeignAvatar moved, CancellationToken token)
{
foreach (var following in await _dbEntities.Followings.Match(f => f.TargetActorURI == old.ActorURI).ExecuteAsync(token))
foreach (var following in await dbEntities.Followings.Match(f => f.TargetActorURI == old.ActorURI).ExecuteAsync(token))
{
var persona = await _localActors.FindById(LocalActorKind.Person, following.AvatarId, token);
var persona = await localActors.FindById(LocalActorKind.Person, following.AvatarId, token);
if (persona == default)
continue;
var lists = await DB.Default.Find<PersonaListMember>().Match(m => m.AvatarId == persona.Id && m.AccountId == old.ID).ExecuteAsync(token);
var endorsed = await DB.Default.Find<Endorsement>().Match(e => e.AvatarId == persona.Id && e.TargetActorURI == old.ActorURI).ExecuteAnyAsync(token);
var followed = await _follows.FollowAs(persona, moved.ActorURI, following.ShowReblogs, token, following.Notify, following.Languages ?? new List<string>());
await _follows.UnfollowAs(persona, old.ActorURI, token);
var followed = await follows.FollowAs(persona, moved.ActorURI, following.ShowReblogs, following.Notify, following.Languages ?? new List<string>(), token);
await follows.UnfollowAs(persona, old.ActorURI, token);
if (followed == default)
continue;
// the persona's endorsement and private note go with the follow, as Mastodon carries them over (R8)
@@ -100,14 +89,14 @@ namespace PrivaPub.Federation.Inbox.Handlers
async Task CarryOver(ForeignAvatar old, ForeignAvatar moved, CancellationToken token)
{
foreach (var mute in await DB.Default.Find<Mute>().Match(m => m.TargetActorURI == old.ActorURI).ExecuteAsync(token))
if (await _localActors.FindById(LocalActorKind.Person, mute.AvatarId, token) is { } persona
if (await localActors.FindById(LocalActorKind.Person, mute.AvatarId, token) is { } persona
&& !await DB.Default.Find<Mute>().Match(m => m.AvatarId == persona.Id && m.TargetActorURI == moved.ActorURI).ExecuteAnyAsync(token))
await _relationships.Mute(persona, moved.ActorURI, moved.ID, mute.HideNotifications,
await relationships.Mute(persona, moved.ActorURI, moved.ID, mute.HideNotifications,
mute.ExpiresAt is { } expires ? expires - DateTime.UtcNow : default(TimeSpan?), token);
foreach (var block in await DB.Default.Find<Block>().Match(b => b.TargetActorURI == old.ActorURI).ExecuteAsync(token))
if (await _localActors.FindById(LocalActorKind.Person, block.AvatarId, token) is { } persona
if (await localActors.FindById(LocalActorKind.Person, block.AvatarId, token) is { } persona
&& !await DB.Default.Find<Block>().Match(b => b.AvatarId == persona.Id && b.TargetActorURI == moved.ActorURI).ExecuteAnyAsync(token))
await _relationships.Block(persona, moved.ActorURI, moved.ID, token);
await relationships.Block(persona, moved.ActorURI, moved.ID, token);
}
}
}
@@ -5,17 +5,11 @@ using System.Text.Json.Nodes;
namespace PrivaPub.Federation.Inbox.Handlers
{
public class QuoteRequestHandler : IActivityHandler
public class QuoteRequestHandler(
IQuoteService quotes) : IActivityHandler
{
readonly IQuoteService _quotes;
public QuoteRequestHandler(IQuoteService quotes)
{
_quotes = quotes;
}
public string Type => "QuoteRequest";
public Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token) => _quotes.ReceiveRequest(activity, actor, token);
public Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token) => quotes.ReceiveRequest(activity, actor, token);
}
}
@@ -7,18 +7,15 @@ namespace PrivaPub.Federation.Inbox.Handlers
{
// FEP-5624 (PeerTube): the author of a post whose canReply asks it to approve each reply lets one of ours in, which then
// goes to its audience carrying the approval (replyApproval)
public class ReplyApprovalHandler : IActivityHandler
public class ReplyApprovalHandler(
IInteractionApprovals approvals) : IActivityHandler
{
readonly IInteractionApprovals _approvals;
public ReplyApprovalHandler(IInteractionApprovals approvals) => _approvals = approvals;
public virtual string Type => "ApproveReply";
protected virtual bool Approves => true;
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token) =>
await _approvals.Answered(activity, actor, Approves, token);
await approvals.Answered(activity, actor, Approves, token);
}
// and refuses one, which stays ours alone
@@ -15,18 +15,12 @@ namespace PrivaPub.Federation.Inbox.Handlers
// Lemmy 1.0 tells a report's sender that it was dealt with: Resolve{Flag}, the Flag being one we sent
// (`/grunts/flag-<report id>`). It is believed from the server holding what was reported, or the community it was
// reported to, and kept on the report beside our own moderators' resolution, which it never replaces.
public class ResolveHandler : IActivityHandler
public class ResolveHandler(
DbEntities dbEntities,
ILocalActorService localActors) : IActivityHandler
{
const string FlagMarker = "/grunts/flag-";
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
public ResolveHandler(DbEntities dbEntities, ILocalActorService localActors)
{
_dbEntities = dbEntities;
_localActors = localActors;
}
public string Type => "Resolve";
@@ -36,7 +30,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
var flagId = Id(flag);
var marker = flagId?.IndexOf(FlagMarker, StringComparison.Ordinal) ?? -1;
if (flag is JsonObject && Value(flag, "type") != "Flag" || marker < 0
|| !flagId.StartsWith(_localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
|| !flagId.StartsWith(localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
{
Arrival.Drop("not-ours");
return;
@@ -49,8 +43,8 @@ namespace PrivaPub.Federation.Inbox.Handlers
return;
}
var concerned = new List<string>(report.ObjectURIs) { report.TargetActorURI };
foreach (var post in report.PostIds.Count == 0 ? new List<Models.Post.Post>() : await _dbEntities.Posts.Match(p => report.PostIds.Contains(p.ID)).ExecuteAsync(token))
concerned.Add(await Communities.Of(post, _dbEntities, token));
foreach (var post in report.PostIds.Count == 0 ? new List<Models.Post.Post>() : await dbEntities.Posts.Match(p => report.PostIds.Contains(p.ID)).ExecuteAsync(token))
concerned.Add(await Communities.Of(post, dbEntities, token));
if (!concerned.Any(uri => Origin.Same(uri, actor.ActorURI)))
{
Arrival.Drop("misattributed");
@@ -16,19 +16,11 @@ using PostEntity = PrivaPub.Models.Post.Post;
namespace PrivaPub.Federation.Inbox.Handlers
{
public class UndoHandler : IActivityHandler
public class UndoHandler(
DbEntities dbEntities,
ILocalActorService localActors,
IReactions reactions) : IActivityHandler
{
readonly IReactions _reactions;
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
public UndoHandler(DbEntities dbEntities, ILocalActorService localActors, IReactions reactions)
{
_reactions = reactions;
_dbEntities = dbEntities;
_localActors = localActors;
}
public string Type => "Undo";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
@@ -48,11 +40,11 @@ namespace PrivaPub.Federation.Inbox.Handlers
if (innerType is null or "Dislike")
undone |= await UndoDislike(inner, innerId, actor, token);
if (innerType is null or "Block")
undone |= await BlockHandler.Undo(inner, innerId, actor, _localActors, token);
undone |= await BlockHandler.Undo(inner, innerId, actor, localActors, token);
if (innerType == "Lock")
undone |= await LockHandler.Set(_dbEntities, Id(inner["object"]), actor, locked: false, token);
undone |= await LockHandler.Set(dbEntities, Id(inner["object"]), actor, locked: false, token);
if (innerType is null or "Like" or "EmojiReact")
undone |= await _reactions.Withdraw(actor, innerId, inner is JsonObject ? Id(inner["object"]) : default,
undone |= await reactions.Withdraw(actor, innerId, inner is JsonObject ? Id(inner["object"]) : default,
inner is JsonObject ? Value(inner, "_misskey_reaction") ?? Value(inner, "content") : default, token);
if (undone)
Arrival.Accept("undone");
@@ -67,7 +59,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
: await DB.Default.Find<Downvote>().Match(d => d.ActivityURI == innerId && d.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
if (downvote == default && inner is JsonObject && Id(inner["object"]) is { } objectUri)
{
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
var post = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
downvote = post == default
? default
: await DB.Default.Find<Downvote>().Match(d => d.PostId == post.ID && d.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
@@ -81,9 +73,9 @@ namespace PrivaPub.Federation.Inbox.Handlers
async Task<bool> UndoFollow(JsonNode inner, string innerId, ForeignAvatar actor, CancellationToken token)
{
var followers = await _dbEntities.Followers.Match(f => f.ActorURI == actor.ActorURI).ExecuteAsync(token);
var followers = await dbEntities.Followers.Match(f => f.ActorURI == actor.ActorURI).ExecuteAsync(token);
var targetUri = inner is JsonObject ? Id(inner["object"]) : default;
var target = targetUri == default ? default : await _localActors.FindByAddress(targetUri, token);
var target = targetUri == default ? default : await localActors.FindByAddress(targetUri, token);
var undone = false;
foreach (var follower in followers)
{
@@ -104,13 +96,13 @@ namespace PrivaPub.Federation.Inbox.Handlers
{
var favourite = innerId == default
? default
: await _dbEntities.Favourites.Match(f => f.ActivityURI == innerId && f.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
: await dbEntities.Favourites.Match(f => f.ActivityURI == innerId && f.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
if (favourite == default && inner is JsonObject && Id(inner["object"]) is { } objectUri)
{
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
var post = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
favourite = post == default
? default
: await _dbEntities.Favourites.Match(f => f.PostId == post.ID && f.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
: await dbEntities.Favourites.Match(f => f.PostId == post.ID && f.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
}
if (favourite == default)
return false;
@@ -123,7 +115,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
{
if (innerId == default)
return false;
var reblog = await _dbEntities.Posts
var reblog = await dbEntities.Posts
.Match(p => p.ObjectURI == innerId && p.ActorURI == actor.ActorURI && p.ReblogOfPostId != null)
.ExecuteFirstAsync(token);
if (reblog == default)
@@ -16,29 +16,15 @@ using PostEntity = PrivaPub.Models.Post.Post;
namespace PrivaPub.Federation.Inbox.Handlers
{
public class UpdateHandler : IActivityHandler
public class UpdateHandler(
DbEntities dbEntities,
ILocalActorService localActors,
IRemoteActorService remoteActors,
IGroupDistributor groups,
IObjectRecords records,
IQuoteService quotes,
IDeliveryService delivery = default) : IActivityHandler
{
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly IRemoteActorService _remoteActors;
readonly IGroupDistributor _groups;
readonly IObjectRecords _records;
readonly IQuoteService _quotes;
readonly IDeliveryService _delivery;
public UpdateHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IGroupDistributor groups,
IObjectRecords records, IQuoteService quotes, IDeliveryService delivery = default)
{
_delivery = delivery;
_quotes = quotes;
_records = records;
_groups = groups;
_dbEntities = dbEntities;
_localActors = localActors;
_remoteActors = remoteActors;
}
public string Type => "Update";
public async Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token)
@@ -46,7 +32,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
var inner = activity["object"];
if (Id(inner) == actor.ActorURI)
{
await _remoteActors.GetActor(actor.ActorURI, refresh: true, token);
await remoteActors.GetActor(actor.ActorURI, refresh: true, token);
Arrival.Accept("actor-refresh");
Arrival.About(actor.AvatarType.ToString());
return;
@@ -61,7 +47,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
// as that server has it now
if (note != default && note.AttributedTo != actor.ActorURI && Origin.Same(note.AttributedTo, actor.ActorURI))
{
using var fetched = await _remoteActors.FetchObject(note.Id, token);
using var fetched = await remoteActors.FetchObject(note.Id, token);
var current = fetched == default ? default : NoteParser.Parse(JsonNode.Parse(fetched.Root.GetRawText()));
if (current == default || current.Id != note.Id || current.AttributedTo != note.AttributedTo)
{
@@ -69,7 +55,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
return;
}
note = current;
actor = await _remoteActors.GetActor(note.AttributedTo, refresh: false, token);
actor = await remoteActors.GetActor(note.AttributedTo, refresh: false, token);
if (actor == default)
{
Arrival.Drop("author-unavailable");
@@ -82,7 +68,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
return;
}
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == note.Id && p.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
var post = await dbEntities.Posts.Match(p => p.ObjectURI == note.Id && p.ActorURI == actor.ActorURI).ExecuteFirstAsync(token);
if (post == default || post.DeletedAt.HasValue)
{
Arrival.Drop("unknown-object");
@@ -90,12 +76,12 @@ namespace PrivaPub.Federation.Inbox.Handlers
}
Arrival.About(note.Type, post.Visibility, post.CreationDate);
var edited = await RemoteEdits.Apply(post, note, Id(activity), _localActors, _records, _quotes, token);
var edited = await RemoteEdits.Apply(post, note, Id(activity), localActors, records, quotes, token);
Arrival.Accept(edited ? "edit" : "refresh");
if (edited)
await ReplyRelay.Pass(post, _dbEntities, _localActors, _delivery, token);
if (edited && !string.IsNullOrEmpty(post.GroupId) && await _localActors.FindById(Models.Federation.LocalActorKind.Group, post.GroupId, token) is { IsCircle: false } community)
await _groups.Announce(community, activity.AsObject(), post.ObjectURI, isNewPost: false, token);
await ReplyRelay.Pass(post, dbEntities, localActors, delivery, token);
if (edited && !string.IsNullOrEmpty(post.GroupId) && await localActors.FindById(Models.Federation.LocalActorKind.Group, post.GroupId, token) is { IsCircle: false } community)
await groups.Announce(community, activity.AsObject(), post.ObjectURI, isNewPost: false, token);
}
}
}
@@ -17,20 +17,13 @@ namespace PrivaPub.Federation.Inbox.Handlers
// [persona], object: post, summary: reason, audience: community}), sent to the persona's inbox alone. It becomes the
// persona's moderation_warning notification, once the sender is shown to moderate that community and the post is the
// persona's own, in it.
public class WarnHandler : IActivityHandler
public class WarnHandler(
DbEntities dbEntities,
ILocalActorService localActors,
IRemoteActorService remoteActors) : IActivityHandler
{
const int MaxReason = 1000;
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly IRemoteActorService _remoteActors;
public WarnHandler(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors)
{
_dbEntities = dbEntities;
_localActors = localActors;
_remoteActors = remoteActors;
}
public string Type => "Warn";
@@ -38,7 +31,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
{
LocalActor persona = default;
foreach (var uri in Recipients(activity["to"]))
if (await _localActors.FindByUri(uri, token) is { Kind: LocalActorKind.Person } found)
if (await localActors.FindByUri(uri, token) is { Kind: LocalActorKind.Person } found)
{
persona = found;
break;
@@ -49,7 +42,7 @@ namespace PrivaPub.Federation.Inbox.Handlers
return;
}
var communityUri = Id(activity["audience"]);
var community = communityUri == default ? default : await _remoteActors.GetActor(communityUri, refresh: false, token);
var community = communityUri == default ? default : await remoteActors.GetActor(communityUri, refresh: false, token);
if (community is not { AvatarType: AvatarType.Group })
{
Arrival.Drop("unknown-community");
@@ -58,21 +51,21 @@ namespace PrivaPub.Federation.Inbox.Handlers
var objectUri = Id(activity["object"]);
var post = objectUri == default
? default
: await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.GroupUserId == persona.Id && !p.IsFederatedCopy).ExecuteFirstAsync(token);
if (post == default || await Communities.Of(post, _dbEntities, token) != community.ActorURI)
: await dbEntities.Posts.Match(p => p.ObjectURI == objectUri && p.GroupUserId == persona.Id && !p.IsFederatedCopy).ExecuteFirstAsync(token);
if (post == default || await Communities.Of(post, dbEntities, token) != community.ActorURI)
{
Arrival.Drop("unknown-object");
return;
}
Arrival.About(visibility: post.Visibility, created: post.CreationDate, local: persona);
if (!await Communities.Moderates(community, actor.ActorURI, _remoteActors, token))
if (!await Communities.Moderates(community, actor.ActorURI, remoteActors, token))
{
Arrival.Drop("not-moderator");
return;
}
var reason = Value(activity, "summary") ?? Value(activity, "content");
await Notifications.Add(persona.Id, NotificationType.ModerationWarning, community.ID, community.ActorURI, post.ID, token,
text: string.IsNullOrWhiteSpace(reason) ? default : reason.Trim().Length <= MaxReason ? reason.Trim() : reason.Trim()[..MaxReason]);
await Notifications.Add(persona.Id, NotificationType.ModerationWarning, community.ID, community.ActorURI, post.ID,
text: string.IsNullOrWhiteSpace(reason) ? default : reason.Trim().Length <= MaxReason ? reason.Trim() : reason.Trim()[..MaxReason], token: token);
Arrival.Accept("warned");
}
+16 -26
View File
@@ -20,27 +20,17 @@ namespace PrivaPub.Federation.Inbox
Task Handle(JsonNode activity, ForeignAvatar actor, CancellationToken token);
}
public class InboxProcessor : IJobHandler
public class InboxProcessor(
IRemoteActorService remoteActors,
IEnumerable<IActivityHandler> handlers,
ILogger<InboxProcessor> logger,
IInteractionLedger ledger = default,
ILocalActorService localActors = default,
Microsoft.Extensions.Options.IOptionsMonitor<Infrastructure.Http.FederationOptions> federation = default) : IJobHandler
{
readonly IRemoteActorService _remoteActors;
readonly IReadOnlyDictionary<string, IActivityHandler> _handlers;
readonly ILogger<InboxProcessor> _logger;
readonly IInteractionLedger _ledger;
readonly ILocalActorService _localActors;
readonly IReadOnlyDictionary<string, IActivityHandler> _handlers = handlers.ToDictionary(h => h.Type, StringComparer.Ordinal);
readonly int _concurrency = Math.Max(1, federation?.CurrentValue.InboxConcurrency ?? 8);
readonly int _concurrency;
public InboxProcessor(IRemoteActorService remoteActors, IEnumerable<IActivityHandler> handlers, ILogger<InboxProcessor> logger,
IInteractionLedger ledger = default, ILocalActorService localActors = default,
Microsoft.Extensions.Options.IOptions<Infrastructure.Http.FederationOptions> federation = default)
{
_concurrency = Math.Max(1, federation?.Value.InboxConcurrency ?? 8);
_localActors = localActors;
_remoteActors = remoteActors;
_handlers = handlers.ToDictionary(h => h.Type, StringComparer.Ordinal);
_logger = logger;
_ledger = ledger;
}
public JobKind Kind => JobKind.ProcessInbox;
public int Concurrency => _concurrency;
@@ -59,7 +49,7 @@ namespace PrivaPub.Federation.Inbox
return JobOutcome.Done;
}
var actor = await _remoteActors.GetActor(payload.ActorURI, refresh: false, token);
var actor = await remoteActors.GetActor(payload.ActorURI, refresh: false, token);
if (actor == default)
{
Record(job, payload, activity, type, started, new ArrivalVerdict(), Interactions.Deferred, "actor-unavailable");
@@ -73,10 +63,10 @@ namespace PrivaPub.Federation.Inbox
return JobOutcome.Done;
}
if (payload.ForwardedBy != default && Forwarded.NamesUnknownKey(activity, actor))
actor = await _remoteActors.GetActor(actor.ActorURI, refresh: true, token) ?? actor;
actor = await remoteActors.GetActor(actor.ActorURI, refresh: true, token) ?? actor;
if (payload.ForwardedBy != default && !Forwarded.Proven(activity, actor))
{
var (confirmed, drop) = await Forwarded.Confirm(activity, type, actor.ActorURI, _remoteActors, token);
var (confirmed, drop) = await Forwarded.Confirm(activity, type, actor.ActorURI, remoteActors, token);
if (drop != default)
{
Record(job, payload, activity, type, started, new ArrivalVerdict(), Interactions.Dropped, drop);
@@ -91,7 +81,7 @@ namespace PrivaPub.Federation.Inbox
Arrival.Current = arrival;
try
{
await LocalReferences.Canonicalise(activity, _localActors?.BaseAddress, token);
await LocalReferences.Canonicalise(activity, localActors?.BaseAddress, token);
await handler.Handle(activity, actor, token);
}
catch (Exception ex) when (ex is not OperationCanceledException)
@@ -104,16 +94,16 @@ namespace PrivaPub.Federation.Inbox
Arrival.Current = default;
}
Record(job, payload, activity, type, started, arrival.Verdict, default, default);
_logger.LogInformation("Processed {Type} {Id} from {Actor}", type, Id(activity), actor.ActorURI);
logger.LogInformation("Processed {Type} {Id} from {Actor}", type, Id(activity), actor.ActorURI);
return JobOutcome.Done;
}
void Record(Job job, InboxPayload payload, JsonNode activity, string type, long started, ArrivalVerdict verdict, string outcome, string reason)
{
if (_ledger == default || payload == default)
if (ledger == default || payload == default)
return;
var embedded = activity?["object"] as JsonObject;
_ledger.Record(new InteractionEvent
ledger.Record(new InteractionEvent
{
Channel = Interactions.In,
Host = Interactions.HostOf(payload.ActorURI),
+27 -37
View File
@@ -28,29 +28,18 @@ namespace PrivaPub.Federation.Inbox
InboxResult NoSuchRecipient(HttpRequest request);
}
public class InboxReceiver : IInboxReceiver
public class InboxReceiver(
ILocalActorService localActors,
IRemoteActorService remoteActors,
IJobQueue queue,
IDomainBlocks domainBlocks,
ILogger<InboxReceiver> logger,
IInteractionLedger ledger = default) : IInboxReceiver
{
const int KeyRetrySeconds = 300;
const int MaxBodyBytes = 1024 * 1024;
readonly ILocalActorService _localActors;
readonly IRemoteActorService _remoteActors;
readonly IJobQueue _queue;
readonly IDomainBlocks _domainBlocks;
readonly ILogger<InboxReceiver> _logger;
readonly IInteractionLedger _ledger;
public InboxReceiver(ILocalActorService localActors, IRemoteActorService remoteActors, IJobQueue queue, IDomainBlocks domainBlocks,
ILogger<InboxReceiver> logger, IInteractionLedger ledger = default)
{
_localActors = localActors;
_remoteActors = remoteActors;
_queue = queue;
_domainBlocks = domainBlocks;
_logger = logger;
_ledger = ledger;
}
sealed class Receipt
{
@@ -91,10 +80,10 @@ namespace PrivaPub.Federation.Inbox
void Record(Receipt receipt, InboxResult result, int latencyMs)
{
if (_ledger == default || result == default)
if (ledger == default || result == default)
return;
var verified = receipt.VerifiedActor != default;
_ledger.Record(new InteractionEvent
ledger.Record(new InteractionEvent
{
Channel = Interactions.Receive,
Host = verified ? Interactions.HostOf(receipt.VerifiedActor) : receipt.ClaimedHost,
@@ -111,6 +100,7 @@ namespace PrivaPub.Federation.Inbox
}, verified ? receipt.VerifiedActor : default, hostClaimed: !verified && !receipt.Blocked);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("HouseRules", "HR0007", Justification = "ActivityPub servers retry a 503 with Retry-After when the signing key cannot be fetched yet; a 4XX would make them drop the activity.")]
async Task<InboxResult> Receive(HttpRequest request, LocalActor recipient, Receipt receipt, CancellationToken token)
{
if (request.ContentLength > MaxBodyBytes)
@@ -123,7 +113,7 @@ namespace PrivaPub.Federation.Inbox
var body = buffer.ToArray();
receipt.Bytes = body.Length;
JsonNode activity;
var activity = default(JsonNode);
try
{
activity = JsonNode.Parse(body);
@@ -149,10 +139,10 @@ namespace PrivaPub.Federation.Inbox
return new(StatusCodes.Status401Unauthorized, "missing or unreadable Signature header", Reason: "no-signature");
receipt.Signature = signature.Scheme;
receipt.ClaimedHost ??= HostOf(signature.KeyId);
if (_domainBlocks.IsSuspended(HostOf(signature.KeyId)) || _domainBlocks.IsSuspended(HostOf(actorUri)))
if (domainBlocks.IsSuspended(HostOf(signature.KeyId)) || domainBlocks.IsSuspended(HostOf(actorUri)))
{
receipt.Blocked = true;
receipt.ClaimedHost = _domainBlocks.IsSuspended(HostOf(actorUri)) ? HostOf(actorUri) : HostOf(signature.KeyId);
receipt.ClaimedHost = domainBlocks.IsSuspended(HostOf(actorUri)) ? HostOf(actorUri) : HostOf(signature.KeyId);
return new(StatusCodes.Status202Accepted, Reason: "suspended");
}
@@ -160,14 +150,14 @@ namespace PrivaPub.Federation.Inbox
if (requestProblem != default)
return new(StatusCodes.Status401Unauthorized, requestProblem, Reason: HttpSignatures.ProblemCode(requestProblem));
var signed = signature.Signed(request, _localActors.BaseAddress);
var keyOwner = await _remoteActors.GetActorByKeyId(signature.KeyId, refresh: false, token);
var signed = signature.Signed(request, localActors.BaseAddress);
var keyOwner = await remoteActors.GetActorByKeyId(signature.KeyId, refresh: false, token);
if (keyOwner == default && type == "Delete" && Id(activity["object"]) == actorUri)
return new(StatusCodes.Status202Accepted, Reason: "self-delete-unknown-key");
if (keyOwner == default || !signature.VerifiedBy(keyOwner.PublicKey, signed))
{
keyOwner = await _remoteActors.GetActorByKeyId(signature.KeyId, refresh: true, token);
if (keyOwner == default && _remoteActors.KeyTemporarilyUnavailable(signature.KeyId))
keyOwner = await remoteActors.GetActorByKeyId(signature.KeyId, refresh: true, token);
if (keyOwner == default && remoteActors.KeyTemporarilyUnavailable(signature.KeyId))
return new(StatusCodes.Status503ServiceUnavailable, "the signing key could not be fetched; try again later", KeyRetrySeconds, "key-unavailable");
if (keyOwner == default || !signature.VerifiedBy(keyOwner.PublicKey, signed))
return new(StatusCodes.Status401Unauthorized, "the signature does not verify", Reason: "signature-invalid");
@@ -178,7 +168,7 @@ namespace PrivaPub.Federation.Inbox
receipt.VerifiedActor = keyOwner.ActorURI;
var forwardedBy = string.Equals(keyOwner.ActorURI, actorUri, StringComparison.Ordinal) ? default : keyOwner.ActorURI;
// (ours come back too, Friendica forwarding our comments in its threads: we know them already)
if (forwardedBy != default && (!Forwarded.Takeable(type, activity, actorUri) || Origin.Same(actorUri, _localActors.BaseAddress)))
if (forwardedBy != default && (!Forwarded.Takeable(type, activity, actorUri) || Origin.Same(actorUri, localActors.BaseAddress)))
return new(StatusCodes.Status202Accepted, Reason: "forwarded-ignored");
var shapeProblem = await ShapeProblem(type, activity, actorUri, token);
@@ -193,21 +183,21 @@ namespace PrivaPub.Federation.Inbox
var dedupe = activityId == default ? default : (forwardedBy == default ? "inbox|" : "inbox|forwarded|") + activityId;
var queuedPayload = JsonSerializer.Serialize(payload);
var host = new Uri(actorUri).Host.ToLowerInvariant();
var queued = await _queue.Enqueue(JobKind.ProcessInbox, queuedPayload, host, dedupe, token);
var queued = await queue.Enqueue(JobKind.ProcessInbox, queuedPayload, host, dedupe, token);
// one id its sender gave two activities is no duplicate (Friendica's ids are uniqid(), a prefix and the
// microsecond, which two of its processes answering at once share): what each carries tells them apart, and a
// true copy carries the same
if (!queued && dedupe != default && CarriesOther(await _queue.Payload(dedupe, token), activity))
queued = await _queue.Enqueue(JobKind.ProcessInbox, queuedPayload, host, $"{dedupe}|{Digest(activity)}", token);
if (!queued && dedupe != default && CarriesOther(await queue.Payload(dedupe, token), activity))
queued = await queue.Enqueue(JobKind.ProcessInbox, queuedPayload, host, $"{dedupe}|{Digest(activity)}", token);
// an Undo of a Follow that comes again while the follow it ends exists again is meant for that follow: Mastodon
// gives every Undo it sends after reading a roll-call (FEP-8fcf) one id, `{actor}#follows//undo`. It is kept
// once per follow, so a retry still counts as a copy
if (!queued && dedupe != default && type == "Undo" && await FollowAgain(activity, actorUri, token) is { } follow)
queued = await _queue.Enqueue(JobKind.ProcessInbox, queuedPayload, host, $"{dedupe}|{Digest(activity)}|{follow}", token);
queued = await queue.Enqueue(JobKind.ProcessInbox, queuedPayload, host, $"{dedupe}|{Digest(activity)}|{follow}", token);
// FEP-8fcf: what the sender says of its followers here (FollowingSynchronization)
if (queued && forwardedBy == default && FollowersSynchronization.Claim(actorUri, request.Headers[FollowersSynchronization.Header].ToString()) is { } claim)
await _queue.Enqueue(JobKind.SynchronizeFollowing, JsonSerializer.Serialize(claim), host, FollowingSynchronization.DedupeKey(claim, activityId), token);
_logger.LogInformation("Inbox {Recipient}: {Type} from {Actor} queued", recipient?.Handle ?? "shared", type, actorUri);
await queue.Enqueue(JobKind.SynchronizeFollowing, JsonSerializer.Serialize(claim), host, FollowingSynchronization.DedupeKey(claim, activityId), token);
logger.LogInformation("Inbox {Recipient}: {Type} from {Actor} queued", recipient?.Handle ?? "shared", type, actorUri);
return new(StatusCodes.Status202Accepted, Reason: !queued ? "duplicate" : forwardedBy != default ? "forwarded" : "queued");
}
@@ -215,7 +205,7 @@ namespace PrivaPub.Federation.Inbox
async Task<string> FollowAgain(JsonNode activity, string actorUri, CancellationToken token)
{
if (activity["object"] is not JsonObject inner || Value(inner, "type") != "Follow" || Id(inner["object"]) is not { } targetUri
|| await _localActors.FindByUri(targetUri, token) is not { } target)
|| await localActors.FindByUri(targetUri, token) is not { } target)
return default;
var follower = await MongoDB.Entities.DB.Default.Find<Follower>()
.Match(f => f.ActorURI == actorUri && f.LocalActorId == target.Id && f.LocalActorKind == target.Kind)
@@ -244,7 +234,7 @@ namespace PrivaPub.Federation.Inbox
var activityId = Id(activity);
// (Funkwhale names its answer after the activity it answers: `<our Follow's id>/accept`)
if (activityId != default && !Origin.Same(activityId, actorUri)
&& !(type is "Accept" or "Reject" && Id(activity["object"]) is { } answered && Origin.Same(answered, _localActors.BaseAddress)
&& !(type is "Accept" or "Reject" && Id(activity["object"]) is { } answered && Origin.Same(answered, localActors.BaseAddress)
&& activityId.StartsWith(answered + "/", StringComparison.Ordinal)))
return new(StatusCodes.Status400BadRequest, "the activity's id is not on its actor's origin", Reason: "id-cross-origin");
@@ -253,7 +243,7 @@ namespace PrivaPub.Federation.Inbox
{
case "Follow":
// (by its actor's id, or the profile page Forte names instead)
var target = await _localActors.FindByAddress(Id(inner), token);
var target = await localActors.FindByAddress(Id(inner), token);
if (target is not { IsFederated: true } || target.IsServerActor)
return new(StatusCodes.Status404NotFound, "no such local actor", Reason: "unknown-recipient");
break;
+27 -46
View File
@@ -31,29 +31,17 @@ namespace PrivaPub.Federation.Inbox
Task<PostEntity> StoreContext(string objectUri, int depth, CancellationToken token);
}
public class RemotePosts : IRemotePosts
public class RemotePosts(
DbEntities dbEntities,
ILocalActorService localActorService,
IRemoteActorService remoteActors,
IDomainBlocks domainBlockList,
IJobQueue queue,
IObjectRecords records,
ILinkPreviews previews) : IRemotePosts
{
public const int MaxDepth = 10;
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly IRemoteActorService _remoteActors;
readonly IDomainBlocks _domainBlocks;
readonly IJobQueue _queue;
readonly IObjectRecords _records;
readonly ILinkPreviews _previews;
public RemotePosts(DbEntities dbEntities, ILocalActorService localActors, IRemoteActorService remoteActors, IDomainBlocks domainBlocks,
IJobQueue queue, IObjectRecords records, ILinkPreviews previews)
{
_previews = previews;
_records = records;
_dbEntities = dbEntities;
_localActors = localActors;
_remoteActors = remoteActors;
_domainBlocks = domainBlocks;
_queue = queue;
}
// The accounts a remote post names (its Mention tags), plus our personas it is addressed to without naming them: Mastodon
// keeps those as silent mentions, and Akkoma's to[], or an edit that dropped the @name, address someone that way. A
@@ -81,7 +69,7 @@ namespace PrivaPub.Federation.Inbox
public async Task<PostEntity> Build(NoteDocument note, ForeignAvatar author, PostVisibility visibility, IReadOnlyList<string> to,
IReadOnlyList<string> cc, PostEntity parent, CancellationToken token)
{
var mentions = await Mentions(note, to.Concat(cc), _localActors, token);
var mentions = await Mentions(note, to.Concat(cc), localActorService, token);
var post = new PostEntity
{
@@ -127,7 +115,7 @@ namespace PrivaPub.Federation.Inbox
Language = note.Language,
Mentions = mentions,
Tags = note.Tags.ToList(),
Media = _domainBlocks.Find(new Uri(author.ActorURI).Host)?.RejectMedia == true ? new() : note.Attachments.ToList(),
Media = domainBlockList.Find(new Uri(author.ActorURI).Host)?.RejectMedia == true ? new() : note.Attachments.ToList(),
InReplyToURI = note.InReplyTo,
AnsweringToPostId = parent?.ID,
InReplyToAccountId = parent?.AuthorAccountId ?? parent?.GroupUserId,
@@ -135,7 +123,7 @@ namespace PrivaPub.Federation.Inbox
AuthorGone = author.AccountState == AvatarAccountState.Deleted,
// the staff's moderation of its author or its server (R6), as AccountModeration sets it on what came before
AuthorSuspended = author.SuspendedAt.HasValue,
AuthorLimited = Limited(author, _domainBlocks),
AuthorLimited = Limited(author, domainBlockList),
CreationDate = note.Published,
UpdateDate = note.Updated ?? note.Published,
EditedAt = note.Updated,
@@ -172,7 +160,7 @@ namespace PrivaPub.Federation.Inbox
if (links.Count != 1 || links[0] == note.Id || !Origin.Same(links[0], author.ActorURI))
return;
using var scope = HttpScope.For("object");
using var fetched = await _remoteActors.FetchObject(links[0], token);
using var fetched = await remoteActors.FetchObject(links[0], token);
var episode = fetched == default ? default : NoteParser.Parse(JsonNode.Parse(fetched.Root.GetRawText()));
if (episode is not { Type: "PodcastEpisode" } || episode.AttributedTo != author.ActorURI)
return;
@@ -187,33 +175,33 @@ namespace PrivaPub.Federation.Inbox
{
if (string.IsNullOrEmpty(inReplyTo))
return default;
var known = await _dbEntities.Posts.Match(p => p.ObjectURI == inReplyTo && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
if (known != default || inReplyTo.StartsWith(_localActors.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
var known = await dbEntities.Posts.Match(p => p.ObjectURI == inReplyTo && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
if (known != default || inReplyTo.StartsWith(localActorService.BaseAddress + "/", StringComparison.OrdinalIgnoreCase))
return known;
return await StoreContext(inReplyTo, depth, token);
}
public async Task<PostEntity> StoreContext(string objectUri, int depth, CancellationToken token)
{
var existing = await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
var existing = await dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
if (existing != default || depth > MaxDepth || await DB.Default.Find<DeletedObject>().Match(d => d.ObjectURI == objectUri).ExecuteAnyAsync(token))
return existing;
using var scope = HttpScope.For("context");
using var fetched = await _remoteActors.FetchObject(objectUri, token);
using var fetched = await remoteActors.FetchObject(objectUri, token);
var note = fetched == default ? default : NoteParser.Parse(JsonNode.Parse(fetched.Root.GetRawText()));
if (note == default || !Origin.Same(note.Id, note.AttributedTo))
return default;
var visibility = Addressing.Classify(note.To, note.Cc, default);
if (visibility is not (PostVisibility.Public or PostVisibility.Unlisted))
return default;
var author = await _remoteActors.GetActor(note.AttributedTo, refresh: false, token);
var author = await remoteActors.GetActor(note.AttributedTo, refresh: false, token);
if (author == default)
return default;
var grandparent = string.IsNullOrEmpty(note.InReplyTo)
? default
: await _dbEntities.Posts.Match(p => p.ObjectURI == note.InReplyTo && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
: await dbEntities.Posts.Match(p => p.ObjectURI == note.InReplyTo && !p.DeletedAt.HasValue).ExecuteFirstAsync(token);
var post = await Build(note, author, visibility, note.To, note.Cc, grandparent, token);
try
{
@@ -221,29 +209,22 @@ namespace PrivaPub.Federation.Inbox
}
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)
{
return await _dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
return await dbEntities.Posts.Match(p => p.ObjectURI == objectUri).ExecuteFirstAsync(token);
}
await _records.Record(note, post, ObjectPath.Fetched, refetched: false, token);
await _previews.Wanted(post, token);
await records.Record(note, post, ObjectPath.Fetched, refetched: false, token);
await previews.Wanted(post, token);
if (grandparent == default && !string.IsNullOrEmpty(note.InReplyTo) && depth < MaxDepth)
await _queue.Enqueue(JobKind.FetchAncestors, JsonSerializer.Serialize(new AncestorsPayload(post.ID, depth + 1)), new Uri(note.InReplyTo).Host,
await queue.Enqueue(JobKind.FetchAncestors, JsonSerializer.Serialize(new AncestorsPayload(post.ID, depth + 1)), new Uri(note.InReplyTo).Host,
"ancestors|" + post.ID, token);
return post;
}
}
public class AncestorsJobHandler : IJobHandler
public class AncestorsJobHandler(
DbEntities dbEntities,
IRemotePosts remotePosts) : IJobHandler
{
readonly DbEntities _dbEntities;
readonly IRemotePosts _remotePosts;
public AncestorsJobHandler(DbEntities dbEntities, IRemotePosts remotePosts)
{
_dbEntities = dbEntities;
_remotePosts = remotePosts;
}
public JobKind Kind => JobKind.FetchAncestors;
public int Concurrency => 1;
public int MaxAttempts => 3;
@@ -252,11 +233,11 @@ namespace PrivaPub.Federation.Inbox
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
{
var payload = JsonSerializer.Deserialize<AncestorsPayload>(job.Payload);
var child = await _dbEntities.Posts.MatchID(payload.PostId).ExecuteFirstAsync(token);
var child = await dbEntities.Posts.MatchID(payload.PostId).ExecuteFirstAsync(token);
if (child == default || string.IsNullOrEmpty(child.InReplyToURI) || !string.IsNullOrEmpty(child.AnsweringToPostId))
return JobOutcome.Done;
var parent = await _remotePosts.Parent(child.InReplyToURI, payload.Depth, token);
var parent = await remotePosts.Parent(child.InReplyToURI, payload.Depth, token);
if (parent == default)
return JobOutcome.Done;
await DB.Default.Update<PostEntity>().MatchID(child.ID)
+13 -21
View File
@@ -27,7 +27,11 @@ namespace PrivaPub.Federation.Inbox
// replies' own, two levels down. Read with the instance actor's signature, never a persona's, a few pages and at most
// MaxItems posts a job; each one is fetched from its own server and stored like any post fetched for a thread
// (RemotePosts.StoreContext: public and unlisted only, its author checked against its origin).
public class RepliesJobHandler : IJobHandler
public class RepliesJobHandler(
DbEntities dbEntities,
IRemoteActorService remoteActors,
IRemotePosts remotePosts,
IJobQueue queue) : IJobHandler
{
public const int MaxPages = 5;
public const int MaxItems = 100;
@@ -39,18 +43,6 @@ namespace PrivaPub.Federation.Inbox
"Collection", "OrderedCollection", "CollectionPage", "OrderedCollectionPage"
};
readonly DbEntities _dbEntities;
readonly IRemoteActorService _remoteActors;
readonly IRemotePosts _remotePosts;
readonly IJobQueue _queue;
public RepliesJobHandler(DbEntities dbEntities, IRemoteActorService remoteActors, IRemotePosts remotePosts, IJobQueue queue)
{
_dbEntities = dbEntities;
_remoteActors = remoteActors;
_remotePosts = remotePosts;
_queue = queue;
}
public JobKind Kind => JobKind.FetchReplies;
public int Concurrency => 2;
@@ -69,7 +61,7 @@ namespace PrivaPub.Federation.Inbox
public async Task<JobOutcome> Handle(Job job, CancellationToken token)
{
var payload = JsonSerializer.Deserialize<RepliesPayload>(job.Payload);
var post = await _dbEntities.Posts.MatchID(payload.PostId).ExecuteFirstAsync(token);
var post = await dbEntities.Posts.MatchID(payload.PostId).ExecuteFirstAsync(token);
if (post is not { IsFederatedCopy: true, Visibility: PostVisibility.Public or PostVisibility.Unlisted } || post.DeletedAt.HasValue
|| string.IsNullOrEmpty(post.ObjectURI))
return JobOutcome.Done;
@@ -80,7 +72,7 @@ namespace PrivaPub.Federation.Inbox
{
var uri = Id(document[name]);
// Forte's `context` is the thread's first post
if (uri != default && await _dbEntities.Posts.Match(p => p.ObjectURI == uri).ExecuteAnyAsync(token))
if (uri != default && await dbEntities.Posts.Match(p => p.ObjectURI == uri).ExecuteAnyAsync(token))
continue;
var known = uri == default ? default : await DB.Default.Find<ThreadCollection>().Match(c => c.URI == uri).ExecuteFirstAsync(token);
var (thread, etag, unchanged) = await Collection(document[name], known?.ETag, token);
@@ -106,7 +98,7 @@ namespace PrivaPub.Federation.Inbox
return JobOutcome.Done;
var (answers, _) = await StoreAll(replies, false, token);
if (payload.Depth < MaxDepth)
await _queue.EnqueueMany(answers.Where(a => a.IsFederatedCopy && a.AnsweringToPostId == post.ID).Take(MaxBranches)
await queue.EnqueueMany(answers.Where(a => a.IsFederatedCopy && a.AnsweringToPostId == post.ID).Take(MaxBranches)
.Select(a => For(a, payload.Depth + 1)), token);
return JobOutcome.Done;
}
@@ -119,7 +111,7 @@ namespace PrivaPub.Federation.Inbox
if (record != default)
return JsonNode.Parse(record.Raw) as JsonObject;
using var scope = HttpScope.For("replies");
using var fetched = await _remoteActors.FetchObject(post.ObjectURI, token);
using var fetched = await remoteActors.FetchObject(post.ObjectURI, token);
return fetched == default ? default : JsonNode.Parse(fetched.Root.GetRawText()) as JsonObject;
}
@@ -133,7 +125,7 @@ namespace PrivaPub.Federation.Inbox
if (uri != default && Uri.TryCreate(uri, UriKind.Absolute, out var parsed) && (parsed.Scheme == Uri.UriSchemeHttps || parsed.Scheme == Uri.UriSchemeHttp))
{
using var scope = HttpScope.For("replies");
using var fetched = await _remoteActors.FetchObject(uri, etag, token);
using var fetched = await remoteActors.FetchObject(uri, etag, token);
if (fetched?.NotModified == true)
return (default, etag, true);
node = fetched == default ? default : JsonNode.Parse(fetched.Root.GetRawText());
@@ -160,8 +152,8 @@ namespace PrivaPub.Federation.Inbox
var id = activities ? await Added(item, token) : PostId(item);
if (id == default)
continue;
var post = await _dbEntities.Posts.Match(p => p.ObjectURI == id && !p.DeletedAt.HasValue).ExecuteFirstAsync(token)
?? await _remotePosts.StoreContext(id, 0, token);
var post = await dbEntities.Posts.Match(p => p.ObjectURI == id && !p.DeletedAt.HasValue).ExecuteFirstAsync(token)
?? await remotePosts.StoreContext(id, 0, token);
if (post != default)
posts.Add(post);
}
@@ -200,7 +192,7 @@ namespace PrivaPub.Federation.Inbox
if (node is JsonValue && Id(node) is { } uri)
{
using var scope = HttpScope.For("replies");
using var fetched = await _remoteActors.FetchObject(uri, token);
using var fetched = await remoteActors.FetchObject(uri, token);
node = fetched == default ? default : JsonNode.Parse(fetched.Root.GetRawText());
}
return node as JsonObject;
+1 -1
View File
@@ -37,7 +37,7 @@ namespace PrivaPub.Federation.Inbox
var replier = Interactions.HostOf(reply.ActorURI);
var inboxes = (await delivery.FollowerInboxes(owner, token)).Where(i => Interactions.HostOf(i) != replier).ToList();
if (inboxes.Count > 0)
await delivery.Enqueue(owner, inboxes, activity, token);
await delivery.Enqueue(owner, inboxes, activity, token: token);
}
}
}
@@ -11,20 +11,15 @@ namespace PrivaPub.Federation.Moderation
Task Reload(CancellationToken token);
}
public class DomainBlocks : IDomainBlocks
public class DomainBlocks(
ILogger<DomainBlocks> logger) : IDomainBlocks
{
static readonly TimeSpan Staleness = TimeSpan.FromMinutes(5);
readonly ILogger<DomainBlocks> _logger;
IReadOnlyDictionary<string, DomainBlock> _blocks = new Dictionary<string, DomainBlock>();
DateTime _loadedAt = DateTime.MinValue;
int _reloading;
public DomainBlocks(ILogger<DomainBlocks> logger)
{
_logger = logger;
}
public static string Normalise(string domain) => domain?.Trim().Trim('.').ToLowerInvariant();
public DomainBlock Find(string host)
@@ -67,7 +62,7 @@ namespace PrivaPub.Federation.Moderation
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Domain blocks could not be reloaded");
logger.LogWarning(ex, "Domain blocks could not be reloaded");
}
finally
{
@@ -11,31 +11,22 @@ using System.Text.Json;
namespace PrivaPub.Federation.Objects
{
public class InstanceDescriber : IJobHandler
public class InstanceDescriber(
IFederationHttp federationHttp,
IGeoLocator geoLocator = default,
IConnectedAddresses addresses = default,
ICdnRanges cdnRanges = default,
IEdgeHints edgeHints = default) : IJobHandler
{
const int MaxRawChars = 64 * 1024;
readonly IFederationHttp _http;
readonly IGeoLocator _geo;
readonly IConnectedAddresses _addresses;
readonly ICdnRanges _cdnRanges;
readonly IEdgeHints _edges;
public InstanceDescriber(IFederationHttp http, IGeoLocator geo = default, IConnectedAddresses addresses = default, ICdnRanges cdnRanges = default,
IEdgeHints edges = default)
{
_http = http;
_geo = geo;
_addresses = addresses;
_cdnRanges = cdnRanges;
_edges = edges;
}
public JobKind Kind => JobKind.DescribeInstance;
public int Concurrency => 1;
public int MaxAttempts => 2;
public int PerHostLimit => 1;
[System.Diagnostics.CodeAnalysis.SuppressMessage("HouseRules", "HR0011", Justification = "An ISO week key such as 2026-W05, not a number shown to a reader.")]
public static string Week(DateTime at) => $"{ISOWeek.GetYear(at)}-W{ISOWeek.GetWeekOfYear(at):00}";
public static string DedupeKey(string host, DateTime at) => $"describe|{host}|{ISOWeek.GetYear(at)}-{ISOWeek.GetWeekOfYear(at)}";
@@ -54,18 +45,19 @@ namespace PrivaPub.Federation.Objects
public async Task<bool> Describe(string host, bool crawled, Func<string, bool> allowed, CancellationToken token)
{
allowed ??= _ => true;
NodeInfoFacts facts = default;
string rawNodeInfo = default, error;
var facts = default(NodeInfoFacts);
var rawNodeInfo = default(string);
var error = default(string);
using (HttpScope.For("nodeinfo"))
{
error = allowed("/.well-known/nodeinfo") ? "no NodeInfo" : "robots.txt disallows NodeInfo";
using var links = allowed("/.well-known/nodeinfo")
? await _http.GetJson(Address($"https://{host}/.well-known/nodeinfo"), "application/json", sign: default, token)
? await federationHttp.GetJson(Address($"https://{host}/.well-known/nodeinfo"), "application/json", sign: default, token)
: default;
var (href, version) = links == default ? default : InstanceDocuments.NodeInfoLink(links.Root);
if (href != default && allowed(new Uri(href).AbsolutePath))
{
using var nodeInfo = await _http.GetJson(Address(href), "application/json", sign: default, token);
using var nodeInfo = await federationHttp.GetJson(Address(href), "application/json", sign: default, token);
if (nodeInfo != default)
{
facts = InstanceDocuments.NodeInfo(nodeInfo.Root, version);
@@ -78,7 +70,7 @@ namespace PrivaPub.Federation.Objects
// no NodeInfo: NodeInfo2, Castopod's
if (facts == default && allowed("/.well-known/x-nodeinfo2"))
{
using var nodeInfo2 = await _http.GetJson(Address($"https://{host}/.well-known/x-nodeinfo2"), "application/json", sign: default, token);
using var nodeInfo2 = await federationHttp.GetJson(Address($"https://{host}/.well-known/x-nodeinfo2"), "application/json", sign: default, token);
if (nodeInfo2 != default && InstanceDocuments.NodeInfo2(nodeInfo2.Root) is { } second)
{
facts = second;
@@ -96,7 +88,7 @@ namespace PrivaPub.Federation.Objects
{
if (!allowed(path))
continue;
using var document = await _http.GetJson(Address($"https://{host}{path}"), "application/json", sign: default, token);
using var document = await federationHttp.GetJson(Address($"https://{host}{path}"), "application/json", sign: default, token);
if (document == default || document.Root.ValueKind != JsonValueKind.Object)
continue;
api = InstanceDocuments.InstanceApi(document.Root);
@@ -182,9 +174,9 @@ namespace PrivaPub.Federation.Objects
InstanceGeo Locate(string host)
{
var reachedHost = new Uri(Address($"https://{host}/")).Host;
var reached = _addresses?.Of(reachedHost);
var fix = reached == default ? default : _geo?.Locate(reached);
var (cdn, cdnSource) = Cdn(reached, reachedHost, fix?.Asn, _cdnRanges, _edges);
var reached = addresses?.Of(reachedHost);
var fix = reached == default ? default : geoLocator?.Locate(reached);
var (cdn, cdnSource) = Cdn(reached, reachedHost, fix?.Asn, cdnRanges, edgeHints);
if (fix == default && cdn == default)
return default;
return new InstanceGeo
@@ -199,7 +191,7 @@ namespace PrivaPub.Federation.Objects
CdnDomain = cdn?.Domain,
CdnSource = cdnSource,
Ipv6 = reached?.AddressFamily == AddressFamily.InterNetworkV6,
Source = _geo?.Source,
Source = geoLocator?.Source,
LocatedAt = DateTime.UtcNow
};
}
@@ -102,7 +102,7 @@ namespace PrivaPub.Federation.Objects
public static InstanceApiFacts InstanceApi(JsonElement root)
{
var registrations = root.ValueKind == JsonValueKind.Object && root.TryGetProperty("registrations", out var r) ? r : default;
string mode;
var mode = default(string);
if (registrations.ValueKind == JsonValueKind.Object)
mode = Flag(registrations, "enabled") switch
{
@@ -147,7 +147,7 @@ namespace PrivaPub.Federation.Objects
{
if (parent.ValueKind != JsonValueKind.Object || !parent.TryGetProperty(name, out var value))
return default;
long? number = value.ValueKind switch
var number = value.ValueKind switch
{
JsonValueKind.Number when value.TryGetInt64(out var whole) => whole,
JsonValueKind.Number when value.TryGetDouble(out var real) && real is >= 0 and < MaxCount => (long)real,
@@ -41,6 +41,7 @@ namespace PrivaPub.Federation.Objects
public PostSource Source { get; init; }
public PostPoll Poll { get; init; }
public List<PostFlair> Flairs { get; init; } = new();
[System.Diagnostics.CodeAnalysis.SuppressMessage("HouseRules", "HR0006", Justification = "Null when the note does not state searchableBy (FEP-268d), which is not the same as an empty list.")]
public List<string> SearchableBy { get; init; }
public InteractionRule QuotePolicy { get; init; }
public InteractionRule ReplyPolicy { get; init; }
+3 -5
View File
@@ -21,15 +21,13 @@ namespace PrivaPub.Federation.Objects
Task Revise(NoteDocument note, string activityId, CancellationToken token);
}
public class ObjectRecords : IObjectRecords
public class ObjectRecords(
IJobQueue queue) : IObjectRecords
{
public const int MaxRawBytes = 256 * 1024;
const int MaxRevisions = 10;
static readonly TimeSpan DescriptionAge = TimeSpan.FromDays(7);
readonly IJobQueue _queue;
public ObjectRecords(IJobQueue queue) => _queue = queue;
public async Task Record(NoteDocument note, PostEntity post, ObjectPath path, bool refetched, CancellationToken token)
{
@@ -99,7 +97,7 @@ namespace PrivaPub.Federation.Objects
var known = await DB.Default.Find<RemoteInstance>().Match(i => i.Host == host).ExecuteFirstAsync(token);
if (known?.DescribedAt > DateTime.UtcNow - DescriptionAge)
return;
await _queue.Enqueue(JobKind.DescribeInstance, host, host, InstanceDescriber.DedupeKey(host, DateTime.UtcNow), token);
await queue.Enqueue(JobKind.DescribeInstance, host, host, InstanceDescriber.DedupeKey(host, DateTime.UtcNow), token);
}
public static (string Text, string Hash, int Bytes, bool Truncated) Capture(JsonNode raw)
+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);
}
}
}
+13 -23
View File
@@ -33,31 +33,21 @@ namespace PrivaPub.Federation.Relays
// subscribes, and takes what they pass on: public posts, forwarded as their authors sent them (read again from their
// origin, as any forwarded post) or announced by the relay. A persona's public posts go to the relays that accepted us
// (owner decision 2026-10-06), as Mastodon sends them; nothing else of a persona's does.
public class Relays : IRelays
public class Relays(
IOptionsMonitor<FederationOptions> options,
IRemoteActorService remoteActors,
ILocalActorService localActors,
IDeliveryService delivery,
Domain.Instance.IInstanceSettings instanceSettings) : IRelays
{
static readonly TimeSpan AskAgain = TimeSpan.FromDays(1);
readonly IOptions<FederationOptions> _options;
readonly IRemoteActorService _remoteActors;
readonly ILocalActorService _localActors;
readonly IDeliveryService _delivery;
readonly Domain.Instance.IInstanceSettings _settings;
readonly SemaphoreSlim _reconciling = new(1, 1);//the hourly round and the administrator's page, one at a time
volatile HashSet<string> _accepted;
public Relays(IOptions<FederationOptions> options, IRemoteActorService remoteActors, ILocalActorService localActors, IDeliveryService delivery,
Domain.Instance.IInstanceSettings settings)
{
_options = options;
_remoteActors = remoteActors;
_localActors = localActors;
_delivery = delivery;
_settings = settings;
}
public IReadOnlyList<string> Configured => Clean(options.CurrentValue.Relays);
public IReadOnlyList<string> Configured => Clean(_options.Value.Relays);
public IReadOnlyList<string> Named => Clean(Configured.Concat(_settings.Current.Relays ?? new()));
public IReadOnlyList<string> Named => Clean(Configured.Concat(instanceSettings.Current.Relays ?? new()));
static List<string> Clean(IEnumerable<string> addresses) =>
(addresses ?? Array.Empty<string>()).Where(r => !string.IsNullOrWhiteSpace(r)).Select(r => r.Trim()).Distinct(StringComparer.Ordinal).ToList();
@@ -84,12 +74,12 @@ namespace PrivaPub.Federation.Relays
var known = await DB.Default.Find<RelaySubscription>().ExecuteAsync(token);
if (configured.Count == 0 && known.Count == 0)
return;
var instance = await _localActors.GetInstanceActor(token);
var instance = await localActors.GetInstanceActor(token);
var kept = known.Where(k => configured.Contains(k.Configured)).Select(k => k.ActorURI).Where(a => a != default).ToHashSet(StringComparer.Ordinal);
foreach (var gone in known.Where(k => !configured.Contains(k.Configured)))
{
if (gone.State != RelayState.Rejected && !string.IsNullOrEmpty(gone.InboxURL) && !kept.Contains(gone.ActorURI))
await _delivery.Enqueue(instance, new[] { gone.InboxURL }, Undo(instance, gone.FollowActivityURI), token);
await delivery.Enqueue(instance, new[] { gone.InboxURL }, Undo(instance, gone.FollowActivityURI), token: token);
await DB.Default.DeleteAsync<RelaySubscription>(gone.ID);
}
_accepted = default;
@@ -109,7 +99,7 @@ namespace PrivaPub.Federation.Relays
subscription.RequestedAt = DateTime.UtcNow;
subscription.AnsweredAt = default;
await DB.Default.SaveAsync(subscription, token);
await _delivery.Enqueue(instance, new[] { relay.InboxURL }, Follow(instance, subscription.FollowActivityURI), token);
await delivery.Enqueue(instance, new[] { relay.InboxURL }, Follow(instance, subscription.FollowActivityURI), token: token);
}
_accepted = default;
}
@@ -150,10 +140,10 @@ namespace PrivaPub.Federation.Relays
// the relay's actor, from its address or, for a relay named by its inbox, from the /actor beside it
async Task<ForeignAvatar> Resolve(string address, CancellationToken token)
{
var actor = await _remoteActors.GetActor(address, refresh: false, token);
var actor = await remoteActors.GetActor(address, refresh: false, token);
if (actor != default || !address.EndsWith("/inbox", StringComparison.Ordinal))
return actor;
return await _remoteActors.GetActor(address[..^"/inbox".Length] + "/actor", refresh: false, token);
return await remoteActors.GetActor(address[..^"/inbox".Length] + "/actor", refresh: false, token);
}
static JsonObject Follow(LocalActor instance, string id) => new()
@@ -68,7 +68,7 @@ namespace PrivaPub.Federation.Signing
break;
var key = header[i..equals].Trim();
i = equals + 1;
string value;
var value = default(string);
if (i < header.Length && header[i] == '"')
{
var close = header.IndexOf('"', i + 1);
@@ -89,7 +89,7 @@ namespace PrivaPub.Federation.Signing
if (!values.TryGetValue("keyId", out var keyId) || !values.TryGetValue("signature", out var signature))
return default;
byte[] signatureBytes;
var signatureBytes = default(byte[]);
try
{
signatureBytes = Convert.FromBase64String(signature);
@@ -35,7 +35,7 @@ namespace PrivaPub.Federation.Signing
var parameters = Parameters(value[(close + 1)..]);
if (!parameters.TryGetValue("keyid", out var keyId) || bytes.Length < 2 || bytes[0] != ':' || bytes[^1] != ':')
continue;
byte[] decoded;
var decoded = default(byte[]);
try
{
decoded = Convert.FromBase64String(bytes[1..^1]);
@@ -20,20 +20,11 @@ namespace PrivaPub.Federation.Signing
// a DM or a circle post refetches it, signed by the account it was for or by its instance actor, and Mastodon deletes its
// copy when the refetch answers 404. So the post is served to a signed request from someone it was for, or from the
// instance actor of a server where someone it was for lives; everyone else still gets 404.
public class SignedFetchAuthorizer : ISignedFetchAuthorizer
public class SignedFetchAuthorizer(
IRemoteActorService remoteActors,
DbEntities dbEntities,
ILocalActorService localActors = default) : ISignedFetchAuthorizer
{
readonly IRemoteActorService _remoteActors;
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
public SignedFetchAuthorizer(IRemoteActorService remoteActors, DbEntities dbEntities, ILocalActorService localActors = default)
{
_remoteActors = remoteActors;
_dbEntities = dbEntities;
_localActors = localActors;
}
// the actor whose signature (draft-cavage, or RFC 9421 when there is a base address to check its target against)
// verifies on the request
public async Task<ForeignAvatar> Requester(HttpRequest request, CancellationToken token)
@@ -41,11 +32,11 @@ namespace PrivaPub.Federation.Signing
var signature = RequestSignature.Of(request);
if (signature == default || signature.Problem(request, body: default) != default)
return default;
var signed = signature.Signed(request, _localActors?.BaseAddress ?? $"{request.Scheme}://{request.Host}");
var actor = await _remoteActors.GetActorByKeyId(signature.KeyId, refresh: false, token);
var signed = signature.Signed(request, localActors?.BaseAddress ?? $"{request.Scheme}://{request.Host}");
var actor = await remoteActors.GetActorByKeyId(signature.KeyId, refresh: false, token);
if (actor != default && signature.VerifiedBy(actor.PublicKey, signed))
return actor;
actor = await _remoteActors.GetActorByKeyId(signature.KeyId, refresh: true, token);
actor = await remoteActors.GetActorByKeyId(signature.KeyId, refresh: true, token);
return actor != default && signature.VerifiedBy(actor.PublicKey, signed) ? actor : default;
}
@@ -58,14 +49,14 @@ namespace PrivaPub.Federation.Signing
case PostVisibility.Public or PostVisibility.Unlisted:
return true;
case PostVisibility.Circle:
var circle = string.IsNullOrEmpty(post.GroupId) ? default : await _dbEntities.Groups.MatchID(post.GroupId).ExecuteFirstAsync(token);
var circle = string.IsNullOrEmpty(post.GroupId) ? default : await dbEntities.Groups.MatchID(post.GroupId).ExecuteFirstAsync(token);
return circle != default && MayReadCircle(circle, requester);
case PostVisibility.Direct:
return Addressed(post).Any(uri => Is(requester, uri));
case PostVisibility.FollowersOnly:
if (Addressed(post).Any(uri => Is(requester, uri)))
return true;
var followers = await _dbEntities.Followers
var followers = await dbEntities.Followers
.Match(f => f.LocalActorId == post.GroupUserId && f.LocalActorKind == LocalActorKind.Person && f.IsAccepted)
.ExecuteAsync(token);
return followers.Any(f => Is(requester, f.ActorURI));