-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathroot_notes.go
More file actions
167 lines (145 loc) · 3.3 KB
/
Copy pathroot_notes.go
File metadata and controls
167 lines (145 loc) · 3.3 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bufio"
"os"
"strings"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/nip10"
)
const (
ThreadExternal = "EX"
ThreadOld = "OL"
ThreadArchived = "AR"
)
// RootNotes tracks thread root IDs with their state
type RootNotes struct {
notes map[string]string // id -> state ("", "EX", "OL", "AR")
filename string
}
func NewRootNotes(filename string) *RootNotes {
return &RootNotes{
notes: make(map[string]string),
filename: filename,
}
}
func (r *RootNotes) Size() int {
return len(r.notes)
}
func (r *RootNotes) Add(id, state string) {
r.notes[id] = state
}
func (r *RootNotes) Include(id string) bool {
_, exists := r.notes[id]
return exists
}
func (r *RootNotes) State(id string) string {
return r.notes[id]
}
func (r *RootNotes) SaveToFile() error {
file, err := os.Create(r.filename)
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
for id, state := range r.notes {
line := id
if state != "" {
line = id + " " + state
}
if _, err := writer.WriteString(line + "\n"); err != nil {
return err
}
}
return writer.Flush()
}
func addEventToRootList(event nostr.Event) {
if event.Kind != nostr.KindTextNote &&
event.Kind != nostr.KindArticle &&
event.Kind != nostr.KindReaction &&
event.Kind != nostr.KindZapRequest {
return
}
if event.Kind == nostr.KindReaction || event.Kind == nostr.KindZapRequest {
if event.PubKey.Hex() != config.OwnerPubkey {
return
}
var targetID string
for tag := range event.Tags.FindAll("e") {
targetID = tag[1]
}
if targetID == "" {
return
}
rootID := targetID
refID, err := nostr.IDFromHex(targetID)
if err != nil {
return
}
for targetEvent := range store.QueryEvents(nostr.Filter{IDs: []nostr.ID{refID}}, 1) {
if rootRef := nip10.GetThreadRoot(targetEvent.Tags); rootRef != nil {
rootID = rootRef.AsTagReference()
}
}
if !rootNotesList.Include(rootID) {
rootNotesList.Add(rootID, ThreadExternal)
}
return
}
rootReference := nip10.GetThreadRoot(event.Tags)
if rootReference == nil {
rootNotesList.Add(event.ID.Hex(), "")
return
}
rootID := rootReference.AsTagReference()
if event.PubKey.Hex() != config.OwnerPubkey {
if !rootNotesList.Include(rootID) {
rootNotesList.Add(rootID, "")
}
return
}
existing := rootNotesList.State(rootID)
if existing == ThreadOld || existing == ThreadArchived {
rootNotesList.Add(rootID, ThreadExternal)
return
}
if existing == ThreadExternal || existing == "" && rootNotesList.Include(rootID) {
return
}
refID, err := nostr.IDFromHex(rootID)
if err != nil {
return
}
state := ThreadExternal
for rootEvent := range store.QueryEvents(nostr.Filter{IDs: []nostr.ID{refID}}, 1) {
if rootEvent.PubKey.Hex() == config.OwnerPubkey {
state = ""
}
}
rootNotesList.Add(rootID, state)
}
func (r *RootNotes) LoadFromFile() error {
if _, err := os.Stat(r.filename); os.IsNotExist(err) {
_, err := os.Create(r.filename)
return err
}
file, err := os.Open(r.filename)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
parts := strings.SplitN(line, " ", 2)
if len(parts) == 2 {
r.notes[parts[0]] = parts[1]
} else {
r.notes[parts[0]] = ""
}
}
return scanner.Err()
}