mirror of
https://github.com/fiatjaf/nak.git
synced 2024-11-22 08:19:06 -05:00
fetch method to fetch events from nip19 codes and relay hints.
This commit is contained in:
parent
ada76f281a
commit
db157e6181
60
fetch.go
Normal file
60
fetch.go
Normal 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
|
||||||
|
},
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user