-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter.go
77 lines (60 loc) · 1.35 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
package nostr
import "strings"
type Filter struct {
// An id here can be a prefix string to an event ID.
Ids []string `json:"ids,omitempty"`
// Filter by author public keys.
Authors []string `json:"authors,omitempty"`
// Only broadcast messages with kind in list.
Kinds []uint32 `json:"kinds,omitempty"`
// Maximum number of events to be returned in the initial query.
Limit int `json:"limit,omitempty"`
}
type Filters []Filter
func (s Filters) Match(event *Event) bool {
for _, f := range s {
if f.Matches(event) {
return true
}
}
return false
}
func (s Filter) Matches(event *Event) bool {
if event == nil {
return false
}
if s.Ids != nil && !containsPrefix(s.Ids, event.Id) {
return false
}
if s.Authors != nil && !containsAuthor(s.Authors, event.PubKey) {
return false
}
if s.Kinds != nil && !contains(s.Kinds, event.Kind) {
return false
}
return true
}
func containsAuthor(authors []string, pub string) bool {
for _, v := range authors {
if strings.HasPrefix(pub, v) {
return true
}
}
return false
}
func containsPrefix(prefixlist []string, id string) bool {
for _, prefix := range prefixlist {
if strings.HasPrefix(string(id), prefix) {
return true
}
}
return false
}
func contains(s []uint32, target uint32) bool {
for _, item := range s {
if item == target {
return true
}
}
return false
}