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