From d8113c11e4d84a6d04d56b58d337c235154a535b Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Fri, 7 Mar 2025 15:04:34 +0100 Subject: [feature] Parse content warning to HTML, serialize via client API as plaintext (#3876) * [feature] Parse content warning as HTML, serialize via API to plaintext * tidy up some cruft * whoops * oops * i'm da joker baybee * clemency muy lorde * rename some of the text functions for clarity * jiggle the opts * fiddle de deee * hopefully the last test fix i ever have to do in my beautiful life --- internal/typeutils/internaltofrontend.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'internal/typeutils/internaltofrontend.go') diff --git a/internal/typeutils/internaltofrontend.go b/internal/typeutils/internaltofrontend.go index 3c5bd5ca0..537eeb6db 100644 --- a/internal/typeutils/internaltofrontend.go +++ b/internal/typeutils/internaltofrontend.go @@ -40,6 +40,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/language" "github.com/superseriousbusiness/gotosocial/internal/log" "github.com/superseriousbusiness/gotosocial/internal/media" + "github.com/superseriousbusiness/gotosocial/internal/text" "github.com/superseriousbusiness/gotosocial/internal/uris" "github.com/superseriousbusiness/gotosocial/internal/util" ) @@ -1125,13 +1126,16 @@ func (c *Converter) StatusToWebStatus( } webStatus := &apimodel.WebStatus{ - Status: apiStatus, - Account: acct, + Status: apiStatus, + SpoilerContent: s.ContentWarning, + Account: acct, } // Whack a newline before and after each "pre" to make it easier to outdent it. webStatus.Content = strings.ReplaceAll(webStatus.Content, "
", "\n
")
 	webStatus.Content = strings.ReplaceAll(webStatus.Content, "
", "
\n") + webStatus.SpoilerContent = strings.ReplaceAll(webStatus.SpoilerContent, "
", "\n
")
+	webStatus.SpoilerContent = strings.ReplaceAll(webStatus.SpoilerContent, "
", "
\n") // Add additional information for template. // Assume empty langs, hope for not empty language. @@ -1372,7 +1376,6 @@ func (c *Converter) baseStatusToFrontend( InReplyToID: nil, // Set below. InReplyToAccountID: nil, // Set below. Sensitive: *s.Sensitive, - SpoilerText: s.ContentWarning, Visibility: c.VisToAPIVis(ctx, s.Visibility), LocalOnly: s.IsLocalOnly(), Language: nil, // Set below. @@ -1393,6 +1396,11 @@ func (c *Converter) baseStatusToFrontend( Text: s.Text, ContentType: ContentTypeToAPIContentType(s.ContentType), InteractionPolicy: *apiInteractionPolicy, + + // Mastodon API says spoiler_text should be *text*, not HTML, so + // parse any HTML back to plaintext when serializing via the API, + // attempting to preserve semantic intent to keep it readable. + SpoilerText: text.ParseHTMLToPlain(s.ContentWarning), } if at := s.EditedAt; !at.IsZero() { -- cgit v1.2.3