Phase 1: quick flow fixes

- One message per job: the status message is deleted once the file is
  sent, or shortened to "Sent." when Telegram keeps it.
- No reply quotes in private chats; groups keep them.
- Queue status only mentions the position when the wait is above
  QUEUE_NOTICE_THRESHOLD_SECONDS or somebody is ahead. Status edits are
  throttled to one per 2.5 s per message; a state that arrives too soon
  is shown later instead of dropped. The wait estimate is 0 with a free
  slot.
- Caption with title (uploader and date when generic), uploader,
  duration, a short source link without tracking parameters and an
  optional "via @bot" line (CAPTION_ATTRIBUTION). Uploaded videos get a
  320 px JPEG thumbnail; merged MP4s are written with faststart.
- Buttons under the result: Audio (MP3), Other quality, Thumbnail,
  Delete. Requester only in groups.
- Language resolved once per update and passed in the context. A
  fallback to English is logged as a bug; a test walks every screen in
  Spanish and Dutch. Language picker on the first /start.
- Sizes without a zero decimal ("50 MB").
- First link of a new user shows the card once and sets the default to
  "best that fits my limit".
- Errors offer the next step (Try again, Smaller quality, Upgrade, Open
  app); passing lookup failures are retried once.
- Up to MULTI_LINK_MAX links per message; a link sent a moment ago gets
  "already sent" with a Send again button.
- Mini App menu button, labelled per private chat in the user's
  language.
- Personal /settings in private chats with an "Add me to a group"
  button.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 20:08:58 +00:00
co-authored by Claude Opus 5.5
parent 3c153af97f
commit 6843053582
52 changed files with 3369 additions and 271 deletions
+89
View File
@@ -0,0 +1,89 @@
package links
import (
"net/url"
"strings"
"unicode/utf8"
)
// trackingParams are query keys that only identify who shared a link or
// which campaign it came from. No site needs them to find the content.
var trackingParams = map[string]bool{
"igsh": true, "igshid": true, "jplx": true, "fbclid": true, "gclid": true,
"dclid": true, "gbraid": true, "wbraid": true, "msclkid": true, "yclid": true,
"twclid": true, "ttclid": true, "si": true, "mc_cid": true, "mc_eid": true,
"mibextid": true, "ref_src": true, "ref_url": true, "_hsenc": true, "_hsmi": true,
"feature": true, "share_id": true, "rdt": true,
}
// siteTracking are keys that are tracking on one site and meaningful on
// others: "t" is a share token on X but a start time on YouTube.
var siteTracking = map[string]map[string]bool{
"x.com": {"s": true, "t": true},
"twitter.com": {"s": true, "t": true},
"tiktok.com": {"is_from_webapp": true, "sender_device": true, "sender_web_id": true,
"_t": true, "_r": true, "is_copy_url": true, "web_id": true},
}
// isTracking reports whether a query key of a link to site is tracking.
func isTracking(site, key string) bool {
k := strings.ToLower(key)
if strings.HasPrefix(k, "utm_") || trackingParams[k] {
return true
}
for host, keys := range siteTracking {
if (site == host || strings.HasSuffix(site, "."+host)) && keys[k] {
return true
}
}
return false
}
// Clean removes tracking parameters from a link and keeps everything a
// site needs, such as v on YouTube. The order of the kept parameters does
// not change. A link that does not parse is returned as it is.
func Clean(raw string) string {
u, err := url.Parse(raw)
if err != nil {
return raw
}
site := Site(raw)
var kept []string
for _, part := range strings.Split(u.RawQuery, "&") {
if part == "" {
continue
}
key, _, _ := strings.Cut(part, "=")
if k, err := url.QueryUnescape(key); err == nil {
key = k
}
if !isTracking(site, key) {
kept = append(kept, part)
}
}
u.RawQuery = strings.Join(kept, "&")
u.ForceQuery = false
return u.String()
}
// Short is a link written for people: no scheme, no "www.", no trailing
// slash, cut to at most max characters with an ellipsis.
func Short(raw string, max int) string {
u, err := url.Parse(raw)
if err != nil || u.Host == "" {
return shortText(raw, max)
}
s := strings.TrimPrefix(strings.ToLower(u.Host), "www.") + u.EscapedPath()
if u.RawQuery != "" {
s += "?" + u.RawQuery
}
return shortText(strings.TrimSuffix(s, "/"), max)
}
func shortText(s string, max int) string {
if max < 2 || utf8.RuneCountInString(s) <= max {
return s
}
r := []rune(s)
return string(r[:max-1]) + "…"
}
+45
View File
@@ -143,3 +143,48 @@ func TestRedact(t *testing.T) {
t.Fatalf("RedactText leaked: %q", text)
}
}
func TestCleanRemovesTrackingParameters(t *testing.T) {
cases := []struct{ in, want string }{
// Campaign and share tags go, the video ID stays.
{"https://www.youtube.com/watch?v=abc123&utm_source=x&utm_medium=y&si=SHARE", "https://www.youtube.com/watch?v=abc123"},
{"https://youtu.be/abc123?si=XYZ&t=42", "https://youtu.be/abc123?t=42"},
{"https://www.youtube.com/watch?feature=share&v=abc&list=PL1", "https://www.youtube.com/watch?v=abc&list=PL1"},
{"https://www.instagram.com/reel/C1x/?igsh=MWQ1ZGUxMzBkMA==", "https://www.instagram.com/reel/C1x/"},
{"https://www.instagram.com/p/C1x/?igshid=abc&img_index=2", "https://www.instagram.com/p/C1x/?img_index=2"},
{"https://example.com/v?fbclid=IwAR0&jplx=1&id=9", "https://example.com/v?id=9"},
// "t" is a share token on X and a start time on YouTube.
{"https://x.com/user/status/1?s=20&t=abcdef", "https://x.com/user/status/1"},
{"https://mobile.twitter.com/user/status/1?s=46", "https://mobile.twitter.com/user/status/1"},
{"https://www.youtube.com/watch?v=abc&t=90s", "https://www.youtube.com/watch?v=abc&t=90s"},
{"https://www.tiktok.com/@u/video/7?is_from_webapp=1&sender_device=pc&lang=en", "https://www.tiktok.com/@u/video/7?lang=en"},
// Keys are compared without case; nothing else changes.
{"https://example.com/a?UTM_Campaign=1", "https://example.com/a"},
{"https://example.com/a", "https://example.com/a"},
{"https://example.com/a?", "https://example.com/a"},
{"https://example.com/a?b=1&&c=2", "https://example.com/a?b=1&c=2"},
}
for _, c := range cases {
if got := Clean(c.in); got != c.want {
t.Errorf("Clean(%q) = %q, want %q", c.in, got, c.want)
}
}
}
func TestShortLink(t *testing.T) {
cases := []struct {
in string
max int
want string
}{
{"https://www.youtube.com/watch?v=abc", 40, "youtube.com/watch?v=abc"},
{"https://example.com/", 40, "example.com"},
{"https://example.com/a/very/long/path/that/goes/on", 20, "example.com/a/very/…"},
{"https://www.instagram.com/reel/C1x/", 40, "instagram.com/reel/C1x"},
}
for _, c := range cases {
if got := Short(c.in, c.max); got != c.want {
t.Errorf("Short(%q, %d) = %q, want %q", c.in, c.max, got, c.want)
}
}
}