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

@@ -79,6 +79,7 @@
public PrivaPubPlace Place { get; set; }//a remote post's own place (Pixelfed), shown only
public Account Wall { get; set; }//whose wall it was written on (FEP-400e), when not its author's
public PrivaPubVotes Votes { get; set; } = new();
public List<PrivaPubFlair> Flairs { get; set; } = new();
}
public class PrivaPubSource
@@ -163,6 +164,18 @@
public string Url { get; set; }
}
public class PrivaPubFlair
{
public string Id { get; set; }
public string Name { get; set; }
public string Slug { get; set; }
public string Description { get; set; }
public string Color { get; set; }
public string TextColor { get; set; }
public string BackgroundColor { get; set; }
public bool BlurImages { get; set; }
}
public class PrivaPubVotes
{
public int Up { get; set; }
@@ -543,7 +543,12 @@ namespace PrivaPub.Api.Mastodon.Mappers
{
Name = post.Place.Name, Latitude = post.Place.Latitude, Longitude = post.Place.Longitude, Country = post.Place.Country
},
Votes = new PrivaPubVotes { Up = post.FavouritesCount, Down = post.DownvotesCount }
Votes = new PrivaPubVotes { Up = post.FavouritesCount, Down = post.DownvotesCount },
Flairs = post.Flairs?.Select(f => new PrivaPubFlair
{
Id = f.Id, Name = f.Name, Slug = f.Slug, Description = f.Description, Color = f.Color, TextColor = f.TextColor,
BackgroundColor = f.BackgroundColor, BlurImages = f.BlurImages
}).ToList() ?? new()
};
PrivaPubVariant Variant(MediaVariant variant) => new()
@@ -37,6 +37,7 @@ namespace PrivaPub.Federation.Actors
public string HeaderDescription { get; init; }
public string MovedTo { get; init; }
public string Moderators { get; init; }
public List<PostFlair> Flairs { get; init; } = new();
public List<string> AlsoKnownAs { get; init; } = new();//the accounts it says it also is (a Move's target names the moved one)
public DateTime? Published { get; init; }
public List<CustomEmoji> Emojis { get; init; } = new();
@@ -93,6 +94,8 @@ namespace PrivaPub.Federation.Actors
IconDescription = root.TryGetProperty("icon", out var described) ? Alt(described) : default,
HeaderDescription = root.TryGetProperty("image", out var headerImage) ? Alt(headerImage) : default,
MovedTo = RemoteActorService.Text(root, "movedTo"),
Flairs = type != "Group" ? new() : ObjectShapes.Flairs(root.TryGetProperty("tag", out var labels) ? JsonNode.Parse(labels.GetRawText()) : default,
root.TryGetProperty("lemmy:tagsForPosts", out var older) ? JsonNode.Parse(older.GetRawText()) : default),
Moderators = type == "Group" ? RemoteActorService.Text(root, "attributedTo") ?? RemoteActorService.Text(root, "moderators") : default,
AlsoKnownAs = root.TryGetProperty("alsoKnownAs", out var aliases) ? Uris(aliases) : new(),
Published = DateTimeOffset.TryParse(RemoteActorService.Text(root, "published"), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var published)
@@ -235,6 +235,7 @@ namespace PrivaPub.Federation.Actors
.Modify(a => a.IsMemorial, actor.Memorial)
.Modify(a => a.MovedToURL, Origin.Of(actor.MovedTo) == default ? default : actor.MovedTo)
.Modify(a => a.ModeratorsURL, Origin.Same(actor.Moderators, actor.Id) ? actor.Moderators : default)
.Modify(a => a.Flairs, actor.Flairs)
.Modify(a => a.AlsoKnownAs, actor.AlsoKnownAs)
.Modify(a => a.Published, actor.Published)
.Modify(a => a.Emojis, actor.Emojis)
+1
View File
@@ -38,6 +38,7 @@ namespace PrivaPub.Federation.Inbox
post.Audio = note.Audio ?? post.Audio;
post.Event = note.Event ?? post.Event;
post.Place = note.Place;
post.Flairs = await RemotePosts.Flairs(note, post.AudienceURI, token);
if (note.CommentsEnabled is { } enabled)
post.LockedAt = enabled ? null : post.LockedAt ?? DateTime.UtcNow;
if (!IsEdit(note, post))
+10
View File
@@ -137,10 +137,20 @@ namespace PrivaPub.Federation.Inbox
EditedAt = note.Updated,
Revisions = note.FormerRepresentations.ToList()
};
post.Flairs = await Flairs(note, post.AudienceURI, token);
await Episode(post, note, author, token);
return post;
}
// a post's flairs, filled in from its community's list
public static async Task<List<PostFlair>> Flairs(NoteDocument note, string communityUri, CancellationToken token)
{
if (note.Flairs.Count == 0)
return default;
var community = communityUri == default ? default : await DB.Default.Find<ForeignAvatar>().Match(f => f.ActorURI == communityUri).ExecuteFirstAsync(token);
return ObjectShapes.Described(note.Flairs, community?.Flairs);
}
static readonly System.Text.RegularExpressions.Regex Href = new(@"href=""([^""]+)""", System.Text.RegularExpressions.RegexOptions.Compiled);
// a post that is only a link to a podcast episode of its own author (Castopod announces each episode so): the
@@ -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();
+2
View File
@@ -46,6 +46,8 @@ namespace PrivaPub.Models.Post
public PostPlace Place { get; set; }
[BsonIgnoreIfNull]
public PostPoll Poll { get; set; }
[BsonIgnoreIfNull]
public List<PostFlair> Flairs { get; set; }
public List<float> Location { get; set; } = new();
public float RangeKm { get; set; } = 5.0f;
[BsonIgnoreIfNull]
+13
View File
@@ -38,6 +38,19 @@
public List<string> Manual { get; set; } = new();
}
// a community's label on a post (Lemmy 1.0's CommunityPostTag, PieFed's lemmy:CommunityTag): one of the community's own
public class PostFlair
{
public string Id { get; set; }
public string Name { get; set; }
public string Slug { get; set; }
public string Description { get; set; }
public string Color { get; set; }//Lemmy's colour slot, color01 to color10
public string TextColor { get; set; }//PieFed's, as #rrggbb
public string BackgroundColor { get; set; }
public bool BlurImages { get; set; }
}
public class PostSource
{
public string Content { get; set; }
+1
View File
@@ -77,6 +77,7 @@ namespace PrivaPub.Models.User
public string InboxURL { get; set; }
public string OutboxURL { get; set; }
public string FeaturedURL { get; set; }//featured: the posts it pins
public List<PrivaPub.Models.Post.PostFlair> Flairs { get; set; } = new();//a community's labels for its posts
public string ModeratorsURL { get; set; }//a community's attributedTo (Lemmy, PieFed, Mbin): the collection of its moderators
public List<AssertionKey> AssertionKeys { get; set; } = new();//its Ed25519 keys (FEP-521a), which prove what it sends (FEP-8b32)
public string WallURL { get; set; }//sm:wall (FEP-400e): where others write to it, Smithereen's walls