forked from nbd-wtf/go-nostr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
90 lines (70 loc) · 1.53 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
package nostr
type EventFilters []EventFilter
type EventFilter struct {
IDs StringList `json:"ids,omitempty"`
Kinds IntList `json:"kinds,omitempty"`
Authors StringList `json:"authors,omitempty"`
Since uint32 `json:"since,omitempty"`
Until uint32 `json:"until,omitempty"`
TagE StringList `json:"#e,omitempty"`
TagP StringList `json:"#p,omitempty"`
}
func (eff EventFilters) Match(event *Event) bool {
for _, filter := range eff {
if filter.Matches(event) {
return true
}
}
return false
}
func (ef EventFilter) Matches(event *Event) bool {
if event == nil {
return false
}
if ef.IDs != nil && !ef.IDs.Contains(event.ID) {
return false
}
if ef.Kinds != nil && !ef.Kinds.Contains(event.Kind) {
return false
}
if ef.Authors != nil && !ef.Authors.Contains(event.PubKey) {
return false
}
if ef.TagE != nil && !event.Tags.ContainsAny("e", ef.TagE) {
return false
}
if ef.TagP != nil && !event.Tags.ContainsAny("p", ef.TagP) {
return false
}
if ef.Since != 0 && event.CreatedAt < ef.Since {
return false
}
if ef.Until != 0 && event.CreatedAt >= ef.Until {
return false
}
return true
}
func FilterEqual(a EventFilter, b EventFilter) bool {
if !a.Kinds.Equals(b.Kinds) {
return false
}
if !a.IDs.Equals(b.IDs) {
return false
}
if !a.Authors.Equals(b.Authors) {
return false
}
if !a.TagE.Equals(b.TagE) {
return false
}
if !a.TagP.Equals(b.TagP) {
return false
}
if a.Since != b.Since {
return false
}
if a.Until != b.Until {
return false
}
return true
}