Skip to content

Commit

Permalink
implements direction to nip77
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmoura authored and fiatjaf committed Dec 17, 2024
1 parent f22d4b7 commit b02f0d6
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions nip77/nip77.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import (
"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

data, err := store.QuerySync(ctx, filter)
Expand Down Expand Up @@ -69,19 +76,17 @@ func NegentropySync(ctx context.Context, store nostr.RelayStore, url string, fil
r.Write(clse)
}()

type direction struct {
label string
items chan string
source nostr.RelayStore
target nostr.RelayStore
}

wg := sync.WaitGroup{}
pool := newidlistpool(50)
for _, dir := range []direction{
{"up", neg.Haves, store, r},
{"down", neg.HaveNots, r, store},
} {

// Define sync directions
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)
go func(dir direction) {
defer wg.Done()
Expand Down Expand Up @@ -136,3 +141,15 @@ func NegentropySync(ctx context.Context, store nostr.RelayStore, url string, fil

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"]
}
}

0 comments on commit b02f0d6

Please sign in to comment.