Files
nostrlib/nip77/idsonly.go
fiatjaf 7289da9c72 improve/refactor websocket connections hoping this will fix the undetected disconnections we're seeing.
this commit also remove all the sonic envelope parsing and reintroduces filters in REQ as a slice instead of as a singleton.

why? well, the sonic stuff wasn't really that fast, it was a little bit but only got fast enough once I introduced unsafe conversions between []byte and string and did weird unsafe reuse of []byte in order to save the values of tags, which would definitely cause issues in the future if the caller wasn't aware of it (and even if they were, like myself).

and the filters stuff is because we abandoned the idea of changing NIP-01 to only accept one filter per REQ.
2025-07-10 22:58:37 -03:00

71 lines
1.5 KiB
Go

package nip77
import (
"context"
"fmt"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/nip77/negentropy"
"fiatjaf.com/nostr/nip77/negentropy/storage/empty"
)
func FetchIDsOnly(
ctx context.Context,
url string,
filter nostr.Filter,
) (<-chan nostr.ID, error) {
id := "nl-tmp" // for now we can't have more than one subscription in the same connection
neg := negentropy.New(empty.Empty{}, 1024*1024)
result := make(chan error)
var r *nostr.Relay
r, err := nostr.RelayConnect(ctx, url, nostr.RelayOptions{CustomHandler: func(data string) {
envelope := ParseNegMessage(data)
if envelope == nil {
return
}
switch env := envelope.(type) {
case *OpenEnvelope, *CloseEnvelope:
result <- fmt.Errorf("unexpected %s received from relay", env.Label())
return
case *ErrorEnvelope:
result <- fmt.Errorf("relay returned a %s: %s", env.Label(), env.Reason)
return
case *MessageEnvelope:
nextmsg, err := neg.Reconcile(env.Message)
if err != nil {
result <- fmt.Errorf("failed to reconcile: %w", err)
return
}
if nextmsg != "" {
msgb, _ := MessageEnvelope{id, nextmsg}.MarshalJSON()
r.Write(msgb)
}
}
}})
if err != nil {
return nil, err
}
msg := neg.Start()
open, _ := OpenEnvelope{id, filter, msg}.MarshalJSON()
err = r.WriteWithError(open)
if err != nil {
return nil, fmt.Errorf("failed to write to relay: %w", err)
}
ch := make(chan nostr.ID)
go func() {
for id := range neg.HaveNots {
ch <- id
}
clse, _ := CloseEnvelope{id}.MarshalJSON()
r.Write(clse)
close(ch)
}()
return ch, nil
}