mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-23 17:07:13 +00:00
Compare commits
2 Commits
682a2074f3
...
416f7a3158
Author | SHA1 | Date | |
---|---|---|---|
416f7a3158 | |||
e4f44a66cd |
@ -74,9 +74,9 @@ rate_limit:
|
|||||||
burst: 50
|
burst: 50
|
||||||
|
|
||||||
event_purge:
|
event_purge:
|
||||||
enabled: true # Toggle to enable/disable event purging
|
enabled: false # Toggle to enable/disable event purging
|
||||||
keep_interval_hours: 24 # Number of hours to keep events before purging
|
keep_interval_hours: 24 # Number of hours to keep events before purging
|
||||||
purge_interval_minutes: 1 # Interval in minutes for running the purge
|
purge_interval_minutes: 240 # Interval in minutes for running the purge
|
||||||
purge_by_category: # Configure purging based on categories
|
purge_by_category: # Configure purging based on categories
|
||||||
regular: true
|
regular: true
|
||||||
replaceable: false
|
replaceable: false
|
||||||
|
@ -10,86 +10,92 @@ import (
|
|||||||
|
|
||||||
// CheckWhitelist checks if an event meets the whitelist criteria.
|
// CheckWhitelist checks if an event meets the whitelist criteria.
|
||||||
func CheckWhitelist(evt nostr.Event) (bool, string) {
|
func CheckWhitelist(evt nostr.Event) (bool, string) {
|
||||||
whitelistCfg := GetWhitelistConfig()
|
whitelistCfg := GetWhitelistConfig()
|
||||||
if whitelistCfg == nil {
|
if whitelistCfg == nil {
|
||||||
return false, "Internal server error: whitelist configuration is missing"
|
return false, "Internal server error: whitelist configuration is missing"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the event's kind is whitelisted
|
// Check if the event's kind is whitelisted
|
||||||
if whitelistCfg.KindWhitelist.Enabled && !IsKindWhitelisted(evt.Kind) {
|
if whitelistCfg.KindWhitelist.Enabled && !IsKindWhitelisted(evt.Kind) {
|
||||||
return false, "not allowed: event kind is not whitelisted"
|
return false, "not allowed: event kind is not whitelisted"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the event's pubkey is whitelisted
|
// Check if the event's pubkey is whitelisted
|
||||||
if whitelistCfg.PubkeyWhitelist.Enabled && !IsPubKeyWhitelisted(evt.PubKey) {
|
if whitelistCfg.PubkeyWhitelist.Enabled && !IsPubKeyWhitelisted(evt.PubKey, false) {
|
||||||
return false, "not allowed: pubkey or npub is not whitelisted"
|
return false, "not allowed: pubkey or npub is not whitelisted"
|
||||||
}
|
}
|
||||||
|
|
||||||
return true, ""
|
return true, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsPubKeyWhitelisted checks if a pubkey or npub is whitelisted, considering pubkeys from domains.
|
// IsPubKeyWhitelisted checks if a pubkey or npub is whitelisted, considering pubkeys from domains.
|
||||||
func IsPubKeyWhitelisted(pubKey string) bool {
|
// The `forPurge` flag indicates if the check is for purging purposes.
|
||||||
cfg := GetWhitelistConfig()
|
func IsPubKeyWhitelisted(pubKey string, forPurge bool) bool {
|
||||||
if cfg == nil || !cfg.PubkeyWhitelist.Enabled {
|
cfg := GetWhitelistConfig()
|
||||||
return true // Whitelisting is not enforced if the configuration is missing or disabled
|
if cfg == nil {
|
||||||
}
|
return false // No configuration means no whitelisting.
|
||||||
|
}
|
||||||
|
|
||||||
// Check statically defined pubkeys
|
// If the whitelist is disabled but this check is for purging, we still evaluate it.
|
||||||
for _, whitelistedKey := range cfg.PubkeyWhitelist.Pubkeys {
|
if !cfg.PubkeyWhitelist.Enabled && !forPurge {
|
||||||
if pubKey == whitelistedKey {
|
return true // Whitelisting is not enforced for posting if disabled.
|
||||||
return true
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check statically defined npubs after decoding them to pubkeys
|
// Check statically defined pubkeys
|
||||||
for _, npub := range cfg.PubkeyWhitelist.Npubs {
|
for _, whitelistedKey := range cfg.PubkeyWhitelist.Pubkeys {
|
||||||
decodedPubKey, err := utils.DecodeNpub(npub)
|
if pubKey == whitelistedKey {
|
||||||
if err != nil {
|
return true
|
||||||
log.Printf("Error decoding npub: %v", err)
|
}
|
||||||
continue
|
}
|
||||||
}
|
|
||||||
if pubKey == decodedPubKey {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch and check pubkeys from domains if domain whitelisting is enabled
|
// Check statically defined npubs after decoding them to pubkeys
|
||||||
if cfg.DomainWhitelist.Enabled {
|
for _, npub := range cfg.PubkeyWhitelist.Npubs {
|
||||||
domains := cfg.DomainWhitelist.Domains
|
decodedPubKey, err := utils.DecodeNpub(npub)
|
||||||
pubkeys, err := utils.FetchPubkeysFromDomains(domains)
|
if err != nil {
|
||||||
if err != nil {
|
log.Printf("Error decoding npub: %v", err)
|
||||||
log.Printf("Error fetching pubkeys from domains: %v", err)
|
continue
|
||||||
return false // Consider returning true or handling based on your application's needs
|
}
|
||||||
}
|
if pubKey == decodedPubKey {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, domainPubKey := range pubkeys {
|
// Fetch and check pubkeys from domains if domain whitelisting is enabled
|
||||||
if pubKey == domainPubKey {
|
if cfg.DomainWhitelist.Enabled {
|
||||||
return true
|
domains := cfg.DomainWhitelist.Domains
|
||||||
}
|
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
|
for _, domainPubKey := range pubkeys {
|
||||||
|
if pubKey == domainPubKey {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false // Not whitelisted
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if a kind is whitelisted
|
// Check if a kind is whitelisted
|
||||||
func IsKindWhitelisted(kind int) bool {
|
func IsKindWhitelisted(kind int) bool {
|
||||||
cfg := GetWhitelistConfig()
|
cfg := GetWhitelistConfig()
|
||||||
if !cfg.KindWhitelist.Enabled {
|
if !cfg.KindWhitelist.Enabled {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, whitelistedKindStr := range cfg.KindWhitelist.Kinds {
|
for _, whitelistedKindStr := range cfg.KindWhitelist.Kinds {
|
||||||
whitelistedKind, err := strconv.Atoi(whitelistedKindStr)
|
whitelistedKind, err := strconv.Atoi(whitelistedKindStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error converting whitelisted kind to int:", err)
|
fmt.Println("Error converting whitelisted kind to int:", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if kind == whitelistedKind {
|
if kind == whitelistedKind {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,14 @@ func PurgeOldEvents(cfg *types.EventPurgeConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client := GetClient()
|
client := GetClient()
|
||||||
cutoff := time.Now().Add(-time.Duration(cfg.KeepIntervalHours) * time.Hour).Unix()
|
|
||||||
|
// Calculate the cutoff time
|
||||||
|
currentTime := time.Now().Unix()
|
||||||
|
cutoff := currentTime - int64(cfg.KeepIntervalHours*3600) // Convert hours to seconds
|
||||||
|
|
||||||
var collectionsToPurge []string
|
var collectionsToPurge []string
|
||||||
|
totalPurged := 0
|
||||||
|
totalKept := 0
|
||||||
|
|
||||||
// Determine collections to purge
|
// Determine collections to purge
|
||||||
if cfg.PurgeByKindEnabled {
|
if cfg.PurgeByKindEnabled {
|
||||||
@ -30,7 +36,6 @@ func PurgeOldEvents(cfg *types.EventPurgeConfig) {
|
|||||||
collectionsToPurge = append(collectionsToPurge, "event-kind"+strconv.Itoa(kind))
|
collectionsToPurge = append(collectionsToPurge, "event-kind"+strconv.Itoa(kind))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If `purge_by_kind_enabled` is false, add all potential event kinds or find dynamically
|
|
||||||
collectionsToPurge = getAllEventCollections(client)
|
collectionsToPurge = getAllEventCollections(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,27 +54,45 @@ func PurgeOldEvents(cfg *types.EventPurgeConfig) {
|
|||||||
var evt nostr.Event
|
var evt nostr.Event
|
||||||
if err := cursor.Decode(&evt); err != nil {
|
if err := cursor.Decode(&evt); err != nil {
|
||||||
log.Printf("Error decoding event from %s: %v", collectionName, err)
|
log.Printf("Error decoding event from %s: %v", collectionName, err)
|
||||||
|
totalKept++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip if the pubkey is whitelisted
|
// Debug log to check created_at and cutoff
|
||||||
if cfg.ExcludeWhitelisted && config.IsPubKeyWhitelisted(evt.PubKey) {
|
//log.Printf("Processing event ID: %s, pubkey: %s, created_at: %d, cutoff: %d", evt.ID, evt.PubKey, evt.CreatedAt, cutoff)
|
||||||
log.Printf("Skipping purging for whitelisted event ID: %s, pubkey: %s", evt.ID, evt.PubKey)
|
|
||||||
|
// If the event is not older than the cutoff, mark it as kept
|
||||||
|
if evt.CreatedAt >= cutoff {
|
||||||
|
totalKept++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if purging by category is enabled and if the event matches the allowed category
|
// 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
|
||||||
category := utils.DetermineEventCategory(evt.Kind)
|
category := utils.DetermineEventCategory(evt.Kind)
|
||||||
if purge, exists := cfg.PurgeByCategory[category]; exists && purge {
|
if purge, exists := cfg.PurgeByCategory[category]; !exists || !purge {
|
||||||
_, err := collection.DeleteOne(context.TODO(), bson.M{"id": evt.ID})
|
totalKept++
|
||||||
if err != nil {
|
continue
|
||||||
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)
|
// Proceed to delete the event
|
||||||
}
|
_, 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("Purging completed: Total events purged = %d, Total events kept = %d", totalPurged, totalKept)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getAllEventCollections returns a list of all event collections if purging all kinds.
|
// getAllEventCollections returns a list of all event collections if purging all kinds.
|
||||||
@ -97,6 +120,6 @@ func ScheduleEventPurging(cfg *types.ServerConfig) {
|
|||||||
|
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
PurgeOldEvents(&cfg.EventPurge)
|
PurgeOldEvents(&cfg.EventPurge)
|
||||||
log.Println("Scheduled purging completed.")
|
//log.Println("Scheduled purging completed.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user