2023-10-15 08:18:19 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-25 21:18:26 -04:00
|
|
|
"context"
|
|
|
|
|
2024-07-30 10:43:14 -04:00
|
|
|
"github.com/fiatjaf/cli/v3"
|
2023-10-15 08:18:19 -04:00
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip19"
|
2023-11-07 22:13:28 -05:00
|
|
|
sdk "github.com/nbd-wtf/nostr-sdk"
|
2023-10-15 08:18:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var fetch = &cli.Command{
|
2023-10-20 20:01:11 -04:00
|
|
|
Name: "fetch",
|
2024-06-06 14:38:40 -04:00
|
|
|
Usage: "fetches events related to the given nip19 code from the included relay hints or the author's NIP-65 relays.",
|
2023-10-20 20:01:11 -04:00
|
|
|
Description: `example usage:
|
|
|
|
nak fetch nevent1qqsxrwm0hd3s3fddh4jc2574z3xzufq6qwuyz2rvv3n087zvym3dpaqprpmhxue69uhhqatzd35kxtnjv4kxz7tfdenju6t0xpnej4
|
|
|
|
echo npub1h8spmtw9m2huyv6v2j2qd5zv956z2zdugl6mgx02f2upffwpm3nqv0j4ps | nak fetch --relay wss://relay.nostr.band`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "relay",
|
|
|
|
Aliases: []string{"r"},
|
|
|
|
Usage: "also use these relays to fetch from",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ArgsUsage: "[nip19code]",
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
|
|
|
pool := nostr.NewSimplePool(ctx)
|
2024-01-11 19:29:39 -05:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
pool.Relays.Range(func(_ string, relay *nostr.Relay) bool {
|
|
|
|
relay.Close()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
2024-03-19 10:34:59 -04:00
|
|
|
for code := range getStdinLinesOrArguments(c.Args()) {
|
2023-11-08 10:50:36 -05:00
|
|
|
filter := nostr.Filter{}
|
2023-10-15 08:18:19 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
prefix, value, err := nip19.Decode(code)
|
|
|
|
if err != nil {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "failed to decode: %s", err)
|
2023-11-08 10:50:36 -05:00
|
|
|
continue
|
|
|
|
}
|
2023-10-15 08:18:19 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
relays := c.StringSlice("relay")
|
2024-06-06 14:38:40 -04:00
|
|
|
if err := normalizeAndValidateRelayURLs(relays); err != nil {
|
2023-11-08 10:50:36 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
var authorHint string
|
2023-10-15 08:22:39 -04:00
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
switch prefix {
|
|
|
|
case "nevent":
|
|
|
|
v := value.(nostr.EventPointer)
|
|
|
|
filter.IDs = append(filter.IDs, v.ID)
|
|
|
|
if v.Author != "" {
|
|
|
|
authorHint = v.Author
|
|
|
|
}
|
2023-11-15 07:48:57 -05:00
|
|
|
relays = append(relays, v.Relays...)
|
2023-11-08 10:50:36 -05:00
|
|
|
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)
|
|
|
|
authorHint = v.PublicKey
|
2023-11-15 07:48:57 -05:00
|
|
|
relays = append(relays, v.Relays...)
|
2023-11-08 10:50:36 -05:00
|
|
|
case "nprofile":
|
|
|
|
v := value.(nostr.ProfilePointer)
|
|
|
|
filter.Authors = append(filter.Authors, v.PublicKey)
|
|
|
|
filter.Kinds = append(filter.Kinds, 0)
|
|
|
|
authorHint = v.PublicKey
|
2023-11-15 07:48:57 -05:00
|
|
|
relays = append(relays, v.Relays...)
|
2023-11-08 10:50:36 -05:00
|
|
|
case "npub":
|
|
|
|
v := value.(string)
|
|
|
|
filter.Authors = append(filter.Authors, v)
|
|
|
|
filter.Kinds = append(filter.Kinds, 0)
|
|
|
|
authorHint = v
|
2023-10-15 08:18:19 -04:00
|
|
|
}
|
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
if authorHint != "" {
|
2024-06-25 21:18:26 -04:00
|
|
|
relayList := sdk.FetchRelaysForPubkey(ctx, pool, authorHint,
|
2024-01-02 09:05:43 -05:00
|
|
|
"wss://purplepag.es", "wss://relay.damus.io", "wss://relay.noswhere.com",
|
|
|
|
"wss://nos.lol", "wss://public.relaying.io", "wss://relay.nostr.band")
|
2023-11-08 10:50:36 -05:00
|
|
|
for _, relayListItem := range relayList {
|
|
|
|
if relayListItem.Outbox {
|
|
|
|
relays = append(relays, relayListItem.URL)
|
|
|
|
}
|
2023-10-15 08:22:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 10:50:36 -05:00
|
|
|
if len(relays) == 0 {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "no relay hints found")
|
2023-11-08 10:50:36 -05:00
|
|
|
continue
|
|
|
|
}
|
2023-10-15 08:18:19 -04:00
|
|
|
|
2024-06-25 21:18:26 -04:00
|
|
|
for ie := range pool.SubManyEose(ctx, relays, nostr.Filters{filter}) {
|
2024-01-24 20:38:51 -05:00
|
|
|
stdout(ie.Event)
|
2023-11-08 10:50:36 -05:00
|
|
|
}
|
2023-10-15 08:18:19 -04:00
|
|
|
}
|
|
|
|
|
2024-06-25 21:18:26 -04:00
|
|
|
exitIfLineProcessingError(ctx)
|
2023-10-15 08:18:19 -04:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|