post.rs (2418B)
1 use crate::error::RadrootsNostrError; 2 use crate::types::{ 3 RadrootsNostrEventBuilder, RadrootsNostrEventId, RadrootsNostrFilter, RadrootsNostrKind, 4 RadrootsNostrPublicKey, RadrootsNostrTag, RadrootsNostrTimestamp, 5 }; 6 7 #[cfg(all(feature = "client", feature = "events"))] 8 use crate::client::RadrootsNostrClient; 9 #[cfg(all(feature = "client", feature = "events"))] 10 use core::time::Duration; 11 12 pub fn radroots_nostr_build_post_event(content: impl Into<String>) -> RadrootsNostrEventBuilder { 13 RadrootsNostrEventBuilder::text_note(content) 14 } 15 16 pub fn radroots_nostr_post_events_filter( 17 limit: Option<u16>, 18 since_unix: Option<u64>, 19 ) -> RadrootsNostrFilter { 20 let mut filter = RadrootsNostrFilter::new().kind(RadrootsNostrKind::TextNote); 21 if let Some(limit) = limit { 22 filter = filter.limit(limit.into()); 23 } 24 if let Some(since) = since_unix { 25 filter = filter.since(RadrootsNostrTimestamp::from(since)); 26 } 27 filter 28 } 29 30 pub fn radroots_nostr_build_post_reply_event( 31 parent_event_id_hex: &str, 32 parent_author_hex: &str, 33 content: impl Into<String>, 34 root_event_id_hex: Option<&str>, 35 ) -> Result<RadrootsNostrEventBuilder, RadrootsNostrError> { 36 let parent_id = RadrootsNostrEventId::from_hex(parent_event_id_hex)?; 37 let parent_pubkey = RadrootsNostrPublicKey::from_hex(parent_author_hex)?; 38 let mut tags: Vec<RadrootsNostrTag> = Vec::new(); 39 40 if let Some(root_hex) = root_event_id_hex 41 && !root_hex.is_empty() 42 && let Ok(root_id) = RadrootsNostrEventId::from_hex(root_hex) 43 { 44 tags.push(RadrootsNostrTag::event(root_id)); 45 } 46 47 tags.push(RadrootsNostrTag::event(parent_id)); 48 tags.push(RadrootsNostrTag::public_key(parent_pubkey)); 49 50 Ok(RadrootsNostrEventBuilder::text_note(content).tags(tags)) 51 } 52 53 #[cfg(all(feature = "client", feature = "events"))] 54 pub async fn radroots_nostr_fetch_post_events( 55 client: &RadrootsNostrClient, 56 limit: u16, 57 since_unix: Option<u64>, 58 ) -> Result< 59 Vec<radroots_events_codec::parsed::RadrootsParsedData<radroots_events::post::RadrootsPost>>, 60 RadrootsNostrError, 61 > { 62 let filter = radroots_nostr_post_events_filter(Some(limit), since_unix); 63 64 let events = client.fetch_events(filter, Duration::from_secs(10)).await?; 65 let out = events 66 .into_iter() 67 .map(|ev| crate::event_adapters::to_post_event_metadata(&ev)) 68 .collect(); 69 70 Ok(out) 71 }