Skip to content

Commit

Permalink
chore: address code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Nov 26, 2023
1 parent 4b8f871 commit 52c9921
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
10 changes: 2 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ func main() {
//connect to the relay
svc.Logger.Infof("Connecting to the relay: %s", cfg.Relay)


relay, err := nostr.RelayConnect(ctx, cfg.Relay, nostr.WithNoticeHandler(svc.noticeHandler))
if err != nil {
svc.Logger.Fatal(err)
Expand All @@ -193,12 +192,7 @@ func main() {
svc.Logger.Info("Subscribing to events")
sub, err := relay.Subscribe(ctx, svc.createFilters())
if err != nil {
//err being non-nil means that we have an error on the websocket error channel. In this case we just try to reconnect.
svc.Logger.WithError(err).Error("Got an error from the relay while subscribing. Reconnecting...")
relay, err = nostr.RelayConnect(ctx, cfg.Relay)
if err != nil {
svc.Logger.Fatal(err)
}
svc.Logger.Fatal(err)
}
err = svc.StartSubscription(ctx, sub)
if err != nil {
Expand Down Expand Up @@ -233,4 +227,4 @@ func (svc *Service) createFilters() nostr.Filters {

func (svc *Service) noticeHandler(notice string) {
svc.Logger.Infof("Received a notice %s", notice)
}
}
31 changes: 19 additions & 12 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (svc *Service) GetUser(c echo.Context) (user *User, err error) {

func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscription) error {
for {
if (sub.Relay.ConnectionError != nil) {
if sub.Relay.ConnectionError != nil {
return sub.Relay.ConnectionError
}
select {
Expand All @@ -75,42 +75,49 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti
}).Errorf("Failed to process event: %v", err)
}
if resp != nil {
status, publishErr := sub.Relay.Publish(ctx, *resp)
status, err := sub.Relay.Publish(ctx, *resp)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"status": status,
"replyEventId": resp.ID,
}).Errorf("Failed to publish reply: %v", err)
return
}

nostrEvent := NostrEvent{}
result := svc.db.Where("nostr_id = ?", event.ID).First(&nostrEvent)
if result.Error != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"status": status,
"replyEventId": resp.ID,
"publishErr": publishErr,
}).Error(result.Error)
return
}
nostrEvent.ReplyId = resp.ID

if status == nostr.PublishStatusFailed || publishErr != nil {
nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_FAILED

if status == nostr.PublishStatusSucceeded {
nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_CONFIRMED
nostrEvent.RepliedAt = time.Now()
svc.db.Save(&nostrEvent)
svc.Logger.WithFields(logrus.Fields{
"nostrEventId": nostrEvent.ID,
"eventId": event.ID,
"status": status,
"replyEventId": resp.ID,
"appId": nostrEvent.AppId,
"publishErr": publishErr,
}).Info("Failed to publish reply")
} else if status == nostr.PublishStatusSucceeded {
nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_CONFIRMED
nostrEvent.RepliedAt = time.Now()
}).Info("Published reply")
} else if status == nostr.PublishStatusFailed {
nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_FAILED
svc.db.Save(&nostrEvent)
svc.Logger.WithFields(logrus.Fields{
"nostrEventId": nostrEvent.ID,
"eventId": event.ID,
"status": status,
"replyEventId": resp.ID,
"appId": nostrEvent.AppId,
}).Info("Published reply")
}).Info("Failed to publish reply")
} else {
nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_UNCONFIRMED
svc.db.Save(&nostrEvent)
Expand Down

0 comments on commit 52c9921

Please sign in to comment.