From 0d5906ba5db5237375240909ef5f55f5f1efa305 Mon Sep 17 00:00:00 2001 From: la-ninpre Date: Mon, 17 Oct 2022 18:45:34 +0300 Subject: implement truncation of a honk should fix #9. --- changelog.txt | 3 ++- main.go | 63 +++++++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/changelog.txt b/changelog.txt index 69671ff..a2a2a10 100644 --- a/changelog.txt +++ b/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 \ No newline at end of file ++ made gethonks retry 5 times before giving up diff --git a/main.go b/main.go index 1d49c67..965ff69 100644 --- a/main.go +++ b/main.go @@ -185,6 +185,7 @@ func NewMess(parseMode string) *Mess { // 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 @@ func NewMessFromHonk(honk *Honk) []*Mess { 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 @@ func NewMessFromHonk(honk *Honk) []*Mess { 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 @@ func NewMessFromHonk(honk *Honk) []*Mess { messes[0].ReplyToMessageID = honk.ReplyToID case "bonked": oonker := fmt.Sprintf("%s:", 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{"", ""}, noise) - noise = honk.Precis + "\n" + noise + text = strings.Join([]string{"", ""}, text) + text = honk.Precis + "\n" + text } - messes[0].Text = noise + messes[0].Text = text return messes } @@ -359,25 +361,44 @@ var ( const emptyNoise = "

\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, "$1\n\n") - noise = reBqTags.ReplaceAllString(noise, "| $1") - noise = reLiTags.ReplaceAllString(noise, "* $1\n") + s = reHrTags.ReplaceAllString(s, "---\n") + s = reHnTags.ReplaceAllString(s, "$1\n\n") + s = reBqTags.ReplaceAllString(s, "| $1") + 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() { -- cgit v1.2.3