Skip to content

Commit

Permalink
chore: add replies event table
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jan 29, 2024
1 parent 6c2fe34 commit 5ee1dfc
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 70 deletions.
6 changes: 1 addition & 5 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,8 +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,
Error: &Nip47Error{
Expand All @@ -79,8 +77,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,
Result: responsePayload,
Expand Down
7 changes: 1 addition & 6 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,8 +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,
Error: &Nip47Error{
Expand All @@ -71,9 +69,6 @@ func (svc *Service) HandleGetInfoEvent(ctx context.Context, request *Nip47Reques
BlockHash: info.BlockHash,
Methods: svc.GetMethods(&app),
}

nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Result: responsePayload,
Expand Down
6 changes: 1 addition & 5 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,8 +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,
Error: &Nip47Error{
Expand All @@ -82,8 +80,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,
Result: responsePayload,
Expand Down
6 changes: 1 addition & 5 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,8 +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,
Error: &Nip47Error{
Expand All @@ -109,8 +107,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,
Result: responsePayload,
Expand Down
6 changes: 1 addition & 5 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,8 +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,
Error: &Nip47Error{
Expand All @@ -107,8 +105,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,
Result: responsePayload,
Expand Down
16 changes: 5 additions & 11 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 @@ -80,7 +80,7 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
return
}

svc.PublishEvent(ctx, sub, event, resp)
svc.PublishEvent(ctx, sub, event, resp, app, ss)
return
}

Expand Down Expand Up @@ -115,7 +115,7 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
}).Errorf("Failed to process event: %v", err)
return
}
svc.PublishEvent(ctx, sub, event, resp)
svc.PublishEvent(ctx, sub, event, resp, app, ss)
return
}

Expand Down Expand Up @@ -146,9 +146,6 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
"appId": app.ID,
"bolt11": bolt11,
}).Infof("Failed to send payment: %v", err)
// TODO: https://github.com/getAlby/nostr-wallet-connect/issues/231
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)

resp, err := svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Expand All @@ -166,13 +163,10 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
}).Errorf("Failed to process event: %v", err)
return
}
svc.PublishEvent(ctx, sub, event, resp)
svc.PublishEvent(ctx, sub, event, resp, app, ss)
return
}
payment.Preimage = &preimage
// TODO: https://github.com/getAlby/nostr-wallet-connect/issues/231
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
svc.db.Save(&payment)
resp, err := svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Expand All @@ -189,7 +183,7 @@ func (svc *Service) HandleMultiPayInvoiceEvent(ctx context.Context, sub *nostr.S
}).Errorf("Failed to process event: %v", err)
return
}
svc.PublishEvent(ctx, sub, event, resp)
svc.PublishEvent(ctx, sub, event, resp, app, ss)
}(invoiceInfo)
}

Expand Down
14 changes: 4 additions & 10 deletions handle_multi_pay_keysend_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func (svc *Service) HandleMultiPayKeysendEvent(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 @@ -80,7 +80,7 @@ func (svc *Service) HandleMultiPayKeysendEvent(ctx context.Context, sub *nostr.S
}).Errorf("Failed to process event: %v", err)
return
}
svc.PublishEvent(ctx, sub, event, resp)
svc.PublishEvent(ctx, sub, event, resp, app, ss)
return
}

Expand Down Expand Up @@ -111,9 +111,6 @@ func (svc *Service) HandleMultiPayKeysendEvent(ctx context.Context, sub *nostr.S
"appId": app.ID,
"senderPubkey": keysendInfo.Pubkey,
}).Infof("Failed to send payment: %v", err)
// TODO: https://github.com/getAlby/nostr-wallet-connect/issues/231
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)

resp, err := svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Expand All @@ -131,13 +128,10 @@ func (svc *Service) HandleMultiPayKeysendEvent(ctx context.Context, sub *nostr.S
}).Errorf("Failed to process event: %v", err)
return
}
svc.PublishEvent(ctx, sub, event, resp)
svc.PublishEvent(ctx, sub, event, resp, app, ss)
return
}
payment.Preimage = &preimage
// TODO: https://github.com/getAlby/nostr-wallet-connect/issues/231
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
svc.db.Save(&payment)
resp, err := svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Expand All @@ -154,7 +148,7 @@ func (svc *Service) HandleMultiPayKeysendEvent(ctx context.Context, sub *nostr.S
}).Errorf("Failed to process event: %v", err)
return
}
svc.PublishEvent(ctx, sub, event, resp)
svc.PublishEvent(ctx, sub, event, resp, app, ss)
}(keysendInfo)
}

