implement truncation of a honk should fix #9.
la-ninpre leobrekalini@gmail.com
Mon, 17 Oct 2022 18:45:34 +0300
2 files changed,
44 insertions(+),
22 deletions(-)
M
changelog.txt
→
changelog.txt
@@ -2,5 +2,6 @@ changelog
=== next ++ added truncation to long honks and donk descriptions + removed TelegramResponse from the honkMap -+ made gethonks retry 5 times before giving up++ made gethonks retry 5 times before giving up
M
main.go
→
main.go
@@ -185,6 +185,7 @@ }
// NewMessFromHonk creates a slice of Mess objects from existing Honk. func NewMessFromHonk(honk *Honk) []*Mess { + var truncateWith = "...\n\nfull honk: " + honk.XID // hardcoded == bad // donks should be sent as a separate messages, so need to create all of 'em // cap(messes) = 1 for honk + 1 for each donk var messes = make([]*Mess, 0, 1+len(honk.Donks))@@ -192,7 +193,7 @@
messes = append(messes, NewMess("html")) for _, donk := range honk.Donks { donkMess := NewMess("MarkdownV2") // donks don't contain html - donkMess.Caption = donk.Desc // FIXME: no check for length + donkMess.Caption = TruncateNoise(donk.Desc, truncateWith, 1024) switch { case strings.HasPrefix(donk.Media, "image/"): donkMess.Photo = donk.URL@@ -214,7 +215,8 @@ messes[0].MessageID = honk.MessID
messes = messes[:1] // don't donk if editing } - var noise = calmNoise(honk.Noise) + var text = CalmNoise(honk.Noise) + text = TruncateNoise(text, truncateWith, 4096) // bonk, then honk back - ok // honk back, then bonk - not gonna sync, is it ok? // upd: bonks work really confusing@@ -225,15 +227,15 @@ case "honked back":
messes[0].ReplyToMessageID = honk.ReplyToID case "bonked": oonker := fmt.Sprintf("<a href=\"%s\">%s</a>:", honk.Oonker, honk.Oondle) - noise = oonker + "\n" + noise + text = oonker + "\n" + text } // danger zone handling if strings.HasPrefix(honk.Precis, "DZ:") { - noise = strings.Join([]string{"<tg-spoiler>", "</tg-spoiler>"}, noise) - noise = honk.Precis + "\n" + noise + text = strings.Join([]string{"<tg-spoiler>", "</tg-spoiler>"}, text) + text = honk.Precis + "\n" + text } - messes[0].Text = noise + messes[0].Text = text return messes }@@ -359,25 +361,44 @@ )
const emptyNoise = "<p></p>\n" -// calmNoise erases and rewrites html tags that are not supported by Telegram. -func calmNoise(noise string) string { - // TODO: check length of a honk - +// CalmNoise erases and rewrites html tags that are not supported by Telegram. +func CalmNoise(s string) string { // delete these - noise = rePTags.ReplaceAllString(noise, "") - noise = reHlTags.ReplaceAllString(noise, "") - noise = reBrTags.ReplaceAllString(noise, "") - noise = reImgTags.ReplaceAllString(noise, "") - noise = reUlTags.ReplaceAllString(noise, "") - noise = reTbTags.ReplaceAllString(noise, "") + s = rePTags.ReplaceAllString(s, "") + s = reHlTags.ReplaceAllString(s, "") + s = reBrTags.ReplaceAllString(s, "") + s = reImgTags.ReplaceAllString(s, "") + s = reUlTags.ReplaceAllString(s, "") + s = reTbTags.ReplaceAllString(s, "") // these can be repurposed - noise = reHrTags.ReplaceAllString(noise, "---\n") - noise = reHnTags.ReplaceAllString(noise, "<b>$1</b>\n\n") - noise = reBqTags.ReplaceAllString(noise, "| <i>$1</i>") - noise = reLiTags.ReplaceAllString(noise, "* $1\n") + s = reHrTags.ReplaceAllString(s, "---\n") + s = reHnTags.ReplaceAllString(s, "<b>$1</b>\n\n") + s = reBqTags.ReplaceAllString(s, "| <i>$1</i>") + s = reLiTags.ReplaceAllString(s, "* $1\n") + + return strings.TrimSpace(s) +} - return strings.TrimSpace(noise) +// TruncateNoise truncates a string up to `length - len(with)` characters long and adds `with` to the end. +func TruncateNoise(s, with string, length int) string { + // telegram can handle posts no longer than 4096 (or 1024) characters _after_ the parsing of entities. + // we could be clever and calculate the true length of text, but let's keep it simple and stupid. + if len(s) <= length { + return s + } + + var b strings.Builder + b.Grow(length) + var end = length - 1 - len(with) + for i, r := range s { + if i >= end { + break + } + b.WriteRune(r) + } + b.WriteString(with) + return b.String() } func init() {