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
+97 -22
View File
@@ -22,6 +22,17 @@ type statusState struct {
buttons string
phase string
at time.Time
// pending is the newest state that arrived too soon after the last
// edit. timer shows it once the gap is over.
pending *pendingStatus
timer *time.Timer
}
type pendingStatus struct {
d *db.Delivery
text string
kb *telego.InlineKeyboardMarkup
phase string
}
// keyboardSignature identifies a keyboard by the callback data and links
@@ -160,14 +171,18 @@ func (b *Bot) renderStatus(ctx context.Context, d *db.Delivery, job *db.Job) (te
cancel := b.cancelJobKeyboard(lang, d.ID)
switch job.Status {
case db.JobQueued:
kb := cancel
if job.Lane == db.LaneFree && b.bill.Enabled() && b.cfg.Prices.SkipQueue > 0 {
kb = b.queuedKeyboard(lang, d.ID)
}
pos, err := b.sched.Position(ctx, job, b.slots())
if err != nil {
b.log.Error("queue position", "job_id", job.ID, "err", err)
}
if b.quietQueue(pos) {
// About to start: the queue is not worth mentioning.
return head + b.tr.HTML(lang, "status.downloading", nil), cancel, "queued"
}
kb := cancel
if job.Lane == db.LaneFree && b.bill.Enabled() && b.cfg.Prices.SkipQueue > 0 {
kb = b.queuedKeyboard(lang, d.ID)
}
return head + b.tr.HTML(lang, "status.queued", i18n.Args{
"Position": pos.Ahead + 1,
"Wait": b.tr.Span(lang, pos.Wait),
@@ -213,27 +228,47 @@ func (b *Bot) failureText(lang, code string) string {
return b.tr.HTML(lang, "status.failed", i18n.Args{"Reason": b.tr.T(lang, "error."+code, nil)})
}
// setStatus edits the status message unless nothing changed or the last
// edit of the same phase was too recent. Telegram limits how often a
// message may be edited, and progress numbers are not worth a flood wait.
func (b *Bot) setStatus(ctx context.Context, d *db.Delivery, text string, kb *telego.InlineKeyboardMarkup, phase string, force bool) {
// quietQueue reports whether a queued job is about to start: first in
// line and a short expected wait. Its status then just says it is
// downloading, without a position.
func (b *Bot) quietQueue(pos sched.Position) bool {
return pos.Ahead == 0 && pos.Wait <= time.Duration(b.cfg.QueueNoticeThresholdSeconds)*time.Second
}
// setStatus edits the status message unless nothing changed. A message is
// edited at most once per statusMinGap: Telegram limits how often a
// message may be edited, and a stream of edits only makes it flicker. A
// state that arrives too soon is not lost; the newest one is shown when
// the gap is over.
func (b *Bot) setStatus(ctx context.Context, d *db.Delivery, text string, kb *telego.InlineKeyboardMarkup, phase string, _ bool) {
if d.StatusMessageID == 0 {
return
}
now := time.Now()
// What the message shows is its text and its buttons. A change in
// either is worth an edit, for example "Skip the queue" disappearing.
shown := text + "\x00" + keyboardSignature(kb)
sig := keyboardSignature(kb)
shown := text + "\x00" + sig
b.statusMu.Lock()
st := b.statuses[d.ID]
if st != nil {
sameButtons := keyboardSignature(kb) == st.buttons
if st.text == shown || (!force && sameButtons && st.phase == phase && now.Sub(st.at) < b.statusMinGap) {
b.statusMu.Unlock()
return
}
if st == nil {
st = &statusState{}
b.statuses[d.ID] = st
}
b.statuses[d.ID] = &statusState{text: shown, buttons: keyboardSignature(kb), phase: phase, at: now}
if st.text == shown {
st.pending = nil
b.statusMu.Unlock()
return
}
if wait := b.statusMinGap - now.Sub(st.at); !st.at.IsZero() && wait > 0 {
st.pending = &pendingStatus{d: d, text: text, kb: kb, phase: phase}
if st.timer == nil {
st.timer = time.AfterFunc(wait, func() { b.flushStatus(ctx, d.ID, st) })
}
b.statusMu.Unlock()
return
}
st.text, st.buttons, st.phase, st.at, st.pending = shown, sig, phase, now, nil
b.statusMu.Unlock()
if b.liveStatus(ctx, d, kb) {
@@ -252,23 +287,63 @@ func (b *Bot) setStatus(ctx context.Context, d *db.Delivery, text string, kb *te
}
}
// flushStatus shows the state that waited for the edit gap, unless the
// delivery ended in the meantime.
func (b *Bot) flushStatus(ctx context.Context, deliveryID string, st *statusState) {
b.statusMu.Lock()
if b.statuses[deliveryID] != st {
b.statusMu.Unlock()
return
}
p := st.pending
st.pending, st.timer = nil, nil
b.statusMu.Unlock()
if p == nil || ctx.Err() != nil {
return
}
b.safely("status", func() { b.setStatus(ctx, p.d, p.text, p.kb, p.phase, false) })
}
// forgetStatus drops the throttling state of a delivery that ended.
func (b *Bot) forgetStatus(deliveryID string) {
b.statusMu.Lock()
if st := b.statuses[deliveryID]; st != nil && st.timer != nil {
st.timer.Stop()
}
delete(b.statuses, deliveryID)
b.statusMu.Unlock()
}
// finalStatus writes the last state of a delivery and forgets its
// throttling state. When the status message is gone a new one is sent, so
// the requester always learns the outcome.
func (b *Bot) finalStatus(ctx context.Context, d *db.Delivery, text string) {
b.statusMu.Lock()
delete(b.statuses, d.ID)
b.statusMu.Unlock()
if b.liveFinal(ctx, d, text) {
b.finalStatusKB(ctx, d, text, nil)
}
// finalStatusKB is finalStatus with buttons under the outcome, such as
// "Try again" under a failure.
func (b *Bot) finalStatusKB(ctx context.Context, d *db.Delivery, text string, kb *telego.InlineKeyboardMarkup) {
b.forgetStatus(d.ID)
if b.liveFinal(ctx, d, text, kb) {
return
}
if d.StatusMessageID != 0 {
err := b.edit(ctx, d.ChatID, d.StatusMessageID, d.StatusIsPhoto, text, nil)
err := b.edit(ctx, d.ChatID, d.StatusMessageID, d.StatusIsPhoto, text, kb)
if err == nil || !tg.IsMessageGone(err) {
return
}
}
_, _ = b.send(ctx, d.ChatID, d.ReplyToMessageID, text, nil)
_, _ = b.send(ctx, d.ChatID, d.ReplyToMessageID, text, kb)
}
// failureKeyboard offers the next step under a failed delivery.
func (b *Bot) failureKeyboard(ctx context.Context, d *db.Delivery, job *db.Job, code string) *telego.InlineKeyboardMarkup {
req, err := b.store.GetRequest(ctx, d.RequestID)
if err != nil {
return nil
}
return b.errorKeyboard(d.Lang, req, jobProfile(job), code)
}
// observe feeds the metrics from job events.