use gpui_kit::{ImageSource, SharedString}; use teams_core::{Gif, ImageRef, OrgEmoji, custom_emoji_html}; use crate::stickers::Sticker; pub const MAX_HEIGHT: u32 = 250; const STICKER_SIZE: u32 = 250; const GIF_ITEMTYPE: &str = "http://schema.skype.com/Giphy"; const STICKER_ITEMTYPE: &str = "http://schema.skype.com/Sticker"; #[derive(Debug, Clone, PartialEq, Eq)] pub enum RemoteKind { Gif, Sticker, CustomEmoji { object_id: String }, } /// A GIF or sticker the message points at by URL; nothing is uploaded. #[derive(Debug, Clone, PartialEq, Eq)] pub struct RemoteImage { pub kind: RemoteKind, pub url: String, pub title: String, pub width: u32, pub height: u32, } impl RemoteImage { pub fn gif(gif: &Gif) -> Self { let (width, height) = capped(gif.width, gif.height); RemoteImage { kind: RemoteKind::Gif, url: gif.url.clone(), title: gif.title.clone(), width, height, } } pub fn sticker(sticker: &Sticker) -> Self { RemoteImage { kind: RemoteKind::Sticker, url: sticker.url(), title: sticker.name.to_owned(), width: STICKER_SIZE, height: STICKER_SIZE, } } pub fn custom_emoji(emoji: &OrgEmoji) -> Self { RemoteImage { kind: RemoteKind::CustomEmoji { object_id: emoji.document_id.clone(), }, url: emoji.image_url(), title: emoji.name.clone(), width: 20, height: 20, } } pub fn is_custom_emoji(&self) -> bool { matches!(self.kind, RemoteKind::CustomEmoji { .. }) } pub fn html(&self) -> String { match &self.kind { RemoteKind::CustomEmoji { object_id } => { custom_emoji_html(&self.title, object_id, &self.url) } RemoteKind::Gif => format!( "\"{}\"", teams_core::escape_html(&self.url), self.width, self.height, teams_core::escape_html(&self.title), ), RemoteKind::Sticker => format!( "\"{}\"", teams_core::escape_html(&self.url), self.width, self.height, teams_core::escape_html(&self.title), ), } } pub fn image_ref(&self) -> ImageRef { ImageRef { id: self.url.clone(), url: self.url.clone(), width: Some(self.width), height: Some(self.height), alt: None, } } } /// Height capped at `MAX_HEIGHT`, aspect ratio kept. pub fn capped(width: u32, height: u32) -> (u32, u32) { if height <= MAX_HEIGHT || height == 0 { return (width, height); } let scaled = (u64::from(width) * u64::from(MAX_HEIGHT) + u64::from(height) / 2) / u64::from(height); ((scaled as u32).max(1), MAX_HEIGHT) } /// Web URLs load through the app's HTTP client, anything else is a local file (demo mode). pub fn image_source(location: &str) -> ImageSource { if location.starts_with("https://") { ImageSource::from(SharedString::from(location.to_owned())) } else { ImageSource::from(std::path::PathBuf::from(location)) } } #[cfg(test)] mod tests { use super::*; fn gif(width: u32, height: u32) -> Gif { Gif { url: "https://media0.giphy.com/media/a/giphy.gif".into(), preview_url: "https://media0.giphy.com/media/a/200w.gif".into(), title: "Say \"hi\" & ".into(), width, height, preview_width: 200, preview_height: 200, } } #[test] fn tall_gifs_are_capped_keeping_the_aspect_ratio() { let image = RemoteImage::gif(&gif(500, 1000)); assert_eq!((image.width, image.height), (125, 250)); let small = RemoteImage::gif(&gif(200, 100)); assert_eq!((small.width, small.height), (200, 100)); } #[test] fn gif_html_escapes_the_title_and_names_the_giphy_itemtype() { let html = RemoteImage::gif(&gif(358, 360)).html(); assert_eq!( html, "\"Say" ); } #[test] fn stickers_are_square_250() { let sticker = crate::stickers::popular()[0]; let html = RemoteImage::sticker(sticker).html(); assert!(html.contains("width=\"250\" height=\"250\"")); assert!(html.ends_with("itemtype=\"http://schema.skype.com/Sticker\">")); assert!(html.contains("clippy-250x250")); } #[test] fn custom_emoji_html_uses_the_official_itemtype_and_itemid() { let emoji = OrgEmoji { id: "em-1".into(), document_id: "0-wcde-d3-5f5e4f463d6bb959b08614fd1d9680b8".into(), name: "absolute-cinema".into(), keywords: vec![], source: None, }; let html = RemoteImage::custom_emoji(&emoji).html(); assert!(html.contains("itemtype=\"http://schema.skype.com/CustomEmoji\"")); assert!(html.contains("itemid=\"absolute-cinema;0-wcde-d3-5f5e4f463d6bb959b08614fd1d9680b8\"")); assert!(html.contains("width=\"20\" height=\"20\"")); assert!(html.contains("/views/imgt2_anim")); } }