Files
LightningTipBot/internal/lnurlcash/service_export.go
T
MichilisandClaude Fable 5.1 3c2ccf8d83 Add the LNURLcash Telegram commands
/note, /notes (list and batch), /redeem and /lnurlmints with confirm
dialogs, group posting with private receipts, claim notifications,
batch export as PDF/JSON/CSV, and LNURLcash notes detected inside the
existing /lnurl, paste and QR-photo flows. Plain withdraw links behave
as before. Two /lnurl log lines no longer print k1.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-13 22:12:27 +00:00

109 lines
2.7 KiB
Go

package lnurlcash
import (
"context"
"errors"
"sort"
"github.com/LightningTipBot/LightningTipBot/internal/lnurlcash/export"
)
// ExportBatch builds the export of a batch for its owner: live notes only,
// so a re-export after some were claimed leaves those out. The note strings
// are built in memory and never stored.
func (s *Service) ExportBatch(batchID, user string) (export.Batch, error) {
b, err := s.store.Batch(batchID)
if err != nil {
return export.Batch{}, err
}
if b.UserName != user {
return export.Batch{}, ErrNotOwner
}
notes, err := s.store.NotesInBatch(b.ID)
if err != nil {
return export.Batch{}, err
}
mint, _ := s.mints.Get(b.MintID)
out := export.Batch{ID: b.ID, Memo: b.Memo, CreatedAt: b.CreatedAt, MintName: b.MintID}
if mint != nil {
out.MintURL, out.MintPubkey = mint.WithdrawLink, mint.PinnedPubkey
}
for _, n := range notes {
if n.Kind != KindUser || n.State != NoteLive {
continue
}
lnurlw, bech, err := s.NoteStrings(n)
if err != nil {
return export.Batch{}, err
}
out.Notes = append(out.Notes, export.Note{Index: n.Index, ID: n.ID, ShortID: ShortID(n.ID), AmountMsat: n.ReportedMsat,
LNURL: bech, LNURLW: lnurlw, Mint: b.MintID, Memo: n.Memo, Cert: n.Certificate, CreatedAt: n.CreatedAt})
}
sort.Slice(out.Notes, func(i, j int) bool { return out.Notes[i].Index < out.Notes[j].Index })
return out, nil
}
// BatchSummary counts a batch's user notes by state.
type BatchSummary struct {
Total, Live, Claimed, Reclaimed, Failed, Pending int
}
// Summarize counts the notes of a batch.
func (s *Service) Summarize(batchID string) (BatchSummary, error) {
notes, err := s.store.NotesInBatch(batchID)
if err != nil {
return BatchSummary{}, err
}
var sum BatchSummary
for _, n := range notes {
if n.Kind != KindUser {
continue
}
sum.Total++
switch n.State {
case NoteLive:
sum.Live++
case NoteClaimed:
sum.Claimed++
case NoteReclaimed:
sum.Reclaimed++
case NoteFailed:
sum.Failed++
default:
sum.Pending++
}
}
return sum, nil
}
// ReclaimBatch reclaims every live note of a batch. It returns how many
// melts were sent and the first error.
func (s *Service) ReclaimBatch(ctx context.Context, batchID, user string) (int, error) {
b, err := s.store.Batch(batchID)
if err != nil {
return 0, err
}
if b.UserName != user {
return 0, ErrNotOwner
}
notes, err := s.store.NotesInBatch(b.ID)
if err != nil {
return 0, err
}
sent := 0
var first error
for _, n := range notes {
if n.Kind != KindUser || n.State != NoteLive {
continue
}
if _, err := s.Reclaim(ctx, n.ID); err != nil {
if !errors.Is(err, ErrNoteClaimed) && first == nil {
first = err
}
continue
}
sent++
}
return sent, first
}