Compare commits

..

No commits in common. "416f7a3158005cb0c59f20b098669dd43674b1a2" and "682a2074f38da706b641c51066205baf2f9cf028" have entirely different histories.

3 changed files with 82 additions and 111 deletions

View File

@ -74,9 +74,9 @@ rate_limit:
burst: 50
event_purge:
enabled: false # 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
purge_interval_minutes: 240 # Interval in minutes for running the purge
purge_interval_minutes: 1 # Interval in minutes for running the purge
purge_by_category: # Configure purging based on categories
regular: true
replaceable: false

View File

@ -21,7 +21,7 @@ func CheckWhitelist(evt nostr.Event) (bool, string) {
}
// Check if the event's pubkey is whitelisted
if whitelistCfg.PubkeyWhitelist.Enabled && !IsPubKeyWhitelisted(evt.PubKey, false) {
if whitelistCfg.PubkeyWhitelist.Enabled && !IsPubKeyWhitelisted(evt.PubKey) {
return false, "not allowed: pubkey or npub is not whitelisted"
}
@ -29,16 +29,10 @@ func CheckWhitelist(evt nostr.Event) (bool, string) {
}
// IsPubKeyWhitelisted checks if a pubkey or npub is whitelisted, considering pubkeys from domains.
// The `forPurge` flag indicates if the check is for purging purposes.
func IsPubKeyWhitelisted(pubKey string, forPurge bool) bool {
func IsPubKeyWhitelisted(pubKey string) bool {
cfg := GetWhitelistConfig()
if cfg == nil {
return false // No configuration means no whitelisting.
}
// If the whitelist is disabled but this check is for purging, we still evaluate it.
if !cfg.PubkeyWhitelist.Enabled && !forPurge {
return true // Whitelisting is not enforced for posting if disabled.
if cfg == nil || !cfg.PubkeyWhitelist.Enabled {
return true // Whitelisting is not enforced if the configuration is missing or disabled
}
// Check statically defined pubkeys
@ -66,7 +60,7 @@ func IsPubKeyWhitelisted(pubKey string, forPurge bool) bool {
pubkeys, err := utils.FetchPubkeysFromDomains(domains)
if err != nil {
log.Printf("Error fetching pubkeys from domains: %v", err)
return false // Consider errors as non-whitelisted for purging
return false // Consider returning true or handling based on your application's needs
}
for _, domainPubKey := range pubkeys {
@ -76,7 +70,7 @@ func IsPubKeyWhitelisted(pubKey string, forPurge bool) bool {
}
}
return false // Not whitelisted
return false
}
// Check if a kind is whitelisted

View File

@ -21,14 +21,8 @@ func PurgeOldEvents(cfg *types.EventPurgeConfig) {
}
client := GetClient()
// Calculate the cutoff time
currentTime := time.Now().Unix()
cutoff := currentTime - int64(cfg.KeepIntervalHours*3600) // Convert hours to seconds
cutoff := time.Now().Add(-time.Duration(cfg.KeepIntervalHours) * time.Hour).Unix()
var collectionsToPurge []string
totalPurged := 0
totalKept := 0
// Determine collections to purge
if cfg.PurgeByKindEnabled {
@ -36,6 +30,7 @@ func PurgeOldEvents(cfg *types.EventPurgeConfig) {
collectionsToPurge = append(collectionsToPurge, "event-kind"+strconv.Itoa(kind))
}
} else {
// If `purge_by_kind_enabled` is false, add all potential event kinds or find dynamically
collectionsToPurge = getAllEventCollections(client)
}
@ -54,45 +49,27 @@ func PurgeOldEvents(cfg *types.EventPurgeConfig) {
var evt nostr.Event
if err := cursor.Decode(&evt); err != nil {
log.Printf("Error decoding event from %s: %v", collectionName, err)
totalKept++
continue
}
// Debug log to check created_at and cutoff
//log.Printf("Processing event ID: %s, pubkey: %s, created_at: %d, cutoff: %d", evt.ID, evt.PubKey, evt.CreatedAt, cutoff)
// If the event is not older than the cutoff, mark it as kept
if evt.CreatedAt >= cutoff {
totalKept++
// Skip if the pubkey is whitelisted
if cfg.ExcludeWhitelisted && config.IsPubKeyWhitelisted(evt.PubKey) {
log.Printf("Skipping purging for whitelisted event ID: %s, pubkey: %s", evt.ID, evt.PubKey)
continue
}
// Skip purging if the pubkey is whitelisted
if cfg.ExcludeWhitelisted && config.IsPubKeyWhitelisted(evt.PubKey, true) {
//log.Printf("Event ID: %s is kept because the pubkey is whitelisted.", evt.ID)
totalKept++
continue
}
// Check if purging by category is enabled and matches the event's category
// 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 {
totalKept++
continue
}
// Proceed to delete the event
_, err = collection.DeleteOne(context.TODO(), bson.M{"id": evt.ID})
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)
totalKept++
} else {
totalPurged++
log.Printf("Purged event ID: %s from %s", evt.ID, collectionName)
}
}
}
}
log.Printf("Purging completed: Total events purged = %d, Total events kept = %d", totalPurged, totalKept)
}
// getAllEventCollections returns a list of all event collections if purging all kinds.
@ -120,6 +97,6 @@ func ScheduleEventPurging(cfg *types.ServerConfig) {
for range ticker.C {
PurgeOldEvents(&cfg.EventPurge)
//log.Println("Scheduled purging completed.")
log.Println("Scheduled purging completed.")
}
}