2026-10-01 11:52:31 +02:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2026-10-04 02:37:38 +02:00
|
|
|
using Microsoft.Extensions.Options;
|
2026-10-01 11:52:31 +02:00
|
|
|
|
|
|
|
|
using MongoDB.Entities;
|
|
|
|
|
|
|
|
|
|
using PrivaPub.Api.Mastodon.Infrastructure;
|
|
|
|
|
using PrivaPub.Domain.Privacy;
|
|
|
|
|
using PrivaPub.Federation.Actors;
|
2026-10-04 02:37:38 +02:00
|
|
|
using PrivaPub.Infrastructure;
|
2026-10-01 11:52:31 +02:00
|
|
|
using PrivaPub.Models.User;
|
|
|
|
|
|
|
|
|
|
using PostEntity = PrivaPub.Models.Post.Post;
|
|
|
|
|
|
|
|
|
|
namespace PrivaPub.Api.Mastodon.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class InstanceController : MastodonController
|
|
|
|
|
{
|
|
|
|
|
public const string Version = "4.2.0 (compatible; PrivaPub)";
|
|
|
|
|
const string Description = "A small ActivityPub server where one private login keeps several unlinkable public personas.";
|
|
|
|
|
|
|
|
|
|
readonly ILocalActorService _localActors;
|
2026-10-04 02:37:38 +02:00
|
|
|
readonly IOptionsMonitor<RegistrationOptions> _registrations;
|
2026-10-01 11:52:31 +02:00
|
|
|
|
2026-10-04 02:37:38 +02:00
|
|
|
public InstanceController(ILocalActorService localActors, IOptionsMonitor<RegistrationOptions> registrations)
|
2026-10-01 11:52:31 +02:00
|
|
|
{
|
|
|
|
|
_localActors = localActors;
|
2026-10-04 02:37:38 +02:00
|
|
|
_registrations = registrations;
|
2026-10-01 11:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-04 02:37:38 +02:00
|
|
|
bool Open => _registrations.CurrentValue.IsOpen;
|
|
|
|
|
|
2026-10-04 03:48:32 +02:00
|
|
|
// the languages this server is set up for (AppConfiguration:SupportedLanguages), "en" when none are named
|
|
|
|
|
public static IReadOnlyList<string> LanguageCodes(IServiceProvider services)
|
|
|
|
|
{
|
|
|
|
|
var configured = services.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<Models.AppConfiguration>>().CurrentValue.SupportedLanguages;
|
|
|
|
|
var codes = (configured ?? new List<string>()).Where(c => !string.IsNullOrWhiteSpace(c)).Select(c => c.Split('-')[0].ToLowerInvariant()).Distinct().ToList();
|
|
|
|
|
return codes.Count == 0 ? new[] { "en" } : codes;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-01 11:52:31 +02:00
|
|
|
string Domain => new Uri(_localActors.BaseAddress).Authority;
|
|
|
|
|
|
2026-10-04 03:48:32 +02:00
|
|
|
// 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 };
|
2026-10-01 11:52:31 +02:00
|
|
|
|
2026-10-04 03:48:32 +02:00
|
|
|
object MediaAttachments
|
2026-10-01 11:52:31 +02:00
|
|
|
{
|
2026-10-04 03:48:32 +02:00
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var media = HttpContext.RequestServices.GetRequiredService<IOptionsMonitor<PrivaPub.Domain.Media.MediaOptions>>().CurrentValue;
|
|
|
|
|
return new
|
|
|
|
|
{
|
|
|
|
|
supported_mime_types = PrivaPub.Domain.Media.MediaService.SupportedTypes,
|
|
|
|
|
image_size_limit = media.MaxImageBytes,
|
|
|
|
|
image_matrix_limit = media.MaxImageSide * media.MaxImageSide,
|
|
|
|
|
video_size_limit = media.MaxVideoBytes,
|
|
|
|
|
video_frame_rate_limit = 60,
|
|
|
|
|
video_matrix_limit = 2_304_000
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-01 11:52:31 +02:00
|
|
|
|
2026-10-04 03:48:32 +02:00
|
|
|
static object Polls => new
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
};
|
2026-10-01 11:52:31 +02:00
|
|
|
|
|
|
|
|
[HttpGet("/api/v1/instance"), AllowAnonymous]
|
|
|
|
|
public async Task<IActionResult> V1(CancellationToken token)
|
|
|
|
|
{
|
2026-10-04 03:48:32 +02:00
|
|
|
var users = await Counted.Users(token);
|
|
|
|
|
var statuses = await Counted.LocalPosts(token);
|
|
|
|
|
var domains = await KnownServers(token);
|
2026-10-01 11:52:31 +02:00
|
|
|
return Json(new
|
|
|
|
|
{
|
|
|
|
|
uri = Domain,
|
|
|
|
|
title = "PrivaPub",
|
|
|
|
|
short_description = Description,
|
|
|
|
|
description = Description,
|
|
|
|
|
email = string.Empty,
|
|
|
|
|
version = Version,
|
2026-10-04 03:48:32 +02:00
|
|
|
urls = new { },//no streaming API yet, so none is advertised
|
2026-10-01 11:52:31 +02:00
|
|
|
stats = new { user_count = users, status_count = statuses, domain_count = domains },
|
|
|
|
|
thumbnail = $"{_localActors.BaseAddress}/media/missing-header.png",
|
2026-10-04 03:48:32 +02:00
|
|
|
languages = LanguageCodes(HttpContext.RequestServices),
|
2026-10-04 02:37:38 +02:00
|
|
|
registrations = Open,
|
2026-10-01 11:52:31 +02:00
|
|
|
approval_required = false,
|
2026-10-04 02:37:38 +02:00
|
|
|
invites_enabled = true,//a group invitation always creates an account
|
2026-10-01 11:52:31 +02:00
|
|
|
configuration = new { accounts = new { max_featured_tags = 0 }, statuses = Statuses, media_attachments = MediaAttachments, polls = Polls },
|
|
|
|
|
contact_account = default(object),
|
|
|
|
|
rules = Array.Empty<object>()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-04 03:48:32 +02:00
|
|
|
// the servers we have exchanged activities with, as the statistics count them. Which ones is not published
|
|
|
|
|
// (/instance/peers stays empty, as on a Mastodon with its peers API off): on a small server it would say who its
|
|
|
|
|
// personas talk to.
|
|
|
|
|
static async Task<long> KnownServers(CancellationToken token) =>
|
|
|
|
|
await DB.Default.CountAsync<Models.Jobs.RemoteInstance>(i => i.Seen == "touched", token);
|
|
|
|
|
|
2026-10-01 11:52:31 +02:00
|
|
|
[HttpGet("/api/v2/instance"), AllowAnonymous]
|
2026-10-04 03:48:32 +02:00
|
|
|
public async Task<IActionResult> V2(CancellationToken token) => Json(new
|
2026-10-01 11:52:31 +02:00
|
|
|
{
|
|
|
|
|
domain = Domain,
|
|
|
|
|
title = "PrivaPub",
|
|
|
|
|
version = Version,
|
2026-10-01 18:53:28 +02:00
|
|
|
api_versions = new { mastodon = 7 },
|
2026-10-01 11:52:31 +02:00
|
|
|
source_url = "https://git.thepra.dev/thepra/SocialPub",
|
|
|
|
|
description = Description,
|
2026-10-04 03:48:32 +02:00
|
|
|
usage = new { users = new { active_month = await Counted.ActiveUsers(30, token) } },
|
2026-10-01 11:52:31 +02:00
|
|
|
thumbnail = new { url = $"{_localActors.BaseAddress}/media/missing-header.png" },
|
2026-10-04 03:48:32 +02:00
|
|
|
languages = LanguageCodes(HttpContext.RequestServices),
|
2026-10-01 11:52:31 +02:00
|
|
|
configuration = new
|
|
|
|
|
{
|
2026-10-04 03:48:32 +02:00
|
|
|
urls = new { },//no streaming API yet, so none is advertised
|
|
|
|
|
accounts = new { max_featured_tags = 0, max_pinned_statuses = StatusesController.MaxPins },
|
2026-10-01 11:52:31 +02:00
|
|
|
statuses = Statuses,
|
|
|
|
|
media_attachments = MediaAttachments,
|
|
|
|
|
polls = Polls,
|
|
|
|
|
translation = new { enabled = false }
|
|
|
|
|
},
|
2026-10-04 02:37:38 +02:00
|
|
|
registrations = new { enabled = Open, approval_required = false, message = Open ? default : RegistrationOptions.ClosedMessage },
|
2026-10-01 11:52:31 +02:00
|
|
|
contact = new { email = string.Empty, account = default(object) },
|
|
|
|
|
rules = Array.Empty<object>()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
[HttpGet("/api/v1/instance/peers"), AllowAnonymous]
|
|
|
|
|
public IActionResult Peers() => Json(Array.Empty<string>());
|
|
|
|
|
|
|
|
|
|
[HttpGet("/api/v1/instance/activity"), AllowAnonymous]
|
|
|
|
|
public IActionResult Activity() => Json(Array.Empty<object>());
|
|
|
|
|
|
|
|
|
|
[HttpGet("/api/v1/instance/rules"), AllowAnonymous]
|
|
|
|
|
public IActionResult Rules() => Json(Array.Empty<object>());
|
|
|
|
|
|
|
|
|
|
[HttpGet("/api/v1/instance/extended_description"), AllowAnonymous]
|
|
|
|
|
public IActionResult ExtendedDescription() => Json(new { updated_at = MastodonJson.Day(DateTime.UtcNow), content = $"<p>{Description}</p>" });
|
|
|
|
|
}
|
|
|
|
|
}
|