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>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package db
|
||||
|
||||
import "context"
|
||||
|
||||
const requestCols = `id, user_id, chat_id, chat_type, source_message_id, card_message_id,
|
||||
card_is_photo, url, status, info, lang, created_at, updated_at`
|
||||
|
||||
func scanRequest(sc interface{ Scan(...any) error }) (*Request, error) {
|
||||
var r Request
|
||||
err := sc.Scan(&r.ID, &r.UserID, &r.ChatID, &r.ChatType, &r.SourceMessageID, &r.CardMessageID,
|
||||
&r.CardIsPhoto, &r.URL, &r.Status, &r.Info, &r.Lang, &r.CreatedAt, &r.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, notFound(err)
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
// InsertRequest stores a new request.
|
||||
func (s *Store) InsertRequest(ctx context.Context, r *Request) error {
|
||||
if r.CreatedAt == 0 {
|
||||
r.CreatedAt = NowMilli()
|
||||
}
|
||||
r.UpdatedAt = r.CreatedAt
|
||||
_, err := s.exec(ctx, `INSERT INTO requests (`+requestCols+`)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
r.ID, r.UserID, r.ChatID, r.ChatType, r.SourceMessageID, r.CardMessageID,
|
||||
r.CardIsPhoto, r.URL, r.Status, r.Info, r.Lang, r.CreatedAt, r.UpdatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetRequest loads a request.
|
||||
func (s *Store) GetRequest(ctx context.Context, id string) (*Request, error) {
|
||||
return scanRequest(s.row(ctx, `SELECT `+requestCols+` FROM requests WHERE id = ?`, id))
|
||||
}
|
||||
|
||||
// SetRequestCard records which message shows the card.
|
||||
func (s *Store) SetRequestCard(ctx context.Context, id string, messageID int64, isPhoto bool) error {
|
||||
_, err := s.exec(ctx, `UPDATE requests SET card_message_id = ?, card_is_photo = ?, updated_at = ? WHERE id = ?`,
|
||||
messageID, isPhoto, NowMilli(), id)
|
||||
return err
|
||||
}
|
||||
|
||||
// TransitionRequest moves a request from one status to another and reports
|
||||
// whether the row was in the expected status. It is the guard against two
|
||||
// button presses racing each other.
|
||||
func (s *Store) TransitionRequest(ctx context.Context, id, from, to string) (bool, error) {
|
||||
n, err := s.exec(ctx, `UPDATE requests SET status = ?, updated_at = ? WHERE id = ? AND status = ?`,
|
||||
to, NowMilli(), id, from)
|
||||
return n == 1, err
|
||||
}
|
||||
Reference in New Issue
Block a user