mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-17 21:32:56 +01:00
91 lines
1.5 KiB
Go
91 lines
1.5 KiB
Go
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
|
|
}
|