Skip to content

Commit

Permalink
filter: add additional builder methods
Browse files Browse the repository at this point in the history
These are needed in notedeck atm

Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Feb 7, 2024
1 parent 53bc80b commit 7246513
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ mod tests {
#[test]
fn note_blocks_work() {
let db = "target/testdbs/note_blocks";
test_util::cleanup_db(&db);

{
let ndb = Ndb::new(db, &Config::new()).expect("ndb");
Expand Down
67 changes: 43 additions & 24 deletions src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::bindings;
use crate::Note;
use std::ffi::CString;
use std::os::raw::c_char;
use std::ptr::null_mut;
Expand Down Expand Up @@ -74,59 +75,77 @@ impl Filter {
};
}

fn start_kind_field(&self) {
unsafe {
bindings::ndb_filter_start_field(
self.as_mut_ptr(),
bindings::ndb_filter_fieldtype_NDB_FILTER_KINDS,
)
};
fn start_field(&self, field: bindings::ndb_filter_fieldtype) {
unsafe { bindings::ndb_filter_start_field(self.as_mut_ptr(), field) };
}

fn start_tags_field(&self, tag: char) {
unsafe { bindings::ndb_filter_start_tag_field(self.as_mut_ptr(), tag as i8) };
}

fn start_kinds_field(&self) {
self.start_field(bindings::ndb_filter_fieldtype_NDB_FILTER_KINDS);
}

fn start_authors_field(&self) {
self.start_field(bindings::ndb_filter_fieldtype_NDB_FILTER_AUTHORS);
}

fn start_events_field(&self) {
self.start_tags_field('e');
}

fn start_pubkeys_field(&self) {
self.start_tags_field('p');
}

fn end_field(&self) {
unsafe { bindings::ndb_filter_end_field(self.as_mut_ptr()) }
}

pub fn authors(self, authors: Vec<[u8; 32]>) -> Filter {
unsafe {
bindings::ndb_filter_start_field(
self.as_mut_ptr(),
bindings::ndb_filter_fieldtype_NDB_FILTER_AUTHORS,
);
};

pub fn authors<'a>(self, authors: Vec<&'a [u8; 32]>) -> Filter {
self.start_authors_field();
for author in authors {
self.add_id_element(&author);
self.add_id_element(author);
}

self.end_field();

self
}

fn start_tag_field(&self, tag: char) {
unsafe {
bindings::ndb_filter_start_tag_field(self.as_mut_ptr(), tag as u8 as c_char);
}
unsafe { bindings::ndb_filter_start_tag_field(self.as_mut_ptr(), tag as u8 as c_char) };
}

pub fn kinds(self, kinds: Vec<u64>) -> Filter {
self.start_kind_field();
self.start_kinds_field();
for kind in kinds {
self.add_int_element(kind);
}
self.end_field();
self
}

pub fn hashtags(self, tags: Vec<String>) -> Filter {
self.start_tag_field('t');
pub fn pubkey<'a>(self, pubkeys: Vec<&'a [u8; 32]>) -> Filter {
self.start_pubkeys_field();
for pubkey in pubkeys {
self.add_id_element(pubkey);
}
self.end_field();
self
}

pub fn tags(self, tags: Vec<String>, tag: char) -> Filter {
self.start_tag_field(tag);
for tag in tags {
self.add_str_element(&tag);
}
self.end_field();
self
}

pub fn matches(&self, note: &Note) -> bool {
unsafe { bindings::ndb_filter_matches(self.as_mut_ptr(), note.as_ptr()) != 0 }
}
}

impl Drop for Filter {
Expand Down

0 comments on commit 7246513

Please sign in to comment.