lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

commit 3b30d297f77991b2a0828fec7ca1a55d176ce466
parent 575dccbc658886baca40afae872b83659fe22b76
Author: triesap <tyson@radroots.org>
Date:   Mon,  6 Oct 2025 18:30:50 +0100

net-core: restructure nostr_client with nested events module

Diffstat:
Acrates/net-core/src/nostr_client/events/post.rs | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acrates/net-core/src/nostr_client/events/profile.rs | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/net-core/src/nostr_client/mod.rs | 6++++--
Dcrates/net-core/src/nostr_client/posts.rs | 90-------------------------------------------------------------------------------
Dcrates/net-core/src/nostr_client/profile.rs | 69---------------------------------------------------------------------
5 files changed, 163 insertions(+), 161 deletions(-)

diff --git a/crates/net-core/src/nostr_client/events/post.rs b/crates/net-core/src/nostr_client/events/post.rs @@ -0,0 +1,90 @@ +use crate::error::{NetError, Result}; +use radroots_events::post::models::RadrootsPostEventMetadata; + +use crate::nostr_client::manager::NostrClientManager; + +impl NostrClientManager { + pub async fn publish_post_event(&self, content: String) -> Result<String> { + let builder = radroots_nostr::events::post::build_post_event(content); + let out = self + .inner + .client + .send_event_builder(builder) + .await + .map_err(|e| NetError::Msg(e.to_string()))?; + Ok(out.val.to_string()) + } + + pub fn publish_post_event_blocking(&self, content: String) -> Result<String> { + let rt = self.inner.rt.clone(); + let this = self.clone(); + rt.block_on(async move { this.publish_post_event(content).await }) + } + + pub async fn publish_post_reply_event( + &self, + parent_event_id_hex: String, + parent_author_hex: String, + content: String, + root_event_id_hex: Option<String>, + ) -> Result<String> { + let builder = radroots_nostr::events::post::build_post_reply_event( + &parent_event_id_hex, + &parent_author_hex, + content, + root_event_id_hex.as_deref(), + ) + .map_err(|e| NetError::Msg(e.to_string()))?; + + let out = self + .inner + .client + .send_event_builder(builder) + .await + .map_err(|e| NetError::Msg(e.to_string()))?; + + Ok(out.val.to_string()) + } + + pub fn publish_post_reply_event_blocking( + &self, + parent_event_id_hex: String, + parent_author_hex: String, + content: String, + root_event_id_hex: Option<String>, + ) -> Result<String> { + let rt = self.inner.rt.clone(); + let this = self.clone(); + rt.block_on(async move { + this.publish_post_reply_event( + parent_event_id_hex, + parent_author_hex, + content, + root_event_id_hex, + ) + .await + }) + } + + pub async fn fetch_post_events( + &self, + limit: u16, + since_unix: Option<u64>, + ) -> Result<Vec<RadrootsPostEventMetadata>> { + let items = + radroots_nostr::events::post::fetch_post_events(&self.inner.client, limit, since_unix) + .await + .map_err(|e| NetError::Msg(e.to_string()))?; + Ok(items) + } + + pub fn fetch_post_events_blocking( + &self, + limit: u16, + since_unix: Option<u64>, + ) -> Result<Vec<RadrootsPostEventMetadata>> { + let rt = self.inner.rt.clone(); + let this = self.clone(); + rt.block_on(async move { this.fetch_post_events(limit, since_unix).await }) + } +} diff --git a/crates/net-core/src/nostr_client/events/profile.rs b/crates/net-core/src/nostr_client/events/profile.rs @@ -0,0 +1,69 @@ +use crate::error::{NetError, Result}; +use radroots_events::profile::models::RadrootsProfileEventMetadata; + +use crate::nostr_client::manager::NostrClientManager; + +impl NostrClientManager { + pub async fn fetch_profile_event( + &self, + author: nostr::PublicKey, + ) -> Result<Option<RadrootsProfileEventMetadata>> { + let ev = radroots_nostr::events::metadata::fetch_metadata_for_author( + &self.inner.client, + author, + core::time::Duration::from_secs(5), + ) + .await + .map_err(|e| NetError::Msg(e.to_string()))?; + if let Some(e) = ev { + if let Some(meta) = radroots_nostr::event_adapters::to_profile_event_metadata(&e) { + return Ok(Some(meta)); + } + return Err(NetError::Msg( + "failed to parse kind:0 metadata content".to_string(), + )); + } + Ok(None) + } + + pub fn fetch_profile_event_blocking( + &self, + author: nostr::PublicKey, + ) -> Result<Option<RadrootsProfileEventMetadata>> { + let rt = self.inner.rt.clone(); + let this = self.clone(); + rt.block_on(async move { this.fetch_profile_event(author).await }) + } + + pub fn publish_profile_event_blocking( + &self, + name: Option<String>, + display_name: Option<String>, + nip05: Option<String>, + about: Option<String>, + ) -> Result<String> { + let rt = self.inner.rt.clone(); + let inner_for_task = self.inner.clone(); + rt.block_on(async move { + let mut md = nostr::Metadata::new(); + if let Some(v) = name { + md = md.name(v); + } + if let Some(v) = display_name { + md = md.display_name(v); + } + if let Some(v) = nip05 { + md = md.nip05(v); + } + if let Some(v) = about { + md = md.about(v); + } + let _ = + radroots_nostr::events::metadata::post_metadata_event(&inner_for_task.client, &md) + .await + .map_err(|e| NetError::Msg(e.to_string()))?; + Ok::<(), NetError>(()) + })?; + Ok("ok".to_string()) + } +} diff --git a/crates/net-core/src/nostr_client/mod.rs b/crates/net-core/src/nostr_client/mod.rs @@ -1,8 +1,10 @@ mod connection; mod inner; mod manager; -mod posts; -mod profile; +mod events { + mod post; + mod profile; +} mod status; pub mod types; diff --git a/crates/net-core/src/nostr_client/posts.rs b/crates/net-core/src/nostr_client/posts.rs @@ -1,90 +0,0 @@ -use crate::error::{NetError, Result}; -use radroots_events::post::models::RadrootsPostEventMetadata; - -use super::manager::NostrClientManager; - -impl NostrClientManager { - pub async fn publish_post_event(&self, content: String) -> Result<String> { - let builder = radroots_nostr::events::post::build_post_event(content); - let out = self - .inner - .client - .send_event_builder(builder) - .await - .map_err(|e| NetError::Msg(e.to_string()))?; - Ok(out.val.to_string()) - } - - pub fn publish_post_event_blocking(&self, content: String) -> Result<String> { - let rt = self.inner.rt.clone(); - let this = self.clone(); - rt.block_on(async move { this.publish_post_event(content).await }) - } - - pub async fn publish_post_reply_event( - &self, - parent_event_id_hex: String, - parent_author_hex: String, - content: String, - root_event_id_hex: Option<String>, - ) -> Result<String> { - let builder = radroots_nostr::events::post::build_post_reply_event( - &parent_event_id_hex, - &parent_author_hex, - content, - root_event_id_hex.as_deref(), - ) - .map_err(|e| NetError::Msg(e.to_string()))?; - - let out = self - .inner - .client - .send_event_builder(builder) - .await - .map_err(|e| NetError::Msg(e.to_string()))?; - - Ok(out.val.to_string()) - } - - pub fn publish_post_reply_event_blocking( - &self, - parent_event_id_hex: String, - parent_author_hex: String, - content: String, - root_event_id_hex: Option<String>, - ) -> Result<String> { - let rt = self.inner.rt.clone(); - let this = self.clone(); - rt.block_on(async move { - this.publish_post_reply_event( - parent_event_id_hex, - parent_author_hex, - content, - root_event_id_hex, - ) - .await - }) - } - - pub async fn fetch_post_events( - &self, - limit: u16, - since_unix: Option<u64>, - ) -> Result<Vec<RadrootsPostEventMetadata>> { - let items = - radroots_nostr::events::post::fetch_post_events(&self.inner.client, limit, since_unix) - .await - .map_err(|e| NetError::Msg(e.to_string()))?; - Ok(items) - } - - pub fn fetch_post_events_blocking( - &self, - limit: u16, - since_unix: Option<u64>, - ) -> Result<Vec<RadrootsPostEventMetadata>> { - let rt = self.inner.rt.clone(); - let this = self.clone(); - rt.block_on(async move { this.fetch_post_events(limit, since_unix).await }) - } -} diff --git a/crates/net-core/src/nostr_client/profile.rs b/crates/net-core/src/nostr_client/profile.rs @@ -1,69 +0,0 @@ -use crate::error::{NetError, Result}; -use radroots_events::profile::models::RadrootsProfileEventMetadata; - -use super::manager::NostrClientManager; - -impl NostrClientManager { - pub async fn fetch_profile_event( - &self, - author: nostr::PublicKey, - ) -> Result<Option<RadrootsProfileEventMetadata>> { - let ev = radroots_nostr::events::metadata::fetch_metadata_for_author( - &self.inner.client, - author, - core::time::Duration::from_secs(5), - ) - .await - .map_err(|e| NetError::Msg(e.to_string()))?; - if let Some(e) = ev { - if let Some(meta) = radroots_nostr::event_adapters::to_profile_event_metadata(&e) { - return Ok(Some(meta)); - } - return Err(NetError::Msg( - "failed to parse kind:0 metadata content".to_string(), - )); - } - Ok(None) - } - - pub fn fetch_profile_event_blocking( - &self, - author: nostr::PublicKey, - ) -> Result<Option<RadrootsProfileEventMetadata>> { - let rt = self.inner.rt.clone(); - let this = self.clone(); - rt.block_on(async move { this.fetch_profile_event(author).await }) - } - - pub fn publish_profile_event_blocking( - &self, - name: Option<String>, - display_name: Option<String>, - nip05: Option<String>, - about: Option<String>, - ) -> Result<String> { - let rt = self.inner.rt.clone(); - let inner_for_task = self.inner.clone(); - rt.block_on(async move { - let mut md = nostr::Metadata::new(); - if let Some(v) = name { - md = md.name(v); - } - if let Some(v) = display_name { - md = md.display_name(v); - } - if let Some(v) = nip05 { - md = md.nip05(v); - } - if let Some(v) = about { - md = md.about(v); - } - let _ = - radroots_nostr::events::metadata::post_metadata_event(&inner_for_task.client, &md) - .await - .map_err(|e| NetError::Msg(e.to_string()))?; - Ok::<(), NetError>(()) - })?; - Ok("ok".to_string()) - } -}