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
@@ -21,18 +21,12 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
// subscription[endpoint], subscription[keys][p256dh], subscription[keys][auth], subscription[standard] (4.4: RFC 8291
|
||||
// and RFC 8292, else the aesgcm draft), data[alerts][<type>] and data[policy] (all, followed, follower, none). Its answer is
|
||||
// Mastodon's WebPushSubscription. What is pushed, and how, is WebPushDispatcher's and WebPushJob's.
|
||||
public class PushController : MastodonController
|
||||
public class PushController(
|
||||
IVapid vapid,
|
||||
IOptionsMonitor<FederationOptions> federation) : MastodonController
|
||||
{
|
||||
const int MaxEndpointLength = 2048;
|
||||
|
||||
readonly IVapid _vapid;
|
||||
readonly IOptionsMonitor<FederationOptions> _federation;
|
||||
|
||||
public PushController(IVapid vapid, IOptionsMonitor<FederationOptions> federation)
|
||||
{
|
||||
_vapid = vapid;
|
||||
_federation = federation;
|
||||
}
|
||||
|
||||
string TokenId => User.GetTokenId();
|
||||
|
||||
@@ -43,7 +37,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
var endpoint = Params.Get("subscription[endpoint]")?.Trim();
|
||||
if (string.IsNullOrEmpty(endpoint))
|
||||
return Error(StatusCodes.Status400BadRequest, "Param is missing or the value is empty: subscription");
|
||||
if (endpoint.Length > MaxEndpointLength || !Uri.TryCreate(endpoint, UriKind.Absolute, out var uri) || !WebPushJob.IsAllowed(uri, _federation.CurrentValue))
|
||||
if (endpoint.Length > MaxEndpointLength || !Uri.TryCreate(endpoint, UriKind.Absolute, out var uri) || !WebPushJob.IsAllowed(uri, federation.CurrentValue))
|
||||
return Error(StatusCodes.Status422UnprocessableEntity, "Validation failed: Endpoint is not a valid URL");
|
||||
var p256dh = Params.Get("subscription[keys][p256dh]")?.Trim();
|
||||
var auth = Params.Get("subscription[keys][auth]")?.Trim();
|
||||
@@ -55,7 +49,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
if (invalid != default)
|
||||
return invalid;
|
||||
var tokenId = TokenId;
|
||||
var accessToken = await AccessToken();
|
||||
var accessToken = await AccessToken(token);
|
||||
if (string.IsNullOrEmpty(tokenId) || string.IsNullOrEmpty(accessToken))
|
||||
return Error(StatusCodes.Status401Unauthorized, "The access token is invalid");
|
||||
|
||||
@@ -74,7 +68,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
// (two at once with one token: the unique index on the token keeps one, and the later replaces it)
|
||||
for (var attempt = 0; ; attempt++)
|
||||
{
|
||||
await WebPush.ForgetToken(tokenId);
|
||||
await WebPush.ForgetToken(tokenId, token);
|
||||
try
|
||||
{
|
||||
subscription.ID = default;
|
||||
@@ -116,7 +110,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
[HttpDelete("/api/v1/push/subscription"), Scope("push")]
|
||||
public async Task<IActionResult> Delete(CancellationToken token)
|
||||
{
|
||||
await WebPush.ForgetToken(TokenId);
|
||||
await WebPush.ForgetToken(TokenId, token);
|
||||
return Json(new { });
|
||||
}
|
||||
|
||||
@@ -143,7 +137,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
|
||||
// the bearer token of this request, wherever the client put it (OpenIddict takes the header, a form field or the
|
||||
// query): the payload carries it back, as Mastodon's does
|
||||
async Task<string> AccessToken()
|
||||
async Task<string> AccessToken(CancellationToken token = default)
|
||||
{
|
||||
var stored = await HttpContext.GetTokenAsync(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme,
|
||||
OpenIddictValidationAspNetCoreConstants.Tokens.AccessToken);
|
||||
@@ -162,7 +156,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
endpoint = subscription.Endpoint,
|
||||
standard = subscription.Standard,
|
||||
alerts = WebPush.Alerts.ToDictionary(alert => alert, alert => subscription.Alerts.Contains(alert)),
|
||||
server_key = (await _vapid.Key(token)).ForApi,
|
||||
server_key = (await vapid.Key(token)).ForApi,
|
||||
policy = WebPush.Name(subscription.Policy)
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user