Skip to content

Commit

Permalink
filter: add since and limit
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 7314782 commit d4a0191
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ impl Filter {
self.start_field(bindings::ndb_filter_fieldtype_NDB_FILTER_AUTHORS);
}

fn start_since_field(&self) {
self.start_field(bindings::ndb_filter_fieldtype_NDB_FILTER_SINCE);
}

fn start_limit_field(&self) {
self.start_field(bindings::ndb_filter_fieldtype_NDB_FILTER_LIMIT);
}

fn start_events_field(&self) {
self.start_tags_field('e');
}
Expand Down Expand Up @@ -143,6 +151,20 @@ impl Filter {
self
}

pub fn since(self, since: u64) -> Filter {
self.start_since_field();
self.add_int_element(since);
self.end_field();
self
}

pub fn limit(self, limit: u64) -> Filter {
self.start_since_field();
self.add_int_element(limit);
self.end_field();
self
}

pub fn matches(&self, note: &Note) -> bool {
unsafe { bindings::ndb_filter_matches(self.as_mut_ptr(), note.as_ptr()) != 0 }
}
Expand Down
4 changes: 2 additions & 2 deletions src/ndb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ mod tests {
{
let ndb = Ndb::new(db, &Config::new()).expect("ndb");
let filter = Filter::new().kinds(vec![1]);
let sub_id = ndb.subscribe(filter).expect("sub_id");
let sub_id = ndb.subscribe(&filter).expect("sub_id");
let waiter = ndb.wait_for_notes(sub_id, 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 @@ -269,7 +269,7 @@ mod tests {
{
let ndb = Ndb::new(db, &Config::new()).expect("ndb");
let filter = Filter::new().kinds(vec![1]);
let sub_id = ndb.subscribe(filter).expect("sub_id");
let sub_id = ndb.subscribe(&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_id, 1);
Expand Down

0 comments on commit d4a0191

Please sign in to comment.