implements direction to nip77

This commit is contained in:
Gabriel Moura
2024-12-17 11:35:11 -03:00
committed by fiatjaf_
parent f22d4b7692
commit b02f0d6537

View File

@@ -10,7 +10,14 @@ import (
"github.com/nbd-wtf/go-nostr/nip77/negentropy/storage/vector" "github.com/nbd-wtf/go-nostr/nip77/negentropy/storage/vector"
) )
func NegentropySync(ctx context.Context, store nostr.RelayStore, url string, filter nostr.Filter) error { type direction struct {
label string
items chan string
source nostr.RelayStore
target nostr.RelayStore
}
func NegentropySync(ctx context.Context, store nostr.RelayStore, url string, filter nostr.Filter, d string) error {
id := "go-nostr-tmp" // for now we can't have more than one subscription in the same connection id := "go-nostr-tmp" // for now we can't have more than one subscription in the same connection
data, err := store.QuerySync(ctx, filter) data, err := store.QuerySync(ctx, filter)
@@ -69,19 +76,17 @@ func NegentropySync(ctx context.Context, store nostr.RelayStore, url string, fil
r.Write(clse) r.Write(clse)
}() }()
type direction struct {
label string
items chan string
source nostr.RelayStore
target nostr.RelayStore
}
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
pool := newidlistpool(50) pool := newidlistpool(50)
for _, dir := range []direction{
{"up", neg.Haves, store, r}, // Define sync directions
{"down", neg.HaveNots, r, store}, directions := map[string][]direction{
} { "up": {{"up", neg.Haves, store, r}},
"down": {{"down", neg.HaveNots, r, store}},
"both": {{"up", neg.Haves, store, r}, {"down", neg.HaveNots, r, store}},
}
for _, dir := range selectDir(directions, d) {
wg.Add(1) wg.Add(1)
go func(dir direction) { go func(dir direction) {
defer wg.Done() defer wg.Done()
@@ -136,3 +141,15 @@ func NegentropySync(ctx context.Context, store nostr.RelayStore, url string, fil
return nil return nil
} }
// selectDir returns the directions to sync based on the user input
func selectDir(directions map[string][]direction, d string) []direction {
switch d {
case "up":
return directions["up"]
case "down":
return directions["down"]
default:
return directions["both"]
}
}