2022-01-02 08:44:18 -03:00
|
|
|
package nostr
|
|
|
|
|
2022-02-08 16:27:33 -03:00
|
|
|
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"
|
2022-02-08 16:27:33 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Filters []Filter
|
|
|
|
|
|
|
|
type Filter struct {
|
2024-05-25 07:34:34 -03:00
|
|
|
IDs []string
|
|
|
|
Kinds []int
|
|
|
|
Authors []string
|
|
|
|
Tags TagMap
|
|
|
|
Since *Timestamp
|
|
|
|
Until *Timestamp
|
|
|
|
Limit int
|
|
|
|
Search string
|
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
|
|
|
}
|
|
|
|
|
2022-11-08 07:15:08 -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)
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:27:33 -03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-09-06 19:37:34 -03:00
|
|
|
func (eff Filters) MatchIgnoringTimestampConstraints(event *Event) bool {
|
|
|
|
for _, filter := range eff {
|
|
|
|
if filter.MatchesIgnoringTimestampConstraints(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)
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:27:33 -03:00
|
|
|
func (ef Filter) Matches(event *Event) bool {
|
2024-09-06 19:37:34 -03:00
|
|
|
if !ef.MatchesIgnoringTimestampConstraints(event) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if ef.Since != nil && event.CreatedAt < *ef.Since {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if ef.Until != nil && event.CreatedAt > *ef.Until {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ef Filter) MatchesIgnoringTimestampConstraints(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
|
|
|
|
}
|
|
|
|
|
2022-11-08 07:15:08 -03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:27:33 -03:00
|
|
|
for f, v := range ef.Tags {
|
|
|
|
if v != nil && !event.Tags.ContainsAny(f, v) {
|
|
|
|
return false
|
|
|
|
}
|
2022-01-02 08:44:18 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:27:33 -03:00
|
|
|
func FilterEqual(a Filter, b Filter) bool {
|
2023-02-27 16:30:48 -03:00
|
|
|
if !similar(a.Kinds, b.Kinds) {
|
2022-01-02 08:44:18 -03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-02-27 16:30:48 -03:00
|
|
|
if !similar(a.IDs, b.IDs) {
|
2022-01-02 08:44:18 -03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-02-27 16:30:48 -03:00
|
|
|
if !similar(a.Authors, b.Authors) {
|
2022-01-02 08:44:18 -03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:27:33 -03:00
|
|
|
if len(a.Tags) != len(b.Tags) {
|
2022-01-02 08:44:18 -03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:27:33 -03:00
|
|
|
for f, av := range a.Tags {
|
|
|
|
if bv, ok := b.Tags[f]; !ok {
|
|
|
|
return false
|
|
|
|
} else {
|
2023-02-27 16:30:48 -03:00
|
|
|
if !similar(av, bv) {
|
2022-02-08 16:27:33 -03:00
|
|
|
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
|
|
|
|
}
|