-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchecks.go
More file actions
114 lines (101 loc) · 2.73 KB
/
Copy pathchecks.go
File metadata and controls
114 lines (101 loc) · 2.73 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
package main
import (
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/nip10"
"fiatjaf.com/nostr/nip13"
)
// acceptedEvent returns true if the event should be stored in the relay
func acceptedEvent(event nostr.Event) bool {
if event.PubKey.Hex() == config.OwnerPubkey {
return true
} else if event.Kind == nostr.KindGiftWrap {
for tag := range event.Tags.FindAll("p") {
if tag[1] == config.OwnerPubkey {
return (belongsToWotNetwork(event) || nip13.Difficulty(event.ID) >= config.PowDMWhitelist)
}
}
return false
} else if belongsToValidThread(event) {
if belongsToWotNetwork(event) || nip13.Difficulty(event.ID) >= config.PowWhitelist {
return true
}
rootRef := nip10.GetThreadRoot(event.Tags)
if rootRef != nil && isWhitelistedForThread(event.PubKey.Hex(), rootRef.AsTagReference()) {
return true
}
}
return false
}
// belongsToValidThread returns true if the event is part of a tracked thread
func belongsToValidThread(event nostr.Event) bool {
if event.Kind == nostr.KindTextNote ||
event.Kind == nostr.KindArticle {
eReference := nip10.GetThreadRoot(event.Tags)
if eReference == nil {
return false
}
return rootNotesList.Include(eReference.AsTagReference())
}
if event.Kind == nostr.KindDeletion && !config.SkipDeletions {
eReference := nip10.GetThreadRoot(event.Tags)
if eReference == nil {
return false
}
refID, err := nostr.IDFromHex(eReference.AsTagReference())
if err != nil {
return false
}
for range store.QueryEvents(nostr.Filter{IDs: []nostr.ID{refID}}, 1) {
return true
}
}
if event.Kind == nostr.KindReaction {
// Per NIP-25, the last e tag is the target event
var targetID string
for tag := range event.Tags.FindAll("e") {
targetID = tag[1]
}
if targetID == "" {
return false
}
refID, err := nostr.IDFromHex(targetID)
if err != nil {
return false
}
for refEvent := range store.QueryEvents(nostr.Filter{IDs: []nostr.ID{refID}}, 1) {
if config.FetchAllInteractions {
return true
}
return refEvent.PubKey.Hex() == config.OwnerPubkey
}
}
if event.Kind == nostr.KindZap {
// Per NIP-57, the p tag is the zap recipient
for tag := range event.Tags.FindAll("p") {
if config.FetchAllInteractions {
return true
}
return tag[1] == config.OwnerPubkey
}
}
return false
}
// isWhitelistedForThread checks if pubkey is whitelisted for a thread
func isWhitelistedForThread(pubkey string, rootEventID string) bool {
ownerPK, err := nostr.PubKeyFromHex(config.OwnerPubkey)
if err != nil {
return false
}
filter := nostr.Filter{
Authors: []nostr.PubKey{ownerPK},
Tags: nostr.TagMap{
"e": []string{rootEventID},
"p": []string{pubkey},
},
Limit: 1,
}
for range store.QueryEvents(filter, 1) {
return true
}
return false
}