add support for ALLOWED_KINDS environment variable to filter event types

This commit is contained in:
mr0x50 2025-02-07 22:24:29 +01:00
parent 1770f61712
commit 645c47f7d1
2 changed files with 40 additions and 1 deletions

View File

@ -12,6 +12,7 @@ services:
POSTGRES_URL: "postgres://postgres:postgres@postgres/postgres?sslmode=disable"
# RELAY_PUBKEY: optional, will use default if not set
# RELAY_ICON: optional, will use default if not set
# ALLOWED_KINDS: "0,1,20" # optional, will allow all kinds if not set
ports:
- 3334:3334
postgres:

40
main.go
View File

@ -1,14 +1,18 @@
package main
import (
"context"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/fiatjaf/eventstore/postgresql"
"github.com/fiatjaf/khatru"
"github.com/fiatjaf/khatru/policies"
"github.com/nbd-wtf/go-nostr"
)
func getEnv(key, fallback string) string {
@ -18,6 +22,23 @@ func getEnv(key, fallback string) string {
return fallback
}
func getAllowedKinds() []int {
allowedKindsStr := getEnv("ALLOWED_KINDS", "")
if allowedKindsStr == "" {
return nil // Allow all kinds
}
kindStrs := strings.Split(allowedKindsStr, ",")
kinds := make([]int, 0, len(kindStrs))
for _, k := range kindStrs {
if kind, err := strconv.Atoi(strings.TrimSpace(k)); err == nil {
kinds = append(kinds, kind)
}
}
return kinds
}
func main() {
// Print ASCII art banner
fmt.Print(`
@ -47,7 +68,24 @@ func main() {
panic(err)
}
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
allowedKinds := getAllowedKinds()
relay.StoreEvent = append(relay.StoreEvent,
func(ctx context.Context, evt *nostr.Event) error {
if allowedKinds == nil {
db.SaveEvent(ctx, evt) // Allow all kinds
return nil
}
for _, kind := range allowedKinds {
if evt.Kind == kind {
db.SaveEvent(ctx, evt)
return nil
}
}
return fmt.Errorf("event kind %d is not allowed", evt.Kind)
},
)
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
relay.ReplaceEvent = append(relay.ReplaceEvent, db.ReplaceEvent)