From 887fc28ab237070d01c0042a6f1d753c8638f5be Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Sat, 26 Feb 2022 09:15:45 -0600 Subject: [PATCH] fix: until filters in subscriptions now used --- src/subscription.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/subscription.rs b/src/subscription.rs index 20767b4..21f0281 100644 --- a/src/subscription.rs +++ b/src/subscription.rs @@ -214,6 +214,7 @@ impl ReqFilter { // self.id.as_ref().map(|v| v == &event.id).unwrap_or(true) self.ids_match(event) && self.since.map(|t| event.created_at > t).unwrap_or(true) + && self.until.map(|t| event.created_at < t).unwrap_or(true) && self.kind_match(event.kind) && self.authors_match(event) && self.tag_match(event) @@ -321,6 +322,50 @@ mod tests { Ok(()) } + #[test] + fn interest_until() -> Result<()> { + // subscription with a filter for ID and time + let s: Subscription = + serde_json::from_str(r#"["REQ","xyz",{"ids": ["abc"], "until": 1000}]"#)?; + let e = Event { + id: "abc".to_owned(), + pubkey: "".to_owned(), + created_at: 50, + kind: 0, + tags: Vec::new(), + content: "".to_owned(), + sig: "".to_owned(), + tagidx: None, + }; + assert!(s.interested_in_event(&e)); + Ok(()) + } + + #[test] + fn interest_range() -> Result<()> { + // subscription with a filter for ID and time + let s_in: Subscription = + serde_json::from_str(r#"["REQ","xyz",{"ids": ["abc"], "since": 100, "until": 200}]"#)?; + let s_before: Subscription = + serde_json::from_str(r#"["REQ","xyz",{"ids": ["abc"], "since": 100, "until": 140}]"#)?; + let s_after: Subscription = + serde_json::from_str(r#"["REQ","xyz",{"ids": ["abc"], "since": 160, "until": 200}]"#)?; + let e = Event { + id: "abc".to_owned(), + pubkey: "".to_owned(), + created_at: 150, + kind: 0, + tags: Vec::new(), + content: "".to_owned(), + sig: "".to_owned(), + tagidx: None, + }; + assert!(s_in.interested_in_event(&e)); + assert!(!s_before.interested_in_event(&e)); + assert!(!s_after.interested_in_event(&e)); + Ok(()) + } + #[test] fn interest_time_and_id() -> Result<()> { // subscription with a filter for ID and time