diff --git a/Cargo.lock b/Cargo.lock index 1ab9e1b..2b6b11f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1926,7 +1926,7 @@ checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" [[package]] name = "oolatoocs" -version = "4.4.2" +version = "4.5.0" dependencies = [ "atrium-api", "bsky-sdk", diff --git a/Cargo.toml b/Cargo.toml index 01feb5b..012d14b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "oolatoocs" -version = "4.4.2" +version = "4.5.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/bsky.rs b/src/bsky.rs index 35b2802..a849989 100644 --- a/src/bsky.rs +++ b/src/bsky.rs @@ -1,4 +1,4 @@ -use crate::{config::BlueskyConfig, OolatoocsError}; +use crate::{config::BlueskyConfig, utils::convert_aspect_ratio, OolatoocsError}; use atrium_api::{ app::bsky::feed::post::RecordData, com::atproto::repo::upload_blob::Output, types::string::Datetime, types::string::Language, types::string::RecordKey, @@ -227,7 +227,9 @@ pub async fn generate_media_records( atrium_api::app::bsky::feed::post::RecordEmbedRefs::AppBskyEmbedVideoMain(Box::new( atrium_api::app::bsky::embed::video::MainData { alt: media.description.clone(), - aspect_ratio: None, + aspect_ratio: convert_aspect_ratio( + &media.meta.as_ref().and_then(|m| m.original.clone()), + ), captions: None, video: blob.data.blob, } @@ -248,7 +250,9 @@ pub async fn generate_media_records( .description .clone() .map_or("".to_string(), |v| v.to_owned()), - aspect_ratio: None, + aspect_ratio: convert_aspect_ratio( + &media.meta.as_ref().and_then(|m| m.original.clone()), + ), image: i.data.blob, } }) diff --git a/src/utils.rs b/src/utils.rs index 9970731..8cd6f85 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,7 +1,8 @@ +use atrium_api::{app::bsky::embed::defs::AspectRatioData, types::Object}; use html_escape::decode_html_entities; -use megalodon::entities::status::Tag; +use megalodon::entities::{attachment::MetaSub, status::Tag}; use regex::Regex; -use std::error::Error; +use std::{error::Error, num::NonZeroU64}; /// Generate 2 contents out of 1 if that content is > 300 chars, None else pub fn generate_multi_tweets(content: &str) -> Option<(String, String)> { @@ -110,10 +111,119 @@ fn strip_html_tags(input: &str) -> String { data } +pub fn convert_aspect_ratio(m: &Option) -> Option> { + match m { + Some(ms) => { + if ms.height.is_some_and(|x| x > 0) && ms.width.is_some_and(|x| x > 0) { + Some( + AspectRatioData { + // unwrap is safe here + height: NonZeroU64::new(ms.height.unwrap().into()).unwrap(), + width: NonZeroU64::new(ms.width.unwrap().into()).unwrap(), + } + .into(), + ) + } else { + None + } + } + None => None, + } +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn test_convert_aspect_ratio() { + // test None orig aspect ratio + let metasub: Option = None; + + let result = convert_aspect_ratio(&metasub); + + assert_eq!(result, None); + + // test complet with image + let metasub = Some(MetaSub { + width: Some(1920), + height: Some(1080), + size: Some(String::from("1920x1080")), + aspect: Some(1.7777777777777777), + frame_rate: None, + duration: None, + bitrate: None, + }); + + let expected_result = Some( + AspectRatioData { + height: NonZeroU64::new(1080).unwrap(), + width: NonZeroU64::new(1920).unwrap(), + } + .into(), + ); + + let result = convert_aspect_ratio(&metasub); + + assert_eq!(result, expected_result); + + // test complete with video + let metasub = Some(MetaSub { + width: Some(500), + height: Some(278), + size: None, + aspect: None, + frame_rate: Some(String::from("10/1")), + duration: Some(0.9), + bitrate: Some(973191), + }); + + let expected_result = Some( + AspectRatioData { + height: NonZeroU64::new(278).unwrap(), + width: NonZeroU64::new(500).unwrap(), + } + .into(), + ); + + let result = convert_aspect_ratio(&metasub); + + assert_eq!(result, expected_result); + + /* test broken shit + * that should never happened but you never know + */ + // zero width + let metasub = Some(MetaSub { + width: Some(0), + height: Some(278), + size: None, + aspect: None, + frame_rate: Some(String::from("10/1")), + duration: Some(0.9), + bitrate: Some(973191), + }); + + let result = convert_aspect_ratio(&metasub); + + assert_eq!(result, None); + + // None height + let metasub = Some(MetaSub { + width: Some(500), + height: None, + size: None, + aspect: None, + frame_rate: Some(String::from("10/1")), + duration: Some(0.9), + bitrate: Some(973191), + }); + + let result = convert_aspect_ratio(&metasub); + + assert_eq!(result, None); + } + #[test] fn test_twitter_count() { let content = "tamerelol?! 🐵";