commit d4a0191aeaa8347aed23fee9214d52f49f4bb755
parent 73147828a76d752427a26361497da3d8265e28fc
Author: William Casarin <jb55@jb55.com>
Date: Wed, 7 Feb 2024 13:11:27 -0800
filter: add since and limit
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
2 files changed, 24 insertions(+), 2 deletions(-)
diff --git a/src/filter.rs b/src/filter.rs
@@ -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');
}
@@ -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 }
}
diff --git a/src/ndb.rs b/src/ndb.rs
@@ -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");
@@ -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);