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
/
event.go
78 lines (65 loc) · 1.57 KB
/
event.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
package main
import (
"log"
"time"
"github.com/docopt/docopt-go"
"github.com/nbd-wtf/go-nostr"
)
func viewEvent(opts docopt.Opts) {
verbose, _ := opts.Bool("--verbose")
jsonformat, _ := opts.Bool("--json")
id := opts["<id>"].(string)
if id == "" {
log.Println("provided event ID was empty")
return
}
initNostr()
_, all := pool.Sub(nostr.Filters{{IDs: []string{id}}})
for event := range nostr.Unique(all) {
if event.ID != id {
log.Printf("got unexpected event %s.\n", event.ID)
continue
}
printEvent(event, nil, verbose, jsonformat)
break
}
}
func deleteEvent(opts docopt.Opts) {
initNostr()
id := opts["<id>"].(string)
if id == "" {
log.Println("Event id is empty! Exiting.")
return
}
event, statuses, err := pool.PublishEvent(&nostr.Event{
CreatedAt: time.Now(),
Kind: nostr.KindDeletion,
Tags: nostr.Tags{nostr.Tag{"e", id}},
})
if err != nil {
log.Printf("Error publishing: %s.\n", err.Error())
return
}
printPublishStatus(event, statuses)
}
// iterEventsWithTimeout returns a channel of events; this channel will be
// closed once events have stopped arriving for timeoutDuration
func iterEventsWithTimeout(events chan nostr.Event, timeoutDuration time.Duration) chan nostr.Event {
timeout := time.After(timeoutDuration)
tick := time.Tick(1 * time.Millisecond)
resulsChan := make(chan nostr.Event)
go func() {
for {
select {
case <-timeout:
close(resulsChan)
return
case <-tick:
timeout = time.After(timeoutDuration)
event := <-events
resulsChan <- event
}
}
}()
return resulsChan
}