mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-17 21:32:56 +01:00
add filter helpers following the new nip-01.
This commit is contained in:
parent
b4481918fe
commit
ba7f7b5398
104
filter/filter.go
Normal file
104
filter/filter.go
Normal file
@ -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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user