Files
LightningTipBot/internal/telegram/notes_list.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

206 lines
6.1 KiB
Go

package telegram
import (
"context"
"fmt"
"sort"
"strconv"
"strings"
"time"
tiperrors "github.com/LightningTipBot/LightningTipBot/internal/errors"
"github.com/LightningTipBot/LightningTipBot/internal/lnurlcash"
"github.com/LightningTipBot/LightningTipBot/internal/storage"
"github.com/LightningTipBot/LightningTipBot/internal/str"
"github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept"
tb "gopkg.in/lightningtipbot/telebot.v3"
)
const notesPerPage = 8
var (
btnNotesPage = (&tb.ReplyMarkup{}).Data("", "lnc_page")
btnNotesOpen = (&tb.ReplyMarkup{}).Data("", "lnc_open")
btnNotesOpenB = (&tb.ReplyMarkup{}).Data("", "lnc_openb")
)
// NotesList is a user's open /notes list; pages are computed from the DB.
type NotesList struct {
*storage.Base
UserName string `json:"user_name"`
}
type notesEntry struct {
id string
batch bool
created time.Time
label string
}
// notesHandler handles /notes and /notes <amount> x <count> [memo].
func (bot *TipBot) notesHandler(ctx intercept.Context) (intercept.Context, error) {
if !bot.lnurlcashReady(ctx) {
return ctx, nil
}
m := ctx.Message()
user, ok := bot.noteCommandUser(ctx)
if !ok {
return ctx, tiperrors.Create(tiperrors.UserNoWalletError)
}
if amount, count, memo, isBatch := parseBatchCommand(m.Text); isBatch {
return bot.noteBatchStart(ctx, user, amount, count, memo)
}
if len(strings.Fields(m.Text)) > 1 {
bot.sendPrivate(m.Sender, TranslateUser(ctx, "notesHelpMessage"))
return ctx, tiperrors.Create(tiperrors.InvalidSyntaxError)
}
if !m.Private() {
NewMessage(m, WithDuration(0, bot))
}
list := &NotesList{Base: storage.New(storage.ID(fmt.Sprintf("lncl-%d-%s", m.Sender.ID, RandStringRunes(6)))), UserName: user.Name}
text, kb := bot.notesPage(ctx, list, 0)
if _, ok := bot.sendPrivate(m.Sender, text, kb); !ok && !m.Private() {
bot.tryReplyMessage(m, Translate(ctx, "lnurlcashStartPrivateFirstMessage"))
return ctx, tiperrors.Create(tiperrors.NoPrivateChatError)
}
return ctx, list.Set(list, bot.Bunt)
}
// notesEntries lists single notes and batches, newest first.
func (bot *TipBot) notesEntries(ctx context.Context, user string) []notesEntry {
store := bot.LNURLCash.Store()
var entries []notesEntry
batches, _ := store.UserBatches(user)
inBatch := map[string]bool{}
for _, b := range batches {
inBatch[b.ID] = true
sum, _ := bot.LNURLCash.Summarize(b.ID)
entries = append(entries, notesEntry{id: b.ID, batch: true, created: b.CreatedAt,
label: fmt.Sprintf(TranslateUser(ctx, "notesBatchItem"), b.Count, formatSat(b.ValueMsat), sum.Live, sum.Claimed, lnurlcash.ShortID(b.ID))})
}
notes, _ := store.UserNotes(user, nil)
for _, n := range notes {
if n.BatchID != nil && inBatch[*n.BatchID] {
continue
}
value := n.ReportedMsat
if value == 0 {
value = n.ValueMsat
}
memo := n.Memo
if len(memo) > 20 {
memo = memo[:19] + "…"
}
entries = append(entries, notesEntry{id: n.ID, created: n.CreatedAt,
label: fmt.Sprintf(TranslateUser(ctx, "notesListItem"), noteStateEmoji(n.State), formatSat(value), lnurlcash.ShortID(n.ID), memo)})
}
sort.Slice(entries, func(i, j int) bool { return entries[i].created.After(entries[j].created) })
return entries
}
func noteStateEmoji(state string) string {
switch state {
case lnurlcash.NoteLive:
return "🟢"
case lnurlcash.NoteClaimed:
return "✅"
case lnurlcash.NoteReclaimed:
return "↩️"
case lnurlcash.NoteReclaiming:
return "🔄"
case lnurlcash.NoteFailed:
return "🚫"
}
return "⏳"
}
// notesPage renders one page of the list with its buttons.
func (bot *TipBot) notesPage(ctx context.Context, list *NotesList, page int) (string, *tb.ReplyMarkup) {
entries := bot.notesEntries(ctx, list.UserName)
menu := &tb.ReplyMarkup{}
if len(entries) == 0 {
return TranslateUser(ctx, "notesListEmpty"), menu
}
pages := (len(entries) + notesPerPage - 1) / notesPerPage
if page < 0 {
page = 0
}
if page >= pages {
page = pages - 1
}
start := page * notesPerPage
end := start + notesPerPage
if end > len(entries) {
end = len(entries)
}
var rows []tb.Row
text := fmt.Sprintf(TranslateUser(ctx, "notesListHeader"), len(entries), page+1, pages) + "\n"
for i, e := range entries[start:end] {
text += fmt.Sprintf("\n%d. %s", start+i+1, str.MarkdownEscape(e.label))
unique := "lnc_open"
if e.batch {
unique = "lnc_openb"
}
rows = append(rows, menu.Row(menu.Data(fmt.Sprintf("%d · %s", start+i+1, lnurlcash.ShortID(e.id)), unique, e.id)))
}
var nav []tb.Btn
if page > 0 {
nav = append(nav, menu.Data("◀️", "lnc_page", list.ID+":"+strconv.Itoa(page-1)))
}
if page < pages-1 {
nav = append(nav, menu.Data("▶️", "lnc_page", list.ID+":"+strconv.Itoa(page+1)))
}
if len(nav) > 0 {
rows = append(rows, menu.Row(nav...))
}
menu.Inline(rows...)
return text, menu
}
// notesPageHandler scrolls the list.
func (bot *TipBot) notesPageHandler(ctx intercept.Context) (intercept.Context, error) {
if bot.LNURLCash == nil {
return ctx, nil
}
id, pageStr, _ := strings.Cut(ctx.Data(), ":")
page, _ := strconv.Atoi(pageStr)
tx := &NotesList{Base: storage.New(storage.ID(id))}
got, err := tx.Get(tx, bot.Bunt)
if err != nil {
return ctx, err
}
list := got.(*NotesList)
if list.UserName != LoadUser(ctx).Name {
return ctx, tiperrors.Create(tiperrors.UnknownError)
}
text, kb := bot.notesPage(ctx, list, page)
bot.tryEditMessage(ctx.Callback().Message, text, kb)
return ctx, nil
}
// notesOpenHandler shows one note with its actions.
func (bot *TipBot) notesOpenHandler(ctx intercept.Context) (intercept.Context, error) {
if bot.LNURLCash == nil {
return ctx, nil
}
n, err := bot.loadOwnNote(ctx)
if err != nil {
return ctx, err
}
bot.trySendMessageEditable(ctx.Sender(), noteViewText(ctx, n), noteKeyboard(ctx, n, true))
return ctx, nil
}
// notesOpenBatchHandler shows one batch with its actions.
func (bot *TipBot) notesOpenBatchHandler(ctx intercept.Context) (intercept.Context, error) {
if bot.LNURLCash == nil {
return ctx, nil
}
b, err := bot.loadOwnBatch(ctx)
if err != nil {
return ctx, err
}
bot.trySendMessageEditable(ctx.Sender(), bot.batchViewText(ctx, b), batchKeyboard(ctx, b.ID))
return ctx, nil
}