lib

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

encode.rs (3665B)


      1 #[cfg(not(feature = "std"))]
      2 use alloc::vec;
      3 #[cfg(not(feature = "std"))]
      4 use alloc::{
      5     format,
      6     string::{String, ToString},
      7     vec::Vec,
      8 };
      9 
     10 use radroots_event::envelope::kind::KIND_MESSAGE_FILE;
     11 use radroots_event::social::message_file::{MessageFile, MessageFileDimensions};
     12 
     13 use crate::error::EventEncodeError;
     14 use crate::message::tags::{build_recipient_tags, build_reply_tag, build_subject_tag};
     15 use radroots_event::wire::Nip01EventWireParts;
     16 
     17 const DEFAULT_KIND: u32 = KIND_MESSAGE_FILE;
     18 
     19 fn validate_required(value: &str, field: &'static str) -> Result<(), EventEncodeError> {
     20     if value.trim().is_empty() {
     21         return Err(EventEncodeError::EmptyRequiredField(field));
     22     }
     23     Ok(())
     24 }
     25 
     26 fn push_required_tag(
     27     tags: &mut Vec<Vec<String>>,
     28     key: &'static str,
     29     value: &str,
     30     field: &'static str,
     31 ) -> Result<(), EventEncodeError> {
     32     validate_required(value, field)?;
     33     tags.push(vec![key.to_string(), value.to_string()]);
     34     Ok(())
     35 }
     36 
     37 fn push_optional_tag(
     38     tags: &mut Vec<Vec<String>>,
     39     key: &'static str,
     40     value: &Option<String>,
     41     field: &'static str,
     42 ) -> Result<(), EventEncodeError> {
     43     if let Some(value) = value {
     44         validate_required(value, field)?;
     45         tags.push(vec![key.to_string(), value.clone()]);
     46     }
     47     Ok(())
     48 }
     49 
     50 fn push_dimensions_tag(tags: &mut Vec<Vec<String>>, dimensions: &Option<MessageFileDimensions>) {
     51     if let Some(dimensions) = dimensions {
     52         tags.push(vec![
     53             "dim".to_string(),
     54             format!("{}x{}", dimensions.w, dimensions.h),
     55         ]);
     56     }
     57 }
     58 
     59 pub fn message_file_build_tags(
     60     message: &MessageFile,
     61 ) -> Result<Vec<Vec<String>>, EventEncodeError> {
     62     let mut tags = build_recipient_tags(&message.recipients)?;
     63     if let Some(tag) = build_reply_tag(&message.reply_to)? {
     64         tags.push(tag);
     65     }
     66     if let Some(tag) = build_subject_tag(&message.subject)? {
     67         tags.push(tag);
     68     }
     69 
     70     push_required_tag(&mut tags, "file-type", &message.file_type, "file_type")?;
     71     push_required_tag(
     72         &mut tags,
     73         "encryption-algorithm",
     74         &message.encryption_algorithm,
     75         "encryption_algorithm",
     76     )?;
     77     push_required_tag(
     78         &mut tags,
     79         "decryption-key",
     80         &message.decryption_key,
     81         "decryption_key",
     82     )?;
     83     push_required_tag(
     84         &mut tags,
     85         "decryption-nonce",
     86         &message.decryption_nonce,
     87         "decryption_nonce",
     88     )?;
     89     push_required_tag(&mut tags, "x", &message.encrypted_hash, "encrypted_hash")?;
     90 
     91     push_optional_tag(&mut tags, "ox", &message.original_hash, "original_hash")?;
     92     if let Some(size) = message.size {
     93         tags.push(vec!["size".to_string(), size.to_string()]);
     94     }
     95     push_dimensions_tag(&mut tags, &message.dimensions);
     96     push_optional_tag(&mut tags, "blurhash", &message.blurhash, "blurhash")?;
     97     push_optional_tag(&mut tags, "thumb", &message.thumb, "thumb")?;
     98     for fallback in &message.fallbacks {
     99         validate_required(fallback, "fallback")?;
    100         tags.push(vec!["fallback".to_string(), fallback.clone()]);
    101     }
    102 
    103     Ok(tags)
    104 }
    105 
    106 pub fn to_wire_parts(message: &MessageFile) -> Result<Nip01EventWireParts, EventEncodeError> {
    107     validate_required(&message.file_url, "file_url")?;
    108     let tags = message_file_build_tags(message)?;
    109     Ok(Nip01EventWireParts {
    110         kind: DEFAULT_KIND,
    111         content: message.file_url.clone(),
    112         tags,
    113     })
    114 }
    115 
    116 pub fn to_wire_parts_with_kind(
    117     message: &MessageFile,
    118     kind: u32,
    119 ) -> Result<Nip01EventWireParts, EventEncodeError> {
    120     if kind != DEFAULT_KIND {
    121         return Err(EventEncodeError::InvalidKind(kind));
    122     }
    123     to_wire_parts(message)
    124 }