aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorla-ninpre <leobrekalini@gmail.com>2022-10-17 18:45:34 +0300
committerla-ninpre <leobrekalini@gmail.com>2022-10-17 18:54:28 +0300
commit0d5906ba5db5237375240909ef5f55f5f1efa305 (patch)
treede166781075857bb6b76584d02a1d1d06227923f
parentb13fad84fbfaff7eee0eda48c584afc58aeb09d6 (diff)
downloadtelebonk-0d5906ba5db5237375240909ef5f55f5f1efa305.tar.gz
telebonk-0d5906ba5db5237375240909ef5f55f5f1efa305.zip
implement truncation of a honk
should fix #9.
-rw-r--r--changelog.txt3
-rw-r--r--main.go63
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("<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 @@ var (
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() {