DRYed up with InitGraph helper

This commit is contained in:
pippellia-btc
2025-09-14 19:37:44 +02:00
parent dc30c2242c
commit aad5523de8
3 changed files with 38 additions and 46 deletions

View File

@@ -11,6 +11,8 @@ import (
"syscall"
"github.com/nbd-wtf/go-nostr"
"github.com/vertex-lab/crawler_v2/pkg/graph"
"github.com/vertex-lab/crawler_v2/pkg/redb"
)
var (
@@ -40,6 +42,30 @@ func HandleSignals(cancel context.CancelFunc) {
cancel()
}
// InitGraph by adding and promoting the provided pubkeys.
func InitGraph(ctx context.Context, db redb.RedisDB, pubkeys []string) error {
if len(pubkeys) == 0 {
return fmt.Errorf("InitGraph: init pubkeys are empty")
}
var initNodes = make([]graph.ID, len(pubkeys))
var err error
for i, pk := range pubkeys {
initNodes[i], err = db.AddNode(ctx, pk)
if err != nil {
return fmt.Errorf("InitGraph: %v", err)
}
}
for _, node := range initNodes {
if err := Promote(db, node); err != nil {
return fmt.Errorf("InitGraph: %v", err)
}
}
return nil
}
// Shutdown iterates over the relays in the pool and closes all connections.
func shutdown(pool *nostr.SimplePool) {
pool.Relays.Range(func(_ string, relay *nostr.Relay) bool {