new event policy

This commit is contained in:
pippellia-btc
2025-07-21 18:26:30 +02:00
parent 9008982a34
commit f84c46fc1f
2 changed files with 21 additions and 8 deletions

View File

@@ -232,16 +232,11 @@ func updateWalks(ctx context.Context, db redb.RedisDB, cache *walks.CachedWalker
return nil return nil
} }
const (
followPrefix = "p"
maxFollows = 50000
)
// Parse unique pubkeys (excluding author) from the "p" tags in the event. // Parse unique pubkeys (excluding author) from the "p" tags in the event.
func ParsePubkeys(event *nostr.Event) []string { func ParsePubkeys(event *nostr.Event) []string {
pubkeys := make([]string, 0, min(len(event.Tags), maxFollows)) pubkeys := make([]string, 0, min(len(event.Tags), maxTags))
for _, tag := range event.Tags { for _, tag := range event.Tags {
if len(pubkeys) > maxFollows { if len(pubkeys) > maxTags {
// stop processing, list is too big // stop processing, list is too big
break break
} }
@@ -251,7 +246,7 @@ func ParsePubkeys(event *nostr.Event) []string {
} }
prefix, pubkey := tag[0], tag[1] prefix, pubkey := tag[0], tag[1]
if prefix != followPrefix { if prefix != "p" {
continue continue
} }

View File

@@ -2,6 +2,7 @@ package pipe
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"log" "log"
"slices" "slices"
@@ -324,3 +325,20 @@ func shutdown(pool *nostr.SimplePool) {
return true return true
}) })
} }
var (
ErrEventTooBig = errors.New("event is too big")
maxTags = 20_000
maxContent = 50_000
)
// EventTooBig is a [nastro.EventPolicy] that errs if the event is too big.
func EventTooBig(e *nostr.Event) error {
if len(e.Tags) > maxTags {
return fmt.Errorf("%w: event with ID %s has too many tags: %d", ErrEventTooBig, e.ID, len(e.Tags))
}
if len(e.Content) > maxContent {
return fmt.Errorf("%w: event with ID %s has too much content: %d", ErrEventTooBig, e.ID, len(e.Content))
}
return nil
}