-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcatEvent.go
101 lines (91 loc) · 1.95 KB
/
catEvent.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
94
95
96
97
98
99
100
101
package main
import (
"context"
"errors"
"fmt"
"github.com/nbd-wtf/go-nostr"
"math"
"strings"
"time"
)
var replacer = strings.NewReplacer(
"\n", "\\n",
"\b", "\\b",
"\f", "\\f",
"\r", "\\r",
"\t", "\\t",
"\\", "\\\\",
"/", "\\/",
"\"", "\\\"",
)
/*
catEvent {{{
*/
func catEvent(args []string, cc confClass) error {
if len(args) < 3 {
return errors.New("invalid argument")
}
eventId := args[2]
if 0 < len(eventId) && is64HexString(eventId) == false {
if pref, err := getPrefixInString(eventId); err == nil {
switch pref {
case "note":
if _, tmpEventId, err := toHex(eventId); err != nil {
return err
} else {
eventId = tmpEventId.(string)
}
case "nevent":
if _, tmpEventId, err := toHex(eventId); err != nil {
return err
} else {
eventId = tmpEventId.(nostr.EventPointer).ID
}
default:
return errors.New(fmt.Sprintf("Invalid id starting with %v", pref))
}
}
}
c := cc.getConf()
num := 1
var rs []string
if err := cc.getRelayList(&rs, readFlag); err != nil {
return err
}
var npub []string
if err := cc.getContactList(&npub); err != nil {
return err
}
var filters []nostr.Filter
filters = []nostr.Filter{{
IDs: []string{eventId},
Kinds: []int{nostr.KindTextNote},
Limit: num,
}}
ctx := context.Background()
pool := nostr.NewSimplePool(ctx)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
wt := time.Duration(int64(math.Ceil(float64(num)*c.Settings.MultiplierReadRelayWaitTime))) * time.Second
timer := time.NewTimer(wt)
defer timer.Stop()
go func() {
ch := pool.SubManyEose(ctx, rs, filters)
fmt.Println("{")
for event := range ch {
switch event.Kind {
case 1:
buf := replacer.Replace(event.Content)
fmt.Printf("\"%v\": {\"date\": \"%v\", \"pubkey\": \"%v\", \"content\": \"%v\"},\n", event.ID, event.CreatedAt, event.PubKey, buf)
}
}
fmt.Println("}")
return
}()
select {
case <-timer.C:
//fmt.Println("}")
return nil
}
}
// }}}