handle OK callbacks properly when there is not a "reason" (which is the expected behavior).

This commit is contained in:
fiatjaf
2023-06-26 21:00:33 -03:00
parent 1f38213b3f
commit 6b625f7896

View File

@ -50,7 +50,7 @@ type Relay struct {
challenges chan string // NIP-42 challenges challenges chan string // NIP-42 challenges
notices chan string // NIP-01 NOTICEs notices chan string // NIP-01 NOTICEs
okCallbacks *xsync.MapOf[string, func(bool, string)] okCallbacks *xsync.MapOf[string, func(bool, *string)]
writeQueue chan writeRequest writeQueue chan writeRequest
subscriptionChannelCloseQueue chan *Subscription subscriptionChannelCloseQueue chan *Subscription
@ -72,7 +72,7 @@ func NewRelay(ctx context.Context, url string, opts ...RelayOption) *Relay {
connectionContext: ctx, connectionContext: ctx,
connectionContextCancel: cancel, connectionContextCancel: cancel,
Subscriptions: xsync.NewMapOf[*Subscription](), Subscriptions: xsync.NewMapOf[*Subscription](),
okCallbacks: xsync.NewMapOf[func(bool, string)](), okCallbacks: xsync.NewMapOf[func(bool, *string)](),
writeQueue: make(chan writeRequest), writeQueue: make(chan writeRequest),
subscriptionChannelCloseQueue: make(chan *Subscription), subscriptionChannelCloseQueue: make(chan *Subscription),
} }
@ -301,7 +301,7 @@ func (r *Relay) Connect(ctx context.Context) error {
} }
case *OKEnvelope: case *OKEnvelope:
if okCallback, exist := r.okCallbacks.Load(env.EventID); exist { if okCallback, exist := r.okCallbacks.Load(env.EventID); exist {
okCallback(env.OK, *env.Reason) okCallback(env.OK, env.Reason)
} }
} }
} }
@ -339,14 +339,18 @@ func (r *Relay) Publish(ctx context.Context, event Event) (Status, error) {
defer cancel() defer cancel()
// listen for an OK callback // listen for an OK callback
okCallback := func(ok bool, msg string) { okCallback := func(ok bool, msg *string) {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
if ok { if ok {
status = PublishStatusSucceeded status = PublishStatusSucceeded
} else { } else {
status = PublishStatusFailed status = PublishStatusFailed
err = fmt.Errorf("msg: %s", msg) reason := ""
if msg != nil {
reason = *msg
}
err = fmt.Errorf("msg: %s", reason)
} }
cancel() cancel()
} }
@ -399,13 +403,17 @@ func (r *Relay) Auth(ctx context.Context, event Event) (Status, error) {
defer cancel() defer cancel()
// listen for an OK callback // listen for an OK callback
okCallback := func(ok bool, msg string) { okCallback := func(ok bool, msg *string) {
mu.Lock() mu.Lock()
if ok { if ok {
status = PublishStatusSucceeded status = PublishStatusSucceeded
} else { } else {
status = PublishStatusFailed status = PublishStatusFailed
err = fmt.Errorf("msg: %s", msg) reason := ""
if msg != nil {
reason = *msg
}
err = fmt.Errorf("msg: %s", reason)
} }
mu.Unlock() mu.Unlock()
cancel() cancel()