From ba7f7b539817511cb3b30fa78e04e9aa70c05008 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 14 Feb 2021 22:38:54 -0300 Subject: [PATCH] add filter helpers following the new nip-01. --- filter/filter.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 filter/filter.go diff --git a/filter/filter.go b/filter/filter.go new file mode 100644 index 0000000..093e6b5 --- /dev/null +++ b/filter/filter.go @@ -0,0 +1,104 @@ +package filter + +import "github.com/fiatjaf/go-nostr/event" + +type EventFilter struct { + ID string `json:"id,omitempty"` + Author string `json:"author,omitempty"` + Kind uint8 `json:"kind,omitempty"` + Authors []string `json:"authors,omitempty"` + TagEvent string `json:"#e,omitempty"` + TagProfile string `json:"#p,omitempty"` + Since uint32 `json:"since,omitempty"` +} + +func (ef EventFilter) Matches(event *event.Event) bool { + if event == nil { + return false + } + + if ef.ID != "" && ef.ID != event.ID { + return false + } + + if ef.Author != "" && ef.Author != event.PubKey { + return false + } + + if ef.Authors != nil { + found := false + for _, pubkey := range ef.Authors { + if pubkey == event.PubKey { + found = true + break + } + } + if !found { + return false + } + } + + if ef.TagEvent != "" { + 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 + } + } + + if ef.TagProfile != "" { + 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 + } + } + + if ef.Since != 0 && event.CreatedAt < ef.Since { + return false + } + + return true +}