mirror of
https://github.com/fiatjaf/nak.git
synced 2024-10-30 00:59:07 -04:00
fix event publishing flow: no need to reconnect and AUTH messages make sense.
This commit is contained in:
parent
30dbe2c1c0
commit
ed3156ae10
58
event.go
58
event.go
|
@ -217,40 +217,36 @@ example:
|
||||||
if len(relays) > 0 {
|
if len(relays) > 0 {
|
||||||
os.Stdout.Sync()
|
os.Stdout.Sync()
|
||||||
for _, relay := range relays {
|
for _, relay := range relays {
|
||||||
|
publish:
|
||||||
log("publishing to %s... ", relay.URL)
|
log("publishing to %s... ", relay.URL)
|
||||||
if relay, err := nostr.RelayConnect(c.Context, relay.URL); err != nil {
|
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
||||||
log("failed to connect: %s\n", err)
|
defer cancel()
|
||||||
} else {
|
|
||||||
publish:
|
|
||||||
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
status, err := relay.Publish(ctx, evt)
|
status, err := relay.Publish(ctx, evt)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// published fine probably
|
// published fine probably
|
||||||
log("%s.\n", status)
|
log("%s.\n", status)
|
||||||
continue nextline
|
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user