2026-10-04 10:07:06 +02:00
using PrivaPub.Models.Post ;
2026-10-01 11:17:14 +02:00
using System.Globalization ;
2026-10-01 17:53:59 +02:00
using System.Text ;
2026-10-01 11:17:14 +02:00
using System.Text.Json.Nodes ;
using static PrivaPub . Federation . Objects . ActivityJson ;
namespace PrivaPub.Federation.Objects
{
public sealed class NoteDocument
{
public string Id { get ; init ; }
public string Type { get ; init ; }
2026-10-01 17:49:22 +02:00
public JsonObject Raw { get ; init ; }
2026-10-01 11:17:14 +02:00
public string AttributedTo { get ; init ; }
public string ContentHtml { get ; init ; }
public string Title { get ; init ; }
public string SpoilerText { get ; init ; }
2026-10-01 17:39:58 +02:00
public string Excerpt { get ; init ; }
2026-10-01 11:17:14 +02:00
public bool Sensitive { get ; init ; }
public string Language { get ; init ; }
public string Url { get ; init ; }
public string InReplyTo { get ; init ; }
public string Context { get ; init ; }
public string Audience { get ; init ; }
2026-10-06 08:17:10 +02:00
public string Wall { get ; init ; } //FEP-400e: the collection it says it was written in (`target`), a wall
public string WallOwner { get ; init ; } //that collection's attributedTo
2026-10-01 11:17:14 +02:00
public string QuoteUri { get ; init ; }
2026-10-05 11:43:58 +02:00
public string ReplyAuthorization { get ; init ; } //the parent author's ReplyAuthorization (approvedBy before GoToSocial 0.21)
2026-10-01 18:53:28 +02:00
public string QuoteAuthorization { get ; init ; }
public bool QuotesByConsent { get ; init ; } //FEP-044f `quote`, as opposed to the legacy keys that ask nobody
public bool QuoteDeleted { get ; init ; }
2026-10-01 11:17:14 +02:00
public DateTime Published { get ; init ; }
public DateTime ? Updated { get ; init ; }
public IReadOnlyList < string > To { get ; init ; } = Array . Empty < string >();
public IReadOnlyList < string > Cc { get ; init ; } = Array . Empty < string >();
public IReadOnlyList < PostMention > Mentions { get ; init ; } = Array . Empty < PostMention >();
public IReadOnlyList < string > Tags { get ; init ; } = Array . Empty < string >();
public IReadOnlyList < PostMedia > Attachments { get ; init ; } = Array . Empty < PostMedia >();
2026-10-01 17:53:59 +02:00
public PostSource Source { get ; init ; }
2026-10-01 18:35:20 +02:00
public PostPoll Poll { get ; init ; }
2026-10-07 21:14:32 +02:00
public List < PostFlair > Flairs { get ; init ; } = new ();
2026-11-10 01:24:43 +01:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("HouseRules", "HR0006", Justification = "Null when the note does not state searchableBy (FEP-268d), which is not the same as an empty list.")]
2026-10-07 21:17:36 +02:00
public List < string > SearchableBy { get ; init ; }
2026-10-01 18:53:28 +02:00
public InteractionRule QuotePolicy { get ; init ; }
2026-10-05 11:43:58 +02:00
public InteractionRule ReplyPolicy { get ; init ; }
public InteractionRule LikePolicy { get ; init ; }
public InteractionRule AnnouncePolicy { get ; init ; }
2026-10-06 16:16:34 +02:00
public InteractionRule ReplyApprovals { get ; init ; } //FEP-5624's canReply: who may reply, each reply then approved or refused
2026-10-01 18:35:20 +02:00
public IReadOnlyList < CustomEmoji > Emojis { get ; init ; } = Array . Empty < CustomEmoji >();
2026-10-01 17:53:59 +02:00
public string CoverURL { get ; init ; }
public PostLink Link { get ; init ; }
public VideoDetails Video { get ; init ; }
public AudioDetails Audio { get ; init ; }
public EventDetails Event { get ; init ; }
2026-10-04 10:07:06 +02:00
public PostPlace Place { get ; init ; }
2026-10-05 06:10:25 +02:00
public bool? CommentsEnabled { get ; init ; } //Lemmy, PieFed and Mbin: false once its moderators lock it; null when not said
2026-10-06 12:07:49 +02:00
public IReadOnlyList < PostRevision > FormerRepresentations { get ; init ; } = Array . Empty < PostRevision >(); //its earlier versions, oldest first
2026-10-01 11:17:14 +02:00
}
public static class NoteParser
{
2026-10-06 11:12:05 +02:00
static readonly string [] NoteTypes = { "Note" , "Article" , "Page" , "Question" , "Event" , "Video" , "Audio" , "Image" , "ChatMessage" , "PodcastEpisode" };
2026-10-01 11:17:14 +02:00
const int MaxAttachments = 16 ;
const int MaxTags = 64 ;
2026-10-06 12:07:49 +02:00
public const int MaxRevisions = 20 ;
2026-10-01 11:17:14 +02:00
public static NoteDocument Parse ( JsonNode node )
{
if ( node is not JsonObject note )
return default ;
var type = Value ( note , "type" );
var id = Id ( note );
if (! NoteTypes . Contains ( type ) || Origin . Of ( id ) == default )
return default ;
2026-10-06 11:12:05 +02:00
if ( type == "PodcastEpisode" )
note = Episode ( note );
2026-10-01 11:17:14 +02:00
var ( contentHtml , language ) = Content ( note );
var summary = Value ( note , "summary" );
2026-10-01 17:53:59 +02:00
var excerpt = ObjectShapes . Text ( summary , 1000 )
?? ( note [ "preview" ] is JsonObject preview && Value ( preview , "type" ) == "Note" ? ObjectShapes . Text ( Value ( preview , "content" ), 1000 ) : default );
2026-10-06 12:07:49 +02:00
var ( sensitive , summaryWarns ) = Warns ( note , type , summary );
2026-10-01 11:17:14 +02:00
return new NoteDocument
{
Id = id ,
Type = type ,
2026-10-01 17:49:22 +02:00
Raw = note ,
2026-10-01 11:17:14 +02:00
AttributedTo = Attribution ( note [ "attributedTo" ]),
ContentHtml = ContentSanitizer . Html ( contentHtml ),
2026-10-01 17:53:59 +02:00
Title = ObjectShapes . Text ( Value ( note , "name" ), 500 ),
2026-10-01 17:39:58 +02:00
SpoilerText = summaryWarns ? Plain ( summary , 500 ) : default ,
2026-10-01 17:53:59 +02:00
Excerpt = summaryWarns ? default : excerpt ,
2026-10-06 12:07:49 +02:00
Sensitive = sensitive ,
2026-10-01 11:17:14 +02:00
Language = language ,
Url = UrlOf ( note [ "url" ]) ?? id ,
InReplyTo = Id ( note [ "inReplyTo" ]),
Context = Id ( note [ "context" ]) ?? Value ( note , "conversation" ),
Audience = Id ( note [ "audience" ]),
2026-10-06 08:17:10 +02:00
Wall = note [ "target" ] is JsonObject target && Value ( target , "type" ) is "Collection" or "OrderedCollection" ? Id ( target ) : default ,
WallOwner = note [ "target" ] is JsonObject wallTarget ? Id ( wallTarget [ "attributedTo" ]) : default ,
2026-10-01 18:53:28 +02:00
QuoteUri = Id ( note [ "quote" ]) ?? Value ( note , "quoteUrl" ) ?? Value ( note , "quoteUri" ) ?? Value ( note , "_misskey_quote" ) ?? QuoteLink ( note ),
2026-10-05 11:43:58 +02:00
ReplyAuthorization = Id ( note [ "replyAuthorization" ]) ?? Id ( note [ "approvedBy" ]),
2026-10-01 18:53:28 +02:00
QuoteAuthorization = Id ( note [ "quoteAuthorization" ]),
QuotesByConsent = note . ContainsKey ( "quote" ),
QuoteDeleted = note [ "quote" ] is JsonObject quoted && Value ( quoted , "type" ) == "Tombstone" ,
2026-10-01 11:17:14 +02:00
Published = Time ( Value ( note , "published" )) ?? DateTime . UtcNow ,
Updated = Time ( Value ( note , "updated" )),
To = List ( note [ "to" ]),
Cc = List ( note [ "cc" ]),
Mentions = Tags ( note , "Mention" ). Select ( t => new PostMention { ActorURI = Value ( t , "href" ), Handle = Value ( t , "name" ) })
. Where ( m => Origin . Of ( m . ActorURI ) != default )
. DistinctBy ( m => m . ActorURI )
. Take ( MaxTags )
. ToList (),
2026-10-01 17:53:59 +02:00
Tags = Tags ( note , "Hashtag" ). Select ( t => Value ( t , "name" )?. TrimStart ( '#' ). Normalize ( NormalizationForm . FormKC ). ToLowerInvariant ())
2026-10-01 11:17:14 +02:00
. Where ( t => ! string . IsNullOrEmpty ( t ))
. Distinct ()
. Take ( MaxTags )
. ToList (),
2026-10-01 17:53:59 +02:00
Attachments = Attachments ( note ),
Source = ObjectShapes . Source ( note ),
2026-10-01 18:35:20 +02:00
Poll = ObjectShapes . Poll ( note ),
2026-10-07 21:14:32 +02:00
Flairs = ObjectShapes . Flairs ( note [ "tag" ]),
2026-10-07 21:17:36 +02:00
SearchableBy = ObjectShapes . SearchableBy ( note [ "searchableBy" ]),
2026-10-01 18:53:28 +02:00
QuotePolicy = ObjectShapes . QuotePolicy ( note ),
2026-10-05 11:43:58 +02:00
ReplyPolicy = ObjectShapes . Policy ( note , "canReply" ),
LikePolicy = ObjectShapes . Policy ( note , "canLike" ),
AnnouncePolicy = ObjectShapes . Policy ( note , "canAnnounce" ),
2026-10-06 16:16:34 +02:00
ReplyApprovals = ObjectShapes . ReplyApprovals ( note ),
2026-10-01 18:35:20 +02:00
Emojis = ObjectShapes . Emojis ( note [ "tag" ]),
2026-10-01 17:53:59 +02:00
CoverURL = ObjectShapes . Cover ( note ),
Link = ObjectShapes . Link ( note ),
Video = ObjectShapes . Video ( note ),
2026-10-06 11:12:05 +02:00
Audio = ObjectShapes . Audio ( type == "PodcastEpisode" ? note [ "audio" ] as JsonObject : note ),
2026-10-04 10:07:06 +02:00
Event = ObjectShapes . Event ( note ),
2026-10-05 06:10:25 +02:00
Place = ObjectShapes . NotePlace ( note ),
2026-10-06 12:07:49 +02:00
CommentsEnabled = note [ "commentsEnabled" ] is JsonValue comments && comments . TryGetValue < bool >( out var enabled ) ? enabled : null ,
FormerRepresentations = Former ( note , type )
2026-10-01 11:17:14 +02:00
};
}
2026-10-06 12:07:49 +02:00
// a summary is a content warning only on a Note or a Question, or a post marked sensitive; elsewhere it is an excerpt
static ( bool Sensitive , bool SummaryWarns ) Warns ( JsonObject note , string type , string summary )
{
var marked = note [ "sensitive" ] is JsonValue flag && flag . TryGetValue < bool >( out var value ) && value ;
var warns = type is "Note" or "Question" || marked ;
return ( marked || warns && ! string . IsNullOrWhiteSpace ( summary ), warns );
}
// Pleroma's and Akkoma's earlier versions of an edited post (`formerRepresentations`, newest first there), each read as
// the post itself is: the newest twenty, oldest first, each dated when it was written
static List < PostRevision > Former ( JsonObject note , string type )
{
if ( note [ "formerRepresentations" ] is not JsonObject former || former [ "orderedItems" ] is not JsonArray items )
return [];
return items . OfType < JsonObject >(). Take ( MaxRevisions * 2 )
. Select ( version =>
{
var summary = Value ( version , "summary" );
var ( sensitive , warns ) = Warns ( version , type , summary );
return new PostRevision
{
Title = ObjectShapes . Text ( Value ( version , "name" ), 500 ),
SpoilerText = warns ? Plain ( summary , 500 ) : default ,
ContentHtml = ContentSanitizer . Html ( Content ( version ). Html ),
HasContentWarning = sensitive ,
2026-10-17 05:57:25 +02:00
EditedAt = Time ( Value ( version , "updated" )) ?? Time ( Value ( version , "published" )) ?? default ,
Emojis = ObjectShapes . Emojis ( version [ "tag" ])
2026-10-06 12:07:49 +02:00
};
})
. Reverse ()
. OrderBy ( r => r . EditedAt )
. TakeLast ( MaxRevisions )
. ToList ();
}
2026-10-06 11:12:05 +02:00
// Castopod's PodcastEpisode: its words in `description` (a Note of its own), its sound in `audio`, its title the
// sound's name; read as a post with those
static JsonObject Episode ( JsonObject episode )
{
var note = episode . DeepClone (). AsObject ();
if ( note [ "description" ] is JsonObject description )
{
note [ "content" ] = description [ "content" ]?. DeepClone ();
note [ "contentMap" ] = description [ "contentMap" ]?. DeepClone ();
note [ "mediaType" ] = description [ "mediaType" ]?. DeepClone ();
}
if ( note [ "audio" ] is JsonObject audio )
{
note [ "name" ] ??= audio [ "name" ]?. DeepClone ();
note [ "attachment" ] = new JsonArray ( audio . DeepClone ());
}
return note ;
}
2026-10-01 11:17:14 +02:00
static ( string Html , string Language ) Content ( JsonObject note )
{
var content = Value ( note , "content" );
string language = default ;
if ( note [ "contentMap" ] is JsonObject map && map . Count > 0 )
{
var first = map . First ();
language = first . Key ;
if ( string . IsNullOrEmpty ( content ) && first . Value is JsonValue value && value . TryGetValue < string >( out var mapped ))
content = mapped ;
}
if ( string . IsNullOrEmpty ( content ) && Value ( note , "_misskey_content" ) is { } misskey )
content = System . Net . WebUtility . HtmlEncode ( misskey ). Replace ( "\n" , "<br>" );
2026-10-01 17:53:59 +02:00
if (! string . IsNullOrEmpty ( content ) && Value ( note , "mediaType" )?. StartsWith ( "text/markdown" , StringComparison . OrdinalIgnoreCase ) == true )
content = Rendering . ActivityPubRenderer . Html ( content );
return ( content , language ?? ObjectShapes . Language ( note ));
2026-10-01 11:17:14 +02:00
}
static string Attribution ( JsonNode node ) => node switch
{
JsonArray array => array . OfType < JsonObject >(). Where ( o => Value ( o , "type" ) is "Person" or "Service" or "Application" or "Organization" ). Select ( Id )
. Concat ( array . OfType < JsonValue >(). Select ( Id ))
. Concat ( array . OfType < JsonObject >(). Where ( o => Value ( o , "type" ) == "Group" ). Select ( Id ))
. FirstOrDefault ( id => id != default ),
_ => Id ( node )
};
static IEnumerable < JsonObject > Tags ( JsonObject note , string type ) =>
( note [ "tag" ] switch
{
JsonArray array => array . OfType < JsonObject >(),
JsonObject single => new [] { single },
_ => Enumerable . Empty < JsonObject >()
}). Where ( t => Value ( t , "type" ) == type );
2026-10-01 17:53:59 +02:00
static List < PostMedia > Attachments ( JsonObject note ) =>
ObjectShapes . Objects ( note [ "attachment" ])
2026-10-01 11:17:14 +02:00
. Where ( a => Value ( a , "type" ) is "Document" or "Image" or "Video" or "Audio" )
2026-10-01 17:53:59 +02:00
. Select ( Attachment )
2026-10-01 11:17:14 +02:00
. Where ( m => Origin . Of ( m . RemoteURL ) != default )
. Take ( MaxAttachments )
. ToList ();
2026-10-01 17:53:59 +02:00
static PostMedia Attachment ( JsonObject attachment )
{
var ( url , linkMediaType ) = ObjectShapes . MediaLink ( attachment [ "url" ] ?? attachment [ "href" ]);
return new PostMedia
{
RemoteURL = url ,
ContentType = ObjectShapes . InferMediaType ( Value ( attachment , "mediaType" ) ?? linkMediaType , url , Value ( attachment , "type" )),
RemotePreviewURL = ObjectShapes . Image ( attachment [ "icon" ]),
DurationSeconds = ObjectShapes . Seconds ( attachment [ "duration" ]),
Sensitive = attachment [ "sensitive" ] is JsonValue flag && flag . TryGetValue < bool >( out var marked ) ? marked : default ( bool? ),
Description = Plain ( Value ( attachment , "name" ) ?? Value ( attachment , "summary" ) ?? FirstOf ( attachment [ "nameMap" ]) ?? FirstOf ( attachment [ "summaryMap" ]), 1500 ),
Blurhash = Value ( attachment , "blurhash" ),
Width = Int ( attachment [ "width" ]),
Height = Int ( attachment [ "height" ]),
Focus = attachment [ "focalPoint" ] is JsonArray focus && focus . Count == 2
? focus . Select ( f => f is JsonValue v && v . TryGetValue < double >( out var d ) ? ( float ) Math . Clamp ( d , - 1 , 1 ) : 0f ). ToArray ()
: default
};
2026-10-01 11:17:14 +02:00
}
2026-10-01 18:53:28 +02:00
static string QuoteLink ( JsonObject note ) =>
Tags ( note , "Link" )
. Where ( t => Value ( t , "mediaType" ) is { } type && ( type . StartsWith ( "application/activity+json" ) || type . StartsWith ( "application/ld+json" )))
. Select ( t => Value ( t , "href" ))
. FirstOrDefault ( href => Origin . Of ( href ) != default );
2026-10-01 17:53:59 +02:00
static string FirstOf ( JsonNode map ) =>
map is JsonObject languages ? languages . Select ( l => l . Value is JsonValue v && v . TryGetValue < string >( out var text ) ? text : default ). FirstOrDefault ( t => t != default ) : default ;
2026-10-01 11:17:14 +02:00
static string UrlOf ( JsonNode node ) => node switch
{
JsonValue value when value . TryGetValue < string >( out var text ) => text ,
JsonObject obj => Value ( obj , "href" ) ?? Value ( obj , "id" ),
JsonArray array => array . Select ( item => item is JsonObject link && Value ( link , "mediaType" ) is { } mediaType && ! mediaType . StartsWith ( "text/html" )
? default
: UrlOf ( item ))
. FirstOrDefault ( url => url != default ) ?? array . Select ( UrlOf ). FirstOrDefault ( url => url != default ),
_ => default
};
static IReadOnlyList < string > List ( JsonNode node ) => node switch
{
JsonArray array => array . Select ( Id ). Where ( id => id != default ). Distinct ( StringComparer . Ordinal ). ToList (),
JsonNode single when Id ( single ) is { } id => new [] { id },
_ => Array . Empty < string >()
};
static DateTime ? Time ( string value ) =>
DateTimeOffset . TryParse ( value , CultureInfo . InvariantCulture , DateTimeStyles . AssumeUniversal , out var parsed )
? parsed . UtcDateTime
2026-10-01 13:19:16 +02:00
: ( DateTime ?) null ;
2026-10-01 11:17:14 +02:00
static int? Int ( JsonNode node ) =>
2026-10-01 12:05:58 +02:00
node is JsonValue value && value . TryGetValue < int >( out var number ) && number is > 0 and < 100_000 ? number : ( int? ) null ;
2026-10-01 11:17:14 +02:00
static string Plain ( string value , int max )
{
if ( string . IsNullOrWhiteSpace ( value ))
return default ;
value = value . Trim ();
return value . Length <= max ? value : value [.. max ];
}
}
}