| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use chrono::{DateTime, Utc}; |
| 4 | use gpui_kit::Image; |
| 5 | use serde::{Deserialize, Serialize}; |
| 6 | use teams_core::{ |
| 7 | AnnouncementBanner, Draft, FileKind, FileReference, HostedImage, LinkPreview, MentionInput, |
| 8 | MessageExtras, OBJECT_MARK, UploadedFile, |
| 9 | }; |
| 10 | |
| 11 | use store::MessageImportance; |
| 12 | |
| 13 | use crate::remote_image::RemoteImage; |
| 14 | |
| 15 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 16 | pub struct InlineImage { |
| 17 | pub name: String, |
| 18 | pub image: Arc<Image>, |
| 19 | pub dimensions: Option<(u32, u32)>, |
| 20 | } |
| 21 | |
| 22 | /// One object in the text: uploaded bytes or a GIF or sticker the message links to. |
| 23 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 24 | pub enum OutgoingImage { |
| 25 | Inline(InlineImage), |
| 26 | Remote(RemoteImage), |
| 27 | } |
| 28 | |
| 29 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 30 | pub struct OutgoingFile { |
| 31 | pub name: String, |
| 32 | pub size: u64, |
| 33 | pub kind: FileKind, |
| 34 | pub reference: FileReference, |
| 35 | pub uploaded: Option<UploadedFile>, |
| 36 | } |
| 37 | |
| 38 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 39 | pub struct ReplyPreview { |
| 40 | pub message_id: String, |
| 41 | pub author: String, |
| 42 | pub excerpt: String, |
| 43 | } |
| 44 | |
| 45 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 46 | pub struct EditPreview { |
| 47 | pub message_id: String, |
| 48 | pub excerpt: String, |
| 49 | pub scheduled: Option<DateTime<Utc>>, |
| 50 | } |
| 51 | |
| 52 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 53 | pub struct Outgoing { |
| 54 | pub draft: Draft, |
| 55 | pub subject: Option<String>, |
| 56 | pub mentions: Vec<MentionInput>, |
| 57 | pub reply: Option<ReplyPreview>, |
| 58 | pub edit: Option<EditPreview>, |
| 59 | pub images: Vec<OutgoingImage>, |
| 60 | pub files: Vec<OutgoingFile>, |
| 61 | pub link_preview: Option<LinkPreview>, |
| 62 | pub importance: MessageImportance, |
| 63 | pub banner: Option<AnnouncementBanner>, |
| 64 | } |
| 65 | |
| 66 | /// Teams shows a code block's language and Copy button only when a marker paragraph links to it. |
| 67 | /// The server adds the `x_` that Teams' own messages show on the marker's id. |
| 68 | fn link_code_blocks(html: &str) -> String { |
| 69 | let mut linked = String::with_capacity(html.len()); |
| 70 | for (index, part) in html.split("<pre ").enumerate() { |
| 71 | if index > 0 { |
| 72 | // Derived from the body, so every html() of one message gives the same text. |
| 73 | let id = code_block_id(html, index); |
| 74 | linked.push_str(&format!( |
| 75 | "<p itemtype=\"{}\" id=\"codeBlockEditor-{id}\"> </p><pre itemid=\"codeBlockEditor-{id}\" ", |
| 76 | teams_core::CODE_BLOCK_ITEMTYPE |
| 77 | )); |
| 78 | } |
| 79 | linked.push_str(part); |
| 80 | } |
| 81 | linked |
| 82 | } |
| 83 | |
| 84 | fn code_block_id(html: &str, index: usize) -> uuid::Uuid { |
| 85 | use std::hash::{Hash, Hasher}; |
| 86 | let mut hasher = std::hash::DefaultHasher::new(); |
| 87 | (html, index).hash(&mut hasher); |
| 88 | let high = hasher.finish(); |
| 89 | high.hash(&mut hasher); |
| 90 | uuid::Uuid::from_u64_pair(high, hasher.finish()) |
| 91 | } |
| 92 | |
| 93 | impl Outgoing { |
| 94 | /// Without list and quote markers, for previews. |
| 95 | pub fn text(&self) -> String { |
| 96 | self.draft.plain_text() |
| 97 | } |
| 98 | |
| 99 | /// The body before mentions become `<at>` tags. |
| 100 | pub fn html(&self) -> String { |
| 101 | let mut html = String::new(); |
| 102 | let mut objects = self.images.iter(); |
| 103 | let mut uploaded = 0; |
| 104 | for character in self.draft.to_html().chars() { |
| 105 | if character != OBJECT_MARK { |
| 106 | html.push(character); |
| 107 | continue; |
| 108 | } |
| 109 | match objects.next() { |
| 110 | Some(OutgoingImage::Inline(_)) => { |
| 111 | uploaded += 1; |
| 112 | html.push_str(&format!( |
| 113 | "<img src=\"../hostedContents/{uploaded}/$value\">" |
| 114 | )); |
| 115 | } |
| 116 | Some(OutgoingImage::Remote(remote)) => html.push_str(&remote.html()), |
| 117 | None => {} |
| 118 | } |
| 119 | } |
| 120 | link_code_blocks(&teams_core::emoticon_tags( |
| 121 | &html, |
| 122 | crate::emoticons::send_tag, |
| 123 | )) |
| 124 | } |
| 125 | |
| 126 | pub fn has_attachments(&self) -> bool { |
| 127 | !self.images.is_empty() || !self.files.is_empty() |
| 128 | } |
| 129 | |
| 130 | pub fn extras(&self) -> MessageExtras { |
| 131 | MessageExtras { |
| 132 | importance: self.importance.stored(), |
| 133 | kept: Vec::new(), |
| 134 | images: self |
| 135 | .images |
| 136 | .iter() |
| 137 | .filter_map(|outgoing| match outgoing { |
| 138 | OutgoingImage::Inline(inline) => Some(HostedImage { |
| 139 | content_type: inline.image.format.mime_type().to_owned(), |
| 140 | bytes: Arc::new(inline.image.bytes.clone()), |
| 141 | }), |
| 142 | OutgoingImage::Remote(_) => None, |
| 143 | }) |
| 144 | .collect(), |
| 145 | files: self |
| 146 | .files |
| 147 | .iter() |
| 148 | .map(|file| file.reference.clone()) |
| 149 | .collect(), |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #[cfg(test)] |
| 155 | mod tests { |
| 156 | use super::*; |
| 157 | |
| 158 | #[test] |
| 159 | fn each_code_block_gets_its_own_teams_marker() { |
| 160 | let html = "<p>a</p><pre class=\"language-rs\"><code>x</code></pre><pre class=\"language-py\"><code><pre y</code></pre>"; |
| 161 | let linked = link_code_blocks(html); |
| 162 | let ids: Vec<&str> = linked |
| 163 | .split("itemid=\"codeBlockEditor-") |
| 164 | .skip(1) |
| 165 | .map(|rest| &rest[..36]) |
| 166 | .collect(); |
| 167 | assert_eq!(ids.len(), 2); |
| 168 | assert_ne!(ids[0], ids[1]); |
| 169 | for id in &ids { |
| 170 | assert!(linked.contains(&format!( |
| 171 | "<p itemtype=\"{}\" id=\"codeBlockEditor-{id}\"> </p><pre itemid=\"codeBlockEditor-{id}\" class=", |
| 172 | teams_core::CODE_BLOCK_ITEMTYPE |
| 173 | ))); |
| 174 | } |
| 175 | assert!(linked.contains("<pre y")); |
| 176 | assert_eq!(link_code_blocks(html), linked); |
| 177 | assert_eq!(link_code_blocks("<p>no code</p>"), "<p>no code</p>"); |
| 178 | } |
| 179 | } |