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