R7 custom emoji: the server's own, added by administrators, sent on posts, profiles and reactions

LocalEmoji, a new collection (unique shortcode whatever its case, never renamed: the shortcode is its id elsewhere),
managed on /clientapi/admin/emoji/list|insert|update|delete and kept in the staff's log (emoji.insert, .update,
.delete). Its picture goes through IMediaService.EmojiImage: PNG, GIF, WebP or JPEG up to 256 KB, a PNG within 256 px
with alpha kept (an animated GIF or WebP stays an animated GIF, with a still PNG), keep=none, shrunk until it fits
GoToSocial's 100 KiB, held by ServerUse "emoji:<shortcode>" so the janitor and the audit leave it. LocalEmojis keeps the
offered ones in memory, reloaded on every change: GET /api/v1/custom_emojis is real; local posts (words, title, warning,
poll options, each revision) and profiles keep the emoji they name; Notes, Questions, actors and EmojiReacts carry
Mastodon's Emoji tag with its id, served as an Emoji at /emojis/{shortcode} (410 once deleted; the path awaits the
owner's confirmation); a persona reacts with one (Pleroma's reactions, announcements). A restore keeps the live emoji
as it keeps the settings, and trashes server pictures nothing holds. tools/pasture/scenarios/emoji.sh joins batch A.

Verified: built, and run locally against a scratch database (uploads of every type, animation kept, metadata gone,
refusals, rendering, reactions, history, announcements, delete and revive, backup and restore, media audit). The
pasture scenario and the sweep were not run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WpVCoDeM1qUTRp4fXQdfof
This commit is contained in:
thepraandClaude Opus 5.5 committed 2026-10-17 05:57:25 +02:00
1 parent 9f68ca3ed0
commit d08fbabaa5
38 files changed
+1117 -54

No files matched your search

@@ -0,0 +1,77 @@
using Microsoft.AspNetCore.Mvc;
using MongoDB.Entities;
using PrivaPub.Domain.Content;
using PrivaPub.Federation.Actors;
using PrivaPub.Federation.Rendering;
using PrivaPub.Models.Post;
using System.Text.Json.Nodes;
namespace PrivaPub.Federation.Controllers
{
// Each of the server's custom emoji as an ActivityPub `Emoji` object at its id, /emojis/{shortcode} (completion program
// R7; the path is LocalEmojis.Path): the tag that posts, actors and reactions carry, with its @context. GoToSocial takes
// an emoji only with an id, and may read it there. A disabled emoji is not served (404, it may come back); a deleted one
// answers 410 with a Tombstone. An emoji tells nothing about anyone, so it is served unsigned under SecureMode too, as the
// server's own actors are; a browser is sent to the picture.
[ApiController]
public class EmojisController : ControllerBase
{
const string ActivityContentType = "application/activity+json; charset=utf-8";
readonly ILocalActorService _localActors;
public EmojisController(ILocalActorService localActors)
{
_localActors = localActors;
}
[HttpGet, Route("/" + LocalEmojis.Path + "/{shortcode}")]
public async Task<IActionResult> Get(string shortcode, CancellationToken token)
{
Response.Headers.Vary = "Accept";
if (!LocalEmojis.IsShortcode(shortcode))
return NotFound();
var key = shortcode.ToLowerInvariant();
var emoji = await DB.Default.Find<LocalEmoji>().Match(e => e.Key == key).ExecuteFirstAsync(token);
if (emoji == default || emoji.Shortcode != shortcode)
return NotFound();
if (emoji.DeletedAt is { } deleted)
return new ContentResult
{
Content = new JsonObject
{
["@context"] = ActivityPubRenderer.ActivityStreams,
["id"] = LocalEmojis.Uri(_localActors.BaseAddress, emoji.Shortcode),
["type"] = "Tombstone",
["formerType"] = "Emoji",
["deleted"] = ActivityPubRenderer.Timestamp(deleted)
}.ToJsonString(),
ContentType = ActivityContentType,
StatusCode = StatusCodes.Status410Gone
};
if (emoji.Disabled || string.IsNullOrEmpty(emoji.Url))
return NotFound();
if (WantsHtml())
return Redirect(emoji.Url);
var live = new LocalEmojis.Live(emoji.ID, emoji.Shortcode, emoji.Url, emoji.StaticUrl ?? emoji.Url, emoji.ContentType ?? "image/png",
emoji.ImageUpdatedAt, emoji.Category, emoji.VisibleInPicker);
return new ContentResult
{
Content = LocalEmojis.Document(live, _localActors.BaseAddress).ToJsonString(),
ContentType = ActivityContentType,
StatusCode = StatusCodes.Status200OK
};
}
bool WantsHtml()
{
var accept = Request.Headers.Accept.ToString();
return accept.Contains("text/html", StringComparison.OrdinalIgnoreCase)
&& !accept.Contains("activity+json", StringComparison.OrdinalIgnoreCase)
&& !accept.Contains("ld+json", StringComparison.OrdinalIgnoreCase);
}
}
}