Compare commits

..

No commits in common. "682a2074f38da706b641c51066205baf2f9cf028" and "8491827b0627662d07c47973ab2b3741bad732f6" have entirely different histories.

5 changed files with 68 additions and 95 deletions

View File

@ -75,16 +75,17 @@ rate_limit:
event_purge: event_purge:
enabled: true # Toggle to enable/disable event purging enabled: true # Toggle to enable/disable event purging
keep_interval_hours: 24 # Number of hours to keep events before purging keep_duration_days: 2 # Number of days to keep events
purge_interval_minutes: 1 # Interval in minutes for running the purge purge_interval_hours: 24 # Runs every 24 hours
purge_by_category: # Configure purging based on categories purge_by_category: # Configure purging based on categories
parameterized_replaceable: false
regular: true regular: true
replaceable: false replaceable: false
parameterized_replaceable: false purge_by_kind: # Configure purging based on event kind
deprecated: true - kind: 0
purge_by_kind_enabled: false # Enable purging by specific kinds, if false, all collections will be purged enabled: false
kinds_to_purge: # List of event kinds to explicitly purge - kind: 1
- 1 enabled: true
- 2 - kind: 3
- 1000 enabled: false
exclude_whitelisted: true # Exclude events from whitelisted pubkeys during purging exclude_whitelisted: true # Exclude events from whitelisted pubkeys during purging

View File

@ -1,11 +1,15 @@
package config package config
type EventPurgeConfig struct { type EventPurgeConfig struct {
Enabled bool `yaml:"enabled"` Enabled bool `yaml:"enabled"`
KeepIntervalHours int `yaml:"keep_interval_hours"` KeepDurationDays int `yaml:"keep_duration_days"`
PurgeIntervalMinutes int `yaml:"purge_interval_minutes"` PurgeIntervalHours int `yaml:"purge_interval_hours"`
PurgeByCategory map[string]bool `yaml:"purge_by_category"` PurgeByCategory map[string]bool `yaml:"purge_by_category"`
PurgeByKindEnabled bool `yaml:"purge_by_kind_enabled"` PurgeByKind []KindPurgeRule `yaml:"purge_by_kind"`
KindsToPurge []int `yaml:"kinds_to_purge"` ExcludeWhitelisted bool `yaml:"exclude_whitelisted"`
ExcludeWhitelisted bool `yaml:"exclude_whitelisted"` }
type KindPurgeRule struct {
Kind int `yaml:"kind"`
Enabled bool `yaml:"enabled"`
} }

View File

