option to run relay without syncing from other relays

This commit is contained in:
Barry Deen 2024-09-16 21:54:16 -04:00
parent 02fc217510
commit aae474531f

69
main.go

@ -28,6 +28,7 @@ type Config struct {
StaticPath string StaticPath string
RefreshInterval int RefreshInterval int
MinimumFollowers int MinimumFollowers int
ArchivalSync bool
} }
var pool *nostr.SimplePool var pool *nostr.SimplePool
@ -172,6 +173,10 @@ func LoadConfig() Config {
os.Setenv("MINIMUM_FOLLOWERS", "1") os.Setenv("MINIMUM_FOLLOWERS", "1")
} }
if os.Getenv("ARCHIVAL_SYNC") == "" {
os.Setenv("ARCHIVAL_SYNC", "TRUE")
}
minimumFollowers, _ := strconv.Atoi(os.Getenv("MINIMUM_FOLLOWERS")) minimumFollowers, _ := strconv.Atoi(os.Getenv("MINIMUM_FOLLOWERS"))
config := Config{ config := Config{
@ -184,6 +189,7 @@ func LoadConfig() Config {
StaticPath: getEnv("STATIC_PATH"), StaticPath: getEnv("STATIC_PATH"),
RefreshInterval: refreshInterval, RefreshInterval: refreshInterval,
MinimumFollowers: minimumFollowers, MinimumFollowers: minimumFollowers,
ArchivalSync: getEnv("ARCHIVAL_SYNC") == "TRUE",
} }
return config return config
@ -335,31 +341,52 @@ func appendOneHopNetwork(pubkey string) {
func archiveTrustedNotes(ctx context.Context, relay *khatru.Relay) { func archiveTrustedNotes(ctx context.Context, relay *khatru.Relay) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(config.RefreshInterval)*time.Hour) timeout, cancel := context.WithTimeout(ctx, time.Duration(config.RefreshInterval)*time.Hour)
defer cancel() defer cancel()
go refreshProfiles(ctx)
filters := []nostr.Filter{{ done := make(chan struct{})
Kinds: []int{
nostr.KindArticle,
nostr.KindDeletion,
nostr.KindContactList,
nostr.KindEncryptedDirectMessage,
nostr.KindMuteList,
nostr.KindReaction,
nostr.KindRelayListMetadata,
nostr.KindRepost,
nostr.KindZapRequest,
nostr.KindZap,
nostr.KindTextNote,
},
}}
log.Println("📦 archiving trusted notes...") go func() {
if config.ArchivalSync {
go refreshProfiles(ctx)
for ev := range pool.SubMany(timeout, seedRelays, filters) { filters := []nostr.Filter{{
go archiveEvent(ctx, relay, *ev.Event) Kinds: []int{
nostr.KindArticle,
nostr.KindDeletion,
nostr.KindContactList,
nostr.KindEncryptedDirectMessage,
nostr.KindMuteList,
nostr.KindReaction,
nostr.KindRelayListMetadata,
nostr.KindRepost,
nostr.KindZapRequest,
nostr.KindZap,
nostr.KindTextNote,
},
}}
log.Println("📦 archiving trusted notes...")
for ev := range pool.SubMany(timeout, seedRelays, filters) {
go archiveEvent(ctx, relay, *ev.Event)
}
log.Println("📦 archived", trustedNotes, "trusted notes and discarded", untrustedNotes, "untrusted notes")
} else {
log.Println("🔄 web of trust will refresh in", config.RefreshInterval, "hours")
select {
case <-timeout.Done():
}
}
close(done)
}()
select {
case <-timeout.Done():
log.Println("restarting process")
case <-done:
log.Println("📦 archiving process completed")
} }
log.Println("📦 archived", trustedNotes, "trusted notes and discarded ", untrustedNotes, "untrusted notes")
} }
func archiveEvent(ctx context.Context, relay *khatru.Relay, ev nostr.Event) { func archiveEvent(ctx context.Context, relay *khatru.Relay, ev nostr.Event) {