From 6d3b5000208f7d325bc42aa31b8d350ee6a4e109 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Tue, 17 Oct 2023 11:03:48 +0200 Subject: [PATCH] nostr: add `FiltersMatchEvent` trait --- crates/nostr/src/message/subscription.rs | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/crates/nostr/src/message/subscription.rs b/crates/nostr/src/message/subscription.rs index 61ddc499c..b56af322a 100644 --- a/crates/nostr/src/message/subscription.rs +++ b/crates/nostr/src/message/subscription.rs @@ -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 { + fn match_event(&self, event: &Event) -> bool { + self.iter().any(|f| f.match_event(event)) + } +} + impl JsonUtil for Filter { type Err = serde_json::Error; } @@ -902,5 +914,19 @@ mod test { ) .unwrap()]); assert!(!filter.match_event(&event)); + + let filters: Vec = 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)); } }