forked from fiatjaf/noscl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.go
108 lines (99 loc) · 2.44 KB
/
home.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
102
103
104
105
106
107
108
package main
import (
"encoding/json"
"log"
"strconv"
"time"
"github.com/docopt/docopt-go"
"github.com/nbd-wtf/go-nostr"
)
func home(opts docopt.Opts, inboxMode bool) {
if len(config.Following) == 0 {
log.Println("You need to be following someone to run 'home'")
return
}
initNostr()
verbose, _ := opts.Bool("--verbose")
jsonformat, _ := opts.Bool("--json")
noreplies, _ := opts.Bool("--noreplies")
onlyreplies, _ := opts.Bool("--onlyreplies")
kinds, kindserr := optSlice(opts, "--kinds")
if kindserr != nil {
return
}
var intkinds []int
for _, kind := range kinds {
if i, e := strconv.Atoi(kind); e == nil {
intkinds = append(intkinds, i)
}
}
since, _ := opts.Int("--since")
until, _ := opts.Int("--until")
limit, _ := opts.Int("--limit")
var keys []string
nameMap := map[string]string{}
for _, follow := range config.Following {
keys = append(keys, follow.Key)
if follow.Name != "" {
nameMap[follow.Key] = follow.Name
}
}
pubkey := getPubKey(config.PrivateKey)
filters := nostr.Filters{{Limit: limit}}
if inboxMode {
// Filter by p tag to me
filters[0].Tags = nostr.TagMap{"p": {pubkey}}
// Force kinds to encrypted messages
intkinds = make([]int, 0)
intkinds = append(intkinds, nostr.KindEncryptedDirectMessage)
} else {
filters[0].Authors = keys
}
if since > 0 {
sinceTime := time.Unix(int64(since), 0)
filters[0].Since = &sinceTime
}
if until > 0 {
untilTime := time.Unix(int64(until), 0)
filters[0].Until = &untilTime
}
filters[0].Kinds = intkinds
_, all := pool.Sub(filters)
for event := range nostr.Unique(all) {
// Do we have a nick for the author of this message?
nick, ok := nameMap[event.PubKey]
if !ok {
nick = ""
}
// If we don't already have a nick for this user, and they are announcing their
// new name, let's use it.
if nick == "" {
if event.Kind == nostr.KindSetMetadata {
var metadata Metadata
err := json.Unmarshal([]byte(event.Content), &metadata)
if err != nil {
log.Println("Failed to parse metadata.")
continue
}
nick = metadata.Name
nameMap[nick] = event.PubKey
}
}
// if only want events referencing another
if onlyreplies || noreplies {
var hasReferences bool = false
for _, tag := range event.Tags {
if tag[0] == "e" {
hasReferences = true
if noreplies {
continue
}
}
}
if onlyreplies && !hasReferences {
continue
}
}
printEvent(event, &nick, verbose, jsonformat)
}
}