This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
publish.go
93 lines (78 loc) · 1.76 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"encoding/json"
"errors"
"log"
"os"
"time"
"github.com/docopt/docopt-go"
"github.com/nbd-wtf/go-nostr"
)
func publish(opts docopt.Opts) {
if config.PrivateKey == "" {
log.Printf("Can't publish. Private key not set.\n")
return
}
initNostr()
var event nostr.Event
if file, _ := opts.String("--file"); file != "" {
jsonb, err := os.ReadFile(file)
if err != nil {
log.Printf("Failed reading content from file: %s", err)
return
}
if err := json.Unmarshal(jsonb, &event); err != nil {
log.Printf("Failed unmarshaling json from file: %s", err)
return
}
} else {
references, err := optSlice(opts, "--reference")
if err != nil {
return
}
var tags nostr.Tags
for _, ref := range references {
tags = append(tags, nostr.Tag{"e", ref})
}
profiles, err := optSlice(opts, "--profile")
if err != nil {
return
}
for _, profile := range profiles {
tags = append(tags, nostr.Tag{"p", profile})
}
content, _ := opts.String("<content>")
if content == "" {
log.Printf("Content must not be empty")
return
}
if content == "-" {
content, err = readContentStdin(4096)
if err != nil {
log.Printf("Failed reading content from stdin: %s", err)
return
}
}
event = nostr.Event{
CreatedAt: time.Now(),
Kind: nostr.KindTextNote,
Tags: tags,
Content: content,
}
}
publishEvent, statuses, err := pool.PublishEvent(&event)
if err != nil {
log.Printf("Error publishing: %s.\n", err.Error())
return
}
printPublishStatus(publishEvent, 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")
}