Compare commits

..

2 Commits

Author SHA1 Message Date
fiatjaf
c0848182c4 OverwriteResponseEvent for optionally modifying events before sending them to client. 2023-11-04 17:39:28 -03:00
fiatjaf
80586c4573 update go-nostr to fix the OK envelope bug. 2023-11-04 17:31:39 -03:00
5 changed files with 17 additions and 12 deletions

View File

@@ -86,7 +86,11 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
}
}
for _, ovw := range rl.OverwriteResponseEvent {
ovw(ctx, evt)
}
notifyListeners(evt)
return nil
}

2
go.mod
View File

@@ -6,7 +6,7 @@ require (
github.com/fasthttp/websocket v1.5.3
github.com/fiatjaf/eventstore v0.1.0
github.com/gobwas/ws v1.2.0
github.com/nbd-wtf/go-nostr v0.25.0
github.com/nbd-wtf/go-nostr v0.25.1
github.com/puzpuzpuz/xsync/v2 v2.5.1
github.com/rs/cors v1.7.0
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53

4
go.sum
View File

@@ -90,8 +90,8 @@ github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/nbd-wtf/go-nostr v0.25.0 h1:6ArnEX5NqjTaIBH6F5KYIJ0uw0uaKSWu8zjDb9za0Cg=
github.com/nbd-wtf/go-nostr v0.25.0/go.mod h1:bkffJI+x914sPQWum9ZRUn66D7NpDnAoWo1yICvj3/0=
github.com/nbd-wtf/go-nostr v0.25.1 h1:YTLTDUgngfzd3qQ0fWmQmq20flwnGtHH0g0Q8S3HlW4=
github.com/nbd-wtf/go-nostr v0.25.1/go.mod h1:bkffJI+x914sPQWum9ZRUn66D7NpDnAoWo1yICvj3/0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View File

@@ -124,12 +124,10 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
// check signature (requires the ID to be set)
if ok, err := evt.CheckSignature(); err != nil {
reason := "error: failed to verify signature"
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: &reason})
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: "error: failed to verify signature"})
return
} else if !ok {
reason := "invalid: signature is invalid"
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: &reason})
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: "invalid: signature is invalid"})
return
}
@@ -139,12 +137,12 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
} else {
err = rl.AddEvent(ctx, &evt)
}
var reason *string
var reason string
if err == nil {
ok = true
} else {
msg := err.Error()
reason = &msg
reason = err.Error()
}
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: ok, Reason: reason})
case "COUNT":
@@ -236,6 +234,9 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
go func(ch chan *nostr.Event) {
for event := range ch {
for _, ovw := range rl.OverwriteResponseEvent {
ovw(ctx, event)
}
ws.WriteJSON(nostr.EventEnvelope{SubscriptionID: &id, Event: *event})
}
eose.Done()
@@ -273,8 +274,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
ctx = context.WithValue(ctx, AUTH_CONTEXT_KEY, pubkey)
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: true})
} else {
reason := "error: failed to authenticate"
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: &reason})
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: "error: failed to authenticate"})
}
}
}

View File

@@ -45,6 +45,7 @@ type Relay struct {
RejectFilter []func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
RejectCountFilter []func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
OverwriteDeletionOutcome []func(ctx context.Context, target *nostr.Event, deletion *nostr.Event) (acceptDeletion bool, msg string)
OverwriteResponseEvent []func(ctx context.Context, event *nostr.Event)
StoreEvent []func(ctx context.Context, event *nostr.Event) error
DeleteEvent []func(ctx context.Context, event *nostr.Event) error
QueryEvents []func(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error)