fetch method to fetch events from nip19 codes and relay hints.

This commit is contained in:
fiatjaf 2023-10-15 09:18:19 -03:00
parent ada76f281a
commit db157e6181
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
2 changed files with 61 additions and 0 deletions

60
fetch.go Normal file
View File

@ -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
},
}

View File

@ -14,6 +14,7 @@ func main() {
Commands: []*cli.Command{
req,
count,
fetch,
event,
decode,
encode,