lib

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

mod.rs (2186B)


      1 #[cfg(feature = "events")]
      2 pub mod application_handler;
      3 pub mod jobs;
      4 pub mod metadata;
      5 pub mod post;
      6 
      7 extern crate alloc;
      8 use alloc::{string::String, vec::Vec};
      9 
     10 use crate::error::RadrootsNostrError;
     11 use crate::types::{
     12     RadrootsNostrEventBuilder, RadrootsNostrKind, RadrootsNostrTag, RadrootsNostrTagKind,
     13 };
     14 
     15 pub fn radroots_nostr_build_event(
     16     kind_u32: u32,
     17     content: impl Into<String>,
     18     tag_slices: Vec<Vec<String>>,
     19 ) -> Result<RadrootsNostrEventBuilder, RadrootsNostrError> {
     20     let mut tags: Vec<RadrootsNostrTag> = Vec::new();
     21     for mut s in tag_slices {
     22         if s.is_empty() {
     23             continue;
     24         }
     25         let key = s.remove(0);
     26         let values = s;
     27         tags.push(RadrootsNostrTag::custom(
     28             RadrootsNostrTagKind::Custom(key.into()),
     29             values,
     30         ));
     31     }
     32     let builder =
     33         RadrootsNostrEventBuilder::new(RadrootsNostrKind::Custom(kind_u32 as u16), content.into())
     34             .tags(tags)
     35             .allow_self_tagging();
     36     Ok(builder)
     37 }
     38 
     39 #[cfg(test)]
     40 mod tests {
     41     use super::radroots_nostr_build_event;
     42     use crate::test_fixtures::FIXTURE_ALICE_PUBLIC_KEY_HEX;
     43     use crate::types::{RadrootsNostrPublicKey, RadrootsNostrTagKind};
     44 
     45     #[test]
     46     fn build_event_preserves_self_p_tag() {
     47         let pubkey_hex = FIXTURE_ALICE_PUBLIC_KEY_HEX;
     48         let pubkey = RadrootsNostrPublicKey::from_hex(pubkey_hex).expect("pubkey");
     49         let tags = vec![
     50             vec!["x".to_string(), "v".to_string()],
     51             vec!["p".to_string(), pubkey_hex.to_string()],
     52         ];
     53 
     54         let builder = radroots_nostr_build_event(1, "test", tags).expect("builder");
     55         let event = builder.build(pubkey);
     56 
     57         let has_self_tag = event.tags.iter().any(|tag| {
     58             tag.kind() == RadrootsNostrTagKind::p() && tag.content() == Some(pubkey_hex)
     59         });
     60         assert!(has_self_tag);
     61         let has_other_self_tag = event.tags.iter().any(|tag| {
     62             tag.kind() == RadrootsNostrTagKind::p()
     63                 && tag.content()
     64                     == Some("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
     65         });
     66         assert!(!has_other_self_tag);
     67     }
     68 }