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:
co-authored by
Claude Opus 5.5
parent
3c153af97f
commit
6843053582
+101
-9
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/noderunners/dlbot/internal/service"
|
||||
"github.com/noderunners/dlbot/internal/storage"
|
||||
"github.com/noderunners/dlbot/internal/tg"
|
||||
"github.com/noderunners/dlbot/internal/thumb"
|
||||
"github.com/noderunners/dlbot/internal/widget"
|
||||
)
|
||||
|
||||
@@ -50,6 +51,15 @@ type Deps struct {
|
||||
ReloadI18n func() error
|
||||
// Metrics records what happens. Nil records nothing.
|
||||
Metrics *metrics.Metrics
|
||||
// Thumbnails makes the thumbnail uploaded with a video. Nil gets the
|
||||
// default, which fetches the source's thumbnail image.
|
||||
Thumbnails Thumbnailer
|
||||
}
|
||||
|
||||
// Thumbnailer turns a thumbnail URL into a JPEG Telegram accepts as the
|
||||
// thumbnail of a video, or nil.
|
||||
type Thumbnailer interface {
|
||||
Fetch(ctx context.Context, url string) []byte
|
||||
}
|
||||
|
||||
// Bot handles Telegram updates and job events.
|
||||
@@ -79,6 +89,7 @@ type Bot struct {
|
||||
baseCtx context.Context
|
||||
|
||||
metrics *metrics.Metrics
|
||||
thumbs Thumbnailer
|
||||
// widgets decides whether HTML widgets are tried; nil turns them off.
|
||||
widgets *widget.Breaker
|
||||
liveMu sync.Mutex
|
||||
@@ -92,6 +103,9 @@ type Bot struct {
|
||||
// cancelledAt is when each user last sent /cancel. A playlist item
|
||||
// that was already being looked up at that moment must not start.
|
||||
cancelledAt map[int64]time.Time
|
||||
// menuButtons is the language each private chat's menu button was
|
||||
// last labelled in.
|
||||
menuButtons map[int64]string
|
||||
// broadcasts holds texts waiting for the owner's confirmation.
|
||||
broadcasts map[string]string
|
||||
reloadI18n func() error
|
||||
@@ -142,22 +156,27 @@ func New(ctx context.Context, d Deps) (*Bot, error) {
|
||||
baseCtx: ctx,
|
||||
statuses: map[string]*statusState{},
|
||||
metrics: d.Metrics,
|
||||
thumbs: d.Thumbnails,
|
||||
live: map[string]*liveJob{},
|
||||
liveTried: map[string]int{},
|
||||
sending: map[string]bool{},
|
||||
pumping: map[int64]bool{},
|
||||
cancelledAt: map[int64]time.Time{},
|
||||
broadcasts: map[string]string{},
|
||||
menuButtons: map[int64]string{},
|
||||
reloadI18n: d.ReloadI18n,
|
||||
broadcastGap: 50 * time.Millisecond,
|
||||
admins: d.Admins,
|
||||
reconcileEvery: 5 * time.Second,
|
||||
statusMinGap: 3 * time.Second,
|
||||
statusMinGap: 2500 * time.Millisecond,
|
||||
invoiceGrace: 5 * time.Second,
|
||||
}
|
||||
if b.admins == nil {
|
||||
b.admins = groups.NewAdmins(d.API, 0)
|
||||
}
|
||||
if b.thumbs == nil {
|
||||
b.thumbs = thumb.New()
|
||||
}
|
||||
if d.Config.Telegram.Widgets {
|
||||
b.widgets = widget.NewBreaker()
|
||||
}
|
||||
@@ -189,8 +208,13 @@ func (b *Bot) SetTimings(reconcile, statusGap time.Duration) {
|
||||
b.statusMinGap = statusGap
|
||||
b.invoiceGrace = 4 * reconcile
|
||||
b.broadcastGap = time.Millisecond
|
||||
b.svc.SetProbeRetryDelay(10 * time.Millisecond)
|
||||
}
|
||||
|
||||
// SetThumbnails replaces the thumbnail maker. Tests use it to stay off the
|
||||
// network.
|
||||
func (b *Bot) SetThumbnails(t Thumbnailer) { b.thumbs = t }
|
||||
|
||||
// Admins returns the group admin checker, shared with the Mini App API.
|
||||
func (b *Bot) Admins() *groups.Admins { return b.admins }
|
||||
|
||||
@@ -287,9 +311,18 @@ func updateChatID(u telego.Update) int64 {
|
||||
return int64(u.UpdateID)
|
||||
}
|
||||
|
||||
// HandleUpdate routes one update.
|
||||
// HandleUpdate routes one update. The user behind it and the language to
|
||||
// answer in are resolved once, here, and travel with the context.
|
||||
func (b *Bot) HandleUpdate(ctx context.Context, u telego.Update) {
|
||||
b.metrics.Update(updateType(u))
|
||||
if from, chat := updateUser(u); from != nil {
|
||||
act, err := b.resolveActor(ctx, from, chat)
|
||||
if err != nil {
|
||||
b.log.Error("load user", "user_id", from.ID, "err", err)
|
||||
return
|
||||
}
|
||||
ctx = withActor(ctx, act)
|
||||
}
|
||||
switch {
|
||||
case u.Message != nil:
|
||||
b.handleMessage(ctx, u.Message)
|
||||
@@ -302,6 +335,22 @@ func (b *Bot) HandleUpdate(ctx context.Context, u telego.Update) {
|
||||
}
|
||||
}
|
||||
|
||||
// updateUser returns the person behind an update that needs an answer in
|
||||
// their language, and the chat it happened in.
|
||||
func updateUser(u telego.Update) (*telego.User, *telego.Chat) {
|
||||
switch {
|
||||
case u.Message != nil && u.Message.From != nil && !u.Message.From.IsBot:
|
||||
return u.Message.From, &u.Message.Chat
|
||||
case u.CallbackQuery != nil:
|
||||
if m := u.CallbackQuery.Message; m != nil {
|
||||
chat := m.GetChat()
|
||||
return &u.CallbackQuery.From, &chat
|
||||
}
|
||||
return &u.CallbackQuery.From, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func updateType(u telego.Update) string {
|
||||
switch {
|
||||
case u.Message != nil:
|
||||
@@ -320,6 +369,42 @@ func updateType(u telego.Update) string {
|
||||
type actor struct {
|
||||
user *db.User
|
||||
lang string
|
||||
// group holds the settings of the group the update came from, nil in
|
||||
// a private chat.
|
||||
group *db.GroupSettings
|
||||
}
|
||||
|
||||
type actorKey struct{}
|
||||
|
||||
func withActor(ctx context.Context, act actor) context.Context {
|
||||
return context.WithValue(ctx, actorKey{}, act)
|
||||
}
|
||||
|
||||
// actorFrom returns the actor HandleUpdate resolved for this update.
|
||||
func actorFrom(ctx context.Context) (actor, bool) {
|
||||
act, ok := ctx.Value(actorKey{}).(actor)
|
||||
return act, ok
|
||||
}
|
||||
|
||||
// resolveActor loads the user and picks the language to answer in: the
|
||||
// language the user chose, then Telegram's language_code, then English.
|
||||
// In a group with a language of its own, that language wins.
|
||||
func (b *Bot) resolveActor(ctx context.Context, from *telego.User, chat *telego.Chat) (actor, error) {
|
||||
act, err := b.actorFor(ctx, from)
|
||||
if err != nil {
|
||||
return act, err
|
||||
}
|
||||
if chat != nil && groups.IsGroup(chat.Type) {
|
||||
gset, err := b.store.GetGroupSettings(ctx, chat.ID)
|
||||
if err != nil {
|
||||
return act, err
|
||||
}
|
||||
act.group = gset
|
||||
if gset.Lang != "" {
|
||||
act.lang = gset.Lang
|
||||
}
|
||||
}
|
||||
return act, nil
|
||||
}
|
||||
|
||||
func (b *Bot) actorFor(ctx context.Context, from *telego.User) (actor, error) {
|
||||
@@ -327,11 +412,15 @@ func (b *Bot) actorFor(ctx context.Context, from *telego.User) (actor, error) {
|
||||
if err != nil {
|
||||
return actor{}, err
|
||||
}
|
||||
lang := u.LangOverride
|
||||
if lang == "" {
|
||||
lang = b.tr.Resolve(u.Lang)
|
||||
return actor{user: u, lang: b.userLang(u)}, nil
|
||||
}
|
||||
|
||||
// userLang is the language a user reads in a private chat.
|
||||
func (b *Bot) userLang(u *db.User) string {
|
||||
if u.LangOverride != "" {
|
||||
return u.LangOverride
|
||||
}
|
||||
return actor{user: u, lang: lang}, nil
|
||||
return b.tr.Resolve(u.Lang)
|
||||
}
|
||||
|
||||
func isPrivate(chatType string) bool { return chatType == telego.ChatTypePrivate }
|
||||
@@ -340,8 +429,11 @@ func isPrivate(chatType string) bool { return chatType == telego.ChatTypePrivate
|
||||
|
||||
func chatID(id int64) telego.ChatID { return telego.ChatID{ID: id} }
|
||||
|
||||
func replyParams(messageID int64) *telego.ReplyParameters {
|
||||
if messageID == 0 {
|
||||
// replyParams quotes a message, in groups only. In a private chat there
|
||||
// is one conversation and a quote only adds noise. Private chats have the
|
||||
// user's own, positive ID; groups have negative IDs.
|
||||
func replyParams(chat, messageID int64) *telego.ReplyParameters {
|
||||
if messageID == 0 || chat > 0 {
|
||||
return nil
|
||||
}
|
||||
return &telego.ReplyParameters{MessageID: int(messageID), AllowSendingWithoutReply: true}
|
||||
@@ -356,7 +448,7 @@ func (b *Bot) send(ctx context.Context, chat, replyTo int64, text string, kb *te
|
||||
Text: text,
|
||||
ParseMode: telego.ModeHTML,
|
||||
LinkPreviewOptions: noPreview,
|
||||
ReplyParameters: replyParams(replyTo),
|
||||
ReplyParameters: replyParams(chat, replyTo),
|
||||
}
|
||||
if kb != nil {
|
||||
p.ReplyMarkup = kb
|
||||
|
||||
Reference in New Issue
Block a user