Phase 2: command screens
- /help: a short introduction with the user's default format, how to be asked every time and the limits on one line, plus buttons Commands, Account, Groups, Open app and Upgrade that switch the same message to a section. Group commands are hidden in private chats; "Add me to a group" takes their place. - /quality: Ask every time, Best that fits my limit, Best, 1080p, 720p, 480p and Audio only, with the active one checked. Free users see which qualities may go over their limit. - /history: pages of five compact buttons (site, title, format, when), a view per download with Send again, Thumbnail, Remove from history, and Clear history with a confirmation. Thumbnails are grouped under their video. Migration 00007 stores extractor, title, uploader and profile on deliveries and adds a hidden flag. - docs/SCREENS.md shows every screen in en, es and nl, generated by the language test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
6843053582
commit
123cede7f3
@@ -319,18 +319,6 @@ func (b *Bot) cmdStart(ctx context.Context, m *telego.Message, act actor) {
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(act.lang, "start.private", i18n.Args{"Name": act.user.FirstName}), kb)
|
||||
}
|
||||
|
||||
func (b *Bot) cmdHelp(ctx context.Context, m *telego.Message, act actor) {
|
||||
dec, err := b.svc.Entitlement(ctx, service.Who{UserID: act.user.ID, ChatID: m.Chat.ID, ChatType: m.Chat.Type}, false)
|
||||
if err != nil {
|
||||
b.internalError(ctx, m.Chat.ID, act, err)
|
||||
return
|
||||
}
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(act.lang, "help.text", i18n.Args{
|
||||
"MaxSize": b.tr.Size(act.lang, dec.Limits.MaxSize),
|
||||
"MaxDuration": b.tr.Span(act.lang, secondsDuration(dec.Limits.MaxDuration)),
|
||||
}), nil)
|
||||
}
|
||||
|
||||
// cmdDownload handles /dl, /audio and /thumb. The link comes from the
|
||||
// command itself or from the message the command replies to. The bot
|
||||
// answers under the message that holds the link.
|
||||
@@ -522,6 +510,10 @@ func (b *Bot) handleCallback(ctx context.Context, q *telego.CallbackQuery) {
|
||||
b.cbPlaylistStart(ctx, q, act, id, arg)
|
||||
case cbResend:
|
||||
b.cbResendFile(ctx, q, act, id)
|
||||
case cbHistory, cbHistoryView, cbHistoryDel, cbHistoryClear, cbHistoryWipe:
|
||||
b.cbHistoryAction(ctx, q, act, action, id, arg)
|
||||
case cbHelp:
|
||||
b.cbHelpSection(ctx, q, act, id)
|
||||
case cbFollowAudio:
|
||||
b.cbFollow(ctx, q, act, id, media.MP3)
|
||||
case cbFollowThumb:
|
||||
|
||||
+1
-56
@@ -311,39 +311,6 @@ func (b *Bot) subsCard(ctx context.Context, lr linkRequest, messageID int64, req
|
||||
_ = b.edit(ctx, lr.chatID, messageID, false, titleLine(info.Title)+"\n"+b.tr.HTML(lang, "subs.pick", nil), keyboard(rows...))
|
||||
}
|
||||
|
||||
// --- history -----------------------------------------------------------------
|
||||
|
||||
func (b *Bot) cmdHistory(ctx context.Context, m *telego.Message, act actor) {
|
||||
lang := act.lang
|
||||
dels, err := b.store.History(ctx, act.user.ID, 10)
|
||||
if err != nil {
|
||||
b.internalError(ctx, m.Chat.ID, act, err)
|
||||
return
|
||||
}
|
||||
if len(dels) == 0 {
|
||||
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(lang, "history.empty", nil), nil)
|
||||
return
|
||||
}
|
||||
var sb strings.Builder
|
||||
sb.WriteString(b.tr.HTML(lang, "history.header", nil))
|
||||
var rows [][]telego.InlineKeyboardButton
|
||||
n := 0
|
||||
for _, d := range dels {
|
||||
job, err := b.store.GetJob(ctx, d.JobID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
n++
|
||||
label := b.profileLabel(lang, jobProfile(job))
|
||||
sb.WriteString("\n")
|
||||
sb.WriteString(b.tr.HTML(lang, "history.line", i18n.Args{"N": n, "Title": shorten(job.Title, 60), "Label": label}))
|
||||
rows = append(rows, []telego.InlineKeyboardButton{
|
||||
button(b.tr.T(lang, "btn.resend", i18n.Args{"N": n, "Title": shorten(job.Title, 28)}), cbData(cbResend, d.ID)),
|
||||
})
|
||||
}
|
||||
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), sb.String(), keyboard(rows...))
|
||||
}
|
||||
|
||||
// cbResendFile sends a past download again. It is a new request like any
|
||||
// other: same limits, same quota, same price. The cache usually makes it
|
||||
// instant.
|
||||
@@ -371,29 +338,7 @@ func (b *Bot) cbResendFile(ctx context.Context, q *telego.CallbackQuery, act act
|
||||
chat, chatType = q.Message.GetChat().ID, q.Message.GetChat().Type
|
||||
}
|
||||
b.answer(ctx, q, "", false)
|
||||
info.Fresh = false
|
||||
req := &db.Request{
|
||||
ID: ids.New(), UserID: act.user.ID, ChatID: chat, ChatType: chatType, URL: orig.URL,
|
||||
Status: db.RequestReady, Info: info.JSON(), Lang: lang,
|
||||
}
|
||||
msg, err := b.send(ctx, chat, 0, titleLine(info.Title)+"\n"+b.tr.HTML(lang, "status.preparing", nil), nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req.CardMessageID = int64(msg.MessageID)
|
||||
if err := b.store.InsertRequest(ctx, req); err != nil {
|
||||
b.internalError(ctx, chat, act, err)
|
||||
return
|
||||
}
|
||||
err = b.startFromRequest(ctx, req, info, profile, nil)
|
||||
var le *service.LimitError
|
||||
switch {
|
||||
case err == nil:
|
||||
case errors.As(err, &le) && le.Upgradable:
|
||||
b.offerUpgrade(ctx, chat, req.CardMessageID, lang, req, profile, le, isPrivate(chatType))
|
||||
default:
|
||||
_ = b.edit(ctx, chat, req.CardMessageID, false, titleLine(info.Title)+"\n"+html.EscapeString(b.startErrorText(lang, err)), nil)
|
||||
}
|
||||
b.startAgain(ctx, act, chat, chatType, 0, orig, info, profile)
|
||||
}
|
||||
|
||||
// --- owner: cache ------------------------------------------------------------
|
||||
|
||||
@@ -526,8 +526,9 @@ func (b *Bot) cbGroupBuyProduct(ctx context.Context, q *telego.CallbackQuery, ac
|
||||
// --- personal preferences ---------------------------------------------------
|
||||
|
||||
func (b *Bot) cmdQuality(ctx context.Context, m *telego.Message, act actor) {
|
||||
kb := b.qualityKeyboard(act, func(v string) string { return cbData(cbQuality, v) }, false)
|
||||
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.tr.HTML(act.lang, "quality.pick", nil), kb)
|
||||
risky, maxSize := b.riskyQualities(ctx, act)
|
||||
kb := b.qualityKeyboard(act, risky, func(v string) string { return cbData(cbQuality, v) }, false)
|
||||
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), b.qualityText(act.lang, risky, maxSize), kb)
|
||||
}
|
||||
|
||||
func (b *Bot) cbSetQuality(ctx context.Context, q *telego.CallbackQuery, act actor, value string) {
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/mymmrac/telego"
|
||||
|
||||
"github.com/noderunners/dlbot/internal/groups"
|
||||
"github.com/noderunners/dlbot/internal/i18n"
|
||||
"github.com/noderunners/dlbot/internal/service"
|
||||
)
|
||||
|
||||
// /help is a short introduction with buttons that turn the same message
|
||||
// into one section at a time: commands, account, groups.
|
||||
|
||||
// cbHelp switches the help message to a section: hp:<section>.
|
||||
const cbHelp = "hp"
|
||||
|
||||
// Help sections.
|
||||
const (
|
||||
helpHome = "home"
|
||||
helpCommands = "cmds"
|
||||
helpAccount = "acct"
|
||||
helpGroups = "groups"
|
||||
)
|
||||
|
||||
// helpView renders one section of the help for a chat.
|
||||
func (b *Bot) helpView(ctx context.Context, act actor, chat int64, chatType, section string) (string, *telego.InlineKeyboardMarkup, error) {
|
||||
lang := act.lang
|
||||
private := isPrivate(chatType)
|
||||
back := keyboard([]telego.InlineKeyboardButton{button(b.tr.T(lang, "btn.back", nil), cbData(cbHelp, helpHome))})
|
||||
switch section {
|
||||
case helpCommands:
|
||||
if private {
|
||||
return b.tr.HTML(lang, "help.commands", nil), back, nil
|
||||
}
|
||||
return b.tr.HTML(lang, "help.commands_group", nil), back, nil
|
||||
case helpAccount:
|
||||
return b.tr.HTML(lang, "help.account", nil), back, nil
|
||||
case helpGroups:
|
||||
if private {
|
||||
// Group commands mean nothing here: how to get the bot into a
|
||||
// group instead.
|
||||
return b.tr.HTML(lang, "help.groups_private", nil), keyboard(
|
||||
[]telego.InlineKeyboardButton{b.addToGroupButton(lang)},
|
||||
back.InlineKeyboard[0],
|
||||
), nil
|
||||
}
|
||||
return b.tr.HTML(lang, "help.groups_group", nil), back, nil
|
||||
}
|
||||
|
||||
dec, err := b.svc.Entitlement(ctx, service.Who{UserID: act.user.ID, ChatID: chat, ChatType: chatType}, false)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
l := dec.Limits
|
||||
var format string
|
||||
if private {
|
||||
format = b.tr.HTML(lang, "help.format", i18n.Args{"Quality": b.userQualityName(lang, act.user.Quality)})
|
||||
} else {
|
||||
q := ""
|
||||
if act.group != nil {
|
||||
q = act.group.Quality
|
||||
}
|
||||
format = b.tr.HTML(lang, "help.format_group", i18n.Args{"Quality": b.qualityName(lang, q)})
|
||||
}
|
||||
text := b.tr.HTML(lang, "help.intro", nil) + "\n\n" + format + "\n\n" +
|
||||
b.tr.HTML(lang, "help.limits", i18n.Args{
|
||||
"MaxSize": b.capSize(lang, min64(l.MaxSize, b.cfg.MaxUploadBytes())),
|
||||
"MaxDuration": b.tr.Span(lang, secondsDuration(l.MaxDuration)),
|
||||
"Daily": b.capNumber(lang, int64(l.DailyDownloads)),
|
||||
})
|
||||
rows := [][]telego.InlineKeyboardButton{
|
||||
{
|
||||
button(b.tr.T(lang, "btn.help_commands", nil), cbData(cbHelp, helpCommands)),
|
||||
button(b.tr.T(lang, "btn.help_account", nil), cbData(cbHelp, helpAccount)),
|
||||
},
|
||||
{button(b.tr.T(lang, "btn.help_groups", nil), cbData(cbHelp, helpGroups))},
|
||||
}
|
||||
var last []telego.InlineKeyboardButton
|
||||
if btn, ok := b.appButton(lang, private, ""); ok {
|
||||
last = append(last, btn)
|
||||
}
|
||||
if !dec.Source.Paid() {
|
||||
last = append(last, b.upgradeButton(lang, private)...)
|
||||
}
|
||||
rows = append(rows, last)
|
||||
return text, keyboard(rows...), nil
|
||||
}
|
||||
|
||||
func (b *Bot) cmdHelp(ctx context.Context, m *telego.Message, act actor) {
|
||||
text, kb, err := b.helpView(ctx, act, m.Chat.ID, m.Chat.Type, helpHome)
|
||||
if err != nil {
|
||||
b.internalError(ctx, m.Chat.ID, act, err)
|
||||
return
|
||||
}
|
||||
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), text, kb)
|
||||
}
|
||||
|
||||
// cbHelpSection turns the help message into another section.
|
||||
func (b *Bot) cbHelpSection(ctx context.Context, q *telego.CallbackQuery, act actor, section string) {
|
||||
if q.Message == nil {
|
||||
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
||||
return
|
||||
}
|
||||
chat := q.Message.GetChat()
|
||||
if groups.IsGroup(chat.Type) && act.group == nil {
|
||||
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
||||
return
|
||||
}
|
||||
text, kb, err := b.helpView(ctx, act, chat.ID, chat.Type, section)
|
||||
if err != nil {
|
||||
b.answer(ctx, q, b.tr.T(act.lang, "error.internal", nil), true)
|
||||
return
|
||||
}
|
||||
b.answer(ctx, q, "", false)
|
||||
_ = b.edit(ctx, chat.ID, int64(q.Message.GetMessageID()), false, text, kb)
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"html"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mymmrac/telego"
|
||||
|
||||
"github.com/noderunners/dlbot/internal/db"
|
||||
"github.com/noderunners/dlbot/internal/i18n"
|
||||
"github.com/noderunners/dlbot/internal/links"
|
||||
"github.com/noderunners/dlbot/internal/media"
|
||||
"github.com/noderunners/dlbot/internal/service"
|
||||
)
|
||||
|
||||
// /history is one message that pages through past downloads. Each
|
||||
// download is a compact button; tapping it shows the download with its
|
||||
// actions. Thumbnails are not listed as downloads of their own: they are
|
||||
// an action under their video.
|
||||
|
||||
const (
|
||||
cbHistory = "hi" // hi:<page>
|
||||
cbHistoryView = "hv" // hv:<delivery id>:<page>
|
||||
cbHistoryDel = "hd" // hd:<delivery id>:<page>
|
||||
cbHistoryClear = "hc" // hc:<page>, asks first
|
||||
cbHistoryWipe = "hy" // hy, confirmed
|
||||
)
|
||||
|
||||
// historyPageSize is how many downloads one page lists.
|
||||
const historyPageSize = 5
|
||||
|
||||
// siteNames are the names people know sites by, keyed by yt-dlp extractor.
|
||||
var siteNames = map[string]string{
|
||||
"youtube": "YouTube", "instagram": "Instagram", "tiktok": "TikTok", "twitter": "X", "x": "X",
|
||||
"facebook": "Facebook", "reddit": "Reddit", "vimeo": "Vimeo", "soundcloud": "SoundCloud",
|
||||
"twitch": "Twitch", "dailymotion": "Dailymotion", "bandcamp": "Bandcamp", "threads": "Threads",
|
||||
"bilibili": "Bilibili", "pinterest": "Pinterest", "linkedin": "LinkedIn", "rumble": "Rumble",
|
||||
"archiveorg": "Internet Archive", "peertube": "PeerTube", "odysee": "Odysee", "lbry": "Odysee",
|
||||
}
|
||||
|
||||
// siteName names the source of a download: the site's usual name for a
|
||||
// known extractor, the host for yt-dlp's generic one.
|
||||
func siteName(extractor, url string) string {
|
||||
key := strings.ToLower(extractor)
|
||||
if i := strings.IndexAny(key, ":_"); i > 0 {
|
||||
key = key[:i] // youtube:tab, instagram_story
|
||||
}
|
||||
if name, ok := siteNames[key]; ok {
|
||||
return name
|
||||
}
|
||||
if key == "" || key == "generic" || key == "fake" {
|
||||
if host := links.Site(url); host != "" {
|
||||
return host
|
||||
}
|
||||
}
|
||||
if key == "" {
|
||||
return "?"
|
||||
}
|
||||
return strings.ToUpper(key[:1]) + key[1:]
|
||||
}
|
||||
|
||||
// when says how long ago something happened, in days: today, yesterday,
|
||||
// 3 days ago, then the date.
|
||||
func (b *Bot) when(lang string, at, now time.Time) string {
|
||||
y1, m1, d1 := at.UTC().Date()
|
||||
y2, m2, d2 := now.UTC().Date()
|
||||
days := int(time.Date(y2, m2, d2, 0, 0, 0, 0, time.UTC).Sub(time.Date(y1, m1, d1, 0, 0, 0, 0, time.UTC)).Hours() / 24)
|
||||
switch {
|
||||
case days <= 0:
|
||||
return b.tr.T(lang, "history.today", nil)
|
||||
case days == 1:
|
||||
return b.tr.T(lang, "history.yesterday", nil)
|
||||
case days < 7:
|
||||
return b.tr.T(lang, "history.days_ago", i18n.Args{"Count": days})
|
||||
}
|
||||
return b.tr.Date(lang, at)
|
||||
}
|
||||
|
||||
// historyLabel is the button of one past download, such as
|
||||
// "Instagram · nasa · 720p · today".
|
||||
func (b *Bot) historyLabel(lang string, r db.HistoryRow, now time.Time) string {
|
||||
title, _ := b.displayTitle(lang, &service.CardInfo{Title: r.Title, Uploader: r.Uploader, Site: siteName(r.Extractor, r.URL)})
|
||||
return b.tr.T(lang, "history.item", i18n.Args{
|
||||
"Site": siteName(r.Extractor, r.URL),
|
||||
"Title": shorten(title, 22),
|
||||
"Format": b.profileLabel(lang, profileOf(r.Profile)),
|
||||
"When": b.when(lang, time.UnixMilli(r.CreatedAt), now),
|
||||
})
|
||||
}
|
||||
|
||||
func profileOf(s string) media.Profile {
|
||||
p, err := media.Parse(s)
|
||||
if err != nil {
|
||||
return media.Best
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// historyPage renders one page of the list. ok is false when the history
|
||||
// is empty.
|
||||
func (b *Bot) historyPage(ctx context.Context, act actor, page int) (string, *telego.InlineKeyboardMarkup, error) {
|
||||
lang := act.lang
|
||||
if page < 0 {
|
||||
page = 0
|
||||
}
|
||||
rows, total, err := b.store.HistoryPage(ctx, act.user.ID, page*historyPageSize, historyPageSize)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
pages := (total + historyPageSize - 1) / historyPageSize
|
||||
if len(rows) == 0 && page > 0 && total > 0 {
|
||||
// The page emptied (deleted items): show the last one there is.
|
||||
return b.historyPage(ctx, act, pages-1)
|
||||
}
|
||||
if total == 0 {
|
||||
return b.tr.HTML(lang, "history.empty", nil), nil, nil
|
||||
}
|
||||
p := strconv.Itoa(page)
|
||||
now := time.Now()
|
||||
var kb [][]telego.InlineKeyboardButton
|
||||
for _, r := range rows {
|
||||
kb = append(kb, []telego.InlineKeyboardButton{button(b.historyLabel(lang, r, now), cbData(cbHistoryView, r.DeliveryID, p))})
|
||||
}
|
||||
var nav []telego.InlineKeyboardButton
|
||||
if page > 0 {
|
||||
nav = append(nav, button(b.tr.T(lang, "btn.newer", nil), cbData(cbHistory, strconv.Itoa(page-1))))
|
||||
}
|
||||
if page < pages-1 {
|
||||
nav = append(nav, button(b.tr.T(lang, "btn.older", nil), cbData(cbHistory, strconv.Itoa(page+1))))
|
||||
}
|
||||
kb = append(kb, nav, []telego.InlineKeyboardButton{button(b.tr.T(lang, "btn.clear_history", nil), cbData(cbHistoryClear, p))})
|
||||
text := b.tr.HTML(lang, "history.header", i18n.Args{"Page": page + 1, "Pages": pages, "Count": total})
|
||||
return text, keyboard(kb...), nil
|
||||
}
|
||||
|
||||
func (b *Bot) cmdHistory(ctx context.Context, m *telego.Message, act actor) {
|
||||
text, kb, err := b.historyPage(ctx, act, 0)
|
||||
if err != nil {
|
||||
b.internalError(ctx, m.Chat.ID, act, err)
|
||||
return
|
||||
}
|
||||
_, _ = b.send(ctx, m.Chat.ID, int64(m.MessageID), text, kb)
|
||||
}
|
||||
|
||||
// cbHistoryAction handles every button of the history message. Each one
|
||||
// edits the same message.
|
||||
func (b *Bot) cbHistoryAction(ctx context.Context, q *telego.CallbackQuery, act actor, action, id, arg string) {
|
||||
if q.Message == nil {
|
||||
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
||||
return
|
||||
}
|
||||
chat, msgID := q.Message.GetChat().ID, int64(q.Message.GetMessageID())
|
||||
show := func(text string, kb *telego.InlineKeyboardMarkup) {
|
||||
_ = b.edit(ctx, chat, msgID, false, text, kb)
|
||||
}
|
||||
page := func(s string) int { n, _ := strconv.Atoi(s); return n }
|
||||
list := func(p int, notice string) {
|
||||
text, kb, err := b.historyPage(ctx, act, p)
|
||||
if err != nil {
|
||||
b.answer(ctx, q, b.tr.T(act.lang, "error.internal", nil), true)
|
||||
return
|
||||
}
|
||||
b.answer(ctx, q, notice, false)
|
||||
show(text, kb)
|
||||
}
|
||||
switch action {
|
||||
case cbHistory:
|
||||
list(page(id), "")
|
||||
case cbHistoryView:
|
||||
text, kb, ok := b.historyItem(ctx, act, id, arg)
|
||||
if !ok {
|
||||
list(page(arg), b.tr.T(act.lang, "notice.expired", nil))
|
||||
return
|
||||
}
|
||||
b.answer(ctx, q, "", false)
|
||||
show(text, kb)
|
||||
case cbHistoryDel:
|
||||
if _, err := b.store.HideFromHistory(ctx, act.user.ID, id); err != nil {
|
||||
b.answer(ctx, q, b.tr.T(act.lang, "error.internal", nil), true)
|
||||
return
|
||||
}
|
||||
list(page(arg), b.tr.T(act.lang, "notice.removed", nil))
|
||||
case cbHistoryClear:
|
||||
_, total, err := b.store.HistoryPage(ctx, act.user.ID, 0, 1)
|
||||
if err != nil {
|
||||
b.answer(ctx, q, b.tr.T(act.lang, "error.internal", nil), true)
|
||||
return
|
||||
}
|
||||
b.answer(ctx, q, "", false)
|
||||
show(b.tr.HTML(act.lang, "history.clear_confirm", i18n.Args{"Count": total}), keyboard(
|
||||
[]telego.InlineKeyboardButton{button(b.tr.T(act.lang, "btn.clear_yes", nil), cbHistoryWipe)},
|
||||
[]telego.InlineKeyboardButton{button(b.tr.T(act.lang, "btn.cancel", nil), cbData(cbHistory, id))},
|
||||
))
|
||||
case cbHistoryWipe:
|
||||
if _, err := b.store.ClearHistory(ctx, act.user.ID); err != nil {
|
||||
b.answer(ctx, q, b.tr.T(act.lang, "error.internal", nil), true)
|
||||
return
|
||||
}
|
||||
b.answer(ctx, q, b.tr.T(act.lang, "notice.removed", nil), false)
|
||||
show(b.tr.HTML(act.lang, "history.cleared", nil), nil)
|
||||
}
|
||||
}
|
||||
|
||||
// historyItem renders one past download with what can be done with it.
|
||||
func (b *Bot) historyItem(ctx context.Context, act actor, deliveryID, page string) (string, *telego.InlineKeyboardMarkup, bool) {
|
||||
lang := act.lang
|
||||
d, err := b.store.GetDelivery(ctx, deliveryID)
|
||||
if err != nil || d.UserID != act.user.ID || d.Status != db.DeliverySent || d.Hidden {
|
||||
return "", nil, false
|
||||
}
|
||||
info := &service.CardInfo{}
|
||||
url := ""
|
||||
if req, err := b.store.GetRequest(ctx, d.RequestID); err == nil {
|
||||
url = req.URL
|
||||
if ci, err := service.ParseCardInfo(req.Info); err == nil {
|
||||
info = ci
|
||||
}
|
||||
}
|
||||
if d.Title != "" {
|
||||
info.Title = d.Title
|
||||
}
|
||||
if d.Uploader != "" {
|
||||
info.Uploader = d.Uploader
|
||||
}
|
||||
title, _ := b.displayTitle(lang, info)
|
||||
facts := []string{html.EscapeString(siteName(d.Extractor, url)), html.EscapeString(b.profileLabel(lang, profileOf(d.Profile)))}
|
||||
if job, err := b.store.GetJob(ctx, d.JobID); err == nil && job.FileSize > 0 {
|
||||
facts = append(facts, b.tr.Size(lang, job.FileSize))
|
||||
}
|
||||
facts = append(facts, html.EscapeString(b.tr.Date(lang, time.UnixMilli(d.CreatedAt))))
|
||||
text := titleLine(title) + "\n" + strings.Join(facts, " · ")
|
||||
if url != "" {
|
||||
clean := links.Clean(url)
|
||||
text += "\n" + `<a href="` + html.EscapeString(clean) + `">` + html.EscapeString(links.Short(clean, 40)) + `</a>`
|
||||
}
|
||||
rows := [][]telego.InlineKeyboardButton{{button(b.tr.T(lang, "btn.send_again", nil), cbData(cbResend, d.ID))}}
|
||||
if info.Thumbnail != "" && profileOf(d.Profile).Kind() == media.KindVideo {
|
||||
rows[0] = append(rows[0], button(b.profileLabel(lang, media.Thumb), cbData(cbFollowThumb, d.RequestID)))
|
||||
}
|
||||
rows = append(rows,
|
||||
[]telego.InlineKeyboardButton{button(b.tr.T(lang, "btn.remove_from_history", nil), cbData(cbHistoryDel, d.ID, page))},
|
||||
[]telego.InlineKeyboardButton{button(b.tr.T(lang, "btn.back", nil), cbData(cbHistory, page))},
|
||||
)
|
||||
return text, keyboard(rows...), true
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/noderunners/dlbot/internal/db"
|
||||
"github.com/noderunners/dlbot/internal/i18n"
|
||||
"github.com/noderunners/dlbot/internal/media"
|
||||
"github.com/noderunners/dlbot/internal/service"
|
||||
)
|
||||
|
||||
// This file is /settings in a private chat: the person's own settings in
|
||||
@@ -90,7 +91,7 @@ func (b *Bot) personalView(ctx context.Context, act actor) (string, *telego.Inli
|
||||
b.tr.T(lang, "settings.button", i18n.Args{"Label": label, "Value": value}), cbData(cbPersonal, key))}
|
||||
}
|
||||
rows := [][]telego.InlineKeyboardButton{
|
||||
row(psQuality, b.tr.T(lang, "settings.quality", nil), b.userQualityName(lang, u.Quality)),
|
||||
row(psQuality, b.tr.T(lang, "settings.format", nil), b.userQualityName(lang, u.Quality)),
|
||||
row(psAudio, b.tr.T(lang, "settings.audio_only", nil), b.onOff(lang, u.Quality == string(media.MP3))),
|
||||
row(psAsk, b.tr.T(lang, "settings.ask", nil), b.onOff(lang, u.Quality == qualityAsk)),
|
||||
row(psLanguage, b.tr.T(lang, "settings.language", nil), langName),
|
||||
@@ -141,7 +142,8 @@ func (b *Bot) cbPersonalSetting(ctx context.Context, q *telego.CallbackQuery, ac
|
||||
case psHome:
|
||||
case psQuality:
|
||||
b.answer(ctx, q, "", false)
|
||||
show(b.tr.HTML(act.lang, "quality.pick", nil), b.qualityKeyboard(act, func(v string) string {
|
||||
risky, maxSize := b.riskyQualities(ctx, act)
|
||||
show(b.qualityText(act.lang, risky, maxSize), b.qualityKeyboard(act, risky, func(v string) string {
|
||||
return cbData(cbPersonal, psSetQ, v)
|
||||
}, true))
|
||||
return
|
||||
@@ -207,9 +209,46 @@ func (b *Bot) cbPersonalSetting(ctx context.Context, q *telego.CallbackQuery, ac
|
||||
show(b.personalView(ctx, act))
|
||||
}
|
||||
|
||||
// qualityKeyboard lists the default formats with the active one checked.
|
||||
// data builds the callback data of a choice.
|
||||
func (b *Bot) qualityKeyboard(act actor, data func(v string) string, back bool) *telego.InlineKeyboardMarkup {
|
||||
// typicalBitrate is a usual total bitrate per quality, in bits per
|
||||
// second, for warning free users before they pick a default.
|
||||
var typicalBitrate = map[media.Profile]int64{
|
||||
media.Best: 6_000_000, media.P1080: 4_500_000, media.P720: 2_500_000, media.P480: 1_200_000, media.MP3: 192_000,
|
||||
}
|
||||
|
||||
// riskyQualities are the defaults that are likely to go over a free
|
||||
// user's size limit on a video as long as their duration limit allows.
|
||||
// Paid users get no warning: their limits are far apart from these. The
|
||||
// pick text then explains what happens in that case.
|
||||
func (b *Bot) riskyQualities(ctx context.Context, act actor) (map[string]bool, int64) {
|
||||
dec, err := b.svc.Entitlement(ctx, service.Who{UserID: act.user.ID, ChatID: act.user.ID, ChatType: telego.ChatTypePrivate}, false)
|
||||
if err != nil || dec.Source.Paid() {
|
||||
return nil, 0
|
||||
}
|
||||
maxSize := min64(dec.Limits.MaxSize, b.cfg.MaxUploadBytes())
|
||||
if maxSize <= 0 || dec.Limits.MaxDuration <= 0 {
|
||||
return nil, 0
|
||||
}
|
||||
out := map[string]bool{}
|
||||
for p, bps := range typicalBitrate {
|
||||
if bps/8*dec.Limits.MaxDuration > maxSize {
|
||||
out[string(p)] = true
|
||||
}
|
||||
}
|
||||
return out, maxSize
|
||||
}
|
||||
|
||||
// qualityText is the text above the quality picker.
|
||||
func (b *Bot) qualityText(lang string, risky map[string]bool, maxSize int64) string {
|
||||
text := b.tr.HTML(lang, "quality.pick", nil)
|
||||
if len(risky) > 0 {
|
||||
text += "\n\n" + b.tr.HTML(lang, "quality.over_note", i18n.Args{"MaxSize": b.tr.Size(lang, maxSize)})
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
// qualityKeyboard lists the default formats with the active one checked,
|
||||
// and the risky ones marked. data builds the callback data of a choice.
|
||||
func (b *Bot) qualityKeyboard(act actor, risky map[string]bool, data func(v string) string, back bool) *telego.InlineKeyboardMarkup {
|
||||
lang := act.lang
|
||||
cur := act.user.Quality
|
||||
if cur == qualityUnset {
|
||||
@@ -217,6 +256,9 @@ func (b *Bot) qualityKeyboard(act actor, data func(v string) string, back bool)
|
||||
}
|
||||
opt := func(v string) telego.InlineKeyboardButton {
|
||||
label := b.userQualityName(lang, v)
|
||||
if risky[v] {
|
||||
label = b.tr.T(lang, "btn.maybe_over", i18n.Args{"Label": label})
|
||||
}
|
||||
if v == cur {
|
||||
label = b.tr.T(lang, "btn.checked", i18n.Args{"Label": label})
|
||||
}
|
||||
|
||||
@@ -219,3 +219,35 @@ func TestStatusEditsAreThrottledButNotLost(t *testing.T) {
|
||||
t.Fatalf("edits = %v, want nothing after the delivery ended", texts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistoryLabels(t *testing.T) {
|
||||
b := testBot(t, nil)
|
||||
now := time.Date(2025, 5, 10, 15, 0, 0, 0, time.UTC)
|
||||
at := func(daysAgo int) int64 { return now.AddDate(0, 0, -daysAgo).Add(-time.Hour).UnixMilli() }
|
||||
cases := []struct {
|
||||
lang string
|
||||
row db.HistoryRow
|
||||
want string
|
||||
}{
|
||||
{"en", db.HistoryRow{Extractor: "Instagram", Title: "Video by nasa", Uploader: "nasa", Profile: "720", CreatedAt: at(0)}, "Instagram · nasa · 720p · today"},
|
||||
{"en", db.HistoryRow{Extractor: "youtube:tab", Title: "A very long title that goes on and on", Profile: "best", CreatedAt: at(1)}, "YouTube · A very long title tha… · Best · yesterday"},
|
||||
{"es", db.HistoryRow{Extractor: "generic", URL: "https://www.example.org/x.mp4", Title: "Clip", Profile: "mp3", CreatedAt: at(3)}, "example.org · Clip · MP3 · hace 3 días"},
|
||||
{"nl", db.HistoryRow{Extractor: "TikTok", Title: "Dans", Profile: "480", CreatedAt: at(30)}, "TikTok · Dans · 480p · 10 apr 2025"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := b.historyLabel(c.lang, c.row, now); got != c.want {
|
||||
t.Errorf("%s: label = %q, want %q", c.lang, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSiteNames(t *testing.T) {
|
||||
for in, want := range map[[2]string]string{
|
||||
{"youtube", ""}: "YouTube", {"twitter", ""}: "X", {"instagram:story", ""}: "Instagram",
|
||||
{"generic", "https://m.example.com/v"}: "example.com", {"newsite", ""}: "Newsite", {"", ""}: "?",
|
||||
} {
|
||||
if got := siteName(in[0], in[1]); got != want {
|
||||
t.Errorf("siteName(%q, %q) = %q, want %q", in[0], in[1], got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user