Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial tags and signature support #3

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* automatically generated by rust-bindgen 0.69.2 */
/* automatically generated by rust-bindgen 0.69.1 */

#[repr(C)]
#[derive(Default)]
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ mod config;
mod error;
mod filter;
mod ndb;
mod ndb_str;
mod note;
mod profile;
mod query;
mod result;
mod subscription;
mod tags;
mod transaction;

pub use block::{Block, BlockType, Blocks, Mention};
Expand All @@ -26,6 +28,7 @@ pub use error::Error;
pub use filter::Filter;
pub use ndb::Ndb;
pub use ndb_profile::{NdbProfile, NdbProfileRecord};
pub use ndb_str::{NdbStr, NdbStrVariant};
pub use note::{Note, NoteKey};
pub use profile::ProfileRecord;
pub use query::QueryResult;
Expand Down
16 changes: 8 additions & 8 deletions src/ndb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl Ndb {
}
}

pub fn poll_for_notes(&self, sub: &Subscription, max_notes: u32) -> Vec<u64> {
pub fn poll_for_notes(&self, sub: &Subscription, max_notes: u32) -> Vec<NoteKey> {
let mut vec = vec![];
vec.reserve_exact(max_notes as usize);
let sub_id = sub.id;
Expand All @@ -144,14 +144,14 @@ impl Ndb {
vec.set_len(res as usize);
};

vec
vec.into_iter().map(|n| NoteKey::new(n)).collect()
}

pub async fn wait_for_notes(&self, sub: &Subscription, max_notes: u32) -> Result<Vec<u64>> {
pub async fn wait_for_notes(&self, sub: &Subscription, max_notes: u32) -> Result<Vec<NoteKey>> {
let ndb = self.clone();
let sub_id = sub.id;
let handle = task::spawn_blocking(move || {
let mut vec = vec![];
let mut vec: Vec<u64> = vec![];
vec.reserve_exact(max_notes as usize);
let res = unsafe {
bindings::ndb_wait_for_notes(
Expand All @@ -172,7 +172,7 @@ impl Ndb {
});

match handle.await {
Ok(Ok(res)) => Ok(res),
Ok(Ok(res)) => Ok(res.into_iter().map(|n| NoteKey::new(n)).collect()),
Ok(Err(err)) => Err(err),
Err(_) => Err(Error::SubscriptionError),
}
Expand Down Expand Up @@ -335,7 +335,7 @@ mod tests {
let waiter = ndb.wait_for_notes(&sub, 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");
assert_eq!(res, vec![1]);
assert_eq!(res, vec![NoteKey::new(1)]);
let txn = Transaction::new(&ndb).expect("txn");
let res = ndb.query(&txn, filters, 1).expect("query ok");
assert_eq!(res.len(), 1);
Expand All @@ -360,7 +360,7 @@ mod tests {
let waiter = ndb.wait_for_notes(&sub, 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");
assert_eq!(res, vec![1]);
assert_eq!(res, vec![NoteKey::new(1)]);
}
}

Expand All @@ -383,7 +383,7 @@ mod tests {
std::thread::sleep(std::time::Duration::from_millis(100));
// now we should have something
let res = ndb.poll_for_notes(&sub, 1);
assert_eq!(res, vec![1]);
assert_eq!(res, vec![NoteKey::new(1)]);
}
}

Expand Down
54 changes: 54 additions & 0 deletions src/ndb_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::{bindings, Note};

pub struct NdbStr<'a> {
ndb_str: bindings::ndb_str,
note: &'a Note<'a>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum NdbStrVariant<'a> {
Id(&'a [u8; 32]),
Str(&'a str),
}

impl bindings::ndb_str {
pub fn str(&self) -> *const ::std::os::raw::c_char {
unsafe { self.__bindgen_anon_1.str_ }
}

pub fn id(&self) -> *const ::std::os::raw::c_uchar {
unsafe { self.__bindgen_anon_1.id }
}
}

impl<'a> NdbStr<'a> {
pub fn note(&self) -> &'a Note<'a> {
self.note
}

pub(crate) fn new(ndb_str: bindings::ndb_str, note: &'a Note<'a>) -> Self {
NdbStr { ndb_str, note }
}

pub fn len(&self) -> usize {
if self.ndb_str.flag == (bindings::NDB_PACKED_ID as u8) {
32
} else {
unsafe { libc::strlen(self.ndb_str.str()) }
}
}

pub fn variant(&self) -> NdbStrVariant<'a> {
if self.ndb_str.flag == (bindings::NDB_PACKED_ID as u8) {
unsafe { NdbStrVariant::Id(&*(self.ndb_str.id() as *const [u8; 32])) }
} else {
let s = unsafe {
let byte_slice =
std::slice::from_raw_parts(self.ndb_str.str() as *const u8, self.len());
std::str::from_utf8_unchecked(byte_slice)
};

NdbStrVariant::Str(s)
}
}
}
15 changes: 14 additions & 1 deletion src/note.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::bindings;
use crate::tags::Tags;
use crate::transaction::Transaction;
use std::hash::Hash;

Expand All @@ -15,7 +16,7 @@ impl NoteKey {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum Note<'a> {
/// A note in-memory outside of nostrdb. This note is a pointer to a note in
/// memory and will be free'd when [Drop]ped. Method such as [Note::from_json]
Expand Down Expand Up @@ -135,6 +136,18 @@ impl<'a> Note<'a> {
pub fn kind(&self) -> u32 {
unsafe { bindings::ndb_note_kind(self.as_ptr()) }
}

pub fn tags(&'a self) -> Tags<'a> {
let tags = unsafe { bindings::ndb_note_tags(self.as_ptr()) };
Tags::new(tags, self)
}

pub fn sig(&self) -> &'a [u8; 32] {
unsafe {
let ptr = bindings::ndb_note_sig(self.as_ptr());
&*(ptr as *const [u8; 32])
}
}
}

impl<'a> Drop for Note<'a> {
Expand Down
Loading
Loading