Skip to content

Commit

Permalink
chore: refactor nip47 handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed May 31, 2024
1 parent a0f2362 commit a4187c7
Showing 1 changed file with 54 additions and 81 deletions.
135 changes: 54 additions & 81 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,51 +303,31 @@ func (svc *Service) NIP47Handler(c echo.Context) error {
"relayUrl": requestData.RelayUrl,
}).Info("Processing request event")

requestEvent := RequestEvent{}
findRequestResult := svc.db.Where("nostr_id = ?", requestData.SignedEvent.ID).Find(&requestEvent)

if findRequestResult.RowsAffected != 0 {
responseEvent := ResponseEvent{}
findResponseResult := svc.db.Where("request_id = ?", requestEvent.ID).Find(&responseEvent)

if findResponseResult.RowsAffected != 0 {
return c.JSON(http.StatusBadRequest, NIP47Response{
Event: &nostr.Event{
ID: responseEvent.NostrId,
CreatedAt: nostr.Timestamp(responseEvent.RepliedAt.Unix()),
Content: responseEvent.Content,
},
State: EVENT_ALREADY_PUBLISHED,
})
}
} else {
requestEvent = RequestEvent{
NostrId: requestData.SignedEvent.ID,
Content: requestData.SignedEvent.Content,
}
if svc.db.Where("nostr_id = ?", requestData.SignedEvent.ID).First(&RequestEvent{}).RowsAffected != 0 {
return c.JSON(http.StatusBadRequest, NIP47Response{
State: EVENT_ALREADY_PUBLISHED,
})
}

if err := svc.db.Create(&requestEvent).Error; err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Failed to store request event",
Error: err.Error(),
})
}
requestEvent := RequestEvent{
NostrId: requestData.SignedEvent.ID,
Content: requestData.SignedEvent.Content,
}

subscription := Subscription{
RelayUrl: requestData.RelayUrl,
Open: true,
Authors: &[]string{requestData.WalletPubkey},
Kinds: &[]int{NIP_47_RESPONSE_KIND},
Tags: &nostr.TagMap{"e": []string{requestData.SignedEvent.ID}},
Since: time.Now(),
Limit: 1,
RequestEvent: requestData.SignedEvent,
RequestEventDB: requestEvent,
EventChan: make(chan *nostr.Event, 1),
if err := svc.db.Create(&requestEvent).Error; err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Failed to store request event",
Error: err.Error(),
})
}

if err := svc.db.Create(&subscription).Error; err != nil {
subscription, err := svc.prepareNIP47Subscription(NIP47WebhookRequest{
RelayUrl: requestData.RelayUrl,
WalletPubkey: requestData.WalletPubkey,
SignedEvent: requestData.SignedEvent,
}, requestEvent)

if err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Failed to store subscription",
Error: err.Error(),
Expand Down Expand Up @@ -420,52 +400,27 @@ func (svc *Service) NIP47WebhookHandler(c echo.Context) error {
"webhookUrl": requestData.WebhookUrl,
}).Info("Processing request event")

requestEvent := RequestEvent{}
findRequestResult := svc.db.Where("nostr_id = ?", requestData.SignedEvent.ID).Find(&requestEvent)

if findRequestResult.RowsAffected != 0 {
responseEvent := ResponseEvent{}
findResponseResult := svc.db.Where("request_id = ?", requestEvent.ID).Find(&responseEvent)

if findResponseResult.RowsAffected != 0 {
return c.JSON(http.StatusBadRequest, NIP47Response{
Event: &nostr.Event{
ID: responseEvent.NostrId,
CreatedAt: nostr.Timestamp(responseEvent.RepliedAt.Unix()),
Content: responseEvent.Content,
},
State: EVENT_ALREADY_PUBLISHED,
})
}
} else {
requestEvent = RequestEvent{
NostrId: requestData.SignedEvent.ID,
Content: requestData.SignedEvent.Content,
}
if svc.db.Where("nostr_id = ?", requestData.SignedEvent.ID).First(&RequestEvent{}).RowsAffected != 0 {
return c.JSON(http.StatusBadRequest, NIP47Response{
State: EVENT_ALREADY_PUBLISHED,
})
}

if err := svc.db.Create(&requestEvent).Error; err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Failed to store request event",
Error: err.Error(),
})
}
requestEvent := RequestEvent{
NostrId: requestData.SignedEvent.ID,
Content: requestData.SignedEvent.Content,
}

subscription := Subscription{
RelayUrl: requestData.RelayUrl,
WebhookUrl: requestData.WebhookUrl,
Open: true,
Authors: &[]string{requestData.WalletPubkey},
Kinds: &[]int{NIP_47_RESPONSE_KIND},
Tags: &nostr.TagMap{"e": []string{requestData.SignedEvent.ID}},
Since: time.Now(),
Limit: 1,
RequestEvent: requestData.SignedEvent,
RequestEventDB: requestEvent,
EventChan: make(chan *nostr.Event, 1),
if err := svc.db.Create(&requestEvent).Error; err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Failed to store request event",
Error: err.Error(),
})
}

if err := svc.db.Create(&subscription).Error; err != nil {
subscription, err := svc.prepareNIP47Subscription(requestData, requestEvent)

if err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Failed to store subscription",
Error: err.Error(),
Expand All @@ -481,6 +436,24 @@ func (svc *Service) NIP47WebhookHandler(c echo.Context) error {
})
}

func (svc *Service) prepareNIP47Subscription(requestData NIP47WebhookRequest, requestEvent RequestEvent) (Subscription, error) {
subscription := Subscription{
RelayUrl: requestData.RelayUrl,
WebhookUrl: requestData.WebhookUrl,
Open: true,
Authors: &[]string{requestData.WalletPubkey},
Kinds: &[]int{NIP_47_RESPONSE_KIND},
Tags: &nostr.TagMap{"e": []string{requestData.SignedEvent.ID}},
Since: time.Now(),
Limit: 1,
RequestEvent: requestData.SignedEvent,
RequestEventDB: requestEvent,
EventChan: make(chan *nostr.Event, 1),
}

return subscription, svc.db.Create(&subscription).Error
}

func (svc *Service) NIP47NotificationHandler(c echo.Context) error {
var requestData NIP47NotificationRequest
// send in a pubkey and authenticate by signing
Expand Down

0 comments on commit a4187c7

Please sign in to comment.