|
|
|
@ -6,6 +6,7 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"regexp"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"git.sp4ke.com/sp4ke/bit4sat/bus"
|
|
|
|
@ -20,6 +21,8 @@ import (
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
LastSettledInvoiceIndexKey = "last_invoice_settled_index_cursor"
|
|
|
|
|
ReUploadInvoice = `bit4sat upload`
|
|
|
|
|
ReDownInvoice = `bit4sat download`
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Last watched invoice
|
|
|
|
@ -93,6 +96,12 @@ func WatchInvoice() {
|
|
|
|
|
//TODO
|
|
|
|
|
func handleExpiredInvoice(invoice *lnrpc.Invoice) {
|
|
|
|
|
|
|
|
|
|
matchUp, err := regexp.MatchString(ReUploadInvoice, invoice.Memo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Error regex match: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if matchUp {
|
|
|
|
|
// Update upload status to "expired"
|
|
|
|
|
uploadId, err := storage.GetUploadIdForInvoice(hex.EncodeToString(invoice.RHash))
|
|
|
|
|
if err != nil {
|
|
|
|
@ -103,6 +112,7 @@ func handleExpiredInvoice(invoice *lnrpc.Invoice) {
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(fmt.Errorf("handleExpiredInvoice %s", err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No need to store the invoice
|
|
|
|
|
// TODO
|
|
|
|
@ -125,6 +135,14 @@ func handleSettledInvoice(invoice *lnrpc.Invoice) {
|
|
|
|
|
// Save last settled index
|
|
|
|
|
setLastSettledInvoiceIndex(invoice.SettleIndex)
|
|
|
|
|
|
|
|
|
|
matchUp, err := regexp.MatchString(ReUploadInvoice, invoice.Memo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Error regex match: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle upload related invoices
|
|
|
|
|
if matchUp {
|
|
|
|
|
|
|
|
|
|
// Update upload status to "paid"
|
|
|
|
|
uploadId, err := storage.GetUploadIdForInvoice(hex.EncodeToString(invoice.RHash))
|
|
|
|
|
if err != nil {
|
|
|
|
@ -156,13 +174,14 @@ func handleSettledInvoice(invoice *lnrpc.Invoice) {
|
|
|
|
|
log.Fatal(fmt.Errorf("handleSettledInvoice: %s", err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// publish invoice was updated to upload_id_paid channel
|
|
|
|
|
log.Printf("Notifying invoice paid %s", uploadId)
|
|
|
|
|
// publish invoice was updated to upload_id_paid channel
|
|
|
|
|
key := fmt.Sprintf("%s:%s", bus.InvoicePaidChannelPrefix,
|
|
|
|
|
newInvoice.RHash)
|
|
|
|
|
|
|
|
|
|
err = db.DB.Redis.Do(radix.FlatCmd(nil, "PUBLISH",
|
|
|
|
|
key, newInvoiceJson))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|