nip77: simplify direction selection.

This commit is contained in:
fiatjaf 2024-12-17 13:30:04 -03:00
parent b02f0d6537
commit ade3996fbf
2 changed files with 21 additions and 19 deletions

View File

@ -45,7 +45,7 @@ func main() {
} }
err := nip77.NegentropySync(ctx, err := nip77.NegentropySync(ctx,
local, "ws://localhost:7777", nostr.Filter{}) local, "ws://localhost:7777", nostr.Filter{}, nip77.Both)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -17,7 +17,21 @@ type direction struct {
target nostr.RelayStore target nostr.RelayStore
} }
func NegentropySync(ctx context.Context, store nostr.RelayStore, url string, filter nostr.Filter, d string) error { type Direction int
const (
Up = 0
Down = 1
Both = 2
)
func NegentropySync(
ctx context.Context,
store nostr.RelayStore,
url string,
filter nostr.Filter,
dir Direction,
) 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)
@ -80,13 +94,13 @@ func NegentropySync(ctx context.Context, store nostr.RelayStore, url string, fil
pool := newidlistpool(50) pool := newidlistpool(50)
// Define sync directions // Define sync directions
directions := map[string][]direction{ directions := [][]direction{
"up": {{"up", neg.Haves, store, r}}, {{"up", neg.Haves, store, r}},
"down": {{"down", neg.HaveNots, r, store}}, {{"down", neg.HaveNots, r, store}},
"both": {{"up", neg.Haves, store, r}, {"down", neg.HaveNots, r, store}}, {{"up", neg.Haves, store, r}, {"down", neg.HaveNots, r, store}},
} }
for _, dir := range selectDir(directions, d) { for _, dir := range directions[dir] {
wg.Add(1) wg.Add(1)
go func(dir direction) { go func(dir direction) {
defer wg.Done() defer wg.Done()
@ -141,15 +155,3 @@ 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"]
}
}