mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-11-15 16:50:16 +01:00
104 lines
1.6 KiB
Go
104 lines
1.6 KiB
Go
package filter
|
|
|
|
import "github.com/fiatjaf/go-nostr/event"
|
|
|
|
type EventFilter struct {
|
|
ID string `json:"id,omitempty"`
|
|
Kind *int `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.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.Kind != nil && *ef.Kind != event.Kind {
|
|
return false
|
|
}
|
|
|
|
if ef.Since != 0 && event.CreatedAt < ef.Since {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|