diff options
author | 2023-08-10 18:26:56 +0200 | |
---|---|---|
committer | 2023-08-10 18:26:56 +0200 | |
commit | 5588d4e88e3913c1ae00a6b57b466b2248cc493c (patch) | |
tree | 0c854a252f00ac5ba3e1b9333f66b4d22cf26687 /internal/typeutils/internaltorss.go | |
parent | [chore] Add test to ensure `show_reblogs` on follow works as expected (#2093) (diff) | |
download | gotosocial-5588d4e88e3913c1ae00a6b57b466b2248cc493c.tar.xz |
[bugfix] Use length in runes when trimming for RSS (#2094)
Diffstat (limited to 'internal/typeutils/internaltorss.go')
-rw-r--r-- | internal/typeutils/internaltorss.go | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/internal/typeutils/internaltorss.go b/internal/typeutils/internaltorss.go index 91f733884..adec26a30 100644 --- a/internal/typeutils/internaltorss.go +++ b/internal/typeutils/internaltorss.go @@ -32,8 +32,8 @@ import ( ) const ( - rssMaxTitleChars = 128 - rssDescriptionMaxChars = 256 + rssTitleMaxRunes = 128 + rssDescriptionMaxRunes = 256 ) func (c *converter) StatusToRSSItem(ctx context.Context, s *gtsmodel.Status) (*feeds.Item, error) { @@ -43,9 +43,9 @@ func (c *converter) StatusToRSSItem(ctx context.Context, s *gtsmodel.Status) (*f // example: Venice Film Festival Tries to Quit Sinking var title string if s.ContentWarning != "" { - title = trimTo(s.ContentWarning, rssMaxTitleChars) + title = trimTo(s.ContentWarning, rssTitleMaxRunes) } else { - title = trimTo(s.Text, rssMaxTitleChars) + title = trimTo(s.Text, rssTitleMaxRunes) } // Link -- The URL of the item. @@ -97,7 +97,7 @@ func (c *converter) StatusToRSSItem(ctx context.Context, s *gtsmodel.Status) (*f descriptionBuilder.WriteString("\"") } - description := trimTo(descriptionBuilder.String(), rssDescriptionMaxChars) + description := trimTo(descriptionBuilder.String(), rssDescriptionMaxRunes) // ID -- A string that uniquely identifies the item. // example: http://inessential.com/2002/09/01.php#a2 @@ -167,10 +167,26 @@ func (c *converter) StatusToRSSItem(ctx context.Context, s *gtsmodel.Status) (*f }, nil } +// trimTo trims the given `in` string to +// the length `to`, measured in runes. +// +// The reason for using runes is to avoid +// cutting off UTF-8 characters in the +// middle, and generating garbled bytes. +// +// If trimming was necessary, the returned +// string will be suffixed with ellipsis +// (`...`) to indicate omission. func trimTo(in string, to int) string { - if len(in) <= to { + var ( + runes = []rune(in) + runesLen = len(runes) + ) + + if runesLen <= to { + // Fine as-is. return in } - return in[:to-3] + "..." + return string(runes[:to-3]) + "..." } |