mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-26 20:08:54 +02:00
asynchronous event parsing on received.
This commit is contained in:
51
relay.go
51
relay.go
@@ -150,29 +150,40 @@ func (r *Relay) Connect(ctx context.Context) error {
|
|||||||
|
|
||||||
var subId string
|
var subId string
|
||||||
json.Unmarshal(jsonMessage[1], &subId)
|
json.Unmarshal(jsonMessage[1], &subId)
|
||||||
if subscription, ok := r.subscriptions.Load(subId); ok {
|
if subscription, ok := r.subscriptions.Load(subId); !ok {
|
||||||
var event Event
|
log.Printf("no subscription with id '%s'\n", subId)
|
||||||
json.Unmarshal(jsonMessage[2], &event)
|
continue
|
||||||
|
} else {
|
||||||
// check signature of all received events, ignore invalid
|
|
||||||
if !r.AssumeValid {
|
|
||||||
if ok, err := event.CheckSignature(); !ok {
|
|
||||||
errmsg := ""
|
|
||||||
if err != nil {
|
|
||||||
errmsg = err.Error()
|
|
||||||
}
|
|
||||||
log.Printf("bad signature: %s\n", errmsg)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if the event matches the desired filter, ignore otherwise
|
|
||||||
func() {
|
func() {
|
||||||
subscription.mutex.Lock()
|
// decode event
|
||||||
defer subscription.mutex.Unlock()
|
var event Event
|
||||||
if !subscription.Filters.Match(&event) || subscription.stopped {
|
json.Unmarshal(jsonMessage[2], &event)
|
||||||
|
|
||||||
|
// check if the event matches the desired filter, ignore otherwise
|
||||||
|
if !subscription.Filters.Match(&event) {
|
||||||
|
log.Printf("filter does not match\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
subscription.mutex.Lock()
|
||||||
|
defer subscription.mutex.Unlock()
|
||||||
|
if subscription.stopped {
|
||||||
|
log.Printf("subscription '%s' is stopped\n", subId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// check signature, ignore invalid, except from trusted (AssumeValid) relays
|
||||||
|
if !r.AssumeValid {
|
||||||
|
if ok, err := event.CheckSignature(); !ok {
|
||||||
|
errmsg := ""
|
||||||
|
if err != nil {
|
||||||
|
errmsg = err.Error()
|
||||||
|
}
|
||||||
|
log.Printf("bad signature: %s\n", errmsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
subscription.Events <- &event
|
subscription.Events <- &event
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user