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