diff --git a/handlers.go b/handlers.go index 8867fc1..13d27eb 100644 --- a/handlers.go +++ b/handlers.go @@ -56,7 +56,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel( context.WithValue( context.Background(), - WS_KEY, ws, + wsKey, ws, ), ) @@ -171,6 +171,9 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) { // a context just for the "stored events" request handler reqCtx, cancelReqCtx := context.WithCancelCause(ctx) + // expose subscription id in the context + reqCtx = context.WithValue(reqCtx, subscriptionIdKey, env.SubscriptionID) + // handle each filter separately -- dispatching events as they're loaded from databases for _, filter := range env.Filters { err := rl.handleRequest(reqCtx, env.SubscriptionID, &eose, ws, filter) diff --git a/helpers.go b/helpers.go index 888b4c6..f032128 100644 --- a/helpers.go +++ b/helpers.go @@ -10,11 +10,6 @@ import ( "github.com/nbd-wtf/go-nostr" ) -const ( - AUTH_CONTEXT_KEY = iota - WS_KEY -) - func pointerHasher[V any](_ maphash.Seed, k *V) uint64 { return uint64(uintptr(unsafe.Pointer(k))) } diff --git a/utils.go b/utils.go index b2dbe84..2bbab86 100644 --- a/utils.go +++ b/utils.go @@ -7,8 +7,13 @@ import ( "github.com/sebest/xff" ) +const ( + wsKey = iota + subscriptionIdKey +) + func GetConnection(ctx context.Context) *WebSocket { - return ctx.Value(WS_KEY).(*WebSocket) + return ctx.Value(wsKey).(*WebSocket) } func GetAuthed(ctx context.Context) string { @@ -19,6 +24,10 @@ func GetIP(ctx context.Context) string { return xff.GetRemoteAddr(GetConnection(ctx).Request) } +func GetSubscriptionID(ctx context.Context) string { + return ctx.Value(subscriptionIdKey).(string) +} + func GetOpenSubscriptions(ctx context.Context) []nostr.Filter { if subs, ok := listeners.Load(GetConnection(ctx)); ok { res := make([]nostr.Filter, 0, listeners.Size()*2)