Skip to content

Commit

Permalink
chore: publish in multi handler
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jan 17, 2024
1 parent 5962c6b commit ddbe70d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 48 deletions.
99 changes: 69 additions & 30 deletions handle_multi_pay_invoice_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ import (
"github.com/sirupsen/logrus"
)

func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (results []*nostr.Event, err error) {
func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.Subscription, request *Nip47Request, event *nostr.Event, app App, ss []byte) {

nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"}
err = svc.db.Create(&nostrEvent).Error
err := svc.db.Create(&nostrEvent).Error
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
}).Errorf("Failed to save nostr event: %v", err)
return nil, err
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
}).Errorf("Failed to process event: %v", err)
return
}

multiPayParams := &Nip47MultiPayParams{}
Expand All @@ -33,7 +37,11 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, request *Nip
"eventKind": event.Kind,
"appId": app.ID,
}).Errorf("Failed to decode nostr event: %v", err)
return nil, err
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
}).Errorf("Failed to process event: %v", err)
return
}

var wg sync.WaitGroup
Expand All @@ -53,13 +61,25 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, request *Nip
"bolt11": bolt11,
}).Errorf("Failed to decode bolt11 invoice: %v", err)

results = svc.createAndAppendResponse(event, Nip47Response{
resp, err := svc.createResponse(event, Nip47Response{
ResultType: NIP_47_PAY_INVOICE_METHOD,
Error: &Nip47Error{
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Failed to decode bolt11 invoice: %s", err.Error()),
},
}, ss, invoiceInfo, results)
}, ss)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"paymentRequest": invoiceInfo.Invoice,
"invoiceId": invoiceInfo.Id,
}).Errorf("Failed to process event: %v", err)
return
}
dTag := []string{"a", fmt.Sprintf("%d:%s:%d", NIP_47_RESPONSE_KIND, event.PubKey, invoiceInfo.Id)}
resp.Tags = append(resp.Tags, dTag)
svc.PublishEvent(ctx, sub, event, resp)
return
}

Expand All @@ -72,13 +92,25 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, request *Nip
"appId": app.ID,
}).Errorf("App does not have permission: %s %s", code, message)

results = svc.createAndAppendResponse(event, Nip47Response{
resp, err := svc.createResponse(event, Nip47Response{
ResultType: NIP_47_PAY_INVOICE_METHOD,
Error: &Nip47Error{
Code: code,
Message: message,
},
}, ss, invoiceInfo, results)
}, ss)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"paymentRequest": invoiceInfo.Invoice,
"invoiceId": invoiceInfo.Id,
}).Errorf("Failed to process event: %v", err)
return
}
dTag := []string{"a", fmt.Sprintf("%d:%s:%d", NIP_47_RESPONSE_KIND, event.PubKey, invoiceInfo.Id)}
resp.Tags = append(resp.Tags, dTag)
svc.PublishEvent(ctx, sub, event, resp)
return
}

Expand Down Expand Up @@ -113,46 +145,53 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, request *Nip
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)

results = svc.createAndAppendResponse(event, Nip47Response{
resp, err := svc.createResponse(event, Nip47Response{
ResultType: NIP_47_PAY_INVOICE_METHOD,
Error: &Nip47Error{
Code: NIP_47_ERROR_INTERNAL,
Message: fmt.Sprintf("Something went wrong while paying invoice: %s", err.Error()),
},
}, ss, invoiceInfo, results)
}, ss)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"paymentRequest": invoiceInfo.Invoice,
"invoiceId": invoiceInfo.Id,
}).Errorf("Failed to process event: %v", err)
return
}
dTag := []string{"a", fmt.Sprintf("%d:%s:%d", NIP_47_RESPONSE_KIND, event.PubKey, invoiceInfo.Id)}
resp.Tags = append(resp.Tags, dTag)
svc.PublishEvent(ctx, sub, event, resp)
return
}
payment.Preimage = &preimage
// TODO: What to do here?
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
svc.db.Save(&payment)
results = svc.createAndAppendResponse(event, Nip47Response{
resp, err := svc.createResponse(event, Nip47Response{
ResultType: NIP_47_PAY_INVOICE_METHOD,
Result: Nip47PayResponse{
Preimage: preimage,
},
}, ss, invoiceInfo, results)
return
}, ss)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"paymentRequest": invoiceInfo.Invoice,
"invoiceId": invoiceInfo.Id,
}).Errorf("Failed to process event: %v", err)
return
}
dTag := []string{"a", fmt.Sprintf("%d:%s:%d", NIP_47_RESPONSE_KIND, event.PubKey, invoiceInfo.Id)}
resp.Tags = append(resp.Tags, dTag)
svc.PublishEvent(ctx, sub, event, resp)
}(invoiceInfo)
}

