Lightning payments through LNbits, wallets, passes, credits, the entitlement resolver and priority lanes. - internal/entitle: pure resolver (one-off, own pass, group pass, group credits, own credits, free) with a table-driven suite and an invariant check over every input combination - internal/billing: wallets, append-only ledger with dedupe keys, stacking passes, purchases, gift codes, referral reward - LNbits client verified against v0.12.12 and v1.7.1 source - Settlement by one conditional UPDATE: idempotent under any mix of webhooks, polls and the check button. Webhooks are unsigned, so every payment is confirmed with LNbits before it is credited - Polling fallback, reconciliation of all unsettled invoices at startup, late payments still honored, no expiry while LNbits is unreachable - Charge, quota and job are written in one transaction; failed, cancelled and undeliverable downloads are refunded exactly once - Paid lane first with one slot of the fleet reserved for free users - Daily download quota and daily volume cap, per-user and per-group rate limits - Bot: /buy /account /limits /credits /redeem /referral /donate, QR invoice message that flips to paid, over-limit offer (passes or this file only), deep links from groups, skip the queue - Fake LNbits server for tests; end-to-end money flows including a payment made while the core was down Cashu redemption is behind an interface and not implemented. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
61 lines
2.2 KiB
Go
61 lines
2.2 KiB
Go
package db
|
|
|
|
import "context"
|
|
|
|
const userCols = `id, username, first_name, lang, lang_override, banned, created_at, last_seen_at, referred_by`
|
|
|
|
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, &u.ReferredBy)
|
|
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
|
|
}
|
|
u, err := s.GetUser(ctx, id)
|
|
if err == nil {
|
|
u.IsNew = u.CreatedAt == now && u.LastSeenAt == now
|
|
}
|
|
return u, err
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
|
|
// SetReferrer records who invited a user. It only works once, and never
|
|
// for a self-invite. It reports whether the referrer was stored.
|
|
func (s *Store) SetReferrer(ctx context.Context, userID, referrerID int64) (bool, error) {
|
|
if userID == referrerID || referrerID == 0 {
|
|
return false, nil
|
|
}
|
|
n, err := s.exec(ctx, `UPDATE users SET referred_by = ? WHERE id = ? AND referred_by = 0
|
|
AND EXISTS (SELECT 1 FROM (SELECT id FROM users WHERE id = ?) r)`, referrerID, userID, referrerID)
|
|
return n == 1, err
|
|
}
|
|
|
|
// CountReferrals counts the users invited by a user.
|
|
func (s *Store) CountReferrals(ctx context.Context, referrerID int64) (int, error) {
|
|
var n int
|
|
err := s.row(ctx, `SELECT COUNT(*) FROM users WHERE referred_by = ?`, referrerID).Scan(&n)
|
|
return n, err
|
|
}
|