| 1 | use gpui_kit::{ImageSource, SharedString}; |
| 2 | use teams_core::{Gif, ImageRef}; |
| 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 | } |
| 16 | |
| 17 | /// A GIF or sticker the message points at by URL; nothing is uploaded. |
| 18 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 19 | pub struct RemoteImage { |
| 20 | pub kind: RemoteKind, |
| 21 | pub url: String, |
| 22 | pub title: String, |
| 23 | pub width: u32, |
| 24 | pub height: u32, |
| 25 | } |
| 26 | |
| 27 | impl RemoteImage { |
| 28 | pub fn gif(gif: &Gif) -> Self { |
| 29 | let (width, height) = capped(gif.width, gif.height); |
| 30 | RemoteImage { |
| 31 | kind: RemoteKind::Gif, |
| 32 | url: gif.url.clone(), |
| 33 | title: gif.title.clone(), |
| 34 | width, |
| 35 | height, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | pub fn sticker(sticker: &Sticker) -> Self { |
| 40 | RemoteImage { |
| 41 | kind: RemoteKind::Sticker, |
| 42 | url: sticker.url(), |
| 43 | title: sticker.name.to_owned(), |
| 44 | width: STICKER_SIZE, |
| 45 | height: STICKER_SIZE, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub fn html(&self) -> String { |
| 50 | let itemtype = match self.kind { |
| 51 | RemoteKind::Gif => GIF_ITEMTYPE, |
| 52 | RemoteKind::Sticker => STICKER_ITEMTYPE, |
| 53 | }; |
| 54 | format!( |
| 55 | "<img src=\"{}\" width=\"{}\" height=\"{}\" alt=\"{}\" itemtype=\"{itemtype}\">", |
| 56 | teams_core::escape_html(&self.url), |
| 57 | self.width, |
| 58 | self.height, |
| 59 | teams_core::escape_html(&self.title), |
| 60 | ) |
| 61 | } |
| 62 | |
| 63 | pub fn image_ref(&self) -> ImageRef { |
| 64 | ImageRef { |
| 65 | id: self.url.clone(), |
| 66 | url: self.url.clone(), |
| 67 | width: Some(self.width), |
| 68 | height: Some(self.height), |
| 69 | alt: None, |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// Height capped at `MAX_HEIGHT`, aspect ratio kept. |
| 75 | pub fn capped(width: u32, height: u32) -> (u32, u32) { |
| 76 | if height <= MAX_HEIGHT || height == 0 { |
| 77 | return (width, height); |
| 78 | } |
| 79 | let scaled = |
| 80 | (u64::from(width) * u64::from(MAX_HEIGHT) + u64::from(height) / 2) / u64::from(height); |
| 81 | ((scaled as u32).max(1), MAX_HEIGHT) |
| 82 | } |
| 83 | |
| 84 | /// Web URLs load through the app's HTTP client, anything else is a local file (demo mode). |
| 85 | pub fn image_source(location: &str) -> ImageSource { |
| 86 | if location.starts_with("https://") { |
| 87 | ImageSource::from(SharedString::from(location.to_owned())) |
| 88 | } else { |
| 89 | ImageSource::from(std::path::PathBuf::from(location)) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[cfg(test)] |
| 94 | mod tests { |
| 95 | use super::*; |
| 96 | |
| 97 | fn gif(width: u32, height: u32) -> Gif { |
| 98 | Gif { |
| 99 | url: "https://media0.giphy.com/media/a/giphy.gif".into(), |
| 100 | preview_url: "https://media0.giphy.com/media/a/200w.gif".into(), |
| 101 | title: "Say \"hi\" & <wave>".into(), |
| 102 | width, |
| 103 | height, |
| 104 | preview_width: 200, |
| 105 | preview_height: 200, |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn tall_gifs_are_capped_keeping_the_aspect_ratio() { |
| 111 | let image = RemoteImage::gif(&gif(500, 1000)); |
| 112 | assert_eq!((image.width, image.height), (125, 250)); |
| 113 | let small = RemoteImage::gif(&gif(200, 100)); |
| 114 | assert_eq!((small.width, small.height), (200, 100)); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn gif_html_escapes_the_title_and_names_the_giphy_itemtype() { |
| 119 | let html = RemoteImage::gif(&gif(358, 360)).html(); |
| 120 | assert_eq!( |
| 121 | html, |
| 122 | "<img src=\"https://media0.giphy.com/media/a/giphy.gif\" width=\"249\" height=\"250\" alt=\"Say "hi" & <wave>\" itemtype=\"http://schema.skype.com/Giphy\">" |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn stickers_are_square_250() { |
| 128 | let sticker = crate::stickers::popular()[0]; |
| 129 | let html = RemoteImage::sticker(sticker).html(); |
| 130 | assert!(html.contains("width=\"250\" height=\"250\"")); |
| 131 | assert!(html.ends_with("itemtype=\"http://schema.skype.com/Sticker\">")); |
| 132 | assert!(html.contains("clippy-250x250")); |
| 133 | } |
| 134 | } |