Group wallets, group passes, spending rules and admin-only settings. - Groups recorded from my_chat_member; admin rights verified with getChatMember and cached for five minutes - Group context in entitlement resolution: group pass, group credits under the spending rule (anyone, admins only, capped per person per day) and the group's daily cap; refunds go back to the wallet that paid - Group gate before any lookup: bot on or off, who may use it, allowed and blocked sites, plus the owner's maintenance mode - /settings with inline toggles checked on every press, /grouplang, /autodetect, /allow, /block, /spending, /groupwallet, /grouppass, /groupstats - Deep links with the target pre-selected: paying for a group happens in the private chat; a refused download in a group offers "Buy for me" and "Add group credits" - Optional link detection, tidy mode, default quality and language per group; per-member usage - /quality, /language, /app, /privacy, /terms, /support - Remaining owner tools: /stats, /queue all, /user, /group, /ban, /unban, /grant (passes and gift codes), /credit, /refund, /blocklist, /broadcast with confirmation, /maint, /reload i18n 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, default_quality`
|
|
|
|
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, &u.Quality)
|
|
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
|
|
}
|