post.rs (2876B)
1 //! Typed authoring and filtering for supported Radroots social root events. 2 3 #[cfg(feature = "events")] 4 use crate::error::Error; 5 #[cfg(feature = "events")] 6 use crate::events::sealed::SealedBuilderCore; 7 #[cfg(feature = "events")] 8 use crate::types::{ 9 ExternalSigningRequest, RadrootsNostrEvent, RadrootsNostrKeys, RadrootsNostrPublicKey, 10 }; 11 use crate::types::{RadrootsNostrFilter, RadrootsNostrKind, RadrootsNostrTimestamp}; 12 13 #[cfg(feature = "events")] 14 use radroots_event::post::{AuthoredAsk, AuthoredPhotoUpdate, AuthoredUpdate}; 15 #[cfg(feature = "events")] 16 use radroots_event_codec::authoring::AuthoredEventBody; 17 18 /// A sealed builder for a validated Radroots root post profile. 19 /// 20 /// The wrapper intentionally exposes no raw builder conversion or tag/content 21 /// mutation. Construct it through one of the typed post authoring functions. 22 #[cfg(feature = "events")] 23 #[must_use = "post event builders must be signed or published"] 24 pub struct PostBuilder { 25 inner: SealedBuilderCore, 26 } 27 28 #[cfg(feature = "events")] 29 impl PostBuilder { 30 /// Sets the event timestamp without changing the validated post shape. 31 pub fn custom_created_at(mut self, created_at: RadrootsNostrTimestamp) -> Self { 32 self.inner = self.inner.custom_created_at(created_at); 33 self 34 } 35 36 /// Signs the validated post directly with local keys. 37 pub fn sign_with_keys(self, keys: &RadrootsNostrKeys) -> Result<RadrootsNostrEvent, Error> { 38 self.inner.sign_with_keys(keys) 39 } 40 41 /// Finalizes the exact authored plan for an external signer. 42 pub fn into_external_signing_request( 43 self, 44 public_key: RadrootsNostrPublicKey, 45 ) -> Result<ExternalSigningRequest, Error> { 46 self.inner.into_external_signing_request(public_key) 47 } 48 } 49 50 #[cfg(feature = "events")] 51 pub fn build_update_event(update: &AuthoredUpdate) -> Result<PostBuilder, Error> { 52 Ok(builder_from_body(AuthoredEventBody::from_update(update)?)) 53 } 54 55 #[cfg(feature = "events")] 56 pub fn build_photo_update_event(photo: &AuthoredPhotoUpdate) -> Result<PostBuilder, Error> { 57 Ok(builder_from_body(AuthoredEventBody::from_photo_update( 58 photo, 59 )?)) 60 } 61 62 #[cfg(feature = "events")] 63 pub fn build_ask_event(ask: &AuthoredAsk) -> Result<PostBuilder, Error> { 64 Ok(builder_from_body(AuthoredEventBody::from_ask(ask)?)) 65 } 66 67 pub fn post_events_filter(limit: Option<u16>, since_unix: Option<u64>) -> RadrootsNostrFilter { 68 let mut filter = RadrootsNostrFilter::new().kind(RadrootsNostrKind::TextNote); 69 if let Some(limit) = limit { 70 filter = filter.limit(limit.into()); 71 } 72 if let Some(since) = since_unix { 73 filter = filter.since(RadrootsNostrTimestamp::from(since)); 74 } 75 filter 76 } 77 78 #[cfg(feature = "events")] 79 fn builder_from_body(body: AuthoredEventBody) -> PostBuilder { 80 PostBuilder { 81 inner: SealedBuilderCore::new(body), 82 } 83 }