fix OKEnvelope always requiring the 4th field.

This commit is contained in:
fiatjaf 2023-11-04 17:30:19 -03:00
parent 0ecffe686d
commit f8fb9e8c26
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
2 changed files with 11 additions and 26 deletions

View File

@ -268,7 +268,7 @@ func (v CloseEnvelope) MarshalJSON() ([]byte, error) {
type OKEnvelope struct { type OKEnvelope struct {
EventID string EventID string
OK bool OK bool
Reason *string Reason string
} }
func (_ OKEnvelope) Label() string { return "OK" } func (_ OKEnvelope) Label() string { return "OK" }
@ -276,15 +276,12 @@ func (_ OKEnvelope) Label() string { return "OK" }
func (v *OKEnvelope) UnmarshalJSON(data []byte) error { func (v *OKEnvelope) UnmarshalJSON(data []byte) error {
r := gjson.ParseBytes(data) r := gjson.ParseBytes(data)
arr := r.Array() arr := r.Array()
if len(arr) < 3 { if len(arr) < 4 {
return fmt.Errorf("failed to decode OK envelope: missing fields") return fmt.Errorf("failed to decode OK envelope: missing fields")
} }
v.EventID = arr[1].Str v.EventID = arr[1].Str
v.OK = arr[2].Raw == "true" v.OK = arr[2].Raw == "true"
v.Reason = arr[3].Str
if len(arr) > 3 {
v.Reason = &arr[3].Str
}
return nil return nil
} }
@ -298,10 +295,8 @@ func (v OKEnvelope) MarshalJSON() ([]byte, error) {
ok = "true" ok = "true"
} }
w.RawString(ok) w.RawString(ok)
if v.Reason != nil { w.RawString(`,`)
w.RawString(`,`) w.Raw(json.Marshal(v.Reason))
w.Raw(json.Marshal(v.Reason))
}
w.RawString(`]`) w.RawString(`]`)
return w.BuildBytes() return w.BuildBytes()
} }

View File

@ -51,7 +51,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
@ -73,7 +73,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),
} }
@ -346,22 +346,17 @@ 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) { r.okCallbacks.Store(event.ID, func(ok bool, reason 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
reason := ""
if msg != nil {
reason = *msg
}
err = fmt.Errorf("msg: %s", reason) err = fmt.Errorf("msg: %s", reason)
} }
cancel() cancel()
} })
r.okCallbacks.Store(event.ID, okCallback)
defer r.okCallbacks.Delete(event.ID) defer r.okCallbacks.Delete(event.ID)
// publish event // publish event
@ -410,22 +405,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) { r.okCallbacks.Store(event.ID, func(ok bool, reason string) {
mu.Lock() mu.Lock()
if ok { if ok {
status = PublishStatusSucceeded status = PublishStatusSucceeded
} else { } else {
status = PublishStatusFailed status = PublishStatusFailed
reason := ""
if msg != nil {
reason = *msg
}
err = fmt.Errorf("msg: %s", reason) err = fmt.Errorf("msg: %s", reason)
} }
mu.Unlock() mu.Unlock()
cancel() cancel()
} })
r.okCallbacks.Store(event.ID, okCallback)
defer r.okCallbacks.Delete(event.ID) defer r.okCallbacks.Delete(event.ID)
// send AUTH // send AUTH