nostrdb-rs

nostrdb in rust!
git clone git://jb55.com/nostrdb-rs
Log | Files | Refs | Submodules | README | LICENSE

profile.rs (2567B)


      1 use crate::ndb_profile::{root_as_ndb_profile_record_unchecked, NdbProfileRecord};
      2 use crate::Transaction;
      3 
      4 pub struct ProfileRecord<'a> {
      5     pub record: NdbProfileRecord<'a>,
      6     pub primary_key: u64,
      7     pub transaction: &'a Transaction,
      8 }
      9 
     10 impl<'a> ProfileRecord<'a> {
     11     pub(crate) fn new(
     12         ptr: *mut ::std::os::raw::c_void,
     13         len: usize,
     14         primary_key: u64,
     15         transaction: &'a Transaction,
     16     ) -> ProfileRecord<'a> {
     17         let record = unsafe {
     18             let bytes = std::slice::from_raw_parts(ptr as *const u8, len);
     19             root_as_ndb_profile_record_unchecked(bytes)
     20         };
     21         ProfileRecord {
     22             record,
     23             transaction,
     24             primary_key,
     25         }
     26     }
     27 }
     28 
     29 #[cfg(test)]
     30 mod tests {
     31     use super::*;
     32 
     33     #[test]
     34     fn profile_record_words() {
     35         use crate::config::Config;
     36         use crate::ndb::Ndb;
     37         use crate::test_util;
     38 
     39         let db = "target/testdbs/profile_record_works";
     40 
     41         {
     42             let cfg = Config::new();
     43             let ndb = Ndb::new(&db, &cfg).unwrap();
     44             let _ = ndb.process_event(r#"["EVENT","nostril-query",{"content":"{\"nip05\":\"_@jb55.com\",\"website\":\"https://damus.io\",\"name\":\"jb55\",\"about\":\"I made damus, npubs and zaps. banned by apple & the ccp. my notes are not for sale.\",\"lud16\":\"jb55@sendsats.lol\",\"banner\":\"https://nostr.build/i/3d6f22d45d95ecc2c19b1acdec57aa15f2dba9c423b536e26fc62707c125f557.jpg\",\"display_name\":\"Will\",\"picture\":\"https://cdn.jb55.com/img/red-me.jpg\"}","created_at":1700855305,"id":"cad04d11f7fa9c36d57400baca198582dfeb94fa138366c4469e58da9ed60051","kind":0,"pubkey":"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245","sig":"7a15e379ff27318460172b4a1d55a13e064c5007d05d5a188e7f60e244a9ed08996cb7676058b88c7a91ae9488f8edc719bc966cb5bf1eb99be44cdb745f915f","tags":[]}]"#);
     45         }
     46 
     47         // Initialize ndb
     48         {
     49             let cfg = Config::new();
     50             let ndb = Ndb::new(&db, &cfg).expect("db open");
     51             let mut txn = Transaction::new(&ndb).expect("new txn");
     52 
     53             let pk =
     54                 hex::decode("32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")
     55                     .expect("hex decode");
     56             let pr = ndb
     57                 .get_profile_by_pubkey(&mut txn, &pk.try_into().expect("bytes"))
     58                 .expect("profile record");
     59 
     60             let profile = pr.record.profile().unwrap();
     61             assert_eq!(Some("jb55"), profile.name());
     62         }
     63 
     64         test_util::cleanup_db(db);
     65     }
     66 }