relay options passed to pool should take the relay sometimes.
This commit is contained in:
14
relay.go
14
relay.go
@@ -40,8 +40,8 @@ type Relay struct {
|
|||||||
connectionContextCancel context.CancelCauseFunc
|
connectionContextCancel context.CancelCauseFunc
|
||||||
|
|
||||||
challenge string // NIP-42 challenge, we only keep the last
|
challenge string // NIP-42 challenge, we only keep the last
|
||||||
authHandler func(context.Context, *Event) error
|
authHandler func(context.Context, *Relay, *Event) error
|
||||||
noticeHandler func(string) // NIP-01 NOTICEs
|
noticeHandler func(*Relay, string) // NIP-01 NOTICEs
|
||||||
customHandler func(string) // nonstandard unparseable messages
|
customHandler func(string) // nonstandard unparseable messages
|
||||||
okCallbacks map[ID]okcallback
|
okCallbacks map[ID]okcallback
|
||||||
okCallbacksMutex sync.Mutex
|
okCallbacksMutex sync.Mutex
|
||||||
@@ -85,11 +85,11 @@ func RelayConnect(ctx context.Context, url string, opts RelayOptions) (*Relay, e
|
|||||||
|
|
||||||
type RelayOptions struct {
|
type RelayOptions struct {
|
||||||
// AuthHandler is fired when an AUTH message is received. It is given the AUTH event, unsigned, and expects you to sign it.
|
// AuthHandler is fired when an AUTH message is received. It is given the AUTH event, unsigned, and expects you to sign it.
|
||||||
AuthHandler func(context.Context, *Event) error
|
AuthHandler func(context.Context, *Relay, *Event) error
|
||||||
|
|
||||||
// NoticeHandler just takes notices and is expected to do something with them.
|
// NoticeHandler just takes notices and is expected to do something with them.
|
||||||
// When not given defaults to logging the notices.
|
// When not given defaults to logging the notices.
|
||||||
NoticeHandler func(notice string)
|
NoticeHandler func(relay *Relay, notice string)
|
||||||
|
|
||||||
// CustomHandler, if given, must be a function that handles any relay message
|
// CustomHandler, if given, must be a function that handles any relay message
|
||||||
// that couldn't be parsed as a standard envelope.
|
// that couldn't be parsed as a standard envelope.
|
||||||
@@ -180,7 +180,7 @@ func (r *Relay) handleMessage(message string) {
|
|||||||
case *NoticeEnvelope:
|
case *NoticeEnvelope:
|
||||||
// see WithNoticeHandler
|
// see WithNoticeHandler
|
||||||
if r.noticeHandler != nil {
|
if r.noticeHandler != nil {
|
||||||
r.noticeHandler(string(*env))
|
r.noticeHandler(r, string(*env))
|
||||||
} else {
|
} else {
|
||||||
log.Printf("NOTICE from %s: '%s'\n", r.URL, string(*env))
|
log.Printf("NOTICE from %s: '%s'\n", r.URL, string(*env))
|
||||||
}
|
}
|
||||||
@@ -190,7 +190,9 @@ func (r *Relay) handleMessage(message string) {
|
|||||||
}
|
}
|
||||||
r.challenge = *env.Challenge
|
r.challenge = *env.Challenge
|
||||||
if r.authHandler != nil {
|
if r.authHandler != nil {
|
||||||
r.Auth(r.Context(), r.authHandler)
|
r.Auth(r.Context(), func(ctx context.Context, evt *Event) error {
|
||||||
|
return r.authHandler(ctx, r, evt)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
case *EventEnvelope:
|
case *EventEnvelope:
|
||||||
// we already have the subscription from the pre-check above, so we can just reuse it
|
// we already have the subscription from the pre-check above, so we can just reuse it
|
||||||
|
|||||||
Reference in New Issue
Block a user