2024-09-22 19:34:32 +00:00
|
|
|
package mongo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-16 13:57:58 +00:00
|
|
|
"grain/config"
|
2024-09-22 19:34:32 +00:00
|
|
|
types "grain/config/types"
|
|
|
|
"grain/server/utils"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
)
|
|
|
|
|
2024-10-16 13:57:58 +00:00
|
|
|
// PurgeOldEvents removes old events based on the configuration and a list of whitelisted pubkeys.
|
2024-09-22 19:34:32 +00:00
|
|
|
func PurgeOldEvents(cfg *types.EventPurgeConfig, whitelist []string) {
|
|
|
|
if !cfg.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := GetClient()
|
|
|
|
collection := client.Database("grain").Collection("events")
|
|
|
|
|
|
|
|
// Calculate the cutoff time
|
|
|
|
cutoff := time.Now().AddDate(0, 0, -cfg.KeepDurationDays).Unix()
|
|
|
|
|
2024-10-16 13:57:58 +00:00
|
|
|
// Create the filter for purging old events
|
2024-09-22 19:34:32 +00:00
|
|
|
filter := bson.M{
|
|
|
|
"created_at": bson.M{"$lt": cutoff}, // Filter older events
|
|
|
|
}
|
|
|
|
|
2024-10-16 13:57:58 +00:00
|
|
|
// Exclude whitelisted pubkeys if specified in the config
|
2024-09-22 19:34:32 +00:00
|
|
|
if cfg.ExcludeWhitelisted && len(whitelist) > 0 {
|
|
|
|
filter["pubkey"] = bson.M{"$nin": whitelist} // Exclude whitelisted pubkeys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle purging by category
|
|
|
|
for category, purge := range cfg.PurgeByCategory {
|
|
|
|
if purge {
|
|
|
|
filter["category"] = category
|
|
|
|
_, err := collection.DeleteMany(context.TODO(), filter)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error purging events by category %s: %v", category, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle purging by kind
|
|
|
|
for _, kindRule := range cfg.PurgeByKind {
|
|
|
|
if kindRule.Enabled {
|
|
|
|
filter["kind"] = kindRule.Kind
|
|
|
|
_, err := collection.DeleteMany(context.TODO(), filter)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error purging events by kind %d: %v", kindRule.Kind, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScheduleEventPurging runs the event purging at a configurable interval.
|
|
|
|
func ScheduleEventPurging(cfg *types.ServerConfig) {
|
|
|
|
// Use the purge interval from the configuration
|
|
|
|
purgeInterval := time.Duration(cfg.EventPurge.PurgeIntervalHours) * time.Hour
|
|
|
|
ticker := time.NewTicker(purgeInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for range ticker.C {
|
2024-10-16 13:57:58 +00:00
|
|
|
// Fetch the whitelisted pubkeys without passing cfg directly
|
|
|
|
whitelist := getWhitelistedPubKeys()
|
2024-09-22 19:34:32 +00:00
|
|
|
PurgeOldEvents(&cfg.EventPurge, whitelist)
|
2024-10-16 13:57:58 +00:00
|
|
|
log.Printf("Purged old events, keeping whitelisted pubkeys: %v", whitelist)
|
2024-09-22 19:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 13:57:58 +00:00
|
|
|
// Fetch whitelisted pubkeys from both the whitelist config and any additional domains.
|
|
|
|
func getWhitelistedPubKeys() []string {
|
|
|
|
// Get the whitelist configuration
|
|
|
|
whitelistCfg := config.GetWhitelistConfig()
|
|
|
|
if whitelistCfg == nil {
|
|
|
|
log.Println("whitelistCfg is nil, returning an empty list of whitelisted pubkeys.")
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start with the statically defined pubkeys
|
|
|
|
whitelistedPubkeys := whitelistCfg.PubkeyWhitelist.Pubkeys
|
2024-09-22 19:34:32 +00:00
|
|
|
|
|
|
|
// Fetch pubkeys from domains if domain whitelist is enabled
|
2024-10-16 13:57:58 +00:00
|
|
|
if whitelistCfg.DomainWhitelist.Enabled {
|
|
|
|
domains := whitelistCfg.DomainWhitelist.Domains
|
2024-09-22 19:34:32 +00:00
|
|
|
pubkeys, err := utils.FetchPubkeysFromDomains(domains)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error fetching pubkeys from domains: %v", err)
|
2024-10-16 13:57:58 +00:00
|
|
|
// Return the existing statically whitelisted pubkeys in case of an error
|
|
|
|
return whitelistedPubkeys
|
2024-09-22 19:34:32 +00:00
|
|
|
}
|
|
|
|
// Append fetched pubkeys from domains to the whitelisted pubkeys
|
|
|
|
whitelistedPubkeys = append(whitelistedPubkeys, pubkeys...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return whitelistedPubkeys
|
2024-10-16 13:57:58 +00:00
|
|
|
}
|