Skip to content

Commit

Permalink
subscriptions: support filter groups
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Feb 7, 2024
1 parent 07c64d6 commit a8f8fab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 11 additions & 5 deletions src/ndb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
}
}
Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/subscription.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Filter;

pub struct Subscription {
pub filter: Filter,
pub filters: Vec<Filter>,
pub id: u64,
}

0 comments on commit a8f8fab

Please sign in to comment.