2 Commits

Author SHA1 Message Date
Jon Staab d6812c040a Add unbanpubkey and unallowpubkey 2026-02-25 19:31:37 -08:00
Jon Staab 43de47addb Switch supported_nips to strings 2026-02-09 14:40:37 -08:00
8 changed files with 100 additions and 30 deletions
+2 -2
View File
@@ -155,14 +155,14 @@ func (rl *Relay) StartExpirationManager(
} }
go rl.expirationManager.start(rl.ctx) go rl.expirationManager.start(rl.ctx)
rl.Info.AddSupportedNIP(40) rl.Info.AddSupportedNIP("40")
} }
func (rl *Relay) DisableExpirationManager() { func (rl *Relay) DisableExpirationManager() {
rl.expirationManager.stop() rl.expirationManager.stop()
rl.expirationManager = nil rl.expirationManager = nil
idx := slices.Index(rl.Info.SupportedNIPs, 40) idx := slices.Index(rl.Info.SupportedNIPs, "40")
if idx != -1 { if idx != -1 {
rl.Info.SupportedNIPs[idx] = rl.Info.SupportedNIPs[len(rl.Info.SupportedNIPs)-1] rl.Info.SupportedNIPs[idx] = rl.Info.SupportedNIPs[len(rl.Info.SupportedNIPs)-1]
rl.Info.SupportedNIPs = rl.Info.SupportedNIPs[0 : len(rl.Info.SupportedNIPs)-1] rl.Info.SupportedNIPs = rl.Info.SupportedNIPs[0 : len(rl.Info.SupportedNIPs)-1]
+1 -1
View File
@@ -31,7 +31,7 @@ func New(rl *khatru.Relay, repositoryDir string) *GraspServer {
}, },
} }
rl.Info.AddSupportedNIP(34) rl.Info.AddSupportedNIP("34")
rl.Info.SupportedGrasps = append(rl.Info.SupportedGrasps, "GRASP-01") rl.Info.SupportedGrasps = append(rl.Info.SupportedGrasps, "GRASP-01")
base := rl.Router() base := rl.Router()
+3 -3
View File
@@ -12,13 +12,13 @@ func (rl *Relay) HandleNIP11(w http.ResponseWriter, r *http.Request) {
info := *rl.Info info := *rl.Info
if nil != rl.DeleteEvent { if nil != rl.DeleteEvent {
info.AddSupportedNIP(9) info.AddSupportedNIP("9")
} }
if nil != rl.Count { if nil != rl.Count {
info.AddSupportedNIP(45) info.AddSupportedNIP("45")
} }
if rl.Negentropy { if rl.Negentropy {
info.AddSupportedNIP(77) info.AddSupportedNIP("77")
} }
// resolve relative icon and banner URLs against base URL // resolve relative icon and banner URLs against base URL
+18
View File
@@ -21,8 +21,10 @@ type RelayManagementAPI struct {
BanPubKey func(ctx context.Context, pubkey nostr.PubKey, reason string) error BanPubKey func(ctx context.Context, pubkey nostr.PubKey, reason string) error
ListBannedPubKeys func(ctx context.Context) ([]nip86.PubKeyReason, error) ListBannedPubKeys func(ctx context.Context) ([]nip86.PubKeyReason, error)
UnbanPubKey func(ctx context.Context, pubkey nostr.PubKey, reason string) error
AllowPubKey func(ctx context.Context, pubkey nostr.PubKey, reason string) error AllowPubKey func(ctx context.Context, pubkey nostr.PubKey, reason string) error
ListAllowedPubKeys func(ctx context.Context) ([]nip86.PubKeyReason, error) ListAllowedPubKeys func(ctx context.Context) ([]nip86.PubKeyReason, error)
UnallowPubKey func(ctx context.Context, pubkey nostr.PubKey, reason string) error
ListEventsNeedingModeration func(ctx context.Context) ([]nip86.IDReason, error) ListEventsNeedingModeration func(ctx context.Context) ([]nip86.IDReason, error)
AllowEvent func(ctx context.Context, id nostr.ID, reason string) error AllowEvent func(ctx context.Context, id nostr.ID, reason string) error
BanEvent func(ctx context.Context, id nostr.ID, reason string) error BanEvent func(ctx context.Context, id nostr.ID, reason string) error
@@ -168,6 +170,14 @@ func (rl *Relay) HandleNIP86(w http.ResponseWriter, r *http.Request) {
} else { } else {
resp.Result = result resp.Result = result
} }
case nip86.UnbanPubKey:
if rl.ManagementAPI.UnbanPubKey == nil {
resp.Error = fmt.Sprintf("method %s not supported", thing.MethodName())
} else if err := rl.ManagementAPI.UnbanPubKey(ctx, thing.PubKey, thing.Reason); err != nil {
resp.Error = err.Error()
} else {
resp.Result = true
}
case nip86.AllowPubKey: case nip86.AllowPubKey:
if rl.ManagementAPI.AllowPubKey == nil { if rl.ManagementAPI.AllowPubKey == nil {
resp.Error = fmt.Sprintf("method %s not supported", thing.MethodName()) resp.Error = fmt.Sprintf("method %s not supported", thing.MethodName())
@@ -184,6 +194,14 @@ func (rl *Relay) HandleNIP86(w http.ResponseWriter, r *http.Request) {
} else { } else {
resp.Result = result resp.Result = result
} }
case nip86.UnallowPubKey:
if rl.ManagementAPI.UnallowPubKey == nil {
resp.Error = fmt.Sprintf("method %s not supported", thing.MethodName())
} else if err := rl.ManagementAPI.UnallowPubKey(ctx, thing.PubKey, thing.Reason); err != nil {
resp.Error = err.Error()
} else {
resp.Result = true
}
case nip86.BanEvent: case nip86.BanEvent:
if rl.ManagementAPI.BanEvent == nil { if rl.ManagementAPI.BanEvent == nil {
resp.Error = fmt.Sprintf("method %s not supported", thing.MethodName()) resp.Error = fmt.Sprintf("method %s not supported", thing.MethodName())
+1 -1
View File
@@ -30,7 +30,7 @@ func NewRelay() *Relay {
Info: &nip11.RelayInformationDocument{ Info: &nip11.RelayInformationDocument{
Software: "https://pkg.go.dev/fiatjaf.com/nostr/khatru", Software: "https://pkg.go.dev/fiatjaf.com/nostr/khatru",
Version: "n/a", Version: "n/a",
SupportedNIPs: []any{1, 11, 42, 70, 86}, SupportedNIPs: []string{"1", "11", "42", "70", "86"},
}, },
upgrader: websocket.Upgrader{ upgrader: websocket.Upgrader{
+18 -18
View File
@@ -9,30 +9,30 @@ import (
func TestAddSupportedNIP(t *testing.T) { func TestAddSupportedNIP(t *testing.T) {
info := RelayInformationDocument{} info := RelayInformationDocument{}
info.AddSupportedNIP(12) info.AddSupportedNIP("12")
info.AddSupportedNIP(12) info.AddSupportedNIP("12")
info.AddSupportedNIP(13) info.AddSupportedNIP("13")
info.AddSupportedNIP(1) info.AddSupportedNIP("1")
info.AddSupportedNIP(12) info.AddSupportedNIP("12")
info.AddSupportedNIP(44) info.AddSupportedNIP("44")
info.AddSupportedNIP(2) info.AddSupportedNIP("2")
info.AddSupportedNIP(13) info.AddSupportedNIP("13")
info.AddSupportedNIP(2) info.AddSupportedNIP("2")
info.AddSupportedNIP(13) info.AddSupportedNIP("13")
info.AddSupportedNIP(0) info.AddSupportedNIP("0")
info.AddSupportedNIP(17) info.AddSupportedNIP("17")
info.AddSupportedNIP(19) info.AddSupportedNIP("19")
info.AddSupportedNIP(1) info.AddSupportedNIP("1")
info.AddSupportedNIP(18) info.AddSupportedNIP("18")
assert.Contains(t, info.SupportedNIPs, 0, 1, 2, 12, 13, 17, 18, 19, 44) assert.Contains(t, info.SupportedNIPs, "0", "1", "2", "12", "13", "17", "18", "19", "44")
} }
func TestAddSupportedNIPs(t *testing.T) { func TestAddSupportedNIPs(t *testing.T) {
info := RelayInformationDocument{} info := RelayInformationDocument{}
info.AddSupportedNIPs([]int{0, 1, 2, 12, 13, 17, 18, 19, 44}) info.AddSupportedNIPs([]int{"0", "1", "2", "12", "13", "17", "18", "19", "44"})
assert.Contains(t, info.SupportedNIPs, 0, 1, 2, 12, 13, 17, 18, 19, 44) assert.Contains(t, info.SupportedNIPs, "0", "1", "2", "12", "13", "17", "18", "19", "44")
} }
func TestFetch(t *testing.T) { func TestFetch(t *testing.T) {
+5 -5
View File
@@ -14,7 +14,7 @@ type RelayInformationDocument struct {
PubKey *nostr.PubKey `json:"pubkey,omitempty"` PubKey *nostr.PubKey `json:"pubkey,omitempty"`
Self *nostr.PubKey `json:"self,omitempty"` Self *nostr.PubKey `json:"self,omitempty"`
Contact string `json:"contact,omitempty"` Contact string `json:"contact,omitempty"`
SupportedNIPs []any `json:"supported_nips,omitempty"` SupportedNIPs []string `json:"supported_nips,omitempty"`
Software string `json:"software,omitempty"` Software string `json:"software,omitempty"`
Version string `json:"version,omitempty"` Version string `json:"version,omitempty"`
@@ -33,16 +33,16 @@ type RelayInformationDocument struct {
SupportedGrasps []string `json:"supported_grasps,omitempty"` SupportedGrasps []string `json:"supported_grasps,omitempty"`
} }
func (info *RelayInformationDocument) AddSupportedNIP(number int) { func (info *RelayInformationDocument) AddSupportedNIP(nip string) {
idx := slices.IndexFunc(info.SupportedNIPs, func(n any) bool { return n == number }) idx := slices.IndexFunc(info.SupportedNIPs, func(n string) bool { return n == nip })
if idx != -1 { if idx != -1 {
return return
} }
info.SupportedNIPs = append(info.SupportedNIPs, number) info.SupportedNIPs = append(info.SupportedNIPs, nip)
} }
func (info *RelayInformationDocument) AddSupportedNIPs(numbers []int) { func (info *RelayInformationDocument) AddSupportedNIPs(numbers []string) {
for _, n := range numbers { for _, n := range numbers {
info.AddSupportedNIP(n) info.AddSupportedNIP(n)
} }
+52
View File
@@ -32,6 +32,24 @@ func DecodeRequest(req Request) (MethodParams, error) {
return BanPubKey{pk, reason}, nil return BanPubKey{pk, reason}, nil
case "listbannedpubkeys": case "listbannedpubkeys":
return ListBannedPubKeys{}, nil return ListBannedPubKeys{}, nil
case "unbanpubkey":
if len(req.Params) == 0 {
return nil, fmt.Errorf("invalid number of params for '%s'", req.Method)
}
pkh, ok := req.Params[0].(string)
if !ok {
return nil, fmt.Errorf("missing pubkey param for '%s'", req.Method)
}
pk, err := nostr.PubKeyFromHex(pkh)
if err != nil {
return nil, fmt.Errorf("invalid pubkey param for '%s'", req.Method)
}
var reason string
if len(req.Params) >= 2 {
reason, _ = req.Params[1].(string)
}
return UnbanPubKey{pk, reason}, nil
case "allowpubkey": case "allowpubkey":
if len(req.Params) == 0 { if len(req.Params) == 0 {
return nil, fmt.Errorf("invalid number of params for '%s'", req.Method) return nil, fmt.Errorf("invalid number of params for '%s'", req.Method)
@@ -52,6 +70,24 @@ func DecodeRequest(req Request) (MethodParams, error) {
return AllowPubKey{pk, reason}, nil return AllowPubKey{pk, reason}, nil
case "listallowedpubkeys": case "listallowedpubkeys":
return ListAllowedPubKeys{}, nil return ListAllowedPubKeys{}, nil
case "unallowpubkey":
if len(req.Params) == 0 {
return nil, fmt.Errorf("invalid number of params for '%s'", req.Method)
}
pkh, ok := req.Params[0].(string)
if !ok {
return nil, fmt.Errorf("missing pubkey param for '%s'", req.Method)
}
pk, err := nostr.PubKeyFromHex(pkh)
if err != nil {
return nil, fmt.Errorf("invalid pubkey param for '%s'", req.Method)
}
var reason string
if len(req.Params) >= 2 {
reason, _ = req.Params[1].(string)
}
return UnallowPubKey{pk, reason}, nil
case "listeventsneedingmoderation": case "listeventsneedingmoderation":
return ListEventsNeedingModeration{}, nil return ListEventsNeedingModeration{}, nil
case "allowevent": case "allowevent":
@@ -219,8 +255,10 @@ var (
_ MethodParams = (*SupportedMethods)(nil) _ MethodParams = (*SupportedMethods)(nil)
_ MethodParams = (*BanPubKey)(nil) _ MethodParams = (*BanPubKey)(nil)
_ MethodParams = (*ListBannedPubKeys)(nil) _ MethodParams = (*ListBannedPubKeys)(nil)
_ MethodParams = (*UnbanPubKey)(nil)
_ MethodParams = (*AllowPubKey)(nil) _ MethodParams = (*AllowPubKey)(nil)
_ MethodParams = (*ListAllowedPubKeys)(nil) _ MethodParams = (*ListAllowedPubKeys)(nil)
_ MethodParams = (*UnallowPubKey)(nil)
_ MethodParams = (*ListEventsNeedingModeration)(nil) _ MethodParams = (*ListEventsNeedingModeration)(nil)
_ MethodParams = (*AllowEvent)(nil) _ MethodParams = (*AllowEvent)(nil)
_ MethodParams = (*BanEvent)(nil) _ MethodParams = (*BanEvent)(nil)
@@ -256,6 +294,13 @@ type ListBannedPubKeys struct{}
func (ListBannedPubKeys) MethodName() string { return "listbannedpubkeys" } func (ListBannedPubKeys) MethodName() string { return "listbannedpubkeys" }
type UnbanPubKey struct {
PubKey nostr.PubKey
Reason string
}
func (UnbanPubKey) MethodName() string { return "unbanpubkey" }
type AllowPubKey struct { type AllowPubKey struct {
PubKey nostr.PubKey PubKey nostr.PubKey
Reason string Reason string
@@ -267,6 +312,13 @@ type ListAllowedPubKeys struct{}
func (ListAllowedPubKeys) MethodName() string { return "listallowedpubkeys" } func (ListAllowedPubKeys) MethodName() string { return "listallowedpubkeys" }
type UnallowPubKey struct {
PubKey nostr.PubKey
Reason string
}
func (UnallowPubKey) MethodName() string { return "unallowpubkey" }
type ListEventsNeedingModeration struct{} type ListEventsNeedingModeration struct{}
func (ListEventsNeedingModeration) MethodName() string { return "listeventsneedingmoderation" } func (ListEventsNeedingModeration) MethodName() string { return "listeventsneedingmoderation" }