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>
247 lines
5.6 KiB
Go
247 lines
5.6 KiB
Go
package db
|
|
|
|
// Request statuses.
|
|
const (
|
|
RequestReady = "ready" // card shown, waiting for a format choice
|
|
RequestChosen = "chosen" // a format was picked and a job exists
|
|
RequestCancelled = "cancelled" // the requester dismissed the card
|
|
)
|
|
|
|
// Job statuses.
|
|
const (
|
|
JobQueued = "queued"
|
|
JobRunning = "running"
|
|
JobDelivering = "delivering" // file is stored, the core is sending it
|
|
JobDone = "done"
|
|
JobFailed = "failed"
|
|
JobCancelled = "cancelled"
|
|
)
|
|
|
|
// Delivery statuses.
|
|
const (
|
|
DeliveryPending = "pending"
|
|
DeliverySending = "sending"
|
|
DeliverySent = "sent"
|
|
DeliveryFailed = "failed"
|
|
DeliveryCancelled = "cancelled"
|
|
)
|
|
|
|
// Queue lanes.
|
|
const (
|
|
LanePaid = "paid"
|
|
LaneFree = "free"
|
|
)
|
|
|
|
// Job kinds.
|
|
const (
|
|
KindDownload = "download"
|
|
)
|
|
|
|
// User is a Telegram user known to the bot.
|
|
type User struct {
|
|
ID int64
|
|
Username string
|
|
FirstName string
|
|
Lang string // detected from Telegram
|
|
LangOverride string // chosen with /language
|
|
Banned bool
|
|
CreatedAt int64
|
|
LastSeenAt int64
|
|
ReferredBy int64 // user who invited this one, 0 for none
|
|
// IsNew is set by UpsertUser when the row was just created.
|
|
IsNew bool
|
|
}
|
|
|
|
// Request is one link sent by a user.
|
|
type Request struct {
|
|
ID string
|
|
UserID int64
|
|
ChatID int64
|
|
ChatType string
|
|
SourceMessageID int64
|
|
CardMessageID int64
|
|
CardIsPhoto bool
|
|
URL string
|
|
Status string
|
|
Info string // JSON, see service.CardInfo
|
|
Lang string
|
|
CreatedAt int64
|
|
UpdatedAt int64
|
|
}
|
|
|
|
// Job is one unit of work for a worker.
|
|
type Job struct {
|
|
ID string
|
|
Kind string
|
|
URL string
|
|
Extractor string
|
|
VideoID string
|
|
Profile string
|
|
CacheKey string
|
|
Status string
|
|
Lane string
|
|
Priority int
|
|
RequiredTags string
|
|
MaxSize int64
|
|
MaxDuration int64 // seconds
|
|
Attempts int
|
|
MaxAttempts int
|
|
WorkerID string
|
|
LeaseExpiresAt int64
|
|
CancelRequested bool
|
|
Stage string
|
|
ProgressBytes int64
|
|
TotalBytes int64
|
|
ErrorCode string
|
|
ErrorDetail string
|
|
StorageKey string
|
|
FileExt string
|
|
FileSize int64
|
|
Duration int64 // seconds
|
|
Width int
|
|
Height int
|
|
Title string
|
|
Uploader string
|
|
CreatedAt int64
|
|
QueuedAt int64
|
|
StartedAt int64
|
|
FinishedAt int64
|
|
}
|
|
|
|
// Active reports whether the job still needs a worker or the core.
|
|
func (j *Job) Active() bool {
|
|
return j.Status == JobQueued || j.Status == JobRunning || j.Status == JobDelivering
|
|
}
|
|
|
|
// Delivery is one requester waiting for a job.
|
|
type Delivery struct {
|
|
ID string
|
|
JobID string
|
|
RequestID string
|
|
UserID int64
|
|
ChatID int64
|
|
ChatType string
|
|
ReplyToMessageID int64
|
|
StatusMessageID int64
|
|
StatusIsPhoto bool
|
|
Lang string
|
|
Status string
|
|
Attempts int
|
|
ErrorCode string
|
|
TgFileID string
|
|
TgMessageID int64
|
|
CreatedAt int64
|
|
UpdatedAt int64
|
|
// Source is the entitlement that paid: see package entitle.
|
|
Source string
|
|
// WalletID is the wallet that was charged, "" for free and passes.
|
|
WalletID string
|
|
// ChargeSats is what was debited (or prepaid) for this delivery.
|
|
ChargeSats int64
|
|
// QuotaDay is the usage day (YYYYMMDD) the download was counted
|
|
// against, 0 when it was not counted.
|
|
QuotaDay int
|
|
// Released is true once charge and quota were given back.
|
|
Released bool
|
|
}
|
|
|
|
// Wallet owner types.
|
|
const (
|
|
OwnerUser = "user"
|
|
OwnerGroup = "group"
|
|
)
|
|
|
|
// Ledger entry kinds. Amounts are always positive; the kind gives the sign.
|
|
const (
|
|
LedgerCredit = "credit"
|
|
LedgerDebit = "debit"
|
|
LedgerRefund = "refund"
|
|
)
|
|
|
|
// Purchase statuses.
|
|
const (
|
|
PurchasePending = "pending"
|
|
PurchasePaid = "paid"
|
|
PurchaseExpired = "expired"
|
|
PurchaseCancelled = "cancelled"
|
|
)
|
|
|
|
// Wallet holds credits for a user or a group.
|
|
type Wallet struct {
|
|
ID string
|
|
OwnerType string
|
|
OwnerID int64
|
|
Balance int64 // sats, cached sum of the ledger
|
|
CreatedAt int64
|
|
}
|
|
|
|
// LedgerEntry is one append-only movement on a wallet.
|
|
type LedgerEntry struct {
|
|
ID string
|
|
WalletID string
|
|
Kind string
|
|
Amount int64
|
|
BalanceAfter int64
|
|
Reason string
|
|
Ref string
|
|
// DedupeKey makes an entry idempotent: a second insert with the same
|
|
// key is ignored.
|
|
DedupeKey string
|
|
ActorID int64
|
|
CreatedAt int64
|
|
}
|
|
|
|
// Pass is a time window of paid limits on a wallet.
|
|
type Pass struct {
|
|
ID string
|
|
WalletID string
|
|
Kind string
|
|
StartsAt int64
|
|
ExpiresAt int64
|
|
Source string
|
|
CreatedAt int64
|
|
}
|
|
|
|
// Purchase is one invoice and what it buys.
|
|
type Purchase struct {
|
|
ID string
|
|
UserID int64 // the payer
|
|
WalletID string
|
|
Product string
|
|
AmountSats int64
|
|
Status string
|
|
PaymentHash string
|
|
Bolt11 string
|
|
ChatID int64
|
|
MessageID int64
|
|
MessageIsPhoto bool
|
|
Lang string
|
|
// RequestID and Profile describe a download to start once paid.
|
|
RequestID string
|
|
Profile string
|
|
// DeliveryID is the waiting delivery a skip-the-queue payment is for.
|
|
DeliveryID string
|
|
Fulfilled bool
|
|
CreatedAt int64
|
|
ExpiresAt int64
|
|
PaidAt int64
|
|
}
|
|
|
|
// Usage is what a user consumed on one UTC day.
|
|
type Usage struct {
|
|
Downloads int
|
|
Bytes int64
|
|
}
|
|
|
|
// GiftCode is a redeemable code.
|
|
type GiftCode struct {
|
|
Code string
|
|
Kind string // "credits" or a pass kind
|
|
Amount int64 // sats for credits, ignored for passes
|
|
MaxUses int
|
|
Uses int
|
|
ExpiresAt int64
|
|
CreatedBy int64
|
|
CreatedAt int64
|
|
}
|