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 -32
View File
@@ -24,15 +24,9 @@ using static OpenIddict.Abstractions.OpenIddictConstants;
namespace PrivaPub.Web.Pages.OAuth
{
[EnableRateLimiting(RateLimiting.Accounts)]
public class LoginModel : PageModel
public class LoginModel(
IRootUsersService users) : PageModel
{
readonly IRootUsersService _users;
public LoginModel(IRootUsersService users)
{
_users = users;
}
[BindProperty(SupportsGet = true)]
public string ReturnUrl { get; set; }
@@ -40,7 +34,7 @@ namespace PrivaPub.Web.Pages.OAuth
public void OnGet() => Harden();
public async Task<IActionResult> OnPostAsync([FromForm] string userName, [FromForm] string password, [FromForm] string oneTimeCode)
public async Task<IActionResult> OnPostAsync([FromForm] string userName, [FromForm] string password, [FromForm] string oneTimeCode, CancellationToken token = default)
{
Harden();
if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
@@ -48,7 +42,7 @@ namespace PrivaPub.Web.Pages.OAuth
Error = "Enter your username and password.";
return Page();
}
var result = await _users.LoginAsync(new LoginForm { UserName = userName, Password = password, OneTimeCode = oneTimeCode });
var result = await users.LoginAsync(new LoginForm { UserName = userName, Password = password, OneTimeCode = oneTimeCode }, token);
if (result.StatusCode == StatusCodes.Status428PreconditionRequired)
{
Error = "Enter the code from your authenticator app, or a recovery code, with your password.";
@@ -75,19 +69,11 @@ namespace PrivaPub.Web.Pages.OAuth
}
[IgnoreAntiforgeryToken]
public class AuthorizeModel : PageModel
public class AuthorizeModel(
DbEntities dbEntities,
ILocalActorService localActors,
OpenIddict.Abstractions.IOpenIddictApplicationManager applications) : PageModel
{
readonly DbEntities _dbEntities;
readonly ILocalActorService _localActors;
readonly OpenIddict.Abstractions.IOpenIddictApplicationManager _applications;
public AuthorizeModel(DbEntities dbEntities, ILocalActorService localActors, OpenIddict.Abstractions.IOpenIddictApplicationManager applications)
{
_dbEntities = dbEntities;
_localActors = localActors;
_applications = applications;
}
public string ApplicationName { get; private set; }
public IReadOnlyList<string> RequestedScopes { get; private set; } = Array.Empty<string>();
public IReadOnlyList<LocalActor> Avatars { get; private set; } = Array.Empty<LocalActor>();
@@ -98,7 +84,7 @@ namespace PrivaPub.Web.Pages.OAuth
public async Task<IActionResult> OnPostAsync([FromForm] string avatarId, [FromForm] string decision, CancellationToken token)
{
var request = HttpContext.GetOpenIddictServerRequest();
var rootId = await SignedInRoot();
var rootId = await SignedInRoot(token);
if (request == default || rootId == default)
return await Show(token);
@@ -110,8 +96,8 @@ namespace PrivaPub.Web.Pages.OAuth
}), OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
var owns = !string.IsNullOrEmpty(avatarId)
&& await _dbEntities.RootToAvatars.Match(r => r.RootId == rootId && r.AvatarId == avatarId).ExecuteAnyAsync(token);
var avatar = owns ? await _localActors.FindById(LocalActorKind.Person, avatarId, token) : default;
&& await dbEntities.RootToAvatars.Match(r => r.RootId == rootId && r.AvatarId == avatarId).ExecuteAnyAsync(token);
var avatar = owns ? await localActors.FindById(LocalActorKind.Person, avatarId, token) : default;
if (avatar is not { Suspended: false })//a suspended persona signs in nowhere (R6)
return await Show(token);
@@ -130,7 +116,7 @@ namespace PrivaPub.Web.Pages.OAuth
if (request == default)
return BadRequest();
var rootId = await SignedInRoot();
var rootId = await SignedInRoot(token);
var forceLogin = string.Equals((string)request["force_login"], "true", StringComparison.OrdinalIgnoreCase)
&& Request.Query["signed_in"] != "1";
if (rootId == default || forceLogin)
@@ -139,13 +125,13 @@ namespace PrivaPub.Web.Pages.OAuth
return Redirect($"/oauth/login?returnUrl={Uri.EscapeDataString("/oauth/authorize?" + query)}");
}
var application = await _applications.FindByClientIdAsync(request.ClientId, token);
ApplicationName = application == default ? "an application" : await _applications.GetDisplayNameAsync(application, token) ?? "an application";
var application = await applications.FindByClientIdAsync(request.ClientId, token);
ApplicationName = application == default ? "an application" : await applications.GetDisplayNameAsync(application, token) ?? "an application";
RequestedScopes = MastodonScopes.Parse(request.Scope);
var avatarIds = (await _dbEntities.RootToAvatars.Match(r => r.RootId == rootId).ExecuteAsync(token)).Select(r => r.AvatarId).ToList();
var avatarIds = (await dbEntities.RootToAvatars.Match(r => r.RootId == rootId).ExecuteAsync(token)).Select(r => r.AvatarId).ToList();
var avatars = new List<LocalActor>();
foreach (var id in avatarIds)
if (await _localActors.FindById(LocalActorKind.Person, id, token) is { Suspended: false } avatar)
if (await localActors.FindById(LocalActorKind.Person, id, token) is { Suspended: false } avatar)
avatars.Add(avatar);
Avatars = avatars;
Parameters = request.GetParameters()
@@ -157,13 +143,13 @@ namespace PrivaPub.Web.Pages.OAuth
return Page();
}
async Task<string> SignedInRoot()
async Task<string> SignedInRoot(CancellationToken token = default)
{
var login = await HttpContext.AuthenticateAsync(OAuthSetup.LoginScheme);
var rootId = login.Succeeded ? login.Principal.FindFirstValue(ClaimTypes.NameIdentifier) : default;
if (rootId == default)
return default;
var root = await _dbEntities.RootUsers.MatchID(rootId).ExecuteFirstAsync();
var root = await dbEntities.RootUsers.MatchID(rootId).ExecuteFirstAsync(token);
return root is { IsBanned: false, DeletedAt: null } ? rootId : default;
}
}
+24 -53
View File
@@ -41,18 +41,12 @@ namespace PrivaPub.Web.Pages
}
}
public class ProfileModel : PublicPageModel
public class ProfileModel(
ILocalActorService localActors,
DbEntities dbEntities) : PublicPageModel
{
const int PageSize = 20;
readonly ILocalActorService _localActors;
readonly DbEntities _dbEntities;
public ProfileModel(ILocalActorService localActors, DbEntities dbEntities)
{
_localActors = localActors;
_dbEntities = dbEntities;
}
public LocalActor Actor { get; private set; }
public string BiographyHtml { get; private set; }
@@ -60,7 +54,7 @@ namespace PrivaPub.Web.Pages
public async Task<IActionResult> OnGetAsync(string user, CancellationToken token)
{
Actor = await _localActors.FindByUserName(user, token);
Actor = await localActors.FindByUserName(user, token);
if (Actor is not { IsFederated: true, IsCircle: false, Suspended: false } || Actor.IsServerActor)
return NotFound();
if (WantsActivityJson())
@@ -69,8 +63,8 @@ namespace PrivaPub.Web.Pages
Harden();
BiographyHtml = ActivityPubRenderer.Html(Actor.Summary);
var posts = await (Actor.Kind == LocalActorKind.Group
? _dbEntities.Posts.Match(p => p.GroupId == Actor.Id)
: _dbEntities.Posts.Match(p => p.GroupUserId == Actor.Id && !p.IsFederatedCopy))
? dbEntities.Posts.Match(p => p.GroupId == Actor.Id)
: dbEntities.Posts.Match(p => p.GroupUserId == Actor.Id && !p.IsFederatedCopy))
.Match(VisibilityPolicy.IsPublic)
.Match(p => p.ReblogOfPostId == null)
.Sort(p => p.ID, Order.Descending)
@@ -83,7 +77,7 @@ namespace PrivaPub.Web.Pages
if (post.IsFederatedCopy)
continue;
if (!authors.TryGetValue(post.GroupUserId, out var author))
authors[post.GroupUserId] = author = await _localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
authors[post.GroupUserId] = author = await localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
if (author is { Suspended: false })
views.Add(PostView.From(post, author));
}
@@ -93,18 +87,12 @@ namespace PrivaPub.Web.Pages
}
// The hashtag links our posts carry (/tags/{tag}): this server's public posts with that tag, as a page like a profile's.
public class TagModel : PublicPageModel
public class TagModel(
ILocalActorService localActors,
DbEntities dbEntities) : PublicPageModel
{
const int PageSize = 20;
readonly ILocalActorService _localActors;
readonly DbEntities _dbEntities;
public TagModel(ILocalActorService localActors, DbEntities dbEntities)
{
_localActors = localActors;
_dbEntities = dbEntities;
}
public string Tag { get; private set; }
public IReadOnlyList<PostView> Posts { get; private set; } = Array.Empty<PostView>();
@@ -115,7 +103,7 @@ namespace PrivaPub.Web.Pages
if (string.IsNullOrEmpty(Tag) || Tag.Length > 100)
return NotFound();
Harden();
var posts = await _dbEntities.Posts
var posts = await dbEntities.Posts
.Match(p => p.Tags.Contains(Tag) && !p.IsFederatedCopy && p.ReblogOfPostId == null && p.Visibility == PostVisibility.Public)
.Match(VisibilityPolicy.IsPublic)
.Match(VisibilityPolicy.IsListed)
@@ -127,7 +115,7 @@ namespace PrivaPub.Web.Pages
foreach (var post in posts)
{
if (!authors.TryGetValue(post.GroupUserId, out var author))
authors[post.GroupUserId] = author = await _localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
authors[post.GroupUserId] = author = await localActors.FindById(LocalActorKind.Person, post.GroupUserId, token);
if (author is { IsFederated: true, Suspended: false })
views.Add(PostView.From(post, author));
}
@@ -136,27 +124,20 @@ namespace PrivaPub.Web.Pages
}
}
public class StatusModel : PublicPageModel
public class StatusModel(
ILocalActorService localActors,
DbEntities dbEntities) : PublicPageModel
{
readonly ILocalActorService _localActors;
readonly DbEntities _dbEntities;
public StatusModel(ILocalActorService localActors, DbEntities dbEntities)
{
_localActors = localActors;
_dbEntities = dbEntities;
}
public LocalActor Actor { get; private set; }
public PostView Post { get; private set; }
public string ObjectUri { get; private set; }
public async Task<IActionResult> OnGetAsync(string user, string id, CancellationToken token)
{
Actor = await _localActors.FindByUserName(user, token);
Actor = await localActors.FindByUserName(user, token);
if (Actor is not { IsFederated: true, Kind: LocalActorKind.Person, Suspended: false })
return NotFound();
var post = await _dbEntities.Posts
var post = await dbEntities.Posts
.Match(p => p.ID == id && p.GroupUserId == Actor.Id && !p.IsFederatedCopy && p.ReblogOfPostId == null)
.Match(VisibilityPolicy.IsPublic)
.ExecuteFirstAsync(token);
@@ -172,24 +153,14 @@ namespace PrivaPub.Web.Pages
}
}
public class StargazingModel : PublicPageModel
public class StargazingModel(
Microsoft.Extensions.Options.IOptionsMonitor<Infrastructure.Statistics.StatisticsOptions> options,
Microsoft.Extensions.Options.IOptionsMonitor<Models.AppConfiguration> app,
Infrastructure.Geo.IGeoLocator geo) : PublicPageModel
{
readonly Microsoft.Extensions.Options.IOptionsMonitor<Infrastructure.Statistics.StatisticsOptions> _options;
readonly Microsoft.Extensions.Options.IOptionsMonitor<Models.AppConfiguration> _app;
readonly Infrastructure.Geo.IGeoLocator _geo;
public StargazingModel(Microsoft.Extensions.Options.IOptionsMonitor<Infrastructure.Statistics.StatisticsOptions> options,
Microsoft.Extensions.Options.IOptionsMonitor<Models.AppConfiguration> app, Infrastructure.Geo.IGeoLocator geo)
{
_options = options;
_app = app;
_geo = geo;
}
public bool CrawlerEnabled => _options.CurrentValue.Crawler.Enabled;
public string GeoSource => _geo.Source;
public string UserAgent => Federation.Crawler.Stargazer.UserAgent(_app.CurrentValue.BackendBaseAddress);
public bool CrawlerEnabled => options.CurrentValue.Crawler.Enabled;
public string GeoSource => geo.Source;
public string UserAgent => Federation.Crawler.Stargazer.UserAgent(app.CurrentValue.BackendBaseAddress);
public void OnGet() => Harden();
}