| 1 | use gpui_kit::{ImageSource, SharedString}; |
| 2 | use teams_core::{Gif, ImageRef, OrgEmoji, custom_emoji_html}; |
| 3 | |
| 4 | use crate::stickers::Sticker; |
| 5 | |
| 6 | pub const MAX_HEIGHT: u32 = 250; |
| 7 | const STICKER_SIZE: u32 = 250; |
| 8 | const GIF_ITEMTYPE: &str = "http://schema.skype.com/Giphy"; |
| 9 | const STICKER_ITEMTYPE: &str = "http://schema.skype.com/Sticker"; |
| 10 | |
| 11 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 12 | pub enum RemoteKind { |
| 13 | Gif, |
| 14 | Sticker, |
| 15 | CustomEmoji { object_id: String }, |
| 16 | } |
| 17 | |
| 18 | /// A GIF or sticker the message points at by URL; nothing is uploaded. |
| 19 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 20 | pub struct RemoteImage { |
| 21 | pub kind: RemoteKind, |
| 22 | pub url: String, |
| 23 | pub title: String, |
| 24 | pub width: u32, |
| 25 | pub height: u32, |
| 26 | } |
| 27 | |
| 28 | impl RemoteImage { |
| 29 | pub fn gif(gif: &Gif) -> Self { |
| 30 | let (width, height) = capped(gif.width, gif.height); |
| 31 | RemoteImage { |
| 32 | kind: RemoteKind::Gif, |
| 33 | url: gif.url.clone(), |
| 34 | title: gif.title.clone(), |
| 35 | width, |
| 36 | height, |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub fn sticker(sticker: &Sticker) -> Self { |
| 41 | RemoteImage { |
| 42 | kind: RemoteKind::Sticker, |
| 43 | url: sticker.url(), |
| 44 | title: sticker.name.to_owned(), |
| 45 | width: STICKER_SIZE, |
| 46 | height: STICKER_SIZE, |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pub fn custom_emoji(emoji: &OrgEmoji) -> Self { |
| 51 | RemoteImage { |
| 52 | kind: RemoteKind::CustomEmoji { |
| 53 | object_id: emoji.document_id.clone(), |
| 54 | }, |
| 55 | url: emoji.image_url(), |
| 56 | title: emoji.name.clone(), |
| 57 | width: 20, |
| 58 | height: 20, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn is_custom_emoji(&self) -> bool { |
| 63 | matches!(self.kind, RemoteKind::CustomEmoji { .. }) |
| 64 | } |
| 65 | |
| 66 | pub fn html(&self) -> String { |
| 67 | let itemtype = match self.kind { |
| 68 | RemoteKind::Gif => GIF_ITEMTYPE, |
| 69 | RemoteKind::Sticker => STICKER_ITEMTYPE, |
| 70 | }; |
| 71 | format!( |
| 72 | "<img src=\"{}\" width=\"{}\" height=\"{}\" alt=\"{}\" itemtype=\"{itemtype}\">", |
| 73 | teams_core::escape_html(&self.url), |
| 74 | self.width, |
| 75 | self.height, |
| 76 | teams_core::escape_html(&self.title), |
| 77 | ) |
| 78 | } |
| 79 | |
| 80 | pub fn image_ref(&self) -> ImageRef { |
| 81 | ImageRef { |
| 82 | id: self.url.clone(), |
| 83 | url: self.url.clone(), |
| 84 | width: Some(self.width), |
| 85 | height: Some(self.height), |
| 86 | alt: None, |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// Height capped at `MAX_HEIGHT`, aspect ratio kept. |
| 92 | pub fn capped(width: u32, height: u32) -> (u32, u32) { |
| 93 | if height <= MAX_HEIGHT || height == 0 { |
| 94 | return (width, height); |
| 95 | } |
| 96 | let scaled = |
| 97 | (u64::from(width) * u64::from(MAX_HEIGHT) + u64::from(height) / 2) / u64::from(height); |
| 98 | ((scaled as u32).max(1), MAX_HEIGHT) |
| 99 | } |
| 100 | |
| 101 | /// Web URLs load through the app's HTTP client, anything else is a local file (demo mode). |
| 102 | pub fn image_source(location: &str) -> ImageSource { |
| 103 | if location.starts_with("https://") { |
| 104 | ImageSource::from(SharedString::from(location.to_owned())) |
| 105 | } else { |
| 106 | ImageSource::from(std::path::PathBuf::from(location)) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #[cfg(test)] |
| 111 | mod tests { |
| 112 | use super::*; |
| 113 | |
| 114 | fn gif(width: u32, height: u32) -> Gif { |
| 115 | Gif { |
| 116 | url: "https://media0.giphy.com/media/a/giphy.gif".into(), |
| 117 | preview_url: "https://media0.giphy.com/media/a/200w.gif".into(), |
| 118 | title: "Say \"hi\" & <wave>".into(), |
| 119 | width, |
| 120 | height, |
| 121 | preview_width: 200, |
| 122 | preview_height: 200, |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn tall_gifs_are_capped_keeping_the_aspect_ratio() { |
| 128 | let image = RemoteImage::gif(&gif(500, 1000)); |
| 129 | assert_eq!((image.width, image.height), (125, 250)); |
| 130 | let small = RemoteImage::gif(&gif(200, 100)); |
| 131 | assert_eq!((small.width, small.height), (200, 100)); |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn gif_html_escapes_the_title_and_names_the_giphy_itemtype() { |
| 136 | let html = RemoteImage::gif(&gif(358, 360)).html(); |
| 137 | assert_eq!( |
| 138 | html, |
| 139 | "<img src=\"https://media0.giphy.com/media/a/giphy.gif\" width=\"249\" height=\"250\" alt=\"Say "hi" & <wave>\" itemtype=\"http://schema.skype.com/Giphy\">" |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn stickers_are_square_250() { |
| 145 | let sticker = crate::stickers::popular()[0]; |
| 146 | let html = RemoteImage::sticker(sticker).html(); |
| 147 | assert!(html.contains("width=\"250\" height=\"250\"")); |
| 148 | assert!(html.ends_with("itemtype=\"http://schema.skype.com/Sticker\">")); |
| 149 | assert!(html.contains("clippy-250x250")); |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn custom_emoji_html_uses_the_official_itemtype_and_itemid() { |
| 154 | let emoji = OrgEmoji { |
| 155 | id: "em-1".into(), |
| 156 | document_id: "0-wcde-d3-5f5e4f463d6bb959b08614fd1d9680b8".into(), |
| 157 | name: "absolute-cinema".into(), |
| 158 | keywords: vec![], |
| 159 | }; |
| 160 | let html = RemoteImage::custom_emoji(&emoji).html(); |
| 161 | assert!(html.contains("itemtype=\"http://schema.skype.com/CustomEmoji\"")); |
| 162 | assert!(html.contains("itemid=\"absolute-cinema;0-wcde-d3-5f5e4f463d6bb959b08614fd1d9680b8\"")); |
| 163 | assert!(html.contains("width=\"20\" height=\"20\"")); |
| 164 | assert!(html.contains("/views/imgt2_anim")); |
| 165 | } |
| 166 | } |