nostrdb-rs

nostrdb in rust!
git clone git://jb55.com/nostrdb-rs
Log | Files | Refs | Submodules | README | LICENSE

commit a8f8fabdc2f1eebe939622000c030639b4b6d51f
parent 07c64d6f9956c3898ed0555c38aa5ed5bd482cd2
Author: William Casarin <jb55@jb55.com>
Date:   Wed,  7 Feb 2024 15:44:26 -0800

subscriptions: support filter groups

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Msrc/filter.rs | 2+-
Msrc/ndb.rs | 16+++++++++++-----
Msrc/subscription.rs | 2+-
3 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/src/filter.rs b/src/filter.rs @@ -8,7 +8,7 @@ use tracing::debug; #[derive(Debug)] pub struct Filter { - data: bindings::ndb_filter, + pub data: bindings::ndb_filter, } impl bindings::ndb_filter { diff --git a/src/ndb.rs b/src/ndb.rs @@ -81,13 +81,19 @@ impl Ndb { Ok(()) } - pub fn subscribe(&self, filter: Filter) -> Result<Subscription> { + pub fn subscribe(&self, filters: Vec<Filter>) -> Result<Subscription> { unsafe { - let id = bindings::ndb_subscribe(self.as_ptr(), filter.as_mut_ptr(), 1); + let mut ndb_filters: Vec<bindings::ndb_filter> = + filters.iter().map(|a| a.data).collect(); + let id = bindings::ndb_subscribe( + self.as_ptr(), + ndb_filters.as_mut_ptr(), + filters.len() as i32, + ); if id == 0 { Err(Error::SubscriptionError) } else { - Ok(Subscription { filter, id }) + Ok(Subscription { filters, id }) } } } @@ -260,7 +266,7 @@ mod tests { let mut filter = Filter::new(); filter.kinds(vec![1]); - let sub = ndb.subscribe(filter).expect("sub_id"); + let sub = ndb.subscribe(vec![filter]).expect("sub_id"); let waiter = ndb.wait_for_notes(&sub, 1); ndb.process_event(r#"["EVENT","b",{"id": "702555e52e82cc24ad517ba78c21879f6e47a7c0692b9b20df147916ae8731a3","pubkey": "32bf915904bfde2d136ba45dde32c88f4aca863783999faea2e847a8fafd2f15","created_at": 1702675561,"kind": 1,"tags": [],"content": "hello, world","sig": "2275c5f5417abfd644b7bc74f0388d70feb5d08b6f90fa18655dda5c95d013bfbc5258ea77c05b7e40e0ee51d8a2efa931dc7a0ec1db4c0a94519762c6625675"}]"#).expect("process ok"); let res = waiter.await.expect("await ok"); @@ -279,7 +285,7 @@ mod tests { let mut filter = Filter::new(); filter.kinds(vec![1]); - let sub = ndb.subscribe(filter).expect("sub_id"); + let sub = ndb.subscribe(vec![filter]).expect("sub_id"); ndb.process_event(r#"["EVENT","b",{"id": "702555e52e82cc24ad517ba78c21879f6e47a7c0692b9b20df147916ae8731a3","pubkey": "32bf915904bfde2d136ba45dde32c88f4aca863783999faea2e847a8fafd2f15","created_at": 1702675561,"kind": 1,"tags": [],"content": "hello, world","sig": "2275c5f5417abfd644b7bc74f0388d70feb5d08b6f90fa18655dda5c95d013bfbc5258ea77c05b7e40e0ee51d8a2efa931dc7a0ec1db4c0a94519762c6625675"}]"#).expect("process ok"); // this is too fast, we should have nothing let res = ndb.poll_for_notes(&sub, 1); diff --git a/src/subscription.rs b/src/subscription.rs @@ -1,6 +1,6 @@ use crate::Filter; pub struct Subscription { - pub filter: Filter, + pub filters: Vec<Filter>, pub id: u64, }