From e82d95e78f916f3d125f5a848dd5bf4757070e1b Mon Sep 17 00:00:00 2001 From: im-adithya Date: Thu, 28 Mar 2024 10:05:15 +0530 Subject: [PATCH 1/2] fix: request event state after publishing response --- service.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/service.go b/service.go index 55633649..984563e6 100644 --- a/service.go +++ b/service.go @@ -582,12 +582,14 @@ func (svc *Service) HandleEvent(ctx context.Context, sub *nostr.Subscription, ev svc.handleUnknownMethod(ctx, nip47Request, publishResponse) } - requestEvent.State = REQUEST_EVENT_STATE_HANDLER_EXECUTED - err = svc.db.Save(&requestEvent).Error - if err != nil { - svc.Logger.WithFields(logrus.Fields{ - "nostrPubkey": event.PubKey, - }).Errorf("Failed to save state to nostr event: %v", err) + if requestEvent.State == REQUEST_EVENT_STATE_HANDLER_EXECUTING { + requestEvent.State = REQUEST_EVENT_STATE_HANDLER_EXECUTED + err = svc.db.Save(&requestEvent).Error + if err != nil { + svc.Logger.WithFields(logrus.Fields{ + "nostrPubkey": event.PubKey, + }).Errorf("Failed to save state to nostr event: %v", err) + } } } From 6de171ba05f2283bff84c01da7fc428d4d4d9b3f Mon Sep 17 00:00:00 2001 From: im-adithya Date: Thu, 28 Mar 2024 11:59:42 +0530 Subject: [PATCH 2/2] chore: move request state update to publishresponse --- service.go | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/service.go b/service.go index 984563e6..50b6a2bd 100644 --- a/service.go +++ b/service.go @@ -531,31 +531,24 @@ func (svc *Service) HandleEvent(ctx context.Context, sub *nostr.Subscription, ev "requestEventNostrId": event.ID, "eventKind": event.Kind, }).Errorf("Failed to create response: %v", err) - requestEvent.State = REQUEST_EVENT_STATE_HANDLER_ERROR - err = svc.db.Save(&requestEvent).Error + } else { + err = svc.PublishEvent(ctx, sub, &requestEvent, resp, &app, ss) if err != nil { svc.Logger.WithFields(logrus.Fields{ - "nostrPubkey": event.PubKey, - }).Errorf("Failed to save state to nostr event: %v", err) + "requestEventNostrId": event.ID, + "eventKind": event.Kind, + }).Errorf("Failed to publish event: %v", err) + requestEvent.State = REQUEST_EVENT_STATE_HANDLER_ERROR + } else { + requestEvent.State = REQUEST_EVENT_STATE_HANDLER_EXECUTED } - return } - err = svc.PublishEvent(ctx, sub, &requestEvent, resp, &app, ss) - + err = svc.db.Save(&requestEvent).Error if err != nil { svc.Logger.WithFields(logrus.Fields{ - "requestEventNostrId": event.ID, - "eventKind": event.Kind, - }).Errorf("Failed to publish event: %v", err) - - requestEvent.State = REQUEST_EVENT_STATE_HANDLER_ERROR - err = svc.db.Save(&requestEvent).Error - if err != nil { - svc.Logger.WithFields(logrus.Fields{ - "nostrPubkey": event.PubKey, - }).Errorf("Failed to save state to nostr event: %v", err) - } + "nostrPubkey": event.PubKey, + }).Errorf("Failed to save state to nostr event: %v", err) } } @@ -581,16 +574,6 @@ func (svc *Service) HandleEvent(ctx context.Context, sub *nostr.Subscription, ev default: svc.handleUnknownMethod(ctx, nip47Request, publishResponse) } - - if requestEvent.State == REQUEST_EVENT_STATE_HANDLER_EXECUTING { - requestEvent.State = REQUEST_EVENT_STATE_HANDLER_EXECUTED - err = svc.db.Save(&requestEvent).Error - if err != nil { - svc.Logger.WithFields(logrus.Fields{ - "nostrPubkey": event.PubKey, - }).Errorf("Failed to save state to nostr event: %v", err) - } - } } func (svc *Service) handleUnknownMethod(ctx context.Context, nip47Request *Nip47Request, publishResponse func(*Nip47Response, nostr.Tags)) {