BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-base / src / remote_image.rs

remote_image.rs

remote_image.rs 5.5 KiB Download Raw
1use gpui_kit::{ImageSource, SharedString};
2use teams_core::{Gif, ImageRef, OrgEmoji, custom_emoji_html};
3
4use crate::stickers::Sticker;
5
6pub const MAX_HEIGHT: u32 = 250;
7const STICKER_SIZE: u32 = 250;
8const GIF_ITEMTYPE: &str = "http://schema.skype.com/Giphy";
9const STICKER_ITEMTYPE: &str = "http://schema.skype.com/Sticker";
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub 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)]
20pub struct RemoteImage {
21 pub kind: RemoteKind,
22 pub url: String,
23 pub title: String,
24 pub width: u32,
25 pub height: u32,
26}
27
28impl 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 match &self.kind {
68 RemoteKind::CustomEmoji { object_id } => {
69 custom_emoji_html(&self.title, object_id, &self.url)
70 }
71 RemoteKind::Gif => format!(
72 "<img src=\"{}\" width=\"{}\" height=\"{}\" alt=\"{}\" itemtype=\"{GIF_ITEMTYPE}\">",
73 teams_core::escape_html(&self.url),
74 self.width,
75 self.height,
76 teams_core::escape_html(&self.title),
77 ),
78 RemoteKind::Sticker => format!(
79 "<img src=\"{}\" width=\"{}\" height=\"{}\" alt=\"{}\" itemtype=\"{STICKER_ITEMTYPE}\">",
80 teams_core::escape_html(&self.url),
81 self.width,
82 self.height,
83 teams_core::escape_html(&self.title),
84 ),
85 }
86 }
87
88 pub fn image_ref(&self) -> ImageRef {
89 ImageRef {
90 id: self.url.clone(),
91 url: self.url.clone(),
92 width: Some(self.width),
93 height: Some(self.height),
94 alt: None,
95 }
96 }
97}
98
99/// Height capped at `MAX_HEIGHT`, aspect ratio kept.
100pub fn capped(width: u32, height: u32) -> (u32, u32) {
101 if height <= MAX_HEIGHT || height == 0 {
102 return (width, height);
103 }
104 let scaled =
105 (u64::from(width) * u64::from(MAX_HEIGHT) + u64::from(height) / 2) / u64::from(height);
106 ((scaled as u32).max(1), MAX_HEIGHT)
107}
108
109/// Web URLs load through the app's HTTP client, anything else is a local file (demo mode).
110pub fn image_source(location: &str) -> ImageSource {
111 if location.starts_with("https://") {
112 ImageSource::from(SharedString::from(location.to_owned()))
113 } else {
114 ImageSource::from(std::path::PathBuf::from(location))
115 }
116}
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121
122 fn gif(width: u32, height: u32) -> Gif {
123 Gif {
124 url: "https://media0.giphy.com/media/a/giphy.gif".into(),
125 preview_url: "https://media0.giphy.com/media/a/200w.gif".into(),
126 title: "Say \"hi\" & <wave>".into(),
127 width,
128 height,
129 preview_width: 200,
130 preview_height: 200,
131 }
132 }
133
134 #[test]
135 fn tall_gifs_are_capped_keeping_the_aspect_ratio() {
136 let image = RemoteImage::gif(&gif(500, 1000));
137 assert_eq!((image.width, image.height), (125, 250));
138 let small = RemoteImage::gif(&gif(200, 100));
139 assert_eq!((small.width, small.height), (200, 100));
140 }
141
142 #[test]
143 fn gif_html_escapes_the_title_and_names_the_giphy_itemtype() {
144 let html = RemoteImage::gif(&gif(358, 360)).html();
145 assert_eq!(
146 html,
147 "<img src=\"https://media0.giphy.com/media/a/giphy.gif\" width=\"249\" height=\"250\" alt=\"Say &quot;hi&quot; &amp; &lt;wave&gt;\" itemtype=\"http://schema.skype.com/Giphy\">"
148 );
149 }
150
151 #[test]
152 fn stickers_are_square_250() {
153 let sticker = crate::stickers::popular()[0];
154 let html = RemoteImage::sticker(sticker).html();
155 assert!(html.contains("width=\"250\" height=\"250\""));
156 assert!(html.ends_with("itemtype=\"http://schema.skype.com/Sticker\">"));
157 assert!(html.contains("clippy-250x250"));
158 }
159
160 #[test]
161 fn custom_emoji_html_uses_the_official_itemtype_and_itemid() {
162 let emoji = OrgEmoji {
163 id: "em-1".into(),
164 document_id: "0-wcde-d3-5f5e4f463d6bb959b08614fd1d9680b8".into(),
165 name: "absolute-cinema".into(),
166 keywords: vec![],
167 };
168 let html = RemoteImage::custom_emoji(&emoji).html();
169 assert!(html.contains("itemtype=\"http://schema.skype.com/CustomEmoji\""));
170 assert!(html.contains("itemid=\"absolute-cinema;0-wcde-d3-5f5e4f463d6bb959b08614fd1d9680b8\""));
171 assert!(html.contains("width=\"20\" height=\"20\""));
172 assert!(html.contains("/views/imgt2_anim"));
173 }
174}