wg.Wait()
return results, nil
}

func (svc *Service) createAndAppendResponse(initialEvent *nostr.Event, content interface{}, ss []byte, invoiceInfo InvoiceInfo, results []*nostr.Event) (result []*nostr.Event) {
resp, err := svc.createResponse(initialEvent, content, ss)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": initialEvent.ID,
"eventKind": initialEvent.Kind,
"paymentRequest": invoiceInfo.Invoice,
"invoiceId": invoiceInfo.Id,
}).Errorf("Failed to process event: %v", err)
return results
}
dTag := []string{"a", fmt.Sprintf("%d:%s:%d", NIP_47_RESPONSE_KIND, initialEvent.PubKey, invoiceInfo.Id)}
resp.Tags = append(resp.Tags, dTag)
return append(results, resp)
return
}
24 changes: 6 additions & 18 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"strings"
"sync"
"time"

"github.com/labstack/echo-contrib/session"
Expand Down Expand Up @@ -81,7 +80,7 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti
}
}

func (svc *Service) publishEvent(ctx context.Context, sub *nostr.Subscription, event *nostr.Event, resp *nostr.Event) {
func (svc *Service) PublishEvent(ctx context.Context, sub *nostr.Subscription, event *nostr.Event, resp *nostr.Event) {
status, err := sub.Relay.Publish(ctx, *resp)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -140,7 +139,6 @@ func (svc *Service) publishEvent(ctx context.Context, sub *nostr.Subscription, e

func (svc *Service) handleAndPublishEvent(ctx context.Context, sub *nostr.Subscription, event *nostr.Event) {
var resp *nostr.Event
var resps []*nostr.Event
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
Expand Down Expand Up @@ -175,7 +173,7 @@ func (svc *Service) handleAndPublishEvent(ctx context.Context, sub *nostr.Subscr
Message: "The public key does not have a wallet connected.",
},
}, ss)
svc.publishEvent(ctx, sub, event, resp)
svc.PublishEvent(ctx, sub, event, resp)
return
}

Expand Down Expand Up @@ -217,12 +215,13 @@ func (svc *Service) handleAndPublishEvent(ctx context.Context, sub *nostr.Subscr
return
}
switch nip47Request.Method {
case NIP_47_MULTI_PAY_INVOICE_METHOD:
svc.HandleMultiPayInvoiceEvent(ctx, sub, nip47Request, event, app, ss)
return
case NIP_47_PAY_INVOICE_METHOD:
resp, err = svc.HandlePayInvoiceEvent(ctx, nip47Request, event, app, ss)
case NIP_47_PAY_KEYSEND_METHOD:
resp, err = svc.HandlePayKeysendEvent(ctx, nip47Request, event, app, ss)
case NIP_47_MULTI_PAY_INVOICE_METHOD:
resps, err = svc.HandleMultiPayInvoiceEvent(ctx, nip47Request, event, app, ss)
case NIP_47_GET_BALANCE_METHOD:
resp, err = svc.HandleGetBalanceEvent(ctx, nip47Request, event, app, ss)
case NIP_47_MAKE_INVOICE_METHOD:
Expand Down Expand Up @@ -250,18 +249,7 @@ func (svc *Service) handleAndPublishEvent(ctx context.Context, sub *nostr.Subscr
}).Errorf("Failed to process event: %v", err)
}
if resp != nil {
svc.publishEvent(ctx, sub, event, resp)
}
if resps != nil {
var wg sync.WaitGroup
for _, resp := range resps {
wg.Add(1)
go func(resp *nostr.Event) {
defer wg.Done()
svc.publishEvent(ctx, sub, event, resp)
}(resp)
}
wg.Wait()
svc.PublishEvent(ctx, sub, event, resp)
}
}

Expand Down

0 comments on commit ddbe70d

Please sign in to comment.