authored.rs (2252B)
1 #[cfg(not(feature = "std"))] 2 use alloc::{ 3 string::{String, ToString}, 4 vec, 5 vec::Vec, 6 }; 7 8 use radroots_event::{ 9 envelope::kind::KIND_POST, 10 post::reply::{AuthoredNip10Reply, Nip10ReplyReference}, 11 wire::Nip01EventWireParts, 12 }; 13 14 /// Builds deterministic unsigned kind-1 wire parts for a strict marked NIP-10 15 /// reply. 16 pub fn authored_nip10_reply_to_wire_parts(reply: &AuthoredNip10Reply) -> Nip01EventWireParts { 17 let mut tags = Vec::with_capacity(if reply.parent().is_some() { 4 } else { 2 }); 18 tags.push(event_tag(reply.root(), "root")); 19 if let Some(parent) = reply.parent() { 20 tags.push(event_tag(parent, "reply")); 21 } 22 tags.push(public_key_tag(reply.root())); 23 if let Some(parent) = reply 24 .parent() 25 .filter(|parent| parent.author() != reply.root().author()) 26 { 27 tags.push(public_key_tag(parent)); 28 } 29 Nip01EventWireParts { 30 kind: KIND_POST, 31 content: reply.content().to_string(), 32 tags, 33 } 34 } 35 36 fn event_tag(reference: &Nip10ReplyReference, marker: &str) -> Vec<String> { 37 vec![ 38 "e".to_string(), 39 reference.event_id().to_hex(), 40 reference.relay_or_empty().to_string(), 41 marker.to_string(), 42 ] 43 } 44 45 fn public_key_tag(reference: &Nip10ReplyReference) -> Vec<String> { 46 vec!["p".to_string(), reference.author().to_hex()] 47 } 48 49 #[cfg(test)] 50 mod tests { 51 use super::*; 52 53 fn h(character: char) -> String { 54 crate::test_fixtures::fixture_public_key_hex(character) 55 } 56 57 #[test] 58 fn nested_reply_deduplicates_equal_reference_authors() { 59 let author = h('b'); 60 let root = Nip10ReplyReference::parse(h('a'), &author, None).expect("root reference"); 61 let parent = Nip10ReplyReference::parse(h('c'), &author, None).expect("parent reference"); 62 let reply = AuthoredNip10Reply::nested("Reply", root, parent).expect("nested reply"); 63 64 assert_eq!( 65 authored_nip10_reply_to_wire_parts(&reply).tags, 66 vec![ 67 vec!["e".to_string(), h('a'), String::new(), "root".to_string(),], 68 vec!["e".to_string(), h('c'), String::new(), "reply".to_string(),], 69 vec!["p".to_string(), author], 70 ] 71 ); 72 } 73 }