Skip to content

Commit

Permalink
chore: remove NostrEvent.State and NostrEvent.ReplyId
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jan 23, 2024
1 parent 1e3d720 commit 92e0df9
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 45 deletions.
4 changes: 1 addition & 3 deletions handle_balance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (

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

nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"}
nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content}
err = svc.db.Create(&nostrEvent).Error
if err != nil {
svc.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -55,7 +55,6 @@ func (svc *Service) HandleGetBalanceEvent(ctx context.Context, request *Nip47Req
"eventKind": event.Kind,
"appId": app.ID,
}).Infof("Failed to fetch balance: %v", err)
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_GET_BALANCE_METHOD,
Expand All @@ -79,7 +78,6 @@ func (svc *Service) HandleGetBalanceEvent(ctx context.Context, request *Nip47Req
responsePayload.BudgetRenewal = appPermission.BudgetRenewal
}

nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_GET_BALANCE_METHOD,
Expand Down
4 changes: 1 addition & 3 deletions handle_info_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

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

nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"}
nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content}
err = svc.db.Create(&nostrEvent).Error
if err != nil {
svc.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -51,7 +51,6 @@ func (svc *Service) HandleGetInfoEvent(ctx context.Context, request *Nip47Reques
"eventKind": event.Kind,
"appId": app.ID,
}).Infof("Failed to fetch node info: %v", err)
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Expand All @@ -72,7 +71,6 @@ func (svc *Service) HandleGetInfoEvent(ctx context.Context, request *Nip47Reques
Methods: svc.GetMethods(&app),
}

nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Expand Down
4 changes: 1 addition & 3 deletions handle_list_transactions_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

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

nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"}
nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content}
err = svc.db.Create(&nostrEvent).Error
if err != nil {
svc.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -66,7 +66,6 @@ func (svc *Service) HandleListTransactionsEvent(ctx context.Context, request *Ni
"eventKind": event.Kind,
"appId": app.ID,
}).Infof("Failed to fetch transactions: %v", err)
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Expand All @@ -82,7 +81,6 @@ func (svc *Service) HandleListTransactionsEvent(ctx context.Context, request *Ni
}
// fmt.Println(responsePayload)

nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Expand Down
4 changes: 1 addition & 3 deletions handle_lookup_invoice_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) {
// TODO: move to a shared function
nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"}
nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content}
err = svc.db.Create(&nostrEvent).Error
if err != nil {
svc.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -94,7 +94,6 @@ func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47
"invoice": lookupInvoiceParams.Invoice,
"paymentHash": lookupInvoiceParams.PaymentHash,
}).Infof("Failed to lookup invoice: %v", err)
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_LOOKUP_INVOICE_METHOD,
Expand All @@ -109,7 +108,6 @@ func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47
Nip47Transaction: *transaction,
}

nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_LOOKUP_INVOICE_METHOD,
Expand Down
4 changes: 1 addition & 3 deletions handle_make_invoice_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) {

// TODO: move to a shared function
nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"}
nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content}
err = svc.db.Create(&nostrEvent).Error
if err != nil {
svc.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -92,7 +92,6 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re
"descriptionHash": makeInvoiceParams.DescriptionHash,
"expiry": makeInvoiceParams.Expiry,
}).Infof("Failed to make invoice: %v", err)
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_MAKE_INVOICE_METHOD,
Expand All @@ -107,7 +106,6 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re
Nip47Transaction: *transaction,
}

nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_MAKE_INVOICE_METHOD,
Expand Down
11 changes: 5 additions & 6 deletions handle_multi_pay_invoice_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

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"}
nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content}
err := svc.db.Create(&nostrEvent).Error
if err != nil {
svc.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -61,7 +61,9 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
"bolt11": bolt11,
}).Errorf("Failed to decode bolt11 invoice: %v", err)

// TODO: Decide what to do if id is empty
// NOTE: if Id is empty, and empty d tag will be returned because
// the invoice cannot be parsed to return the payment hash.
// Currently clients must ensure they pass valid BOLT 11 invoices to this method.
dTag := []string{"d", invoiceInfo.Id}
resp, err := svc.createResponse(event, Nip47Response{
ResultType: NIP_47_MULTI_PAY_INVOICE_METHOD,
Expand Down Expand Up @@ -146,8 +148,6 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
"appId": app.ID,
"bolt11": bolt11,
}).Infof("Failed to send payment: %v", err)
// TODO: What to do here?
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)

