go-nostr/filter.go

156 lines
2.7 KiB
Go
Raw Permalink Normal View History

2022-01-02 08:44:18 -03:00
package nostr
import (
2022-11-26 09:25:31 -03:00
"encoding/json"
2024-02-08 16:33:39 -03:00
"slices"
2023-11-28 15:14:27 -03:00
"github.com/mailru/easyjson"
)
type Filters []Filter
type Filter struct {
IDs []string `json:"ids,omitempty"`
Kinds []int `json:"kinds,omitempty"`
Authors []string `json:"authors,omitempty"`
Tags TagMap `json:"-,omitempty"`
Since *Timestamp `json:"since,omitempty"`
Until *Timestamp `json:"until,omitempty"`
Limit int `json:"limit,omitempty"`
Search string `json:"search,omitempty"`
2024-03-29 08:06:29 -03:00
// 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:"-"`
2022-01-02 08:44:18 -03:00
}
type TagMap map[string][]string
2022-02-09 20:25:20 -03:00
2022-11-26 09:25:31 -03:00
func (eff Filters) String() string {
j, _ := json.Marshal(eff)
return string(j)
}
func (eff Filters) Match(event *Event) bool {
2022-01-02 08:44:18 -03:00
for _, filter := range eff {
if filter.Matches(event) {
return true
}
}
return false
}
2022-11-26 09:25:31 -03:00
func (ef Filter) String() string {
2023-11-28 15:14:27 -03:00
j, _ := easyjson.Marshal(ef)
2022-11-26 09:25:31 -03:00
return string(j)
}
func (ef Filter) Matches(event *Event) bool {
2022-01-02 08:44:18 -03:00
if event == nil {
return false
}
2023-08-05 10:12:57 -03:00
if ef.IDs != nil && !slices.Contains(ef.IDs, event.ID) {
2022-01-02 08:44:18 -03:00
return false
}
if ef.Kinds != nil && !slices.Contains(ef.Kinds, event.Kind) {
2022-01-02 08:44:18 -03:00
return false
}
2023-08-05 10:12:57 -03:00
if ef.Authors != nil && !slices.Contains(ef.Authors, event.PubKey) {
2022-01-02 08:44:18 -03:00
return false
}
for f, v := range ef.Tags {
if v != nil && !event.Tags.ContainsAny(f, v) {
return false
}
2022-01-02 08:44:18 -03:00
}
2023-08-06 20:03:05 -03:00
if ef.Since != nil && event.CreatedAt < *ef.Since {
2022-01-02 08:44:18 -03:00
return false
}
2023-08-06 20:03:05 -03:00
if ef.Until != nil && event.CreatedAt > *ef.Until {
2022-01-02 08:44:18 -03:00
return false
}
return true
}
func FilterEqual(a Filter, b Filter) bool {
if !similar(a.Kinds, b.Kinds) {
2022-01-02 08:44:18 -03:00
return false
}
if !similar(a.IDs, b.IDs) {
2022-01-02 08:44:18 -03:00
return false
}
if !similar(a.Authors, b.Authors) {
2022-01-02 08:44:18 -03:00
return false
}
if len(a.Tags) != len(b.Tags) {
2022-01-02 08:44:18 -03:00
return false
}
for f, av := range a.Tags {
if bv, ok := b.Tags[f]; !ok {
return false
} else {
if !similar(av, bv) {
return false
}
}
2022-01-02 08:44:18 -03:00
}
2023-11-06 18:35:11 -03:00
if !arePointerValuesEqual(a.Since, b.Since) {
2022-01-02 08:44:18 -03:00
return false
}
2023-11-06 18:35:11 -03:00
if !arePointerValuesEqual(a.Until, b.Until) {
2022-01-02 08:44:18 -03:00
return false
}
2023-02-13 20:32:09 -05:00
if a.Search != b.Search {
return false
}
2024-03-29 08:06:29 -03:00
if a.LimitZero != b.LimitZero {
return false
}
2022-01-02 08:44:18 -03:00
return true
}
2023-11-06 18:35:11 -03:00
func (ef Filter) Clone() Filter {
clone := Filter{
2024-03-29 08:06:29 -03:00
IDs: slices.Clone(ef.IDs),
Authors: slices.Clone(ef.Authors),
Kinds: slices.Clone(ef.Kinds),
Limit: ef.Limit,
Search: ef.Search,
LimitZero: ef.LimitZero,
2023-11-06 18:35:11 -03:00
}
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
}