@ -5,13 +5,10 @@ import (
"grain/config" "grain/config"
types "grain/config/types" types "grain/config/types"
nostr "grain/server/types" nostr "grain/server/types"
"grain/server/utils"
"log" "log"
"strconv"
"time" "time"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
) )
// PurgeOldEvents removes old events based on the configuration and a list of whitelisted pubkeys. // PurgeOldEvents removes old events based on the configuration and a list of whitelisted pubkeys.
@ -21,77 +18,49 @@ func PurgeOldEvents(cfg *types.EventPurgeConfig) {
} }
client := GetClient() client := GetClient()
cutoff := time.Now().Add(-time.Duration(cfg.KeepIntervalHours) * time.Hour).Unix() collection := client.Database("grain").Collection("events")
var collectionsToPurge []string
// Determine collections to purge // Calculate the cutoff time
if cfg.PurgeByKindEnabled { cutoff := time.Now().AddDate(0, 0, -cfg.KeepDurationDays).Unix()
for _, kind := range cfg.KindsToPurge {
collectionsToPurge = append(collectionsToPurge, "event-kind"+strconv.Itoa(kind)) // Create the base filter for fetching old events
} baseFilter := bson.M{
} else { "created_at": bson.M{"$lt": cutoff}, // Filter for events older than the cutoff
// If `purge_by_kind_enabled` is false, add all potential event kinds or find dynamically
collectionsToPurge = getAllEventCollections(client)
} }
for _, collectionName := range collectionsToPurge { cursor, err := collection.Find(context.TODO(), baseFilter)
collection := client.Database("grain").Collection(collectionName) if err != nil {
baseFilter := bson.M{"created_at": bson.M{"$lt": cutoff}} log.Printf("Error fetching old events for purging: %v", err)
return
}
defer cursor.Close(context.TODO())
cursor, err := collection.Find(context.TODO(), baseFilter) for cursor.Next(context.TODO()) {
if err != nil { var evt nostr.Event
log.Printf("Error fetching old events for purging from %s: %v", collectionName, err) if err := cursor.Decode(&evt); err != nil {
log.Printf("Error decoding event: %v", err)
continue continue
} }
defer cursor.Close(context.TODO())
for cursor.Next(context.TODO()) { // Check if the event's pubkey is whitelisted and skip purging if configured to do so
var evt nostr.Event if cfg.ExcludeWhitelisted && config.IsPubKeyWhitelisted(evt.PubKey) {
if err := cursor.Decode(&evt); err != nil { log.Printf("Skipping purging for whitelisted event ID: %s, pubkey: %s", evt.ID, evt.PubKey)
log.Printf("Error decoding event from %s: %v", collectionName, err) continue
continue }
}
// Skip if the pubkey is whitelisted // Proceed with deleting the event if it is not whitelisted
if cfg.ExcludeWhitelisted && config.IsPubKeyWhitelisted(evt.PubKey) { _, err := collection.DeleteOne(context.TODO(), bson.M{"id": evt.ID})
log.Printf("Skipping purging for whitelisted event ID: %s, pubkey: %s", evt.ID, evt.PubKey) if err != nil {
continue log.Printf("Error purging event ID %s: %v", evt.ID, err)
} } else {
log.Printf("Purged event ID: %s", evt.ID)
// Check if purging by category is enabled and if the event matches the allowed category
category := utils.DetermineEventCategory(evt.Kind)
if purge, exists := cfg.PurgeByCategory[category]; exists && purge {
_, err := collection.DeleteOne(context.TODO(), bson.M{"id": evt.ID})
if err != nil {
log.Printf("Error purging event ID %s from %s: %v", evt.ID, collectionName, err)
} else {
log.Printf("Purged event ID: %s from %s", evt.ID, collectionName)
}
}
} }
} }
} }
// getAllEventCollections returns a list of all event collections if purging all kinds.
func getAllEventCollections(client *mongo.Client) []string {
var collections []string
collectionNames, err := client.Database("grain").ListCollectionNames(context.TODO(), bson.M{})
if err != nil {
log.Printf("Error listing collection names: %v", err)
return collections
}
for _, name := range collectionNames {
if len(name) > 10 && name[:10] == "event-kind" {
collections = append(collections, name)
}
}
return collections
}
// ScheduleEventPurging runs the event purging at a configurable interval. // ScheduleEventPurging runs the event purging at a configurable interval.
func ScheduleEventPurging(cfg *types.ServerConfig) { func ScheduleEventPurging(cfg *types.ServerConfig) {
purgeInterval := time.Duration(cfg.EventPurge.PurgeIntervalMinutes) * time.Minute purgeInterval := time.Duration(cfg.EventPurge.PurgeIntervalHours) * time.Hour
ticker := time.NewTicker(purgeInterval) ticker := time.NewTicker(purgeInterval)
defer ticker.Stop() defer ticker.Stop()

View File

@ -183,7 +183,7 @@ func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {
func handleRateAndSizeLimits(ws *websocket.Conn, evt nostr.Event, eventSize int) bool { func handleRateAndSizeLimits(ws *websocket.Conn, evt nostr.Event, eventSize int) bool {
rateLimiter := config.GetRateLimiter() rateLimiter := config.GetRateLimiter()
sizeLimiter := config.GetSizeLimiter() sizeLimiter := config.GetSizeLimiter()
category := utils.DetermineEventCategory(evt.Kind) category := determineCategory(evt.Kind)
if allowed, msg := rateLimiter.AllowEvent(evt.Kind, category); !allowed { if allowed, msg := rateLimiter.AllowEvent(evt.Kind, category); !allowed {
response.SendOK(ws, evt.ID, false, msg) response.SendOK(ws, evt.ID, false, msg)
@ -197,3 +197,20 @@ func handleRateAndSizeLimits(ws *websocket.Conn, evt nostr.Event, eventSize int)
return true return true
} }
func determineCategory(kind int) string {
switch {
case kind == 0, kind == 3, kind >= 10000 && kind < 20000:
return "replaceable"
case kind == 1, kind >= 4 && kind < 45, kind >= 1000 && kind < 10000:
return "regular"
case kind == 2:
return "deprecated"
case kind >= 20000 && kind < 30000:
return "ephemeral"
case kind >= 30000 && kind < 40000:
return "parameterized_replaceable"
default:
return "unknown"
}
}

View File

@ -1,18 +0,0 @@
package utils
func DetermineEventCategory(kind int) string {
switch {
case kind == 0, kind == 3, kind >= 10000 && kind < 20000:
return "replaceable"
case kind == 1, kind >= 4 && kind < 45, kind >= 1000 && kind < 10000:
return "regular"
case kind == 2:
return "deprecated"
case kind >= 20000 && kind < 30000:
return "ephemeral"
case kind >= 30000 && kind < 40000:
return "parameterized_replaceable"
default:
return "unknown"
}
}