P7: community flairs, from Lemmy and PieFed
Build / Build (push) Successful in 8m2s

- A post's flairs are read in both dialects: Lemmy 1.0's CommunityPostTag (id, slug, name, description, colour slot)
  and PieFed's lemmy:CommunityTag (display name, text and background colours, whether to blur images). Colours are
  kept only as a colour slot or a hex value.
- A community's own list comes from its `tag` and PieFed's older `lemmy:tagsForPosts`, and fills in a post that names
  only a flair's id and slug, as Lemmy's posts do. Edits and refreshes keep them current.
- Clients get them as `privapub.flairs`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012CzABvBkbcFqoHdmi8b9WB
This commit is contained in:
thepraandClaude Opus 5.5 committed 2026-10-07 21:14:32 +02:00
1 parent 587997dd67
commit d4b1788a4f
14 files changed
+172 -3

No files matched your search

@@ -40,6 +40,7 @@ namespace PrivaPub.Federation.Objects
public IReadOnlyList<PostMedia> Attachments { get; init; } = Array.Empty<PostMedia>();
public PostSource Source { get; init; }
public PostPoll Poll { get; init; }
public List<PostFlair> Flairs { get; init; } = new();
public InteractionRule QuotePolicy { get; init; }
public InteractionRule ReplyPolicy { get; init; }
public InteractionRule LikePolicy { get; init; }
@@ -119,6 +120,7 @@ namespace PrivaPub.Federation.Objects
Attachments = Attachments(note),
Source = ObjectShapes.Source(note),
Poll = ObjectShapes.Poll(note),
Flairs = ObjectShapes.Flairs(note["tag"]),
QuotePolicy = ObjectShapes.QuotePolicy(note),
ReplyPolicy = ObjectShapes.Policy(note, "canReply"),
LikePolicy = ObjectShapes.Policy(note, "canLike"),
@@ -98,6 +98,48 @@ namespace PrivaPub.Federation.Objects
.Take(MaxEmojis)
.ToList();
const int MaxFlairs = 20;
// a post's flairs, or a community's (its `tag`, and PieFed's older `lemmy:tagsForPosts`), in both dialects
public static List<PostFlair> Flairs(params JsonNode[] lists) =>
lists.SelectMany(Objects)
.Where(t => Value(t, "type") is "CommunityPostTag" or "lemmy:CommunityTag" or "CommunityTag")
.Select(t => new PostFlair
{
Id = Id(t) is { } id && Origin.Of(id) != default ? id : default,
Name = Text(Value(t, "name") ?? Value(t, "display_name") ?? Value(t, "preferredUsername"), 100),
Slug = Text(Value(t, "preferredUsername") ?? Value(t, "display_name"), 100),
Description = Text(Value(t, "content"), 500),
Color = Value(t, "color") is { } slot && FlairSlot().IsMatch(slot) ? slot : default,
TextColor = Hex(Value(t, "textColor") ?? Value(t, "text_color")),
BackgroundColor = Hex(Value(t, "backgroundColor") ?? Value(t, "background_color")),
BlurImages = Bool(t, "blurImages") ?? Bool(t, "blur_images") ?? false
})
.Where(f => f.Name != default)
.DistinctBy(f => f.Id ?? f.Slug)
.Take(MaxFlairs)
.ToList();
// a post's flairs as its community describes them: Lemmy names only the id and slug on the post
public static List<PostFlair> Described(List<PostFlair> onPost, List<PostFlair> ofCommunity)
{
if (onPost == default || onPost.Count == 0)
return default;
foreach (var flair in onPost)
if (ofCommunity?.FirstOrDefault(c => c.Id != default && c.Id == flair.Id || c.Slug == flair.Slug) is { } known)
{
flair.Name = flair.Name == flair.Slug ? known.Name ?? flair.Name : flair.Name;
flair.Description ??= known.Description;
flair.Color ??= known.Color;
flair.TextColor ??= known.TextColor;
flair.BackgroundColor ??= known.BackgroundColor;
flair.BlurImages |= known.BlurImages;
}
return onPost;
}
static string Hex(string value) => value != default && HexColour().IsMatch(value) ? value : default;
public static InteractionRule QuotePolicy(JsonObject note) => Policy(note, "canQuote");
// one rule of a post's interactionPolicy (GoToSocial, FEP-044f): who may, automatically or once asked; the old names
@@ -399,6 +441,12 @@ namespace PrivaPub.Federation.Objects
[GeneratedRegex(@"^[\w+\-.@]+$")]
private static partial Regex Shortcode();
[GeneratedRegex(@"^color\d{2}$")]
private static partial Regex FlairSlot();
[GeneratedRegex(@"^#[0-9a-fA-F]{3,8}$")]
private static partial Regex HexColour();
[GeneratedRegex("<[^>]*>")]
private static partial Regex Tags();