| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use gpui_kit::{Image, ImageFormat}; |
| 4 | use serde::{Deserialize, Serialize}; |
| 5 | use store::{AttachmentImage, MessageImportance}; |
| 6 | use teams_core::{AnnouncementBanner, Draft, DraftLine, LinkPreview, MentionInput}; |
| 7 | |
| 8 | use crate::outgoing::{InlineImage, Outgoing, OutgoingFile, OutgoingImage, ReplyPreview}; |
| 9 | use crate::remote_image::{RemoteImage, RemoteKind}; |
| 10 | |
| 11 | const IMAGE_FORMATS: [ImageFormat; 9] = [ |
| 12 | ImageFormat::Png, |
| 13 | ImageFormat::Jpeg, |
| 14 | ImageFormat::Webp, |
| 15 | ImageFormat::Gif, |
| 16 | ImageFormat::Svg, |
| 17 | ImageFormat::Bmp, |
| 18 | ImageFormat::Tiff, |
| 19 | ImageFormat::Ico, |
| 20 | ImageFormat::Pnm, |
| 21 | ]; |
| 22 | |
| 23 | #[derive(Serialize, Deserialize)] |
| 24 | enum StoredObject { |
| 25 | Inline, |
| 26 | Gif(StoredRemote), |
| 27 | Sticker(StoredRemote), |
| 28 | CustomEmoji(StoredRemote), |
| 29 | } |
| 30 | |
| 31 | #[derive(Serialize, Deserialize)] |
| 32 | struct StoredRemote { |
| 33 | url: String, |
| 34 | title: String, |
| 35 | width: u32, |
| 36 | height: u32, |
| 37 | #[serde(default)] |
| 38 | object_id: Option<String>, |
| 39 | } |
| 40 | |
| 41 | #[derive(Serialize, Deserialize)] |
| 42 | struct StoredPreview { |
| 43 | url: String, |
| 44 | title: Option<String>, |
| 45 | description: Option<String>, |
| 46 | image_url: Option<String>, |
| 47 | image_width: Option<u32>, |
| 48 | image_height: Option<u32>, |
| 49 | } |
| 50 | |
| 51 | #[derive(Serialize, Deserialize)] |
| 52 | struct Payload { |
| 53 | lines: Vec<DraftLine>, |
| 54 | #[serde(default)] |
| 55 | subject: Option<String>, |
| 56 | mentions: Vec<MentionInput>, |
| 57 | reply: Option<ReplyPreview>, |
| 58 | files: Vec<OutgoingFile>, |
| 59 | objects: Vec<StoredObject>, |
| 60 | preview: Option<StoredPreview>, |
| 61 | #[serde(default)] |
| 62 | importance: Option<String>, |
| 63 | #[serde(default)] |
| 64 | banner: Option<String>, |
| 65 | } |
| 66 | |
| 67 | pub struct StoredOutgoing { |
| 68 | pub payload: String, |
| 69 | pub images: Vec<AttachmentImage>, |
| 70 | } |
| 71 | |
| 72 | pub fn encode(outgoing: &Outgoing) -> Option<StoredOutgoing> { |
| 73 | let mut images = Vec::new(); |
| 74 | let mut objects = Vec::new(); |
| 75 | for object in &outgoing.images { |
| 76 | match object { |
| 77 | OutgoingImage::Inline(inline) => { |
| 78 | images.push(AttachmentImage { |
| 79 | name: inline.name.clone(), |
| 80 | format: inline.image.format.mime_type().to_owned(), |
| 81 | bytes: inline.image.bytes.clone(), |
| 82 | width: inline.dimensions.map(|(width, _)| width), |
| 83 | height: inline.dimensions.map(|(_, height)| height), |
| 84 | }); |
| 85 | objects.push(StoredObject::Inline); |
| 86 | } |
| 87 | OutgoingImage::Remote(remote) => { |
| 88 | let stored = StoredRemote { |
| 89 | url: remote.url.clone(), |
| 90 | title: remote.title.clone(), |
| 91 | width: remote.width, |
| 92 | height: remote.height, |
| 93 | object_id: match &remote.kind { |
| 94 | RemoteKind::CustomEmoji { object_id } => Some(object_id.clone()), |
| 95 | _ => None, |
| 96 | }, |
| 97 | }; |
| 98 | objects.push(match &remote.kind { |
| 99 | RemoteKind::Gif => StoredObject::Gif(stored), |
| 100 | RemoteKind::Sticker => StoredObject::Sticker(stored), |
| 101 | RemoteKind::CustomEmoji { .. } => StoredObject::CustomEmoji(stored), |
| 102 | }); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | let payload = serde_json::to_string(&Payload { |
| 107 | lines: outgoing.draft.to_lines(), |
| 108 | subject: outgoing.subject.clone(), |
| 109 | mentions: outgoing.mentions.clone(), |
| 110 | reply: outgoing.reply.clone(), |
| 111 | files: outgoing.files.clone(), |
| 112 | objects, |
| 113 | preview: outgoing.link_preview.as_ref().map(|preview| StoredPreview { |
| 114 | url: preview.url.clone(), |
| 115 | title: preview.title.clone(), |
| 116 | description: preview.description.clone(), |
| 117 | image_url: preview.image_url.clone(), |
| 118 | image_width: preview.image_width, |
| 119 | image_height: preview.image_height, |
| 120 | }), |
| 121 | importance: outgoing.importance.stored().map(str::to_owned), |
| 122 | banner: outgoing.banner.as_ref().map(AnnouncementBanner::content), |
| 123 | }) |
| 124 | .ok()?; |
| 125 | Some(StoredOutgoing { payload, images }) |
| 126 | } |
| 127 | |
| 128 | fn inline_image(stored: &AttachmentImage) -> Option<OutgoingImage> { |
| 129 | let format = IMAGE_FORMATS |
| 130 | .into_iter() |
| 131 | .find(|format| format.mime_type() == stored.format)?; |
| 132 | Some(OutgoingImage::Inline(InlineImage { |
| 133 | name: stored.name.clone(), |
| 134 | image: Arc::new(Image::from_bytes(format, stored.bytes.clone())), |
| 135 | dimensions: stored.width.zip(stored.height), |
| 136 | })) |
| 137 | } |
| 138 | |
| 139 | fn remote_image(kind: RemoteKind, stored: StoredRemote) -> OutgoingImage { |
| 140 | let kind = match kind { |
| 141 | RemoteKind::CustomEmoji { .. } => RemoteKind::CustomEmoji { |
| 142 | object_id: stored.object_id.unwrap_or_default(), |
| 143 | }, |
| 144 | other => other, |
| 145 | }; |
| 146 | OutgoingImage::Remote(RemoteImage { |
| 147 | kind, |
| 148 | url: stored.url, |
| 149 | title: stored.title, |
| 150 | width: stored.width, |
| 151 | height: stored.height, |
| 152 | }) |
| 153 | } |
| 154 | |
| 155 | pub fn decode(payload: &str, images: &[AttachmentImage]) -> Option<Outgoing> { |
| 156 | let Payload { |
| 157 | lines, |
| 158 | subject, |
| 159 | mentions, |
| 160 | reply, |
| 161 | files, |
| 162 | objects, |
| 163 | preview, |
| 164 | importance, |
| 165 | banner, |
| 166 | } = serde_json::from_str(payload).ok()?; |
| 167 | let mut inline = images.iter(); |
| 168 | let images = objects |
| 169 | .into_iter() |
| 170 | .filter_map(|object| match object { |
| 171 | StoredObject::Inline => inline.next().and_then(inline_image), |
| 172 | StoredObject::Gif(stored) => Some(remote_image(RemoteKind::Gif, stored)), |
| 173 | StoredObject::Sticker(stored) => Some(remote_image(RemoteKind::Sticker, stored)), |
| 174 | StoredObject::CustomEmoji(stored) => { |
| 175 | let object_id = stored.object_id.clone().unwrap_or_default(); |
| 176 | Some(remote_image(RemoteKind::CustomEmoji { object_id }, stored)) |
| 177 | } |
| 178 | }) |
| 179 | .collect(); |
| 180 | Some(Outgoing { |
| 181 | draft: Draft::from_lines(&lines), |
| 182 | subject, |
| 183 | mentions, |
| 184 | reply, |
| 185 | edit: None, |
| 186 | images, |
| 187 | files, |
| 188 | link_preview: preview.map(|preview| LinkPreview { |
| 189 | url: preview.url, |
| 190 | title: preview.title, |
| 191 | description: preview.description, |
| 192 | image_url: preview.image_url, |
| 193 | image_width: preview.image_width, |
| 194 | image_height: preview.image_height, |
| 195 | }), |
| 196 | importance: MessageImportance::from_stored(importance.as_deref()), |
| 197 | banner: banner.as_deref().and_then(AnnouncementBanner::parse), |
| 198 | }) |
| 199 | } |
| 200 | |
| 201 | #[cfg(test)] |
| 202 | mod tests { |
| 203 | use teams_core::{FileKind, FileReference, MarkKind}; |
| 204 | |
| 205 | use super::*; |
| 206 | |
| 207 | fn outgoing() -> Outgoing { |
| 208 | let mut draft = Draft::plain("hi @Ada Lovelace \u{FFFC}\nsecond"); |
| 209 | draft.toggle(0..2, MarkKind::Bold); |
| 210 | draft.take_edits(); |
| 211 | Outgoing { |
| 212 | draft, |
| 213 | subject: Some("Release plan".to_owned()), |
| 214 | mentions: vec![MentionInput::user("user-1", "Ada Lovelace")], |
| 215 | reply: Some(ReplyPreview { |
| 216 | message_id: "message-1".to_owned(), |
| 217 | author: "Grace".to_owned(), |
| 218 | excerpt: "earlier".to_owned(), |
| 219 | }), |
| 220 | edit: None, |
| 221 | images: vec![ |
| 222 | OutgoingImage::Inline(InlineImage { |
| 223 | name: "shot.png".to_owned(), |
| 224 | image: Arc::new(Image::from_bytes(ImageFormat::Png, vec![1, 2, 3])), |
| 225 | dimensions: Some((40, 30)), |
| 226 | }), |
| 227 | OutgoingImage::Remote(RemoteImage { |
| 228 | kind: RemoteKind::Gif, |
| 229 | url: "https://example.test/a.gif".to_owned(), |
| 230 | title: "wave".to_owned(), |
| 231 | width: 100, |
| 232 | height: 80, |
| 233 | }), |
| 234 | ], |
| 235 | files: vec![OutgoingFile { |
| 236 | name: "plan.pdf".to_owned(), |
| 237 | size: 12, |
| 238 | kind: FileKind::Pdf, |
| 239 | reference: FileReference { |
| 240 | attachment_id: "attachment-1".to_owned(), |
| 241 | content_url: "https://example.test/plan.pdf".to_owned(), |
| 242 | name: "plan.pdf".to_owned(), |
| 243 | }, |
| 244 | uploaded: None, |
| 245 | }], |
| 246 | link_preview: Some(LinkPreview { |
| 247 | url: "https://a.example".to_owned(), |
| 248 | title: Some("A".to_owned()), |
| 249 | description: None, |
| 250 | image_url: None, |
| 251 | image_width: None, |
| 252 | image_height: None, |
| 253 | }), |
| 254 | importance: Default::default(), |
| 255 | banner: Some(AnnouncementBanner { |
| 256 | title: "Ship it".to_owned(), |
| 257 | background: teams_core::BannerBackground::Color("rose".to_owned()), |
| 258 | }), |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn an_announcement_stays_out_of_the_graph_body() { |
| 264 | let extras = outgoing().extras(); |
| 265 | assert!(extras.kept.is_empty()); |
| 266 | assert_eq!(extras.kept_html(), ""); |
| 267 | } |
| 268 | |
| 269 | #[test] |
| 270 | fn an_outgoing_message_round_trips_with_formatting_mention_and_image() { |
| 271 | let original = outgoing(); |
| 272 | let stored = encode(&original).unwrap(); |
| 273 | let restored = decode(&stored.payload, &stored.images).unwrap(); |
| 274 | assert_eq!(restored.draft.text(), original.draft.text()); |
| 275 | assert_eq!(restored.draft.to_html(), original.draft.to_html()); |
| 276 | assert!(restored.draft.to_html().contains("<b>")); |
| 277 | assert_eq!(restored.subject, original.subject); |
| 278 | assert_eq!(restored.mentions, original.mentions); |
| 279 | assert_eq!(restored.reply, original.reply); |
| 280 | assert_eq!(restored.images, original.images); |
| 281 | assert_eq!(restored.files, original.files); |
| 282 | assert_eq!(restored.link_preview, original.link_preview); |
| 283 | assert_eq!(restored.edit, None); |
| 284 | } |
| 285 | |
| 286 | #[test] |
| 287 | fn the_importance_survives_the_outbox_and_old_payloads_stay_standard() { |
| 288 | let original = Outgoing { |
| 289 | importance: MessageImportance::Urgent, |
| 290 | ..outgoing() |
| 291 | }; |
| 292 | let stored = encode(&original).unwrap(); |
| 293 | let decoded = decode(&stored.payload, &stored.images).unwrap(); |
| 294 | assert_eq!(decoded.importance, MessageImportance::Urgent); |
| 295 | assert_eq!(decoded.extras().importance, Some("urgent")); |
| 296 | let plain = encode(&outgoing()).unwrap(); |
| 297 | assert!(!plain.payload.contains("importance\":\"")); |
| 298 | assert_eq!( |
| 299 | decode(&plain.payload, &plain.images).unwrap().importance, |
| 300 | MessageImportance::Normal |
| 301 | ); |
| 302 | } |
| 303 | |
| 304 | #[test] |
| 305 | fn an_unreadable_payload_decodes_to_nothing() { |
| 306 | assert!(decode("not json", &[]).is_none()); |
| 307 | } |
| 308 | } |