From db157e6181b9ea8bdf5a37f8c7c8b5f195414491 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 15 Oct 2023 09:18:19 -0300 Subject: [PATCH] fetch method to fetch events from nip19 codes and relay hints. --- fetch.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + 2 files changed, 61 insertions(+) create mode 100644 fetch.go diff --git a/fetch.go b/fetch.go new file mode 100644 index 0000000..36e866d --- /dev/null +++ b/fetch.go @@ -0,0 +1,60 @@ +package main + +import ( + "fmt" + + "github.com/nbd-wtf/go-nostr" + "github.com/nbd-wtf/go-nostr/nip19" + "github.com/urfave/cli/v2" +) + +var fetch = &cli.Command{ + Name: "fetch", + Usage: "fetches events related to the given nip19 code from the included relay hints", + Description: ``, + Flags: []cli.Flag{}, + ArgsUsage: "[nip19code]", + Action: func(c *cli.Context) error { + filter := nostr.Filter{} + code := c.Args().First() + + prefix, value, err := nip19.Decode(code) + if err != nil { + return err + } + + var relays []string + switch prefix { + case "nevent": + v := value.(nostr.EventPointer) + filter.IDs = append(filter.IDs, v.ID) + if v.Author != "" { + // TODO fetch relays from nip65 + } + relays = v.Relays + case "naddr": + v := value.(nostr.EntityPointer) + filter.Tags = nostr.TagMap{"d": []string{v.Identifier}} + filter.Kinds = append(filter.Kinds, v.Kind) + filter.Authors = append(filter.Authors, v.PublicKey) + // TODO fetch relays from nip65 + relays = v.Relays + case "nprofile": + v := value.(nostr.ProfilePointer) + filter.Authors = append(filter.Authors, v.PublicKey) + // TODO fetch relays from nip65 + relays = v.Relays + } + + if len(relays) == 0 { + return fmt.Errorf("no relay hints found") + } + + pool := nostr.NewSimplePool(c.Context) + for ie := range pool.SubManyEose(c.Context, relays, nostr.Filters{filter}) { + fmt.Println(ie.Event) + } + + return nil + }, +} diff --git a/main.go b/main.go index 0c88b22..15551ca 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ func main() { Commands: []*cli.Command{ req, count, + fetch, event, decode, encode,