You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package watchers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
|
|
"git.sp4ke.com/sp4ke/bit4sat/bus"
|
|
"git.sp4ke.com/sp4ke/bit4sat/db"
|
|
"git.sp4ke.com/sp4ke/bit4sat/ln"
|
|
"git.sp4ke.com/sp4ke/bit4sat/storage"
|
|
"github.com/mediocregopher/radix/v3"
|
|
)
|
|
|
|
type InvoiceWatcher struct {
|
|
listenWatchInvoice chan radix.PubSubMessage
|
|
invoicePaid chan *ln.Invoice
|
|
}
|
|
|
|
func (w *InvoiceWatcher) Run() {
|
|
if err := db.DB.RedisPubSub.Subscribe(w.listenWatchInvoice,
|
|
bus.WatchInvoicesChannelName); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for {
|
|
select {
|
|
case msg := <-w.listenWatchInvoice:
|
|
log.Printf("checking new invoice %s", msg.Message)
|
|
|
|
invoice := ln.Invoice{}
|
|
err := json.Unmarshal(msg.Message, &invoice)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
go ln.PollPaidInvoice(&invoice, w.invoicePaid)
|
|
|
|
case invoice := <-w.invoicePaid:
|
|
// Set upload status to UpWaitingStorage (paid)
|
|
err := storage.SetUploadStatus(invoice.UploadId, storage.UpWaitingStorage)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Update Upload Invoice
|
|
err = storage.SetUploadInvoice(invoice.UploadId, invoice)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// publish invoice was updated to upload_id_paid channel
|
|
updateMsg := bus.UpdateMessage{}
|
|
updateMsg.Status = bus.UpdatePaymentReceived
|
|
updateMsg.UploadId = invoice.UploadId
|
|
updateMsg.Data = invoice
|
|
|
|
updateMsgJson, err := json.Marshal(updateMsg)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
log.Printf("Notifying upload %s paid", invoice.UploadId)
|
|
key := fmt.Sprintf("upload_update_%s", invoice.UploadId)
|
|
|
|
err = db.DB.Redis.Do(radix.FlatCmd(nil, "PUBLISH",
|
|
key, updateMsgJson))
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func NewInvoiceWatcher() *InvoiceWatcher {
|
|
return &InvoiceWatcher{
|
|
listenWatchInvoice: make(chan radix.PubSubMessage),
|
|
invoicePaid: make(chan *ln.Invoice),
|
|
}
|
|
}
|