Skip to content

Commit

Permalink
fix: use getWalletPubkey func
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jul 15, 2024
1 parent 6810d80 commit 259645e
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ func (svc *Service) stopSubscription(subscription *Subscription) error {
func (svc *Service) startSubscription(ctx context.Context, subscription *Subscription, onReceiveEOS OnReceiveEOSFunc, handleEvent HandleEventFunc) {
svc.Logger.WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"wallet_pubkey": (*subscription.Authors)[0],
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Starting subscription")

filter := svc.subscriptionToFilter(subscription)
Expand All @@ -663,7 +663,7 @@ func (svc *Service) startSubscription(ctx context.Context, subscription *Subscri
svc.Logger.WithError(err).WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"relay_url": subscription.RelayUrl,
"wallet_pubkey": (*subscription.Authors)[0],
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Error("Failed get relay connection, retrying in 5s...")
time.Sleep(5 * time.Second) // sleep for 5 seconds
continue
Expand All @@ -675,7 +675,7 @@ func (svc *Service) startSubscription(ctx context.Context, subscription *Subscri
svc.Logger.WithError(err).WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"relay_url": subscription.RelayUrl,
"wallet_pubkey": (*subscription.Authors)[0],
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Error("Failed to subscribe to relay, retrying in 5s...")
time.Sleep(5 * time.Second) // sleep for 5 seconds
continue
Expand All @@ -687,7 +687,7 @@ func (svc *Service) startSubscription(ctx context.Context, subscription *Subscri

svc.Logger.WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"wallet_pubkey": (*subscription.Authors)[0],
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Started subscription")

err = svc.processEvents(ctx, subscription, onReceiveEOS, handleEvent)
Expand All @@ -697,7 +697,7 @@ func (svc *Service) startSubscription(ctx context.Context, subscription *Subscri
svc.Logger.WithError(err).WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"relay_url": subscription.RelayUrl,
"wallet_pubkey": (*subscription.Authors)[0],
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Error("Subscription stopped due to relay error, reconnecting in 5s...")
time.Sleep(5 * time.Second) // sleep for 5 seconds
continue
Expand All @@ -718,7 +718,7 @@ func (svc *Service) startSubscription(ctx context.Context, subscription *Subscri
svc.Logger.WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"relay_url": subscription.RelayUrl,
"wallet_pubkey": (*subscription.Authors)[0],
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Stopping subscription")
break
}
Expand All @@ -735,14 +735,14 @@ func (svc *Service) publishEvent(ctx context.Context, subscription *Subscription
svc.Logger.WithError(err).WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"relay_url": subscription.RelayUrl,
"wallet_pubkey": (*subscription.Authors)[0],
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Error("Failed to publish to relay")
sub.Unsub()
} else {
svc.Logger.WithFields(logrus.Fields{
"publish_status": REQUEST_EVENT_PUBLISH_CONFIRMED,
"event_id": subscription.RequestEvent.ID,
"wallet_pubkey": (*subscription.Authors)[0],
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Published request event successfully")
subscription.RequestEventDB.State = REQUEST_EVENT_PUBLISH_CONFIRMED
}
Expand All @@ -753,7 +753,7 @@ func (svc *Service) handleResponseEvent(event *nostr.Event, subscription *Subscr
"event_id": event.ID,
"event_kind": event.Kind,
"request_event_id": subscription.RequestEvent.ID,
"wallet_pubkey": (*subscription.Authors)[0],
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Received response event")
responseEvent := ResponseEvent{
NostrId: event.ID,
Expand All @@ -777,7 +777,7 @@ func (svc *Service) handleSubscribedEvent(event *nostr.Event, subscription *Subs
"event_id": event.ID,
"event_kind": event.Kind,
"subscription_id": subscription.ID,
"wallet_pubkey": (*subscription.Authors)[0],
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Received event")
responseEvent := ResponseEvent{
NostrId: event.ID,
Expand All @@ -799,7 +799,7 @@ func (svc *Service) processEvents(ctx context.Context, subscription *Subscriptio
<-sub.EndOfStoredEvents
svc.Logger.WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"wallet_pubkey": (*subscription.Authors)[0],
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Received EOS")

if (onReceiveEOS != nil) {
Expand All @@ -813,6 +813,7 @@ func (svc *Service) processEvents(ctx context.Context, subscription *Subscriptio

svc.Logger.WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Relay subscription events channel ended")
}()

Expand Down Expand Up @@ -906,3 +907,10 @@ func (svc *Service) subscriptionToFilter(subscription *Subscription) (*nostr.Fil
}
return &filter
}

func (svc *Service) getWalletPubkey(authors *[]string) string {
if authors != nil && len(*authors) > 0 {
return (*authors)[0]
}
return ""
}

0 comments on commit 259645e

Please sign in to comment.