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
@@ -16,15 +16,9 @@ namespace PrivaPub.Domain.Timelines
|
||||
Task Distribute(Post post, CancellationToken token);
|
||||
}
|
||||
|
||||
public class Fanout : IFanout
|
||||
public class Fanout(
|
||||
DbEntities dbEntities) : IFanout
|
||||
{
|
||||
readonly DbEntities _dbEntities;
|
||||
|
||||
public Fanout(DbEntities dbEntities)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
}
|
||||
|
||||
public async Task Distribute(Post post, CancellationToken token)
|
||||
{
|
||||
// a located post is read only through /clientapi/post/nearby: the Mastodon API looks past it everywhere, so in a
|
||||
@@ -42,8 +36,8 @@ namespace PrivaPub.Domain.Timelines
|
||||
{
|
||||
case PostVisibility.Public or PostVisibility.Unlisted or PostVisibility.FollowersOnly:
|
||||
var followers = post.IsFederatedCopy
|
||||
? await _dbEntities.Followings.Match(f => f.TargetActorURI == post.ActorURI && f.State == FollowState.Accepted && f.FollowerKind == null).ExecuteAsync(token)
|
||||
: await _dbEntities.Followings.Match(f => f.TargetAccountId == post.GroupUserId && f.TargetIsLocal && f.State == FollowState.Accepted).ExecuteAsync(token);
|
||||
? await dbEntities.Followings.Match(f => f.TargetActorURI == post.ActorURI && f.State == FollowState.Accepted && f.FollowerKind == null).ExecuteAsync(token)
|
||||
: await dbEntities.Followings.Match(f => f.TargetAccountId == post.GroupUserId && f.TargetIsLocal && f.State == FollowState.Accepted).ExecuteAsync(token);
|
||||
foreach (var following in followers)
|
||||
if (await Shows(following, post, token))
|
||||
{
|
||||
@@ -53,14 +47,14 @@ namespace PrivaPub.Domain.Timelines
|
||||
}
|
||||
if (!string.IsNullOrEmpty(post.AudienceURI))
|
||||
// (the server's reader of feeds follows communities for nobody's home)
|
||||
foreach (var member in await _dbEntities.Followings.Match(f => f.TargetActorURI == post.AudienceURI && f.State == FollowState.Accepted && f.FollowerKind == null).ExecuteAsync(token))
|
||||
foreach (var member in await dbEntities.Followings.Match(f => f.TargetActorURI == post.AudienceURI && f.State == FollowState.Accepted && f.FollowerKind == null).ExecuteAsync(token))
|
||||
recipients.Add(member.AvatarId);
|
||||
// a post on a wall (FEP-400e): its owner, when a persona, and those following the owner
|
||||
if (!string.IsNullOrEmpty(post.WallOwnerURI))
|
||||
{
|
||||
if (await _dbEntities.Avatars.MatchID(post.WallOwnerAccountId).ExecuteAnyAsync(token))
|
||||
if (await dbEntities.Avatars.MatchID(post.WallOwnerAccountId).ExecuteAnyAsync(token))
|
||||
recipients.Add(post.WallOwnerAccountId);
|
||||
foreach (var follower in await _dbEntities.Followings.Match(f => f.TargetActorURI == post.WallOwnerURI && f.State == FollowState.Accepted).ExecuteAsync(token))
|
||||
foreach (var follower in await dbEntities.Followings.Match(f => f.TargetActorURI == post.WallOwnerURI && f.State == FollowState.Accepted).ExecuteAsync(token))
|
||||
recipients.Add(follower.AvatarId);
|
||||
}
|
||||
// a public post (not a boost, not a reply) comes to those following one of its hashtags, as on Mastodon,
|
||||
@@ -74,7 +68,7 @@ namespace PrivaPub.Domain.Timelines
|
||||
}
|
||||
break;
|
||||
case PostVisibility.Direct when !string.IsNullOrEmpty(post.ConversationId):
|
||||
var conversation = await _dbEntities.DmGroups.MatchID(post.ConversationId).ExecuteFirstAsync(token);
|
||||
var conversation = await dbEntities.DmGroups.MatchID(post.ConversationId).ExecuteFirstAsync(token);
|
||||
foreach (var member in conversation?.Members.Where(m => !m.IsForeign) ?? Enumerable.Empty<GroupMember>())
|
||||
recipients.Add(member.AvatarId);
|
||||
break;
|
||||
@@ -105,9 +99,9 @@ namespace PrivaPub.Domain.Timelines
|
||||
|
||||
if (string.IsNullOrEmpty(post.ReblogOfPostId))
|
||||
foreach (var mention in post.Mentions.Where(m => m.IsLocal && !string.IsNullOrEmpty(m.AccountId)))
|
||||
await Notifications.Add(mention.AccountId, NotificationType.Mention, post.AuthorAccountId ?? post.GroupUserId, post.ActorURI, post.ID, token);
|
||||
await Notifications.Add(mention.AccountId, NotificationType.Mention, post.AuthorAccountId ?? post.GroupUserId, post.ActorURI, post.ID, token: token);
|
||||
foreach (var avatarId in notifying.Where(recipients.Contains).Distinct())
|
||||
await Notifications.Add(avatarId, NotificationType.Status, post.AuthorAccountId ?? post.GroupUserId, post.ActorURI, post.ID, token);
|
||||
await Notifications.Add(avatarId, NotificationType.Status, post.AuthorAccountId ?? post.GroupUserId, post.ActorURI, post.ID, token: token);
|
||||
if (Streams.Anyone)
|
||||
await Stream(post, recipients, token);
|
||||
}
|
||||
@@ -230,7 +224,7 @@ namespace PrivaPub.Domain.Timelines
|
||||
return true;
|
||||
if (string.IsNullOrEmpty(repliedTo))
|
||||
return false;
|
||||
return await _dbEntities.Followings
|
||||
return await dbEntities.Followings
|
||||
.Match(f => f.AvatarId == following.AvatarId && f.TargetAccountId == repliedTo && f.State == FollowState.Accepted)
|
||||
.ExecuteAnyAsync(token);
|
||||
}
|
||||
|
||||
@@ -20,26 +20,20 @@ namespace PrivaPub.Domain.Timelines
|
||||
Task<WebResult> MarkNotificationsRead(string rootUserId, string avatarId, CancellationToken token);
|
||||
}
|
||||
|
||||
public class TimelineService : ITimelineService
|
||||
public class TimelineService(
|
||||
DbEntities dbEntities,
|
||||
IStringLocalizer<GenericRes> localizer) : ITimelineService
|
||||
{
|
||||
const int MaxLimit = 40;
|
||||
|
||||
readonly DbEntities _dbEntities;
|
||||
readonly IStringLocalizer<GenericRes> _localizer;
|
||||
|
||||
public TimelineService(DbEntities dbEntities, IStringLocalizer<GenericRes> localizer)
|
||||
{
|
||||
_dbEntities = dbEntities;
|
||||
_localizer = localizer;
|
||||
}
|
||||
|
||||
public async Task<WebResult> Home(string rootUserId, string avatarId, string maxId, int limit, CancellationToken token)
|
||||
{
|
||||
var result = new WebResult();
|
||||
if (!await Owns(rootUserId, avatarId, token))
|
||||
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
|
||||
var query = _dbEntities.TimelineEntries.Match(e => e.AvatarId == avatarId);
|
||||
var query = dbEntities.TimelineEntries.Match(e => e.AvatarId == avatarId);
|
||||
if (!string.IsNullOrEmpty(maxId))
|
||||
query.Match(f => f.Lt(e => e.PostId, maxId));
|
||||
var entries = await query.Sort(e => e.PostId, Order.Descending).Limit(Math.Clamp(limit, 1, MaxLimit)).ExecuteAsync(token);
|
||||
@@ -47,7 +41,7 @@ namespace PrivaPub.Domain.Timelines
|
||||
var ids = entries.Select(e => e.PostId).Concat(entries.Where(e => e.ReblogOfPostId != default).Select(e => e.ReblogOfPostId)).Distinct().ToList();
|
||||
var posts = ids.Count == 0
|
||||
? new Dictionary<string, Post>()
|
||||
: (await _dbEntities.Posts.Match(p => ids.Contains(p.ID)).Match(VisibilityPolicy.IsShown).ExecuteAsync(token)).ToDictionary(p => p.ID);
|
||||
: (await dbEntities.Posts.Match(p => ids.Contains(p.ID)).Match(VisibilityPolicy.IsShown).ExecuteAsync(token)).ToDictionary(p => p.ID);
|
||||
|
||||
result.Data = entries
|
||||
.Where(e => posts.ContainsKey(e.PostId) && (e.ReblogOfPostId == default || posts.ContainsKey(e.ReblogOfPostId)))
|
||||
@@ -66,9 +60,9 @@ namespace PrivaPub.Domain.Timelines
|
||||
{
|
||||
var result = new WebResult();
|
||||
if (!await Owns(rootUserId, avatarId, token))
|
||||
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
|
||||
var query = _dbEntities.Notifications.Match(n => n.AvatarId == avatarId);
|
||||
var query = dbEntities.Notifications.Match(n => n.AvatarId == avatarId);
|
||||
if (!string.IsNullOrEmpty(maxId))
|
||||
query.Match(f => f.Lt(n => n.ID, maxId));
|
||||
result.Data = (await query.Sort(n => n.ID, Order.Descending).Limit(Math.Clamp(limit, 1, MaxLimit)).ExecuteAsync(token))
|
||||
@@ -90,14 +84,14 @@ namespace PrivaPub.Domain.Timelines
|
||||
{
|
||||
var result = new WebResult();
|
||||
if (!await Owns(rootUserId, avatarId, token))
|
||||
return result.Invalidate(_localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
return result.Invalidate(localizer["Avatar not found."], StatusCodes.Status404NotFound);
|
||||
await DB.Default.Update<Notification>().Match(n => n.AvatarId == avatarId && !n.IsRead).Modify(n => n.IsRead, true).ExecuteAsync(token);
|
||||
return result;
|
||||
}
|
||||
|
||||
async Task<bool> Owns(string rootUserId, string avatarId, CancellationToken token) =>
|
||||
!string.IsNullOrEmpty(rootUserId) && !string.IsNullOrEmpty(avatarId)
|
||||
&& await _dbEntities.RootToAvatars.Match(ra => ra.RootId == rootUserId && ra.AvatarId == avatarId).ExecuteAnyAsync(token);
|
||||
&& await dbEntities.RootToAvatars.Match(ra => ra.RootId == rootUserId && ra.AvatarId == avatarId).ExecuteAnyAsync(token);
|
||||
|
||||
static ViewPost ToView(Post post) => new()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user