The API's JSON is generated at build time, and no answer is an anonymous object
Build / Build (push) Successful in 36s
Build / Build (push) Successful in 36s
Every answer of the Mastodon API and the client API now has its JSON shape generated when the server is built, instead of worked out by reflection while it runs; the bytes sent are unchanged. The small answers that were anonymous objects (the instance documents, notification policies, reports, lists, tags, trends, errors and the empty answers) are named types. A type without generated metadata fails at once in development and on the pasture, and in production is logged and still answered. Three lists that were sent as lazy sequences (moderation reports, the published domain blocks and the languages) are now built first. 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
8a1e4e419b
commit
f3fdeaf038
41 files changed
+1333
-266
No files matched your search
@@ -4,6 +4,7 @@ using Microsoft.Extensions.Options;
|
||||
|
||||
using MongoDB.Entities;
|
||||
|
||||
using PrivaPub.Api.Mastodon.Entities;
|
||||
using PrivaPub.Api.Mastodon.Infrastructure;
|
||||
using PrivaPub.Api.Mastodon.Mappers;
|
||||
using PrivaPub.Domain.Content;
|
||||
@@ -60,7 +61,7 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
string Thumbnail => Settings.ThumbnailUrl ?? $"{localActors.BaseAddress}{ServerSettings.DefaultThumbnailPath}";
|
||||
|
||||
static object Rules(ServerSettings settings) =>
|
||||
settings.ListedRules().Select(r => new { id = r.Id, text = r.Text ?? string.Empty, hint = r.Hint ?? string.Empty }).ToList();
|
||||
settings.ListedRules().Select(r => new RuleEntity { Id = r.Id, Text = r.Text ?? string.Empty, Hint = r.Hint ?? string.Empty }).ToList();
|
||||
|
||||
// the persona the administrator named as the server's contact, while it exists and its login may act
|
||||
async Task<object> ContactAccount(ServerSettings settings, CancellationToken token)
|
||||
@@ -89,31 +90,31 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
.Uri.GetLeftPart(UriPartial.Authority);
|
||||
|
||||
// every limit is what the server enforces, read from where it is enforced
|
||||
static object Statuses => new { max_characters = PrivaPub.Domain.Statuses.StatusService.MaxCharacters, max_media_attachments = 4, characters_reserved_per_url = 23 };
|
||||
static object Statuses => new StatusLimitsEntity { MaxCharacters = PrivaPub.Domain.Statuses.StatusService.MaxCharacters, MaxMediaAttachments = 4, CharactersReservedPerUrl = 23 };
|
||||
|
||||
object MediaAttachments
|
||||
{
|
||||
get
|
||||
{
|
||||
var media = HttpContext.RequestServices.GetRequiredService<IOptionsMonitor<PrivaPub.Domain.Media.MediaOptions>>().CurrentValue;
|
||||
return new
|
||||
return new MediaLimitsEntity
|
||||
{
|
||||
supported_mime_types = PrivaPub.Domain.Media.MediaService.SupportedTypes,
|
||||
image_size_limit = media.MaxImageBytes,
|
||||
image_matrix_limit = media.MaxPixels,
|
||||
video_size_limit = media.MaxVideoBytes,
|
||||
video_frame_rate_limit = (int)media.MaxFrameRate,
|
||||
video_matrix_limit = media.MaxVideoPixels
|
||||
SupportedMimeTypes = PrivaPub.Domain.Media.MediaService.SupportedTypes,
|
||||
ImageSizeLimit = media.MaxImageBytes,
|
||||
ImageMatrixLimit = media.MaxPixels,
|
||||
VideoSizeLimit = media.MaxVideoBytes,
|
||||
VideoFrameRateLimit = (int)media.MaxFrameRate,
|
||||
VideoMatrixLimit = media.MaxVideoPixels
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
static object Polls => new
|
||||
static object Polls => new PollLimitsEntity
|
||||
{
|
||||
max_options = PrivaPub.Domain.Statuses.PollService.MaxOptions,
|
||||
max_characters_per_option = PrivaPub.Domain.Statuses.PollService.MaxOptionLength,
|
||||
min_expiration = PrivaPub.Domain.Statuses.PollService.MinExpiration,
|
||||
max_expiration = PrivaPub.Domain.Statuses.PollService.MaxExpiration
|
||||
MaxOptions = PrivaPub.Domain.Statuses.PollService.MaxOptions,
|
||||
MaxCharactersPerOption = PrivaPub.Domain.Statuses.PollService.MaxOptionLength,
|
||||
MinExpiration = PrivaPub.Domain.Statuses.PollService.MinExpiration,
|
||||
MaxExpiration = PrivaPub.Domain.Statuses.PollService.MaxExpiration
|
||||
};
|
||||
|
||||
[HttpGet(ApiPaths.Instance.GET_V1), AllowAnonymous]
|
||||
@@ -123,24 +124,24 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
var users = await Counted.Users(token);
|
||||
var statuses = await Counted.LocalPosts(token);
|
||||
var domains = await KnownServers(token);
|
||||
return Json(new
|
||||
return Json(new InstanceV1Entity
|
||||
{
|
||||
uri = Domain,
|
||||
title = settings.Title,
|
||||
short_description = settings.ShortDescription ?? string.Empty,
|
||||
description = settings.ShortDescription ?? string.Empty,
|
||||
email = settings.ContactEmail ?? string.Empty,
|
||||
version = Version,
|
||||
urls = new { streaming_api = Streaming },
|
||||
stats = new { user_count = users, status_count = statuses, domain_count = domains },
|
||||
thumbnail = Thumbnail,
|
||||
languages = LanguageCodes(HttpContext.RequestServices),
|
||||
registrations = Open,
|
||||
approval_required = false,
|
||||
invites_enabled = true,//a group invitation always creates an account
|
||||
configuration = new { accounts = new { max_featured_tags = PrivaPub.Domain.Social.FeaturedTags.MaxFeatured }, statuses = Statuses, media_attachments = MediaAttachments, polls = Polls },
|
||||
contact_account = await ContactAccount(settings, token),
|
||||
rules = Rules(settings)
|
||||
Uri = Domain,
|
||||
Title = settings.Title,
|
||||
ShortDescription = settings.ShortDescription ?? string.Empty,
|
||||
Description = settings.ShortDescription ?? string.Empty,
|
||||
Email = settings.ContactEmail ?? string.Empty,
|
||||
Version = Version,
|
||||
Urls = new InstanceV1UrlsEntity { StreamingApi = Streaming },
|
||||
Stats = new InstanceV1StatsEntity { UserCount = users, StatusCount = statuses, DomainCount = domains },
|
||||
Thumbnail = Thumbnail,
|
||||
Languages = LanguageCodes(HttpContext.RequestServices),
|
||||
Registrations = Open,
|
||||
ApprovalRequired = false,
|
||||
InvitesEnabled = true,//a group invitation always creates an account
|
||||
Configuration = new InstanceV1ConfigurationEntity { Accounts = new InstanceV1ConfigurationAccountsEntity { MaxFeaturedTags = PrivaPub.Domain.Social.FeaturedTags.MaxFeatured }, Statuses = Statuses, MediaAttachments = MediaAttachments, Polls = Polls },
|
||||
ContactAccount = await ContactAccount(settings, token),
|
||||
Rules = Rules(settings)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -151,30 +152,30 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
await DB.Default.CountAsync<Models.Jobs.RemoteInstance>(i => i.Seen == "touched", token);
|
||||
|
||||
[HttpGet(ApiPaths.Instance.GET_V2), AllowAnonymous]
|
||||
public async Task<IActionResult> V2(CancellationToken token) => Json(new
|
||||
public async Task<IActionResult> V2(CancellationToken token) => Json(new InstanceV2Entity
|
||||
{
|
||||
domain = Domain,
|
||||
title = Settings.Title,
|
||||
version = Version,
|
||||
api_versions = new { mastodon = 7 },
|
||||
source_url = "https://git.thepra.dev/thepra/SocialPub",
|
||||
description = Settings.ShortDescription ?? string.Empty,
|
||||
usage = new { users = new { active_month = await Counted.ActiveUsers(30, token) } },
|
||||
thumbnail = new { url = Thumbnail },
|
||||
languages = LanguageCodes(HttpContext.RequestServices),
|
||||
configuration = new
|
||||
Domain = Domain,
|
||||
Title = Settings.Title,
|
||||
Version = Version,
|
||||
ApiVersions = new InstanceV2ApiVersionsEntity { Mastodon = 7 },
|
||||
SourceUrl = "https://git.thepra.dev/thepra/SocialPub",
|
||||
Description = Settings.ShortDescription ?? string.Empty,
|
||||
Usage = new InstanceV2UsageEntity { Users = new InstanceV2UsageUsersEntity { ActiveMonth = await Counted.ActiveUsers(30, token) } },
|
||||
Thumbnail = new InstanceV2ThumbnailEntity { Url = Thumbnail },
|
||||
Languages = LanguageCodes(HttpContext.RequestServices),
|
||||
Configuration = new InstanceV2ConfigurationEntity
|
||||
{
|
||||
urls = new { streaming = Streaming },
|
||||
accounts = new { max_featured_tags = PrivaPub.Domain.Social.FeaturedTags.MaxFeatured, max_pinned_statuses = StatusesController.MaxPins },
|
||||
statuses = Statuses,
|
||||
media_attachments = MediaAttachments,
|
||||
polls = Polls,
|
||||
translation = new { enabled = false },
|
||||
vapid = new { public_key = (await vapidKeys.Key(token)).ForApi }//Web Push (R9): the key every push is signed with
|
||||
Urls = new InstanceV2ConfigurationUrlsEntity { Streaming = Streaming },
|
||||
Accounts = new InstanceV2ConfigurationAccountsEntity { MaxFeaturedTags = PrivaPub.Domain.Social.FeaturedTags.MaxFeatured, MaxPinnedStatuses = StatusesController.MaxPins },
|
||||
Statuses = Statuses,
|
||||
MediaAttachments = MediaAttachments,
|
||||
Polls = Polls,
|
||||
Translation = new InstanceV2ConfigurationTranslationEntity { Enabled = false },
|
||||
Vapid = new InstanceV2ConfigurationVapidEntity { PublicKey = (await vapidKeys.Key(token)).ForApi }//Web Push (R9): the key every push is signed with
|
||||
},
|
||||
registrations = new { enabled = Open, approval_required = false, message = Open ? default : RegistrationOptions.ClosedMessage },
|
||||
contact = new { email = Settings.ContactEmail ?? string.Empty, account = await ContactAccount(Settings, token) },
|
||||
rules = Rules(Settings)
|
||||
Registrations = new InstanceV2RegistrationsEntity { Enabled = Open, ApprovalRequired = false, Message = Open ? default : RegistrationOptions.ClosedMessage },
|
||||
Contact = new InstanceV2ContactEntity { Email = Settings.ContactEmail ?? string.Empty, Account = await ContactAccount(Settings, token) },
|
||||
Rules = Rules(Settings)
|
||||
});
|
||||
|
||||
[HttpGet(ApiPaths.Instance.GET_Peers), AllowAnonymous]
|
||||
@@ -190,12 +191,12 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
return Json(Array.Empty<object>());
|
||||
if (_activity is { } cached && DateTime.UtcNow - cached.At < ActivityLifetime)
|
||||
return Json(cached.Weeks);
|
||||
var weeks = (await Counted.Weekly(ActivityWeeks, token)).Select(w => new
|
||||
var weeks = (await Counted.Weekly(ActivityWeeks, token)).Select(w => new ActivityWeekEntity
|
||||
{
|
||||
week = new DateTimeOffset(w.Week, TimeSpan.Zero).ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture),
|
||||
statuses = w.Posts.ToString(CultureInfo.InvariantCulture),
|
||||
logins = "0",
|
||||
registrations = w.Registrations.ToString(CultureInfo.InvariantCulture)
|
||||
Week = new DateTimeOffset(w.Week, TimeSpan.Zero).ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture),
|
||||
Statuses = w.Posts.ToString(CultureInfo.InvariantCulture),
|
||||
Logins = "0",
|
||||
Registrations = w.Registrations.ToString(CultureInfo.InvariantCulture)
|
||||
}).ToList();
|
||||
_activity = new CachedActivity(DateTime.UtcNow, weeks);
|
||||
return Json(weeks);
|
||||
@@ -208,10 +209,10 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
public IActionResult ExtendedDescription()
|
||||
{
|
||||
var settings = Settings;
|
||||
return Json(new
|
||||
return Json(new InstanceDocumentEntity
|
||||
{
|
||||
updated_at = settings.DescriptionUpdatedAt is { } updated ? MastodonJson.Time(updated) : MastodonJson.Day(DateTime.UtcNow),
|
||||
content = ContentRenderer.Document(settings.Description)
|
||||
UpdatedAt = settings.DescriptionUpdatedAt is { } updated ? MastodonJson.Time(updated) : MastodonJson.Day(DateTime.UtcNow),
|
||||
Content = ContentRenderer.Document(settings.Description)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -219,10 +220,10 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
public IActionResult PrivacyPolicy()
|
||||
{
|
||||
var settings = Settings;
|
||||
return Json(new
|
||||
return Json(new InstanceDocumentEntity
|
||||
{
|
||||
updated_at = MastodonJson.Time(settings.PrivacyPolicyUpdatedAt ?? ServerSettings.DefaultPrivacyPolicyUpdatedAt),
|
||||
content = ContentRenderer.Document(settings.PrivacyPolicy)
|
||||
UpdatedAt = MastodonJson.Time(settings.PrivacyPolicyUpdatedAt ?? ServerSettings.DefaultPrivacyPolicyUpdatedAt),
|
||||
Content = ContentRenderer.Document(settings.PrivacyPolicy)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -244,12 +245,12 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
var effective = (settings.TermsEffectiveAt ?? settings.UpdatedAt ?? DateTime.UtcNow).Date;
|
||||
if (on != default && on.Value != effective)
|
||||
return NotFoundError();
|
||||
return Json(new
|
||||
return Json(new TermsOfServiceEntity
|
||||
{
|
||||
effective_date = effective.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
|
||||
effective = effective <= DateTime.UtcNow.Date,
|
||||
content = ContentRenderer.Document(settings.TermsOfService),
|
||||
succeeded_by = default(string)
|
||||
EffectiveDate = effective.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
|
||||
Effective = effective <= DateTime.UtcNow.Date,
|
||||
Content = ContentRenderer.Document(settings.TermsOfService),
|
||||
SucceededBy = default(string)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -258,13 +259,13 @@ namespace PrivaPub.Api.Mastodon.Controllers
|
||||
[HttpGet(ApiPaths.Instance.GET_DomainBlocks), AllowAnonymous]
|
||||
public async Task<IActionResult> DomainBlocks(CancellationToken token) =>
|
||||
Json((await DB.Default.Find<DomainBlock>().Match(b => b.Published).Sort(b => b.Domain, Order.Ascending).ExecuteAsync(token))
|
||||
.Select(b => new
|
||||
.Select(b => new DomainBlockEntity
|
||||
{
|
||||
domain = b.Obfuscate ? Obfuscated(b.Domain) : b.Domain,
|
||||
digest = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(b.Domain ?? string.Empty))),
|
||||
severity = b.Severity switch { DomainBlockSeverity.Suspend => "suspend", DomainBlockSeverity.Silence => "silence", _ => "noop" },
|
||||
comment = b.PublicComment
|
||||
}));
|
||||
Domain = b.Obfuscate ? Obfuscated(b.Domain) : b.Domain,
|
||||
Digest = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(b.Domain ?? string.Empty))),
|
||||
Severity = b.Severity switch { DomainBlockSeverity.Suspend => "suspend", DomainBlockSeverity.Silence => "silence", _ => "noop" },
|
||||
Comment = b.PublicComment
|
||||
}).ToList());
|
||||
|
||||
// Mastodon's DomainBlock#public_domain: a quarter of the name kept at each end, every other character but the dots a *
|
||||
public static string Obfuscated(string domain)
|
||||
|
||||
Reference in new issue
Block a user