GetIP() and GetOpenSubscriptions() utils.

This commit is contained in:
fiatjaf
2023-12-09 08:19:37 -03:00
parent c09d21b621
commit d3a0c545d2
4 changed files with 23 additions and 6 deletions

View File

@@ -2,6 +2,9 @@ package khatru
import (
"context"
"github.com/nbd-wtf/go-nostr"
"github.com/sebest/xff"
)
func GetConnection(ctx context.Context) *WebSocket {
@@ -9,9 +12,21 @@ func GetConnection(ctx context.Context) *WebSocket {
}
func GetAuthed(ctx context.Context) string {
authedPubkey := ctx.Value(AUTH_CONTEXT_KEY)
if authedPubkey == nil {
return ""
}
return authedPubkey.(string)
return GetConnection(ctx).AuthedPublicKey
}
func GetIP(ctx context.Context) string {
return xff.GetRemoteAddr(GetConnection(ctx).Request)
}
func GetOpenSubscriptions(ctx context.Context) []nostr.Filter {
if listeners, ok := listeners.Load(GetConnection(ctx)); ok {
res := make([]nostr.Filter, 0, listeners.Size()*2)
listeners.Range(func(_ string, listener *Listener) bool {
res = append(res, listener.filters...)
return true
})
return res
}
return nil
}