- /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>
329 lines
10 KiB
Go
329 lines
10 KiB
Go
package bot
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/mymmrac/telego"
|
|
|
|
"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
|
|
// one message that edits itself as buttons are pressed. Group settings
|
|
// stay in groups.go.
|
|
|
|
// cbPersonal is a button of the personal settings: ps:<key>[:<value>].
|
|
const cbPersonal = "ps"
|
|
|
|
// Keys of cbPersonal.
|
|
const (
|
|
psHome = "home" // back to the overview
|
|
psQuality = "q" // the quality picker
|
|
psSetQ = "qs" // ps:qs:<quality>
|
|
psAudio = "a" // toggle audio only
|
|
psAsk = "k" // toggle asking every time
|
|
psLanguage = "l" // the language picker
|
|
psSetLang = "ls" // ps:ls:<language or auto>
|
|
psReferral = "r" // the referral program
|
|
)
|
|
|
|
// userQualities are the choices of a person's default format, in order.
|
|
var userQualities = []string{
|
|
qualityAsk, qualityFit, string(media.Best), string(media.P1080), string(media.P720), string(media.P480), string(media.MP3),
|
|
}
|
|
|
|
// userQualityName names a person's quality setting. Somebody who never
|
|
// chose gets "best that fits my limit" after their first link, so that is
|
|
// what an empty value shows.
|
|
func (b *Bot) userQualityName(lang, q string) string {
|
|
switch q {
|
|
case qualityUnset, qualityFit:
|
|
return b.tr.T(lang, "quality.fit", nil)
|
|
case qualityAsk:
|
|
return b.tr.T(lang, "quality.ask", nil)
|
|
case string(media.MP3):
|
|
return b.tr.T(lang, "quality.audio_only", nil)
|
|
}
|
|
p, err := media.Parse(q)
|
|
if err != nil {
|
|
return q
|
|
}
|
|
return b.profileLabel(lang, p)
|
|
}
|
|
|
|
// validUserQuality reports whether a value may be stored as a default.
|
|
func validUserQuality(q string) bool {
|
|
for _, v := range userQualities {
|
|
if v == q {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// personalView renders the overview of a person's settings.
|
|
func (b *Bot) personalView(ctx context.Context, act actor) (string, *telego.InlineKeyboardMarkup) {
|
|
lang := act.lang
|
|
u := act.user
|
|
st, _ := b.bill.State(ctx, db.OwnerUser, u.ID)
|
|
pass := b.tr.T(lang, "word.none", nil)
|
|
if st.PassUntil > 0 {
|
|
pass = b.tr.DateTime(lang, time.UnixMilli(st.PassUntil))
|
|
}
|
|
invited, _ := b.store.CountReferrals(ctx, u.ID)
|
|
langName := b.tr.T(lang, "language.name", nil)
|
|
if u.LangOverride == "" {
|
|
langName = b.tr.T(lang, "settings.lang_follow", i18n.Args{"Lang": langName})
|
|
}
|
|
text := b.tr.HTML(lang, "settings.personal", i18n.Args{
|
|
"Quality": b.userQualityName(lang, u.Quality),
|
|
"Lang": langName,
|
|
"Pass": pass,
|
|
"Balance": b.tr.Sats(lang, st.Credits),
|
|
"Invited": invited,
|
|
})
|
|
row := func(key, label, value string) []telego.InlineKeyboardButton {
|
|
return []telego.InlineKeyboardButton{button(
|
|
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.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),
|
|
{button(b.tr.T(lang, "btn.referral", nil), cbData(cbPersonal, psReferral))},
|
|
}
|
|
if btn, ok := b.appButton(lang, true, ""); ok {
|
|
rows = append(rows, []telego.InlineKeyboardButton{btn})
|
|
}
|
|
rows = append(rows, []telego.InlineKeyboardButton{b.addToGroupButton(lang)})
|
|
return text, keyboard(rows...)
|
|
}
|
|
|
|
// addToGroupButton opens Telegram's group picker to add the bot.
|
|
func (b *Bot) addToGroupButton(lang string) telego.InlineKeyboardButton {
|
|
return urlButton(b.tr.T(lang, "btn.add_to_group", nil), "https://t.me/"+b.username+"?startgroup=settings")
|
|
}
|
|
|
|
// backRow leads back to the overview.
|
|
func (b *Bot) backRow(lang string) []telego.InlineKeyboardButton {
|
|
return []telego.InlineKeyboardButton{button(b.tr.T(lang, "btn.back", nil), cbData(cbPersonal, psHome))}
|
|
}
|
|
|
|
func (b *Bot) cmdPersonalSettings(ctx context.Context, chat int64, act actor) {
|
|
text, kb := b.personalView(ctx, act)
|
|
_, _ = b.send(ctx, chat, 0, text, kb)
|
|
}
|
|
|
|
// cbPersonalSetting handles every button of the personal settings. Each
|
|
// press edits the same message.
|
|
func (b *Bot) cbPersonalSetting(ctx context.Context, q *telego.CallbackQuery, act actor, key, value string) {
|
|
if q.Message == nil || !isPrivate(q.Message.GetChat().Type) {
|
|
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)
|
|
}
|
|
setQuality := func(v string) bool {
|
|
if err := b.store.SetUserQuality(ctx, act.user.ID, v); err != nil {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "error.internal", nil), true)
|
|
return false
|
|
}
|
|
act.user.Quality = v
|
|
return true
|
|
}
|
|
switch key {
|
|
case psHome:
|
|
case psQuality:
|
|
b.answer(ctx, q, "", false)
|
|
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
|
|
case psSetQ:
|
|
if !validUserQuality(value) {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
|
return
|
|
}
|
|
if !setQuality(value) {
|
|
return
|
|
}
|
|
case psAudio:
|
|
v := string(media.MP3)
|
|
if act.user.Quality == v {
|
|
v = qualityFit
|
|
}
|
|
if !setQuality(v) {
|
|
return
|
|
}
|
|
case psAsk:
|
|
v := qualityAsk
|
|
if act.user.Quality == v {
|
|
v = qualityFit
|
|
}
|
|
if !setQuality(v) {
|
|
return
|
|
}
|
|
case psLanguage:
|
|
b.answer(ctx, q, "", false)
|
|
show(b.tr.HTML(act.lang, "language.pick", nil), b.languageKeyboard(act, func(l string) string {
|
|
return cbData(cbPersonal, psSetLang, l)
|
|
}, true))
|
|
return
|
|
case psSetLang:
|
|
lang, ok := b.applyLanguage(ctx, act, value)
|
|
if !ok {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
|
return
|
|
}
|
|
act.lang = lang
|
|
act.user.LangOverride = ""
|
|
if value != "auto" {
|
|
act.user.LangOverride = lang
|
|
}
|
|
case psReferral:
|
|
b.answer(ctx, q, "", false)
|
|
text, err := b.referralText(ctx, act)
|
|
if err != nil {
|
|
b.log.Error("referral text", "err", err)
|
|
return
|
|
}
|
|
show(text, keyboard(b.backRow(act.lang)))
|
|
return
|
|
default:
|
|
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
|
return
|
|
}
|
|
if key != psHome {
|
|
b.answer(ctx, q, b.tr.T(act.lang, "notice.saved", nil), false)
|
|
} else {
|
|
b.answer(ctx, q, "", false)
|
|
}
|
|
show(b.personalView(ctx, act))
|
|
}
|
|
|
|
// 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 {
|
|
cur = qualityFit
|
|
}
|
|
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})
|
|
}
|
|
return button(label, data(v))
|
|
}
|
|
rows := [][]telego.InlineKeyboardButton{
|
|
{opt(qualityAsk)},
|
|
{opt(qualityFit)},
|
|
{opt(string(media.Best)), opt(string(media.P1080)), opt(string(media.P720)), opt(string(media.P480))},
|
|
{opt(string(media.MP3))},
|
|
}
|
|
if back {
|
|
rows = append(rows, b.backRow(lang))
|
|
}
|
|
return keyboard(rows...)
|
|
}
|
|
|
|
// languageKeyboard lists the languages, each under its own name, and the
|
|
// choice to follow Telegram.
|
|
func (b *Bot) languageKeyboard(act actor, data func(l string) string, back bool) *telego.InlineKeyboardMarkup {
|
|
var row []telego.InlineKeyboardButton
|
|
var rows [][]telego.InlineKeyboardButton
|
|
for _, l := range b.visibleLangs(act.user.ID) {
|
|
label := b.tr.T(l, "language.name", nil)
|
|
if l == act.user.LangOverride {
|
|
label = b.tr.T(act.lang, "btn.checked", i18n.Args{"Label": label})
|
|
}
|
|
row = append(row, button(label, data(l)))
|
|
if len(row) == 3 {
|
|
rows = append(rows, row)
|
|
row = nil
|
|
}
|
|
}
|
|
auto := b.tr.T(act.lang, "language.auto", nil)
|
|
if act.user.LangOverride == "" {
|
|
auto = b.tr.T(act.lang, "btn.checked", i18n.Args{"Label": auto})
|
|
}
|
|
rows = append(rows, row, []telego.InlineKeyboardButton{button(auto, data("auto"))})
|
|
if back {
|
|
rows = append(rows, b.backRow(act.lang))
|
|
}
|
|
return keyboard(rows...)
|
|
}
|
|
|
|
// applyLanguage stores a language choice ("auto" follows Telegram) and
|
|
// returns the language the user now reads.
|
|
func (b *Bot) applyLanguage(ctx context.Context, act actor, value string) (string, bool) {
|
|
lang := ""
|
|
if value != "auto" {
|
|
if lang = b.findLang(value, act.user.ID); lang == "" {
|
|
return "", false
|
|
}
|
|
}
|
|
if err := b.store.SetUserLang(ctx, act.user.ID, lang); err != nil {
|
|
b.log.Error("set language", "user_id", act.user.ID, "err", err)
|
|
return "", false
|
|
}
|
|
shown := lang
|
|
if shown == "" {
|
|
shown = b.tr.Resolve(act.user.Lang)
|
|
}
|
|
b.SyncUserMenu(ctx, act.user.ID, lang)
|
|
u := *act.user
|
|
u.LangOverride = lang
|
|
b.syncMenuButton(ctx, actor{user: &u, lang: shown})
|
|
return shown, true
|
|
}
|