summaryrefslogtreecommitdiff
path: root/internal/typeutils/internaltorss.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2023-08-10 18:26:56 +0200
committerLibravatar GitHub <noreply@github.com>2023-08-10 18:26:56 +0200
commit5588d4e88e3913c1ae00a6b57b466b2248cc493c (patch)
tree0c854a252f00ac5ba3e1b9333f66b4d22cf26687 /internal/typeutils/internaltorss.go
parent[chore] Add test to ensure `show_reblogs` on follow works as expected (#2093) (diff)
downloadgotosocial-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.go30
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]) + "..."
}