From ed3156ae107ab0b39614aa12d838cac2b820c6c2 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 9 Dec 2023 09:14:45 -0300 Subject: [PATCH] fix event publishing flow: no need to reconnect and AUTH messages make sense. --- event.go | 58 ++++++++++++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/event.go b/event.go index 19ece30..345dcf0 100644 --- a/event.go +++ b/event.go @@ -217,40 +217,36 @@ example: if len(relays) > 0 { os.Stdout.Sync() for _, relay := range relays { + publish: log("publishing to %s... ", relay.URL) - if relay, err := nostr.RelayConnect(c.Context, relay.URL); err != nil { - log("failed to connect: %s\n", err) - } else { - publish: - ctx, cancel := context.WithTimeout(c.Context, 10*time.Second) - defer cancel() + ctx, cancel := context.WithTimeout(c.Context, 10*time.Second) + defer cancel() - status, err := relay.Publish(ctx, evt) - if err == nil { - // published fine probably - log("%s.\n", status) - continue nextline - } - - // error publishing - if isAuthRequired(err.Error()) && sec != "" && doAuth { - // if the relay is requesting auth and we can auth, let's do it - log("performing auth... ") - st, err := relay.Auth(c.Context, func(evt *nostr.Event) error { return evt.Sign(sec) }) - if st == nostr.PublishStatusSucceeded { - // try to publish again, but this time don't try to auth again - doAuth = false - goto publish - } else { - // auth error - if err == nil { - err = fmt.Errorf("no response from relay") - } - log("auth error: %s. ", err) - } - } - log("failed: %s\n", err) + status, err := relay.Publish(ctx, evt) + if err == nil { + // published fine probably + log("%s.\n", status) + continue nextline } + + // error publishing + if strings.HasPrefix(err.Error(), "msg: auth-required:") && sec != "" && doAuth { + // if the relay is requesting auth and we can auth, let's do it + log("performing auth... ") + st, err := relay.Auth(c.Context, func(evt *nostr.Event) error { return evt.Sign(sec) }) + if st == nostr.PublishStatusSucceeded { + // try to publish again, but this time don't try to auth again + doAuth = false + goto publish + } else { + // auth error + if err == nil { + err = fmt.Errorf("no response from relay") + } + log("auth error: %s. ", err) + } + } + log("failed: %s\n", err) } } }