Skip to content

Commit

Permalink
chore: split handleUnknownMethod function, add TODO about inconsisten…
Browse files Browse the repository at this point in the history
…t method handling
  • Loading branch information
rolznz committed Jan 23, 2024
1 parent 451f7d6 commit 1e3d720
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ func (svc *Service) handleAndPublishEvent(ctx context.Context, sub *nostr.Subscr
}).Errorf("Failed to process event: %v", err)
return
}

// TODO: consider move event publishing to individual methods - multi_* methods are
// inconsistent with single methods because they publish multiple responses
switch nip47Request.Method {
case NIP_47_MULTI_PAY_INVOICE_METHOD:
svc.HandleMultiPayInvoiceEvent(ctx, sub, nip47Request, event, app, ss)
Expand All @@ -233,12 +236,7 @@ func (svc *Service) handleAndPublishEvent(ctx context.Context, sub *nostr.Subscr
case NIP_47_GET_INFO_METHOD:
resp, err = svc.HandleGetInfoEvent(ctx, nip47Request, event, app, ss)
default:
resp, err = svc.createResponse(event, Nip47Response{
ResultType: nip47Request.Method,
Error: &Nip47Error{
Code: NIP_47_ERROR_NOT_IMPLEMENTED,
Message: fmt.Sprintf("Unknown method: %s", nip47Request.Method),
}}, nostr.Tags{}, ss)
resp, err = svc.handleUnknownMethod(ctx, nip47Request, event, app, ss)
}

if err != nil {
Expand All @@ -252,6 +250,15 @@ func (svc *Service) handleAndPublishEvent(ctx context.Context, sub *nostr.Subscr
}
}

func (svc *Service) handleUnknownMethod(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) {
return svc.createResponse(event, Nip47Response{
ResultType: request.Method,
Error: &Nip47Error{
Code: NIP_47_ERROR_NOT_IMPLEMENTED,
Message: fmt.Sprintf("Unknown method: %s", request.Method),
}}, nostr.Tags{}, ss)
}

func (svc *Service) createResponse(initialEvent *nostr.Event, content interface{}, tags nostr.Tags, ss []byte) (result *nostr.Event, err error) {
payloadBytes, err := json.Marshal(content)
if err != nil {
Expand Down

0 comments on commit 1e3d720

Please sign in to comment.