From 05b426e67eb767bfedcc774a39fc69a580630654 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 29 Apr 2026 19:32:47 -0300 Subject: [PATCH] khatru: add and remove listener hooks. --- khatru/listener.go | 19 +++++++++++++++++-- khatru/relay.go | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/khatru/listener.go b/khatru/listener.go index e9f5abe..10e5660 100644 --- a/khatru/listener.go +++ b/khatru/listener.go @@ -100,10 +100,14 @@ func (d *dispatcher) addSubscription(sub subscription) int { return ssid } -func (d *dispatcher) removeSubscription(ssid int) { +func (d *dispatcher) removeSubscription(ssid int) nostr.Filter { + var filter nostr.Filter + d.subscriptions.Compute(ssid, func(sub subscription, loaded bool) (subscription, bool) { indexed := false + filter = sub.filter + if sub.filter.Authors != nil { indexed = true for _, author := range sub.filter.Authors { @@ -150,6 +154,8 @@ func (d *dispatcher) removeSubscription(ssid int) { return sub, true }) + + return filter } func (d *dispatcher) candidates(event nostr.Event) iter.Seq[subscription] { @@ -305,6 +311,10 @@ func (rl *Relay) addListener( cancel: cancel, sid: id, }) + + if rl.OnListenerAdded != nil { + rl.OnListenerAdded(ws, ssid, id, filter) + } } } @@ -319,7 +329,12 @@ func (rl *Relay) removeListenerId(ws *WebSocket, id string) { for _, spec := range specs { if spec.sid == id { spec.cancel(ErrSubscriptionClosedByClient) - rl.dispatcher.removeSubscription(spec.ssid) + filter := rl.dispatcher.removeSubscription(spec.ssid) + + if rl.OnListenerRemoved != nil { + rl.OnListenerRemoved(ws, spec.ssid, id, filter) + } + continue } kept = append(kept, spec) diff --git a/khatru/relay.go b/khatru/relay.go index 107137b..3e5f3e1 100644 --- a/khatru/relay.go +++ b/khatru/relay.go @@ -84,6 +84,8 @@ type Relay struct { RejectConnection func(r *http.Request) bool OnConnect func(ctx context.Context) OnDisconnect func(ctx context.Context) + OnListenerAdded func(ws *WebSocket, ssid int, id string, filter nostr.Filter) + OnListenerRemoved func(ws *WebSocket, ssid int, id string, filter nostr.Filter) OverwriteRelayInformation func(ctx context.Context, r *http.Request, info nip11.RelayInformationDocument) nip11.RelayInformationDocument PreventBroadcast func(ws *WebSocket, filter nostr.Filter, event nostr.Event) bool