Phase 2: money
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>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
3b361c17a1
commit
0d50f453a0
+39
-6
@@ -2,6 +2,7 @@ package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mymmrac/telego"
|
||||
@@ -16,9 +17,27 @@ import (
|
||||
// statusState remembers what a status message shows, so edits that would
|
||||
// change nothing or come too fast are skipped.
|
||||
type statusState struct {
|
||||
text string
|
||||
phase string
|
||||
at time.Time
|
||||
text string
|
||||
buttons string
|
||||
phase string
|
||||
at time.Time
|
||||
}
|
||||
|
||||
// keyboardSignature identifies a keyboard by the callback data and links
|
||||
// of its buttons.
|
||||
func keyboardSignature(kb *telego.InlineKeyboardMarkup) string {
|
||||
if kb == nil {
|
||||
return ""
|
||||
}
|
||||
var sb strings.Builder
|
||||
for _, row := range kb.InlineKeyboard {
|
||||
for _, btn := range row {
|
||||
sb.WriteString(btn.CallbackData)
|
||||
sb.WriteString(btn.URL)
|
||||
sb.WriteByte('|')
|
||||
}
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// eventLoop reacts to job events and periodically reconciles, which covers
|
||||
@@ -33,6 +52,8 @@ func (b *Bot) eventLoop(ctx context.Context) {
|
||||
return
|
||||
case ev := <-b.events:
|
||||
b.safely("event", func() { b.handleEvent(ctx, ev) })
|
||||
case ev := <-b.payEvents:
|
||||
b.safely("payment", func() { b.handlePayEvent(ctx, ev) })
|
||||
case <-t.C:
|
||||
b.safely("reconcile", func() { b.reconcile(ctx) })
|
||||
}
|
||||
@@ -84,6 +105,10 @@ func (b *Bot) reconcile(ctx context.Context) {
|
||||
each(db.JobQueued, func(id string) { b.refreshStatus(ctx, id) })
|
||||
each(db.JobRunning, func(id string) { b.refreshStatus(ctx, id) })
|
||||
|
||||
// Refunds and paid follow-ups that an interruption left undone.
|
||||
b.svc.ReleasePending(ctx)
|
||||
b.fulfilPending(ctx)
|
||||
|
||||
// Jobs whose requesters were all served but that were never closed.
|
||||
delivering, err := b.store.JobsByStatus(ctx, db.JobDelivering, 200)
|
||||
if err != nil {
|
||||
@@ -128,6 +153,10 @@ func (b *Bot) renderStatus(ctx context.Context, d *db.Delivery, job *db.Job) (te
|
||||
cancel := b.cancelJobKeyboard(lang, d.ID)
|
||||
switch job.Status {
|
||||
case db.JobQueued:
|
||||
kb := cancel
|
||||
if job.Lane == db.LaneFree && b.bill.Enabled() && b.cfg.Prices.SkipQueue > 0 {
|
||||
kb = b.queuedKeyboard(lang, d.ID)
|
||||
}
|
||||
pos, err := b.sched.Position(ctx, job, b.slots())
|
||||
if err != nil {
|
||||
b.log.Error("queue position", "job_id", job.ID, "err", err)
|
||||
@@ -135,7 +164,7 @@ func (b *Bot) renderStatus(ctx context.Context, d *db.Delivery, job *db.Job) (te
|
||||
return head + b.tr.HTML(lang, "status.queued", i18n.Args{
|
||||
"Position": pos.Ahead + 1,
|
||||
"Wait": b.tr.Span(lang, pos.Wait),
|
||||
}), cancel, "queued"
|
||||
}), kb, "queued"
|
||||
case db.JobRunning:
|
||||
switch job.Stage {
|
||||
case ytdlp.StageProcessing:
|
||||
@@ -185,15 +214,19 @@ func (b *Bot) setStatus(ctx context.Context, d *db.Delivery, text string, kb *te
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
// What the message shows is its text and its buttons. A change in
|
||||
// either is worth an edit, for example "Skip the queue" disappearing.
|
||||
shown := text + "\x00" + keyboardSignature(kb)
|
||||
b.statusMu.Lock()
|
||||
st := b.statuses[d.ID]
|
||||
if st != nil {
|
||||
if st.text == text || (!force && st.phase == phase && now.Sub(st.at) < b.statusMinGap) {
|
||||
sameButtons := keyboardSignature(kb) == st.buttons
|
||||
if st.text == shown || (!force && sameButtons && st.phase == phase && now.Sub(st.at) < b.statusMinGap) {
|
||||
b.statusMu.Unlock()
|
||||
return
|
||||
}
|
||||
}
|
||||
b.statuses[d.ID] = &statusState{text: text, phase: phase, at: now}
|
||||
b.statuses[d.ID] = &statusState{text: shown, buttons: keyboardSignature(kb), phase: phase, at: now}
|
||||
b.statusMu.Unlock()
|
||||
|
||||
if err := b.edit(ctx, d.ChatID, d.StatusMessageID, d.StatusIsPhoto, text, kb); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user