khatru/utils.go

52 lines
1.0 KiB
Go
Raw Permalink Normal View History

package khatru
import (
"context"
"github.com/nbd-wtf/go-nostr"
"github.com/sebest/xff"
)
2023-12-25 09:14:09 -03:00
const (
wsKey = iota
subscriptionIdKey
)
func RequestAuth(ctx context.Context) {
ws := GetConnection(ctx)
ws.authLock.Lock()
if ws.Authed == nil {
ws.Authed = make(chan struct{})
}
ws.authLock.Unlock()
ws.WriteJSON(nostr.AuthEnvelope{Challenge: &ws.Challenge})
}
func GetConnection(ctx context.Context) *WebSocket {
2023-12-25 09:14:09 -03:00
return ctx.Value(wsKey).(*WebSocket)
}
func GetAuthed(ctx context.Context) string {
return GetConnection(ctx).AuthedPublicKey
}
func GetIP(ctx context.Context) string {
return xff.GetRemoteAddr(GetConnection(ctx).Request)
}
2023-12-25 09:14:09 -03:00
func GetSubscriptionID(ctx context.Context) string {
return ctx.Value(subscriptionIdKey).(string)
}
func GetOpenSubscriptions(ctx context.Context) []nostr.Filter {
2023-12-09 14:41:54 -03:00
if subs, ok := listeners.Load(GetConnection(ctx)); ok {
res := make([]nostr.Filter, 0, listeners.Size()*2)
2023-12-09 14:41:54 -03:00
subs.Range(func(_ string, sub *Listener) bool {
res = append(res, sub.filters...)
return true
})
return res
}
return nil
}