2023-10-15 08:18:19 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip19"
|
2023-10-15 08:22:39 -04:00
|
|
|
"github.com/nbd-wtf/go-nostr/sdk"
|
2023-10-15 08:18:19 -04:00
|
|
|
"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
|
2023-10-15 08:22:39 -04:00
|
|
|
var authorHint string
|
|
|
|
|
2023-10-15 08:18:19 -04:00
|
|
|
switch prefix {
|
|
|
|
case "nevent":
|
|
|
|
v := value.(nostr.EventPointer)
|
|
|
|
filter.IDs = append(filter.IDs, v.ID)
|
|
|
|
if v.Author != "" {
|
2023-10-15 08:22:39 -04:00
|
|
|
authorHint = v.Author
|
2023-10-15 08:18:19 -04:00
|
|
|
}
|
|
|
|
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)
|
2023-10-15 08:22:39 -04:00
|
|
|
authorHint = v.PublicKey
|
2023-10-15 08:18:19 -04:00
|
|
|
relays = v.Relays
|
|
|
|
case "nprofile":
|
|
|
|
v := value.(nostr.ProfilePointer)
|
|
|
|
filter.Authors = append(filter.Authors, v.PublicKey)
|
2023-10-15 08:22:39 -04:00
|
|
|
authorHint = v.PublicKey
|
2023-10-15 08:18:19 -04:00
|
|
|
relays = v.Relays
|
|
|
|
}
|
|
|
|
|
2023-10-15 08:22:39 -04:00
|
|
|
pool := nostr.NewSimplePool(c.Context)
|
|
|
|
if authorHint != "" {
|
|
|
|
relayList := sdk.FetchRelaysForPubkey(c.Context, pool, authorHint,
|
|
|
|
"wss://purplepag.es", "wss://offchain.pub", "wss://public.relaying.io")
|
|
|
|
for _, relayListItem := range relayList {
|
|
|
|
if relayListItem.Outbox {
|
|
|
|
relays = append(relays, relayListItem.URL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 08:18:19 -04:00
|
|
|
if len(relays) == 0 {
|
|
|
|
return fmt.Errorf("no relay hints found")
|
|
|
|
}
|
|
|
|
|
|
|
|
for ie := range pool.SubManyEose(c.Context, relays, nostr.Filters{filter}) {
|
|
|
|
fmt.Println(ie.Event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|