From 5266482b2a8cd2fba1765b8c49f334cd683fc77d Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 8 Feb 2022 16:44:33 -0300 Subject: [PATCH] support prefix search for ids and authors (nip01 update). --- filter.go | 4 ++-- filter_aux.go | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/filter.go b/filter.go index 2507373..92fb38f 100644 --- a/filter.go +++ b/filter.go @@ -29,7 +29,7 @@ func (ef Filter) Matches(event *Event) bool { return false } - if ef.IDs != nil && !ef.IDs.Contains(event.ID) { + if ef.IDs != nil && !ef.IDs.ContainsPrefixOf(event.ID) { return false } @@ -37,7 +37,7 @@ func (ef Filter) Matches(event *Event) bool { return false } - if ef.Authors != nil && !ef.Authors.Contains(event.PubKey) { + if ef.Authors != nil && !ef.Authors.ContainsPrefixOf(event.PubKey) { return false } diff --git a/filter_aux.go b/filter_aux.go index 78cbe2c..362158e 100644 --- a/filter_aux.go +++ b/filter_aux.go @@ -62,6 +62,15 @@ func (haystack StringList) Contains(needle string) bool { return false } +func (haystack StringList) ContainsPrefixOf(needle string) bool { + for _, hay := range haystack { + if strings.HasPrefix(needle, hay) { + return true + } + } + return false +} + func (haystack IntList) Contains(needle int) bool { for _, hay := range haystack { if hay == needle {