mirror of
https://github.com/fiatjaf/nak.git
synced 2024-11-22 16:19:07 -05:00
nak event can also publish to relays directly.
This commit is contained in:
parent
bcb43ada77
commit
bbe4bfdaa0
27
event.go
27
event.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -15,9 +16,12 @@ const CATEGORY_EVENT_FIELDS = "EVENT FIELDS"
|
||||||
|
|
||||||
var event = &cli.Command{
|
var event = &cli.Command{
|
||||||
Name: "event",
|
Name: "event",
|
||||||
Usage: "generates an encoded event",
|
Usage: "generates an encoded event and either prints it or sends it to a set of relays",
|
||||||
Description: `example usage (for sending directly to a relay with 'nostcat'):
|
Description: `example usage (for sending directly to a relay with 'nostcat'):
|
||||||
nak event -k 1 -c hello --envelope | nostcat wss://nos.lol`,
|
nak event -k 1 -c hello --envelope | nostcat wss://nos.lol
|
||||||
|
|
||||||
|
standalone:
|
||||||
|
nak event -k 1 -c hello wss://nos.lol`,
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "sec",
|
Name: "sec",
|
||||||
|
@ -70,6 +74,7 @@ var event = &cli.Command{
|
||||||
Category: CATEGORY_EVENT_FIELDS,
|
Category: CATEGORY_EVENT_FIELDS,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
ArgsUsage: "[relay...]",
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
evt := nostr.Event{
|
evt := nostr.Event{
|
||||||
Kind: c.Int("kind"),
|
Kind: c.Int("kind"),
|
||||||
|
@ -111,6 +116,21 @@ var event = &cli.Command{
|
||||||
return fmt.Errorf("error signing with provided key: %w", err)
|
return fmt.Errorf("error signing with provided key: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
relays := c.Args().Slice()
|
||||||
|
if len(relays) > 0 {
|
||||||
|
for _, url := range relays {
|
||||||
|
fmt.Fprintf(os.Stderr, "publishing to %s... ", url)
|
||||||
|
if relay, err := nostr.RelayConnect(c.Context, url); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to connect: %s\n", err)
|
||||||
|
} else {
|
||||||
|
if status, err := relay.Publish(c.Context, evt); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed: %s\n", err)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(os.Stderr, "%s.\n", status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
var result string
|
var result string
|
||||||
if c.Bool("envelope") {
|
if c.Bool("envelope") {
|
||||||
j, _ := json.Marshal([]any{"EVENT", evt})
|
j, _ := json.Marshal([]any{"EVENT", evt})
|
||||||
|
@ -118,8 +138,9 @@ var event = &cli.Command{
|
||||||
} else {
|
} else {
|
||||||
result = evt.String()
|
result = evt.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user