House rules: primary constructors, a cancellation token on every async call, and the rules fail the build
Build / Build (push) Successful in 30s
Build / Build (push) Successful in 30s
- Services, controllers and jobs take their dependencies through primary constructors and use them directly; where a method already named a local after a dependency, the dependency takes its type's name. - Every async method takes a CancellationToken last and passes it down to MongoDB, HTTP and the job queue. The setup claim is still given back when a request is cancelled. - The federation and media options are read through IOptionsMonitor. - The Mastodon entities keep sending "" for empty strings, and every model field where null means something says so. - The inbox's 503 with Retry-After, which other servers retry, is the one server error left, and says why. - Arasaka.HouseRules.Analyzers checks all of this in every build, here and in PrivaPub.ClientModels as decePubClient builds it. Checked against GoToSocial in the pasture: 64 passed, 0 failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
This commit is contained in:
1 parent
f0a14c737f
commit
0f73f69239
176 files changed
+2789
-3974
No files matched your search
@@ -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; }
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user