Skip to content

Commit

Permalink
nostr: add FiltersMatchEvent trait
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 17, 2023
1 parent 3b55bc1 commit 6d3b500
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions crates/nostr/src/message/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,18 @@ impl Filter {
}
}

/// Filters match event trait
pub trait FiltersMatchEvent {
/// Determine if [`Filter`] match the provided [`Event`].
fn match_event(&self, event: &Event) -> bool;
}

impl FiltersMatchEvent for Vec<Filter> {
fn match_event(&self, event: &Event) -> bool {
self.iter().any(|f| f.match_event(event))
}
}

impl JsonUtil for Filter {
type Err = serde_json::Error;
}
Expand Down Expand Up @@ -902,5 +914,19 @@ mod test {
)
.unwrap()]);
assert!(!filter.match_event(&event));

let filters: Vec<Filter> = vec![
// Filter that match
Filter::new()
.author("379e863e")
.kind(Kind::TextNote)
.since(Timestamp::from(1612808000)),
// Filter that not match
Filter::new().events(vec![EventId::from_hex(
"70b10f70c1318967eddf12527799411b1a9780ad9c43858f5e5fcd45486a13a5",
)
.unwrap()]),
];
assert!(filters.match_event(&event));
}
}

0 comments on commit 6d3b500

Please sign in to comment.