Skip to content

Commit

Permalink
fix OKEnvelope always requiring the 4th field.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Nov 4, 2023
1 parent 0ecffe6 commit f8fb9e8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
15 changes: 5 additions & 10 deletions envelopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,23 +268,20 @@ func (v CloseEnvelope) MarshalJSON() ([]byte, error) {
type OKEnvelope struct {
EventID string
OK bool
Reason *string
Reason string
}

func (_ OKEnvelope) Label() string { return "OK" }

func (v *OKEnvelope) UnmarshalJSON(data []byte) error {
r := gjson.ParseBytes(data)
arr := r.Array()
if len(arr) < 3 {
if len(arr) < 4 {
return fmt.Errorf("failed to decode OK envelope: missing fields")
}
v.EventID = arr[1].Str
v.OK = arr[2].Raw == "true"

if len(arr) > 3 {
v.Reason = &arr[3].Str
}
v.Reason = arr[3].Str

return nil
}
Expand All @@ -298,10 +295,8 @@ func (v OKEnvelope) MarshalJSON() ([]byte, error) {
ok = "true"
}
w.RawString(ok)
if v.Reason != nil {
w.RawString(`,`)
w.Raw(json.Marshal(v.Reason))
}
w.RawString(`,`)
w.Raw(json.Marshal(v.Reason))
w.RawString(`]`)
return w.BuildBytes()
}
Expand Down
22 changes: 6 additions & 16 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Relay struct {

challenges chan string // NIP-42 challenges
notices chan string // NIP-01 NOTICEs
okCallbacks *xsync.MapOf[string, func(bool, *string)]
okCallbacks *xsync.MapOf[string, func(bool, string)]
writeQueue chan writeRequest
subscriptionChannelCloseQueue chan *Subscription

Expand All @@ -73,7 +73,7 @@ func NewRelay(ctx context.Context, url string, opts ...RelayOption) *Relay {
connectionContext: ctx,
connectionContextCancel: cancel,
Subscriptions: xsync.NewMapOf[*Subscription](),
okCallbacks: xsync.NewMapOf[func(bool, *string)](),
okCallbacks: xsync.NewMapOf[func(bool, string)](),
writeQueue: make(chan writeRequest),
subscriptionChannelCloseQueue: make(chan *Subscription),
}
Expand Down Expand Up @@ -346,22 +346,17 @@ func (r *Relay) Publish(ctx context.Context, event Event) (Status, error) {
defer cancel()

// listen for an OK callback
okCallback := func(ok bool, msg *string) {
r.okCallbacks.Store(event.ID, func(ok bool, reason string) {
mu.Lock()
defer mu.Unlock()
if ok {
status = PublishStatusSucceeded
} else {
status = PublishStatusFailed
reason := ""
if msg != nil {
reason = *msg
}
err = fmt.Errorf("msg: %s", reason)
}
cancel()
}
r.okCallbacks.Store(event.ID, okCallback)
})
defer r.okCallbacks.Delete(event.ID)

// publish event
Expand Down Expand Up @@ -410,22 +405,17 @@ func (r *Relay) Auth(ctx context.Context, event Event) (Status, error) {
defer cancel()

// listen for an OK callback
okCallback := func(ok bool, msg *string) {
r.okCallbacks.Store(event.ID, func(ok bool, reason string) {
mu.Lock()
if ok {
status = PublishStatusSucceeded
} else {
status = PublishStatusFailed
reason := ""
if msg != nil {
reason = *msg
}
err = fmt.Errorf("msg: %s", reason)
}
mu.Unlock()
cancel()
}
r.okCallbacks.Store(event.ID, okCallback)
})
defer r.okCallbacks.Delete(event.ID)

// send AUTH
Expand Down

0 comments on commit f8fb9e8

Please sign in to comment.