-
Notifications
You must be signed in to change notification settings - Fork 1
/
getNote.go
425 lines (384 loc) · 8.8 KB
/
getNote.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package main
import (
"context"
"errors"
"fmt"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/yosuke-furukawa/json5/encoding/json5"
//"log"
"math"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"time"
)
/*
const {{{
*/
const (
CatHome = "main.catHome"
CatSelf = "main.catSelf"
CatNSFW = "main.catNSFW"
)
// }}}
/*
filters structure {{{
*/
type Filters struct {
Content string `json:"content"`
Tags string `json:"tags"`
}
// }}}
type Recieve struct {
RelayUrl string // data.Relay.URL
Event nostr.Event // data.Event
}
type UserFilter struct {
strContentExp string
strTagsExp string
}
/*
readUserFilter {{{
*/
func (uf *UserFilter) readUserFilter(cc confClass) error {
f, err := cc.openJSON5(cc.ConfData.Filename.Filters)
if err != nil {
return err
}
defer f.Close()
var data interface{}
dec := json5.NewDecoder(f)
err = dec.Decode(&data)
if err != nil {
return err
}
b, err := json5.Marshal(data)
if err != nil {
return err
}
var c Filters
if err := json5.Unmarshal([]byte(b), &c); err != nil {
return err
}
uf.strContentExp = c.Content
uf.strTagsExp = c.Tags
return nil
}
// }}}
/*
replaceUserFilter {{{
*/
func (uf *UserFilter) replaceUserFilter(ev nostr.RelayEvent) (string, error) {
var builder strings.Builder
// tags process
if 0 < len(uf.strTagsExp) {
re, err := regexp.Compile(uf.strTagsExp)
if err != nil {
return "", err
}
for i := range ev.Tags {
for j := range ev.Tags[i] {
matches := re.FindAllString(ev.Tags[i][j], -1)
if 0 < len(matches) {
builder.WriteString("Detecting data in tags using user filters\n")
for k := range matches {
builder.WriteString(matches[k])
builder.WriteString("\n")
}
builder.WriteString("\n")
}
}
}
}
// content process
if 0 < len(uf.strContentExp) {
re, err := regexp.Compile(uf.strContentExp)
if err != nil {
return "", err
}
matches := re.FindAllString(ev.Content, -1)
if 0 < len(matches) {
builder.WriteString("Detecting data in content using user filters\n")
for i := range matches {
builder.WriteString(matches[i])
builder.WriteString("\n")
}
builder.WriteString("\n")
}
}
if 0 < len(builder.String()) {
buf := fmt.Sprintf("Evint ID : %v\n", ev.ID)
builder.WriteString(buf)
return builder.String(), nil
}
return "", nil
}
// }}}
/* getNote {{{
*/
func getNote(args []string, cc confClass) error {
var ut int64 = 0
//var wb []NOSTRLOG
pc, _, _, _ := runtime.Caller(1)
fn := runtime.FuncForPC(pc)
var uf UserFilter
if err := uf.readUserFilter(cc); err != nil {
return nil
}
c := cc.getConf()
num := c.Settings.DefaultReadNo
for i := range args {
if i < 2 {
continue
}
switch i {
case 2:
tmpnum, err := strconv.Atoi(args[2])
if err != nil {
tp, err := time.Parse(layout, args[2])
if err != nil {
return errors.New("An unknown argument was specified.")
} else {
ut = tp.Unix()
}
} else {
num = tmpnum
}
case 3:
tptmp, err := time.Parse(layout, args[3])
if err != nil {
num, err = strconv.Atoi(args[3])
if err != nil {
return errors.New("An unknown argument was specified.")
}
} else {
ut = tptmp.Unix()
}
}
}
var rs []string
if err := cc.getRelayList(&rs, readFlag); err != nil {
return err
}
var npub []string
if fn.Name() == CatHome || fn.Name() == CatNSFW {
if err := cc.getContactList(&npub); err != nil {
return err
}
} else if fn.Name() == CatSelf {
if err := cc.getMySelfPubkey(&npub); err != nil {
return err
}
} else {
return errors.New("The getNote function is called from a function that cannot use it.")
}
var filters []nostr.Filter
if ut > 0 {
ts := nostr.Timestamp(ut)
filters = []nostr.Filter{{
Kinds: []int{nostr.KindTextNote},
Authors: npub,
Until: &ts,
Limit: num,
}}
} else {
filters = []nostr.Filter{{
Kinds: []int{nostr.KindTextNote},
Authors: npub,
Limit: num,
}}
}
ctx := context.Background()
pool := nostr.NewSimplePool(ctx)
ctx, cancel := context.WithCancel(ctx)
recieveData := []Recieve{}
defer cancel()
wt := time.Duration(int64(math.Ceil(float64(num)*c.Settings.MultiplierReadRelayWaitTime))) * time.Second
timer := time.NewTimer(wt)
defer timer.Stop()
reb := replaceEnginForBech32{}
go func() error {
ch := pool.SubManyEose(ctx, rs, filters)
for event := range ch {
conv := convertRelayEventToRecieve(&event)
if tmp, err := reb.replaceToBech32(conv); err != nil {
return err
} else {
recieveData = append(recieveData, tmp)
}
}
return nil
}()
select {
case <-timer.C:
sort.Slice(recieveData, func(i, j int) bool {
return recieveData[i].Event.CreatedAt > recieveData[j].Event.CreatedAt
})
if data, err := json5.Marshal(recieveData); err != nil {
return err
} else {
fmt.Printf("%v", string(data))
}
return nil
}
}
// }}}
/* replaceNsfw {{{
*/
func replaceNsfw(e nostr.RelayEvent) string {
if checkNsfw(e.Tags) == false {
return e.Content
}
strReason := getNsfwReason(e.Tags)
return fmt.Sprintf("Content Warning!!\n%v\n\nEvent ID : %v", strReason, e.ID)
}
// }}}
/* getNsfwReason {{{
*/
func getNsfwReason(tgs nostr.Tags) string {
if checkNsfw(tgs) == false {
return ""
}
for a := range tgs {
if len(tgs[a]) < 1 {
return ""
}
for cw := range tgs[a] {
if tgs[a][cw] == "content-warning" {
continue
}
return tgs[a][cw]
}
}
return ""
}
// }}}
/* checkNsfw {{{
*/
func checkNsfw(tgs nostr.Tags) bool {
if len(tgs) < 1 {
return false
}
for a := range tgs {
if len(tgs[a]) < 1 {
return false
}
for cw := range tgs[a] {
if tgs[a][cw] == "content-warning" {
return true
}
}
}
return false
}
// }}}
/* convertRelayEventToRecieve {{{
*/
func convertRelayEventToRecieve(data *nostr.RelayEvent) Recieve {
return Recieve{
RelayUrl: data.Relay.URL,
Event: *data.Event,
}
}
// }}}
/* replaceToBech32 {{{
*/
type replaceEnginForBech32 struct {
cache Recieve
}
func (r replaceEnginForBech32) replaceToBech32(data Recieve) (Recieve, error) {
r.cache = data
// go-nostr のコメントには event ID と書かれているが
// 実際には note ID が入っているので注意!!
// event ID だと思って EncordEvent を呼び出すとえらいことに!!
if tmpdata, err := nip19.EncodeNote(data.Event.ID); err != nil {
return data, err
} else {
data.Event.ID = tmpdata
}
if tmpdata, err := nip19.EncodePublicKey(data.Event.PubKey); err != nil {
return data, err
} else {
data.Event.PubKey = tmpdata
}
if tmpdata, err := r.replaceTagsToBech32(data); err != nil {
return data, err
} else {
data = tmpdata
}
return data, nil
}
func (r replaceEnginForBech32) replaceTagsToBech32(data Recieve) (Recieve, error) {
mbl := NewModifyBech32List()
modified := data
for tagIndex, tag := range data.Event.Tags {
if len(tag) < 1 {
continue
}
tagKey := tag[0]
conversions := mbl.GetByTagKey(tagKey)
if conversions == nil {
continue
}
for elementIdx, formats := range conversions {
if elementIdx >= len(tag) {
continue
}
//element := tag[elementIdx]
for _, format := range formats {
newValue, err := r.convert(data, tagIndex, elementIdx, format)
if err != nil {
return modified, err
} else {
modified = newValue
}
}
}
}
return modified, nil
}
/*
この関数は引数で指定されたタグ配列の Hex 文字列を実際に
Bech32 文字列に変換する。
replaceTagsToBech32() から呼び出すことのみを前提にしており
そのためタグ内に変換対象データが必ず存在することを前提としている。
他の言語であれば private メンバー関数であり、コードの他の部分からの
参照は行われないことを前提としている。
*/
func (r replaceEnginForBech32) convert(data Recieve, tagIndex int, elementIndex int, format string) (Recieve, error) {
switch format {
case "nevent":
// tag 内の PubKey の有無をチェックすること
// 現在 e タグを前提としているので、今後変更する可能性がある
switch r.cache.Event.Tags[tagIndex][0] {
case "e", "q":
strPubKey := ""
var indexPubKey int
switch r.cache.Event.Tags[tagIndex][0] {
case "e":
indexPubKey = 4
case "q":
indexPubKey = 3
}
if (indexPubKey + 1) == len(data.Event.Tags[tagIndex]) {
strPubKey = r.cache.Event.Tags[tagIndex][indexPubKey]
}
if tmpdata, err := nip19.EncodeEvent(data.Event.Tags[tagIndex][elementIndex], []string{data.RelayUrl}, strPubKey); err != nil {
return data, err
} else {
data.Event.Tags[tagIndex][elementIndex] = tmpdata
}
}
case "npub":
if tmpdata, err := nip19.EncodePublicKey(data.Event.Tags[tagIndex][elementIndex]); err != nil {
return data, err
} else {
data.Event.Tags[tagIndex][elementIndex] = tmpdata
}
}
return data, nil
}
// }}}