resp, err := svc.createResponse(event, Nip47Response{
Expand All @@ -166,12 +166,11 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
}).Errorf("Failed to process event: %v", err)
return
}

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)
resp, err := svc.createResponse(event, Nip47Response{
Expand Down
4 changes: 1 addition & 3 deletions handle_pay_keysend_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

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

nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"}
nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content}
err = svc.db.Create(&nostrEvent).Error
if err != nil {
svc.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -73,7 +73,6 @@ func (svc *Service) HandlePayKeysendEvent(ctx context.Context, request *Nip47Req
"appId": app.ID,
"senderPubkey": payParams.Pubkey,
}).Infof("Failed to send payment: %v", err)
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Expand All @@ -84,7 +83,6 @@ func (svc *Service) HandlePayKeysendEvent(ctx context.Context, request *Nip47Req
}, nostr.Tags{}, ss)
}
payment.Preimage = &preimage
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
svc.db.Save(&payment)
return svc.createResponse(event, Nip47Response{
Expand Down
4 changes: 1 addition & 3 deletions handle_payment_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

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

nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"}
nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content}
err = svc.db.Create(&nostrEvent).Error
if err != nil {
svc.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -94,7 +94,6 @@ func (svc *Service) HandlePayInvoiceEvent(ctx context.Context, request *Nip47Req
"appId": app.ID,
"bolt11": bolt11,
}).Infof("Failed to send payment: %v", err)
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_PAY_INVOICE_METHOD,
Expand All @@ -105,7 +104,6 @@ func (svc *Service) HandlePayInvoiceEvent(ctx context.Context, request *Nip47Req
}, nostr.Tags{}, ss)
}
payment.Preimage = &preimage
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
svc.db.Save(&payment)
return svc.createResponse(event, Nip47Response{
Expand Down
4 changes: 1 addition & 3 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ type NostrEvent struct {
AppId uint `validate:"required"`
App App
NostrId string `validate:"required"`
ReplyId string
Content string
State string
RepliedAt time.Time
RepliedAt time.Time // for multi_* events this will be the last reply
CreatedAt time.Time
UpdatedAt time.Time
}
Expand Down
17 changes: 2 additions & 15 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ type Service struct {
Logger *logrus.Logger
}

/*var supportedMethods = map[string]bool{
NIP_47_PAY_INVOICE_METHOD: true,
NIP_47_GET_BALANCE_METHOD: true,
NIP_47_GET_INFO_METHOD: true,
NIP_47_MAKE_INVOICE_METHOD: true,
NIP_47_LOOKUP_INVOICE_METHOD: true,
NIP_47_LIST_TRANSACTIONS_METHOD: true,
}*/

func (svc *Service) GetUser(c echo.Context) (user *User, err error) {
sess, _ := session.Get(CookieName, c)
userID := sess.Values["user_id"]
Expand Down Expand Up @@ -61,7 +52,7 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti
svc.Logger.Info("Received EOS")

for event := range sub.Events {
go svc.handleAndPublishEvent(ctx, sub, event)
go svc.HandleEvent(ctx, sub, event)
}
svc.Logger.Info("Subscription ended")
}()
Expand Down Expand Up @@ -101,10 +92,8 @@ func (svc *Service) PublishEvent(ctx context.Context, sub *nostr.Subscription, e
}).Error(result.Error)
return
}
nostrEvent.ReplyId = resp.ID

if status == nostr.PublishStatusSucceeded {
nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_CONFIRMED
nostrEvent.RepliedAt = time.Now()
svc.db.Save(&nostrEvent)
svc.Logger.WithFields(logrus.Fields{
Expand All @@ -115,7 +104,6 @@ func (svc *Service) PublishEvent(ctx context.Context, sub *nostr.Subscription, e
"appId": nostrEvent.AppId,
}).Info("Published reply")
} else if status == nostr.PublishStatusFailed {
nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_FAILED
svc.db.Save(&nostrEvent)
svc.Logger.WithFields(logrus.Fields{
"nostrEventId": nostrEvent.ID,
Expand All @@ -125,7 +113,6 @@ func (svc *Service) PublishEvent(ctx context.Context, sub *nostr.Subscription, e
"appId": nostrEvent.AppId,
}).Info("Failed to publish reply")
} else {
nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_UNCONFIRMED
svc.db.Save(&nostrEvent)
svc.Logger.WithFields(logrus.Fields{
"nostrEventId": nostrEvent.ID,
Expand All @@ -137,7 +124,7 @@ func (svc *Service) PublishEvent(ctx context.Context, sub *nostr.Subscription, e
}
}

func (svc *Service) handleAndPublishEvent(ctx context.Context, sub *nostr.Subscription, event *nostr.Event) {
func (svc *Service) HandleEvent(ctx context.Context, sub *nostr.Subscription, event *nostr.Event) {
var resp *nostr.Event
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
Expand Down

0 comments on commit 92e0df9

Please sign in to comment.