Skip to content

Commit

Permalink
feat(types): event match
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Sep 7, 2024
1 parent 7c41b26 commit 94ef082
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 27 deletions.
55 changes: 35 additions & 20 deletions types/event.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package types

import "slices"

// Range is the events kind ranges.
type Range uint8

type Tag [2]string

const (
Regular Range = iota
Replaceable
Expand All @@ -14,13 +14,13 @@ const (

// Event reperesents an event structure defined on NIP-01.
type Event struct {
ID string `json:"id"`
PublicKey string `json:"pubkey"`
CreatedAt int64 `json:"created_at"`
Kind uint16 `json:"kind"`
Tags [][]string `json:"tags"`
Content string `json:"content"`
Signature string `json:"sig"`
ID string `json:"id"`
PublicKey string `json:"pubkey"`
CreatedAt int64 `json:"created_at"`
Kind uint16 `json:"kind"`
Tags []Tag `json:"tags"`
Content string `json:"content"`
Signature string `json:"sig"`
}

// IsRegular checks if the gived event kind is in Regular range.
Expand Down Expand Up @@ -60,24 +60,39 @@ func (e *Event) Range() Range {
// Note: this method intended to be used for already open subscriptions and recently received events.
// For new subscriptions and queries for stored data use the database query and don't use this to verify the result.
func (e *Event) Match(f Filter) bool {
if !slices.Contains(f.IDs, e.ID) ||
!slices.Contains(f.Authors, e.PublicKey) ||
!slices.Contains(f.Kinds, e.Kind) {
if e == nil {
return false
}

if e.CreatedAt >= f.Since || e.CreatedAt <= f.Until {
if f.IDs != nil && !ContainsString(e.ID, f.IDs) {
return false
}

for _, val := range e.Tags {
v, ok := f.Tags["#"+val[0]]
if !ok {
continue
}
if f.Authors != nil && !ContainsString(e.PublicKey, f.Authors) {
return false
}

if f.Kinds != nil && !ContainsUint16(e.Kind, f.Kinds) {
return false
}

if !slices.Contains(v, v[1]) {
return false
for f, vals := range f.Tags {
for _, t := range e.Tags {
if f != "#"+t[0] { // should we change it(+)?
return false
}

var containsValue bool
for _, v := range vals {
if v == t[1] {
containsValue = true
break
}
}

if !containsValue {
return false
}
}
}

Expand Down
14 changes: 7 additions & 7 deletions types/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package types

// Filter defined the filter structure based on NIP-01 and NIP-50.
type Filter struct {
IDs []string `json:"ids"`
Authors []string `json:"authors"`
Kinds []uint16 `json:"kinds"`
Tags map[string][]string `json:"tags"`
Since int64 `json:"since"`
Until int64 `json:"until"`
Limit int16 `json:"limit"`
IDs []string `json:"ids"`
Authors []string `json:"authors"`
Kinds []uint16 `json:"kinds"`
Tags map[string]Tag `json:"tags"`
Since int64 `json:"since"`
Until int64 `json:"until"`
Limit int16 `json:"limit"`

// Sould we proxy Searchs to index server and elastic search?
Search string `json:"search"` // Check NIP-50
Expand Down
25 changes: 25 additions & 0 deletions types/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package types

// ContainsString checks if the target is presented in arr.
// This non-generic version of contains is faster than `slices.Contains`.
func ContainsString(target string, arr []string) bool {
for _, s := range arr {
if s == target {
return true
}
}

return false
}

// ContainsUint16 checks if the target is presented in arr.
// This non-generic version of contains is faster than `slices.Contains`.
func ContainsUint16(target uint16, arr []uint16) bool {
for _, s := range arr {
if s == target {
return true
}
}

return false
}

0 comments on commit 94ef082

Please sign in to comment.