khatru: add and remove listener hooks.

This commit is contained in:
fiatjaf
2026-04-29 19:32:47 -03:00
parent 744fb0702c
commit 05b426e67e
2 changed files with 19 additions and 2 deletions
+17 -2
View File
@@ -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)
+2
View File
@@ -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