mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-10-10 12:53:33 +02:00
move everything to the same directory.
This commit is contained in:
90
filter.go
Normal file
90
filter.go
Normal file
@@ -0,0 +1,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 Equal(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
|
||||
}
|
Reference in New Issue
Block a user