Files
Media-download-telegram-bot/internal/db/users.go
T
MichilisandClaude Fable 5.2 3b361c17a1 Phase 1: core bot
Link in, card, download, send, in private chats and groups.

- cmd/core with an embedded download worker
- Telegram front end on telego v1.13.0 (Bot API 10.4): link card with
  thumbnail and format buttons, one live status message, /dl /audio /thumb
  /info /queue /cancel, group support with requester-only buttons
- Database backed queue: paid and free lanes, leases with heartbeats, one
  automatic retry, cancel, queue position and wait estimate
- yt-dlp and ffprobe behind a Runner interface with classified, redacted
  errors mapped to friendly messages
- SQLite via modernc with goose migrations, portable schema
- Local file storage behind a Storage interface
- go-i18n with an English catalog, no literal text in handlers
- Free tier limits enforced before queueing and again on the real file
- Bot token and webhook secret are stripped from all log output
- Tests: URL parsing, job lifecycle, worker, yt-dlp wrapper, and end to end
  flows against a fake Bot API server; opt-in test with the real yt-dlp

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 07:33:32 +00:00

39 lines
1.3 KiB
Go

package db
import "context"
const userCols = `id, username, first_name, lang, lang_override, banned, created_at, last_seen_at`
func scanUser(sc interface{ Scan(...any) error }) (*User, error) {
var u User
err := sc.Scan(&u.ID, &u.Username, &u.FirstName, &u.Lang, &u.LangOverride, &u.Banned, &u.CreatedAt, &u.LastSeenAt)
if err != nil {
return nil, notFound(err)
}
return &u, nil
}
// UpsertUser records a sighting of a Telegram user and returns the stored
// row. The detected language follows Telegram; an override is kept.
func (s *Store) UpsertUser(ctx context.Context, id int64, username, firstName, lang string) (*User, error) {
now := NowMilli()
_, err := s.exec(ctx, `
INSERT INTO users (id, username, first_name, lang, created_at, last_seen_at)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT (id) DO UPDATE SET
username = excluded.username,
first_name = excluded.first_name,
lang = CASE WHEN excluded.lang <> '' THEN excluded.lang ELSE users.lang END,
last_seen_at = excluded.last_seen_at`,
id, username, firstName, lang, now, now)
if err != nil {
return nil, err
}
return s.GetUser(ctx, id)
}
// GetUser loads a user by Telegram ID.
func (s *Store) GetUser(ctx context.Context, id int64) (*User, error) {
return scanUser(s.row(ctx, `SELECT `+userCols+` FROM users WHERE id = ?`, id))
}