commit a1da0b9b98f42b02e3fe4c891ec1f243fe6be215
parent 6e8b9a123e5afa3be8066d5bb39d523a81b6d130
Author: William Casarin <jb55@jb55.com>
Date: Mon, 27 Jan 2025 15:35:53 -0800
Merge remote-tracking branch 'pr/35'
Diffstat:
M | src/ndb.rs | | | 114 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 114 insertions(+), 0 deletions(-)
diff --git a/src/ndb.rs b/src/ndb.rs
@@ -1,6 +1,7 @@
use std::ffi::CString;
use std::ptr;
+use crate::bindings::ndb_search;
use crate::{
bindings, Blocks, Config, Error, Filter, Note, NoteKey, ProfileKey, ProfileRecord, QueryResult,
Result, Subscription, SubscriptionState, SubscriptionStream, Transaction,
@@ -429,6 +430,63 @@ impl Ndb {
))
}
+ pub fn search_profile<'a>(
+ &self,
+ transaction: &'a Transaction,
+ search: &str,
+ limit: u32,
+ ) -> Result<Vec<&'a [u8; 32]>> {
+ let mut results = Vec::new();
+
+ let mut ndb_search = ndb_search {
+ key: std::ptr::null_mut(),
+ profile_key: 0,
+ cursor: std::ptr::null_mut(),
+ };
+
+ let c_query = CString::new(search).map_err(|_| Error::DecodeError)?;
+
+ let success = unsafe {
+ bindings::ndb_search_profile(
+ transaction.as_mut_ptr(),
+ &mut ndb_search as *mut ndb_search,
+ c_query.as_c_str().as_ptr(),
+ )
+ };
+
+ if success == 0 {
+ return Ok(results);
+ }
+
+ // Add the first result
+ if let Some(key) = unsafe { ndb_search.key.as_ref() } {
+ results.push(&key.id);
+ }
+
+ // Iterate through additional results up to the limit
+ let mut remaining = limit;
+ while remaining > 0 {
+ let next_success =
+ unsafe { bindings::ndb_search_profile_next(&mut ndb_search as *mut ndb_search) };
+
+ if next_success == 0 {
+ break;
+ }
+
+ if let Some(key) = unsafe { ndb_search.key.as_ref() } {
+ results.push(&key.id);
+ }
+
+ remaining -= 1;
+ }
+
+ unsafe {
+ bindings::ndb_search_profile_end(&mut ndb_search as *mut ndb_search);
+ }
+
+ Ok(results)
+ }
+
/// Get the underlying pointer to the context in C
pub fn as_ptr(&self) -> *mut bindings::ndb {
self.refs.ndb
@@ -480,6 +538,62 @@ mod tests {
}
#[tokio::test]
+ async fn search_profile_works() {
+ let db = "target/testdbs/search_profile";
+ test_util::cleanup_db(&db);
+
+ {
+ let ndb = Ndb::new(db, &Config::new()).expect("ndb");
+
+ let filter = Filter::new().kinds(vec![0]).build();
+ let filters = vec![filter];
+
+ let sub_id = ndb.subscribe(&filters).expect("sub_id");
+ let mut sub = sub_id.stream(&ndb).notes_per_await(1);
+ ndb.process_event(r#"["EVENT","b",{ "id": "0b9f0e14727733e430dcb00c69b12a76a1e100f419ce369df837f7eb33e4523c", "pubkey": "3f770d65d3a764a9c5cb503ae123e62ec7598ad035d836e2a810f3877a745b24", "created_at": 1736785355, "kind": 0, "tags": [ [ "alt", "User profile for Derek Ross" ], [ "i", "twitter:derekmross", "1634343988407726081" ], [ "i", "github:derekross", "3edaf845975fa4500496a15039323fa3I" ] ], "content": "{\"about\":\"Building NostrPlebs.com and NostrNests.com. The purple pill helps the orange pill go down. Nostr is the social glue that binds all of your apps together.\",\"banner\":\"https://i.nostr.build/O2JE.jpg\",\"display_name\":\"Derek Ross\",\"lud16\":\"derekross@strike.me\",\"name\":\"Derek Ross\",\"nip05\":\"derekross@nostrplebs.com\",\"picture\":\"https://i.nostr.build/MVIJ6OOFSUzzjVEc.jpg\",\"website\":\"https://nostrplebs.com\",\"created_at\":1707238393}", "sig": "51e1225ccaf9b6739861dc218ac29045b09d5cf3a51b0ac6ea64bd36827d2d4394244e5f58a4e4a324c84eeda060e1a27e267e0d536e5a0e45b0b6bdc2c43bbc"}]"#).unwrap();
+ ndb.process_event(r#"["EVENT","b",{ "id": "232a02ec7e1b2febf85370b52ed49bf34e2701c385c3d563511508dcf0767bcf", "pubkey": "4a0510f26880d40e432f4865cb5714d9d3c200ca6ebb16b418ae6c555f574967", "created_at": 1736017863, "kind": 0, "tags": [ [ "client", "Damus Notedeck" ] ], "content": "{\"display_name\":\"KernelKind\",\"name\":\"KernelKind\",\"about\":\"hello from notedeck!\",\"lud16\":\"kernelkind@getalby.com\"}", "sig": "18c7dea0da3c30677d6822a31a6dfd9ebc02a18a31d69f0f2ac9ba88409e437d3db0ac433639111df1e4948a6d18451d1582173ee4fcd018d0ec92939f2c1506"}]"#).unwrap();
+ ndb.process_event(r#"["EVENT","b",{ "id": "3e9e3b63a7831f09bf2963616a2440e6f30c6e95adbc7841d59376ec100ae9dc", "pubkey": "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245", "created_at": 1737466417, "kind": 0, "tags": [], "content": "{\"banner\":\"https://nostr.build/i/3d6f22d45d95ecc2c19b1acdec57aa15f2dba9c423b536e26fc62707c125f557.jpg\",\"website\":\"https://damus.io\",\"nip05\":\"_@jb55.com\",\"display_name\":\"\",\"about\":\"I made damus, zaps, and npubs. Bitcoin core, lightning, and nostr dev. \",\"picture\":\"https://cdn.jb55.com/img/red-me.jpg\",\"name\":\"jb55\",\"lud16\":\"jb55@sendsats.lol\"}", "sig": "9cf1c89a4dbb2888e0f5fc300e56f93eb788bd84d3d0f8b52e4ac4abdd92256b0fb694bfd82d917c3923f01e8eac7886bb75c8043dcd9d4e070e4eaa5ab3bd0a"}]"#).unwrap();
+ for _ in 0..3 {
+ let _ = sub.next().await;
+ }
+ let txn = Transaction::new(&ndb).expect("txn");
+
+ let res = ndb.search_profile(&txn, "jb55", 1);
+ assert!(res.is_ok());
+ let res = res.unwrap();
+ assert!(res.len() >= 1);
+ let will_bytes: [u8; 32] = [
+ 0x32, 0xe1, 0x82, 0x76, 0x35, 0x45, 0x0e, 0xbb, 0x3c, 0x5a, 0x7d, 0x12, 0xc1, 0xf8,
+ 0xe7, 0xb2, 0xb5, 0x14, 0x43, 0x9a, 0xc1, 0x0a, 0x67, 0xee, 0xf3, 0xd9, 0xfd, 0x9c,
+ 0x5c, 0x68, 0xe2, 0x45,
+ ];
+ assert_eq!(will_bytes, **res.first().unwrap());
+
+ let res = ndb.search_profile(&txn, "kernel", 1);
+ assert!(res.is_ok());
+ let res = res.unwrap();
+ assert!(res.len() >= 1);
+ let kernelkind_bytes: [u8; 32] = [
+ 0x4a, 0x05, 0x10, 0xf2, 0x68, 0x80, 0xd4, 0x0e, 0x43, 0x2f, 0x48, 0x65, 0xcb, 0x57,
+ 0x14, 0xd9, 0xd3, 0xc2, 0x00, 0xca, 0x6e, 0xbb, 0x16, 0xb4, 0x18, 0xae, 0x6c, 0x55,
+ 0x5f, 0x57, 0x49, 0x67,
+ ];
+ assert_eq!(kernelkind_bytes, **res.first().unwrap());
+
+ let res = ndb.search_profile(&txn, "Derek", 1);
+ assert!(res.is_ok());
+ let res = res.unwrap();
+ assert!(res.len() >= 1);
+ let derek_bytes: [u8; 32] = [
+ 0x3f, 0x77, 0x0d, 0x65, 0xd3, 0xa7, 0x64, 0xa9, 0xc5, 0xcb, 0x50, 0x3a, 0xe1, 0x23,
+ 0xe6, 0x2e, 0xc7, 0x59, 0x8a, 0xd0, 0x35, 0xd8, 0x36, 0xe2, 0xa8, 0x10, 0xf3, 0x87,
+ 0x7a, 0x74, 0x5b, 0x24,
+ ];
+ assert_eq!(derek_bytes, **res.first().unwrap());
+ }
+ }
+
+ #[tokio::test]
async fn subscribe_event_works() {
let db = "target/testdbs/subscribe";
test_util::cleanup_db(&db);