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

@@ -15,23 +15,15 @@ using System.Globalization;
namespace PrivaPub.Api.Mastodon.Controllers
{
public class MediaController : MastodonController
public class MediaController(
IMediaService mediaService,
IMediaProxy mediaProxy,
IJobQueue jobs,
IInteractionLedger ledger = default) : MastodonController
{
const long UploadLimit = 100L * 1024 * 1024;
readonly IMediaService _media;
readonly IMediaProxy _proxy;
readonly IJobQueue _jobs;
readonly IInteractionLedger _ledger;
public MediaController(IMediaService media, IMediaProxy proxy, IJobQueue jobs, IInteractionLedger ledger = default)
{
_media = media;
_proxy = proxy;
_jobs = jobs;
_ledger = ledger;
}
[HttpPost("/api/v1/media"), HttpPost("/api/v2/media"), Scope("write:media"), EnableRateLimiting(RateLimiting.Uploads), RequestSizeLimit(UploadLimit),
RequestFormLimits(MultipartBodyLengthLimit = UploadLimit)]
@@ -42,12 +34,12 @@ namespace PrivaPub.Api.Mastodon.Controllers
var form = await Request.ReadFormAsync(token);
// v2 may answer before audio or video is processed (202, its url null until then), as Mastodon does; v1 waits
var later = Request.Path.StartsWithSegments("/api/v2/media");
var outcome = await _media.Upload(Me, form.Files["file"], form["description"], form["focus"], later, token);
var outcome = await mediaService.Upload(Me, form.Files["file"], form["description"], form["focus"], later, token);
if (!outcome.Ok)
return Error(outcome.Status, outcome.Error);
if (outcome.Attachment.ProcessingState != "pending")
return Json(View(outcome.Attachment));
await _jobs.EnqueueMany(new[] { ProcessMediaJob.JobFor(outcome.Attachment, new Uri(Me.BaseAddress).Host) }, token);
await jobs.EnqueueMany(new[] { ProcessMediaJob.JobFor(outcome.Attachment, new Uri(Me.BaseAddress).Host) }, token);
return new JsonResult(View(outcome.Attachment)) { StatusCode = StatusCodes.Status202Accepted };
}
@@ -74,7 +66,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
return NotFoundError();
if (attachment.PostId != null || attachment.ScheduledStatusId != null)
return Error(StatusCodes.Status422UnprocessableEntity, "Media attachment is currently used by a status");
await _media.Trash(m => m.ID == attachment.ID && m.PostId == null && m.ScheduledStatusId == null, "deleted by its owner", token);
await mediaService.Trash(m => m.ID == attachment.ID && m.PostId == null && m.ScheduledStatusId == null, "deleted by its owner", token);
return Json(new { });
}
@@ -96,19 +88,19 @@ namespace PrivaPub.Api.Mastodon.Controllers
[HttpGet("/media/proxy/{signature}/{encoded}"), AllowAnonymous, EnableRateLimiting(RateLimiting.Proxy), ApiExplorerSettings(IgnoreApi = true)]
public async Task<IActionResult> Proxy(string signature, string encoded, CancellationToken token)
{
var url = _proxy.Verified(signature, encoded);
if (url == default || _proxy.Refuses(url))
var url = mediaProxy.Verified(signature, encoded);
if (url == default || mediaProxy.Refuses(url))
return NotFound();
Response.Headers["X-Content-Type-Options"] = "nosniff";
Response.Headers["Content-Security-Policy"] = "default-src 'none'; sandbox";
if (_proxy.Cached(url) is { Path: not null } cached && Serve(cached.Path, cached.ContentType) is { } hit)
if (mediaProxy.Cached(url) is { Path: not null } cached && Serve(cached.Path, cached.ContentType) is { } hit)
{
_ledger?.Count(Interactions.HostOf(url), "media:hit");
ledger?.Count(Interactions.HostOf(url), "media:hit");
return hit;
}
if (Request.Headers.Range.Count == 0)
{
var (outcome, path, contentType) = await _proxy.Download(url, token);
var (outcome, path, contentType) = await mediaProxy.Download(url, token);
if (outcome == ProxyOutcome.Cached && Serve(path, contentType) is { } fetched)
return fetched;
if (outcome == ProxyOutcome.Failed)
@@ -121,7 +113,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
// once there is something to cache
IActionResult Serve(string path, string contentType)
{
FileStream file;
var file = default(FileStream);
try
{
file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, 64 * 1024, useAsync: true);
@@ -137,7 +129,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
async Task<IActionResult> Stream(string url, CancellationToken token)
{
var range = System.Net.Http.Headers.RangeHeaderValue.TryParse(Request.Headers.Range.ToString(), out var asked) ? asked : default;
using var upstream = await _proxy.Open(url, range, token);
using var upstream = await mediaProxy.Open(url, range, token);
if (upstream == default)
return NotFound();
if (upstream.IsSuccessStatusCode)
@@ -153,7 +145,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
return new EmptyResult();
}
object View(MediaAttachment attachment) => View(_media, attachment);
object View(MediaAttachment attachment) => View(mediaService, attachment);
internal static object View(IMediaService media, MediaAttachment attachment) => new
{