Expand Down
6 changes: 1 addition & 5 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,8 +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,
Error: &Nip47Error{
Expand All @@ -84,8 +82,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{
ResultType: request.Method,
Expand Down
6 changes: 1 addition & 5 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,8 +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,
Error: &Nip47Error{
Expand All @@ -105,8 +103,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{
ResultType: NIP_47_PAY_INVOICE_METHOD,
Expand Down
5 changes: 4 additions & 1 deletion migrations/202401191539_initial_migration.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ CREATE INDEX `idx_app_permissions_app_id` ON `app_permissions`(`app_id`);
CREATE TABLE `payments` (`id` integer,`app_id` integer,`nostr_event_id` integer,`amount` integer,`payment_request` text,`preimage` text,`created_at` datetime,`updated_at` datetime, `preimage2` text,PRIMARY KEY (`id`),CONSTRAINT `fk_payments_app` FOREIGN KEY (`app_id`) REFERENCES `apps`(`id`) ON DELETE CASCADE,CONSTRAINT `fk_payments_nostr_event` FOREIGN KEY (`nostr_event_id`) REFERENCES `nostr_events`(`id`));
CREATE INDEX `idx_payments_nostr_event_id` ON `payments`(`nostr_event_id`);
CREATE INDEX `idx_payments_app_id` ON `payments`(`app_id`);
CREATE TABLE "nostr_events" (`id` integer,`app_id` integer,`nostr_id` text UNIQUE,`reply_id` text,`content` text,`state` text,`replied_at` datetime,`created_at` datetime,`updated_at` datetime,PRIMARY KEY (`id`),CONSTRAINT `fk_nostr_events_app` FOREIGN KEY (`app_id`) REFERENCES `apps`(`id`) ON DELETE CASCADE);
CREATE TABLE "nostr_events" (`id` integer,`app_id` integer,`nostr_id` text UNIQUE,`content` text,`replied_at` datetime,`created_at` datetime,`updated_at` datetime,PRIMARY KEY (`id`),CONSTRAINT `fk_nostr_events_app` FOREIGN KEY (`app_id`) REFERENCES `apps`(`id`) ON DELETE CASCADE);
CREATE UNIQUE INDEX `idx_nostr_events_nostr_id` ON `nostr_events`(`nostr_id`);
CREATE INDEX `idx_nostr_events_app_id` ON `nostr_events`(`app_id`);
CREATE INDEX idx_payment_sum ON payments (app_id, preimage, created_at);
CREATE INDEX idx_nostr_events_app_id_and_id ON nostr_events(app_id, id);
CREATE TABLE "response_events" (`id` integer,`app_id` integer,`nostr_id` text UNIQUE,`request_id` integer,`content` text,`decrypted_content` text,`state` text,`replied_at` datetime,`created_at` datetime,`updated_at` datetime,PRIMARY KEY (`id`),CONSTRAINT `fk_response_events_app` FOREIGN KEY (`app_id`) REFERENCES `apps`(`id`) ON DELETE CASCADE,CONSTRAINT `fk_response_events_nostr_events` FOREIGN KEY (`request_id`) REFERENCES `nostr_events`(`id`));
CREATE UNIQUE INDEX `idx_response_events_nostr_id` ON `response_events`(`nostr_id`);
CREATE INDEX `idx_response_events_app_id` ON `response_events`(`app_id`);
CREATE TABLE "user_configs" ("id" integer, "key" text NOT NULL UNIQUE, "value" text, "encrypted" numeric, `created_at` datetime,`updated_at` datetime, PRIMARY KEY("id"));
CREATE UNIQUE INDEX "idx_user_configs_key" ON "user_configs" ("key");
16 changes: 14 additions & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,26 @@ type NostrEvent struct {
AppId uint `validate:"required"`
App App
NostrId string `validate:"required"`
ReplyId string
Content string
State string
RepliedAt time.Time
CreatedAt time.Time
UpdatedAt time.Time
}

type ResponseEvent struct {
ID uint
AppId uint `validate:"required"`
App App
NostrId string `validate:"required"`
ReplyId string
Content string
DecryptedContent string
State string
RepliedAt time.Time
CreatedAt time.Time
UpdatedAt time.Time
}

type Payment struct {
ID uint
AppId uint `validate:"required"`
Expand Down
Loading

0 comments on commit 5ee1dfc

Please sign in to comment.