forked from nbd-wtf/go-nostr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
297 lines (247 loc) · 5.22 KB
/
filter.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
package nostr
import (
"slices"
"github.com/mailru/easyjson"
)
type (
TagValues []*string
TagMap map[string][]TagValues
Filter struct {
IDs []string
Kinds []int
Authors []string
Tags TagMap
Since *Timestamp
Until *Timestamp
Limit int
Search string
// LimitZero is or must be set when there is a "limit":0 in the filter, and not when "limit" is just omitted
LimitZero bool `json:"-"`
}
Filters []Filter
)
func (m TagMap) SetLiterals(tag string, values ...string) TagMap {
tagValues := make(TagValues, len(values))
for i := range values {
tagValues[i] = &values[i]
}
return m.Set(tag, tagValues...)
}
func (m TagMap) Set(tag string, values ...*string) TagMap {
m[tag] = []TagValues{values}
return m
}
func (m TagMap) Append(tag string, values ...*string) TagMap {
m[tag] = append(m[tag], values)
return m
}
func (m TagMap) HasValues(tag string) bool {
for _, values := range m[tag] {
if !values.Empty() {
return true
}
}
return false
}
func (v TagValues) Empty() bool {
for i := range v {
if v[i] != nil {
return false
}
}
return true
}
func (eff Filters) String() string {
j, _ := json.Marshal(eff)
return string(j)
}
func (eff Filters) Match(event *Event) bool {
for _, filter := range eff {
if filter.Matches(event) {
return true
}
}
return false
}
func (eff Filters) MatchIgnoringTimestampConstraints(event *Event) bool {
for _, filter := range eff {
if filter.MatchesIgnoringTimestampConstraints(event) {
return true
}
}
return false
}
func (ef Filter) String() string {
j, _ := easyjson.Marshal(ef)
return string(j)
}
func (ef Filter) Matches(event *Event) bool {
if !ef.MatchesIgnoringTimestampConstraints(event) {
return false
}
if ef.Since != nil && event.CreatedAt < *ef.Since {
return false
}
if ef.Until != nil && event.CreatedAt > *ef.Until {
return false
}
return true
}
func (ef Filter) MatchesIgnoringTimestampConstraints(event *Event) bool {
if event == nil {
return false
}
if ef.IDs != nil && !slices.Contains(ef.IDs, event.ID) {
return false
}
if ef.Kinds != nil && !slices.Contains(ef.Kinds, event.Kind) {
return false
}
if ef.Authors != nil && !slices.Contains(ef.Authors, event.PubKey) {
return false
}
valuesOf := func(name string) Tag {
for _, tag := range event.Tags {
if tag.Key() == name {
return tag[1:] // Skip the tag name.
}
}
return nil
}
for tag, values := range ef.Tags {
eventTagValues := valuesOf(tag)
if eventTagValues == nil {
return false
}
for i := range values {
for j := range values[i] {
if values[i][j] == nil {
continue
}
if j >= len(eventTagValues) || *values[i][j] != eventTagValues[j] {
return false
}
}
}
}
return true
}
func FilterEqual(a Filter, b Filter) bool {
if !similar(a.Kinds, b.Kinds) {
return false
}
if !similar(a.IDs, b.IDs) {
return false
}
if !similar(a.Authors, b.Authors) {
return false
}
if len(a.Tags) != len(b.Tags) {
return false
}
for f, av := range a.Tags {
bv, ok := b.Tags[f]
if !ok {
return false
}
ok = slices.EqualFunc(av, bv, func(a, b TagValues) bool {
if len(a) != len(b) {
return false
}
for i := range len(a) {
if !arePointerValuesEqual(a[i], b[i]) {
return false
}
}
return true
})
if !ok {
return false
}
}
if !arePointerValuesEqual(a.Since, b.Since) {
return false
}
if !arePointerValuesEqual(a.Until, b.Until) {
return false
}
if a.Search != b.Search {
return false
}
if a.LimitZero != b.LimitZero {
return false
}
return true
}
func (ef Filter) Clone() Filter {
clone := Filter{
IDs: slices.Clone(ef.IDs),
Authors: slices.Clone(ef.Authors),
Kinds: slices.Clone(ef.Kinds),
Limit: ef.Limit,
Search: ef.Search,
LimitZero: ef.LimitZero,
}
if ef.Tags != nil {
clone.Tags = make(TagMap, len(ef.Tags))
for k, v := range ef.Tags {
clone.Tags[k] = slices.Clone(v)
}
}
if ef.Since != nil {
since := *ef.Since
clone.Since = &since
}
if ef.Until != nil {
until := *ef.Until
clone.Until = &until
}
return clone
}
// GetTheoreticalLimit gets the maximum number of events that a normal filter would ever return, for example, if
// there is a number of "ids" in the filter, the theoretical limit will be that number of ids.
//
// It returns -1 if there are no theoretical limits.
//
// The given .Limit present in the filter is ignored.
func GetTheoreticalLimit(filter Filter) int {
if len(filter.IDs) > 0 {
return len(filter.IDs)
}
if len(filter.Kinds) == 0 {
return -1
}
if len(filter.Authors) > 0 {
allAreReplaceable := true
for _, kind := range filter.Kinds {
if !IsReplaceableKind(kind) {
allAreReplaceable = false
break
}
}
if allAreReplaceable {
return len(filter.Authors) * len(filter.Kinds)
}
if len(filter.Tags["d"]) > 0 {
allAreAddressable := true
for _, kind := range filter.Kinds {
if !IsAddressableKind(kind) {
allAreAddressable = false
break
}
}
if allAreAddressable {
var dlen int
for _, values := range filter.Tags["d"] {
for _, value := range values {
if value != nil {
dlen++
}
}
}
return len(filter.Authors) * len(filter.Kinds) * dlen
}
}
}
return -1
}