mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-30 10:30:44 +02:00
nip-01 update: everything as arrays on filters.
This commit is contained in:
@ -5,13 +5,13 @@ import "github.com/fiatjaf/go-nostr/event"
|
|||||||
type EventFilters []EventFilter
|
type EventFilters []EventFilter
|
||||||
|
|
||||||
type EventFilter struct {
|
type EventFilter struct {
|
||||||
ID string `json:"id,omitempty"`
|
IDs []string `json:"ids,omitempty"`
|
||||||
Kind *int `json:"kind,omitempty"`
|
Kinds []int `json:"kinds,omitempty"`
|
||||||
Authors []string `json:"authors,omitempty"`
|
Authors []string `json:"authors,omitempty"`
|
||||||
TagEvent string `json:"#e,omitempty"`
|
|
||||||
TagProfile string `json:"#p,omitempty"`
|
|
||||||
Since uint32 `json:"since,omitempty"`
|
Since uint32 `json:"since,omitempty"`
|
||||||
Until uint32 `json:"until,omitempty"`
|
Until uint32 `json:"until,omitempty"`
|
||||||
|
TagE []string `json:"#e,omitempty"`
|
||||||
|
TagP []string `json:"#p,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eff EventFilters) Match(event *event.Event) bool {
|
func (eff EventFilters) Match(event *event.Event) bool {
|
||||||
@ -20,7 +20,6 @@ func (eff EventFilters) Match(event *event.Event) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,82 +28,23 @@ func (ef EventFilter) Matches(event *event.Event) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if ef.ID != "" && ef.ID != event.ID {
|
if ef.IDs != nil && !stringsContain(ef.IDs, event.ID) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if ef.Authors != nil {
|
if ef.Kinds != nil && !intsContain(ef.Kinds, event.Kind) {
|
||||||
found := false
|
|
||||||
for _, pubkey := range ef.Authors {
|
|
||||||
if pubkey == event.PubKey {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ef.TagEvent != "" {
|
if ef.Authors != nil && !stringsContain(ef.Authors, event.PubKey) {
|
||||||
found := false
|
|
||||||
for _, tag := range event.Tags {
|
|
||||||
if len(tag) < 2 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
tagType, ok := tag[0].(string)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if tagType == "e" {
|
|
||||||
taggedID, ok := tag[1].(string)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if taggedID == ef.TagEvent {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ef.TagProfile != "" {
|
if ef.TagE != nil && !containsAnyTag("e", event.Tags, ef.TagE) {
|
||||||
found := false
|
|
||||||
for _, tag := range event.Tags {
|
|
||||||
if len(tag) < 2 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
tagType, ok := tag[0].(string)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if tagType == "p" {
|
|
||||||
taggedID, ok := tag[1].(string)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if taggedID == ef.TagProfile {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ef.Kind != nil && *ef.Kind != event.Kind {
|
if ef.TagP != nil && !containsAnyTag("p", event.Tags, ef.TagP) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +52,7 @@ func (ef EventFilter) Matches(event *event.Event) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if ef.Until != 0 && event.CreatedAt > ef.Until {
|
if ef.Until != 0 && event.CreatedAt >= ef.Until {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,31 +60,23 @@ func (ef EventFilter) Matches(event *event.Event) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Equal(a EventFilter, b EventFilter) bool {
|
func Equal(a EventFilter, b EventFilter) bool {
|
||||||
if a.Kind == nil && b.Kind != nil ||
|
if !intsEqual(a.Kinds, b.Kinds) {
|
||||||
a.Kind != nil && b.Kind == nil ||
|
|
||||||
a.Kind != b.Kind {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.ID != b.ID {
|
if !stringsEqual(a.IDs, b.IDs) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(a.Authors) != len(b.Authors) {
|
if !stringsEqual(a.Authors, b.Authors) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, _ := range a.Authors {
|
if !stringsEqual(a.TagE, b.TagE) {
|
||||||
if a.Authors[i] != b.Authors[i] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if a.TagEvent != b.TagEvent {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.TagProfile != b.TagProfile {
|
if !stringsEqual(a.TagP, b.TagP) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
87
filter/utils.go
Normal file
87
filter/utils.go
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
package filter
|
||||||
|
|
||||||
|
import "github.com/fiatjaf/go-nostr/event"
|
||||||
|
|
||||||
|
func stringsEqual(as, bs []string) bool {
|
||||||
|
if len(as) != len(bs) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, a := range as {
|
||||||
|
for _, b := range bs {
|
||||||
|
if b == a {
|
||||||
|
goto next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// didn't find a B that corresponded to the current A
|
||||||
|
return false
|
||||||
|
|
||||||
|
next:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func intsEqual(as, bs []int) bool {
|
||||||
|
if len(as) != len(bs) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, a := range as {
|
||||||
|
for _, b := range bs {
|
||||||
|
if b == a {
|
||||||
|
goto next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// didn't find a B that corresponded to the current A
|
||||||
|
return false
|
||||||
|
|
||||||
|
next:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringsContain(haystack []string, needle string) bool {
|
||||||
|
for _, hay := range haystack {
|
||||||
|
if hay == needle {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func intsContain(haystack []int, needle int) bool {
|
||||||
|
for _, hay := range haystack {
|
||||||
|
if hay == needle {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsAnyTag(tagName string, tags event.Tags, values []string) bool {
|
||||||
|
for _, tag := range tags {
|
||||||
|
if len(tag) < 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
currentTagName, ok := tag[0].(string)
|
||||||
|
if !ok || currentTagName != tagName {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
currentTagValue, ok := tag[1].(string)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if stringsContain(values, currentTagValue) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
Reference in New Issue
Block a user