forked from fiatjaf/noscl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.go
65 lines (50 loc) · 1.14 KB
/
publish.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"errors"
"log"
"time"
"github.com/docopt/docopt-go"
"github.com/fiatjaf/go-nostr"
)
func publish(opts docopt.Opts) {
if config.PrivateKey == "" {
log.Printf("Can't publish. Private key not set.\n")
return
}
initNostr()
tags := []nostr.Tag{}
references, err := optSlice(opts, "--reference")
if err != nil {
return
}
for _, ref := range references {
tags = append(tags, nostr.Tag([]interface{}{"e", ref}))
}
profiles, err := optSlice(opts, "--profile")
if err != nil {
return
}
for _, profile := range profiles {
tags = append(tags, nostr.Tag([]interface{}{"p", profile}))
}
event, statuses, err := pool.PublishEvent(&nostr.Event{
CreatedAt: uint32(time.Now().Unix()),
Kind: nostr.KindTextNote,
Tags: tags,
Content: opts["<content>"].(string),
})
if err != nil {
log.Printf("Error publishing: %s.\n", err.Error())
return
}
printPublishStatus(event, statuses)
}
func optSlice(opts docopt.Opts, key string) ([]string, error) {
if v, ok := opts[key]; ok {
vals, ok := v.([]string)
if ok {
return vals, nil
}
}
return []string{}, errors.New("unable to find opt")
}