2023-08-10 14:32:11 -03:00
|
|
|
package khatru
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-09 08:19:37 -03:00
|
|
|
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
2023-08-10 14:32:11 -03:00
|
|
|
)
|
|
|
|
|
2023-12-25 09:14:09 -03:00
|
|
|
const (
|
|
|
|
wsKey = iota
|
|
|
|
subscriptionIdKey
|
|
|
|
)
|
|
|
|
|
2023-12-25 09:30:13 -03:00
|
|
|
func RequestAuth(ctx context.Context) {
|
|
|
|
ws := GetConnection(ctx)
|
2023-12-27 12:30:23 -03:00
|
|
|
ws.authLock.Lock()
|
2023-12-27 13:05:29 -03:00
|
|
|
if ws.Authed == nil {
|
|
|
|
ws.Authed = make(chan struct{})
|
|
|
|
}
|
2023-12-27 12:30:23 -03:00
|
|
|
ws.authLock.Unlock()
|
2023-12-25 09:30:13 -03:00
|
|
|
ws.WriteJSON(nostr.AuthEnvelope{Challenge: &ws.Challenge})
|
|
|
|
}
|
|
|
|
|
2023-08-10 14:32:11 -03:00
|
|
|
func GetConnection(ctx context.Context) *WebSocket {
|
2024-07-03 22:16:44 -03:00
|
|
|
wsi := ctx.Value(wsKey)
|
|
|
|
if wsi != nil {
|
|
|
|
return wsi.(*WebSocket)
|
|
|
|
}
|
|
|
|
return nil
|
2023-08-10 14:32:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetAuthed(ctx context.Context) string {
|
2024-07-03 22:16:44 -03:00
|
|
|
conn := GetConnection(ctx)
|
|
|
|
if conn != nil {
|
|
|
|
return conn.AuthedPublicKey
|
|
|
|
}
|
|
|
|
return ""
|
2023-12-09 08:19:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetIP(ctx context.Context) string {
|
2024-05-12 20:37:00 -03:00
|
|
|
return GetIPFromRequest(GetConnection(ctx).Request)
|
2023-12-09 08:19:37 -03:00
|
|
|
}
|
|
|
|
|
2023-12-25 09:14:09 -03:00
|
|
|
func GetSubscriptionID(ctx context.Context) string {
|
|
|
|
return ctx.Value(subscriptionIdKey).(string)
|
|
|
|
}
|
|
|
|
|
2023-12-09 08:19:37 -03:00
|
|
|
func GetOpenSubscriptions(ctx context.Context) []nostr.Filter {
|
2023-12-09 14:41:54 -03:00
|
|
|
if subs, ok := listeners.Load(GetConnection(ctx)); ok {
|
2023-12-09 08:19:37 -03:00
|
|
|
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...)
|
2023-12-09 08:19:37 -03:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return res
|
2023-08-10 14:32:11 -03:00
|
|
|
}
|
2023-12-09 08:19:37 -03:00
|
|
|
return nil
|
2023-08-10 14:32:11 -03:00
|
|
|
}
|