Skip to content

Commit

Permalink
chore: persist request event state in db
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed May 30, 2024
1 parent db7e2d4 commit 918429e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
45 changes: 22 additions & 23 deletions internal/nostr/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,30 @@ const (
)

type Subscription struct {
ID uint
RelayUrl string `validate:"required"`
WebhookUrl string
Open bool
Ids *[]string `gorm:"-"`
Kinds *[]int `gorm:"-"`
Authors *[]string `gorm:"-"` // WalletPubkey is included in this
Tags *nostr.TagMap `gorm:"-"` // RequestEvent ID goes in the "e" tag
Since time.Time
Until time.Time
Limit int
Search string
CreatedAt time.Time
UpdatedAt time.Time
Uuid string `gorm:"type:uuid;default:gen_random_uuid()"`
EventChan chan *nostr.Event `gorm:"-"`
RequestEvent *nostr.Event `gorm:"-"`
RequestID uint `gorm:"-"`
Published bool `gorm:"-"`
ID uint
RelayUrl string `validate:"required"`
WebhookUrl string
Open bool
Ids *[]string `gorm:"-"`
Kinds *[]int `gorm:"-"`
Authors *[]string `gorm:"-"` // WalletPubkey is included in this
Tags *nostr.TagMap `gorm:"-"` // RequestEvent ID goes in the "e" tag
Since time.Time
Until time.Time
Limit int
Search string
CreatedAt time.Time
UpdatedAt time.Time
Uuid string `gorm:"type:uuid;default:gen_random_uuid()"`
EventChan chan *nostr.Event `gorm:"-"`
RequestEvent *nostr.Event `gorm:"-"`
RequestEventDB RequestEvent `gorm:"-"`

// TODO: fix an elegant solution to store datatypes
IdsString string
KindsString string
AuthorsString string
TagsString string
IdsString string
KindsString string
AuthorsString string
TagsString string
}

func (s *Subscription) BeforeSave(tx *gorm.DB) error {
Expand Down
34 changes: 20 additions & 14 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,17 @@ func (svc *Service) NIP47Handler(c echo.Context) 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,
RequestID: requestEvent.ID,
EventChan: make(chan *nostr.Event, 1),
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 subscription.WebhookUrl != "" {
Expand Down Expand Up @@ -647,6 +647,12 @@ func (svc *Service) startSubscription(ctx context.Context, subscription *Subscri
time.Sleep(5 * time.Second) // sleep for 5 seconds
break
} else {
if (subscription.RequestEvent != nil) {
if (subscription.RequestEventDB.State == "") {
subscription.RequestEventDB.State = REQUEST_EVENT_PUBLISH_FAILED
}
svc.db.Save(&subscription.RequestEventDB)
}
svc.Logger.WithFields(logrus.Fields{
"subscriptionId": subscription.ID,
"relayUrl": subscription.RelayUrl,
Expand All @@ -668,7 +674,7 @@ func (svc *Service) processEvents(ctx context.Context, subscription *Subscriptio
receivedEOS = true

// Publish the NIP47 request once EOS is received
if (!subscription.Published && subscription.RequestEvent != nil) {
if (subscription.RequestEvent != nil && subscription.RequestEventDB.State != REQUEST_EVENT_PUBLISH_CONFIRMED) {
err := sub.Relay.Publish(ctx, *subscription.RequestEvent)
if err != nil {
// TODO: notify user about publish failure
Expand All @@ -683,7 +689,7 @@ func (svc *Service) processEvents(ctx context.Context, subscription *Subscriptio
"status": REQUEST_EVENT_PUBLISH_CONFIRMED,
"eventId": subscription.RequestEvent.ID,
}).Info("Published request event successfully")
subscription.Published = true
subscription.RequestEventDB.State = REQUEST_EVENT_PUBLISH_CONFIRMED
}
}
}()
Expand All @@ -703,7 +709,7 @@ func (svc *Service) processEvents(ctx context.Context, subscription *Subscriptio
}
// NIP47 requests don't persist subscriptions in DB
if subscription.RequestEvent != nil {
responseEvent.RequestId = &subscription.RequestID
responseEvent.RequestId = &subscription.RequestEventDB.ID
} else {
responseEvent.SubscriptionId = &subscription.ID
}
Expand Down

0 comments on commit 918429e

Please sign in to comment.