| 1 | use scraper::{ElementRef, Html}; |
| 2 | use serde::{Deserialize, Serialize}; |
| 3 | use store::MessageRecord; |
| 4 | |
| 5 | use crate::spans::{Span, html_to_spans}; |
| 6 | |
| 7 | const EMOJI_ITEMTYPE: &str = "schema.skype.com/Emoji"; |
| 8 | |
| 9 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 10 | pub struct AttachmentInfo { |
| 11 | pub content_type: Option<String>, |
| 12 | pub name: Option<String>, |
| 13 | pub url: Option<String>, |
| 14 | pub text: Option<String>, |
| 15 | #[serde(default)] |
| 16 | pub size: Option<u64>, |
| 17 | #[serde(default)] |
| 18 | pub quote: Option<QuoteInfo>, |
| 19 | #[serde(default)] |
| 20 | pub id: Option<String>, |
| 21 | /// Raw content, kept only for quotes and cards so an edit can send them back unchanged. |
| 22 | #[serde(default)] |
| 23 | pub content: Option<String>, |
| 24 | } |
| 25 | |
| 26 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 27 | pub enum FileKind { |
| 28 | Word, |
| 29 | Excel, |
| 30 | PowerPoint, |
| 31 | Pdf, |
| 32 | Image, |
| 33 | Archive, |
| 34 | Other, |
| 35 | } |
| 36 | |
| 37 | impl FileKind { |
| 38 | pub fn from_name(name: &str) -> FileKind { |
| 39 | let Some((_, extension)) = name.rsplit_once('.') else { |
| 40 | return FileKind::Other; |
| 41 | }; |
| 42 | match extension.to_ascii_lowercase().as_str() { |
| 43 | "doc" | "docx" | "docm" | "dot" | "dotx" | "rtf" | "odt" => FileKind::Word, |
| 44 | "xls" | "xlsx" | "xlsm" | "xlsb" | "csv" | "ods" => FileKind::Excel, |
| 45 | "ppt" | "pptx" | "pptm" | "pps" | "ppsx" | "odp" => FileKind::PowerPoint, |
| 46 | "pdf" => FileKind::Pdf, |
| 47 | "png" | "jpg" | "jpeg" | "gif" | "bmp" | "webp" | "svg" | "heic" | "tif" | "tiff" => { |
| 48 | FileKind::Image |
| 49 | } |
| 50 | "zip" | "rar" | "7z" | "tar" | "gz" | "tgz" => FileKind::Archive, |
| 51 | _ => FileKind::Other, |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 57 | pub struct QuoteInfo { |
| 58 | pub message_id: String, |
| 59 | pub preview: String, |
| 60 | pub sender_name: Option<String>, |
| 61 | } |
| 62 | |
| 63 | /// Image inside a message body. `key()` is the image cache key. |
| 64 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 65 | pub struct ImageRef { |
| 66 | pub id: String, |
| 67 | pub url: String, |
| 68 | pub width: Option<u32>, |
| 69 | pub height: Option<u32>, |
| 70 | } |
| 71 | |
| 72 | impl ImageRef { |
| 73 | pub fn key(&self) -> &str { |
| 74 | &self.url |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl AttachmentInfo { |
| 79 | pub fn is_card(&self) -> bool { |
| 80 | self.content_type |
| 81 | .as_deref() |
| 82 | .is_some_and(|content_type| content_type.contains("card")) |
| 83 | } |
| 84 | |
| 85 | pub fn file_kind(&self) -> FileKind { |
| 86 | match self.name.as_deref().map(FileKind::from_name) { |
| 87 | Some(FileKind::Other) | None if self.has_image_content_type() => FileKind::Image, |
| 88 | Some(kind) => kind, |
| 89 | None => FileKind::Other, |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | fn has_image_content_type(&self) -> bool { |
| 94 | self.content_type |
| 95 | .as_deref() |
| 96 | .is_some_and(|content_type| content_type.starts_with("image/")) |
| 97 | } |
| 98 | |
| 99 | pub fn is_file(&self) -> bool { |
| 100 | self.content_type.as_deref() == Some("reference") && self.url.is_some() |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 105 | pub struct ReactionInfo { |
| 106 | pub reaction_type: String, |
| 107 | pub user_id: Option<String>, |
| 108 | pub user_name: Option<String>, |
| 109 | } |
| 110 | |
| 111 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 112 | pub struct MentionInfo { |
| 113 | pub user_id: Option<String>, |
| 114 | pub name: String, |
| 115 | } |
| 116 | |
| 117 | pub fn attachments(record: &MessageRecord) -> Vec<AttachmentInfo> { |
| 118 | serde_json::from_str(&record.attachments_json).unwrap_or_default() |
| 119 | } |
| 120 | |
| 121 | pub fn reactions(record: &MessageRecord) -> Vec<ReactionInfo> { |
| 122 | serde_json::from_str(&record.reactions_json).unwrap_or_default() |
| 123 | } |
| 124 | |
| 125 | pub fn mentions(record: &MessageRecord) -> Vec<MentionInfo> { |
| 126 | serde_json::from_str(&record.mentions_json).unwrap_or_default() |
| 127 | } |
| 128 | |
| 129 | pub fn quotes(record: &MessageRecord) -> Vec<QuoteInfo> { |
| 130 | attachments(record) |
| 131 | .into_iter() |
| 132 | .filter_map(|attachment| attachment.quote) |
| 133 | .collect() |
| 134 | } |
| 135 | |
| 136 | pub fn images(record: &MessageRecord) -> Vec<ImageRef> { |
| 137 | if record.deleted { |
| 138 | return Vec::new(); |
| 139 | } |
| 140 | let fragment = Html::parse_fragment(&record.body_html); |
| 141 | let mut found: Vec<ImageRef> = Vec::new(); |
| 142 | for element in fragment |
| 143 | .root_element() |
| 144 | .descendants() |
| 145 | .filter_map(ElementRef::wrap) |
| 146 | { |
| 147 | let value = element.value(); |
| 148 | let is_emoji = value |
| 149 | .attr("itemtype") |
| 150 | .is_some_and(|itemtype| itemtype.contains(EMOJI_ITEMTYPE)); |
| 151 | let Some(url) = value |
| 152 | .attr("src") |
| 153 | .filter(|url| value.name() == "img" && !is_emoji && !url.is_empty()) |
| 154 | else { |
| 155 | continue; |
| 156 | }; |
| 157 | if found.iter().any(|image| image.url == url) { |
| 158 | continue; |
| 159 | } |
| 160 | let style = value.attr("style").unwrap_or_default(); |
| 161 | found.push(ImageRef { |
| 162 | id: image_id(url), |
| 163 | url: url.to_owned(), |
| 164 | width: dimension(value.attr("width"), style, "width"), |
| 165 | height: dimension(value.attr("height"), style, "height"), |
| 166 | }); |
| 167 | } |
| 168 | found |
| 169 | } |
| 170 | |
| 171 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 172 | pub struct EditPreserved { |
| 173 | pub images_html: String, |
| 174 | pub files: Vec<graph::FileReference>, |
| 175 | pub kept: Vec<graph::KeptAttachment>, |
| 176 | } |
| 177 | |
| 178 | /// What an edit must carry over: the `<img>` elements, the file attachments and the quote or card attachments, each matched to its `<attachment id>` tag. |
| 179 | pub fn edit_preserved(record: &MessageRecord) -> EditPreserved { |
| 180 | let fragment = Html::parse_fragment(&record.body_html); |
| 181 | let mut preserved = EditPreserved::default(); |
| 182 | let mut tag_ids: Vec<&str> = Vec::new(); |
| 183 | for element in fragment |
| 184 | .root_element() |
| 185 | .descendants() |
| 186 | .filter_map(ElementRef::wrap) |
| 187 | { |
| 188 | let value = element.value(); |
| 189 | let is_emoji = value |
| 190 | .attr("itemtype") |
| 191 | .is_some_and(|itemtype| itemtype.contains(EMOJI_ITEMTYPE)); |
| 192 | match value.name() { |
| 193 | "img" if !is_emoji && value.attr("src").is_some_and(|src| !src.is_empty()) => { |
| 194 | preserved |
| 195 | .images_html |
| 196 | .push_str(&format!("<p>{}</p>", element.html())); |
| 197 | } |
| 198 | "attachment" => tag_ids.extend(value.attr("id")), |
| 199 | _ => {} |
| 200 | } |
| 201 | } |
| 202 | let stored = attachments(record); |
| 203 | let attachment_id = |attachment: &AttachmentInfo| { |
| 204 | attachment.id.clone().or_else(|| { |
| 205 | attachment |
| 206 | .quote |
| 207 | .as_ref() |
| 208 | .map(|quote| quote.message_id.clone()) |
| 209 | }) |
| 210 | }; |
| 211 | let mut unclaimed: Vec<&str> = tag_ids |
| 212 | .iter() |
| 213 | .copied() |
| 214 | .filter(|id| { |
| 215 | !stored.iter().any(|attachment| { |
| 216 | !attachment.is_file() && attachment_id(attachment).as_deref() == Some(*id) |
| 217 | }) |
| 218 | }) |
| 219 | .collect(); |
| 220 | for attachment in stored { |
| 221 | if attachment.is_file() { |
| 222 | let id = match attachment_id(&attachment) { |
| 223 | Some(id) if tag_ids.contains(&id.as_str()) => id, |
| 224 | Some(_) => continue, |
| 225 | None if unclaimed.is_empty() => continue, |
| 226 | None => unclaimed.remove(0).to_owned(), |
| 227 | }; |
| 228 | if let Some(content_url) = attachment.url { |
| 229 | preserved.files.push(graph::FileReference { |
| 230 | attachment_id: id, |
| 231 | content_url, |
| 232 | name: attachment.name.unwrap_or_default(), |
| 233 | }); |
| 234 | } |
| 235 | } else if let Some(id) = attachment_id(&attachment) |
| 236 | && tag_ids.contains(&id.as_str()) |
| 237 | && attachment.content.is_some() |
| 238 | { |
| 239 | preserved.kept.push(graph::KeptAttachment { |
| 240 | id, |
| 241 | content_type: attachment.content_type, |
| 242 | content: attachment.content, |
| 243 | content_url: attachment.url, |
| 244 | name: attachment.name, |
| 245 | }); |
| 246 | } |
| 247 | } |
| 248 | preserved |
| 249 | } |
| 250 | |
| 251 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 252 | pub struct FileCard { |
| 253 | pub name: String, |
| 254 | pub kind: FileKind, |
| 255 | pub content_type: Option<String>, |
| 256 | pub size: Option<u64>, |
| 257 | pub open_url: String, |
| 258 | } |
| 259 | |
| 260 | /// File attachments (`reference` type) with a name and a URL. `size` is rarely known; Graph does not send it for these. |
| 261 | pub fn files(record: &MessageRecord) -> Vec<FileCard> { |
| 262 | attachments(record) |
| 263 | .into_iter() |
| 264 | .filter(AttachmentInfo::is_file) |
| 265 | .map(|attachment| FileCard { |
| 266 | kind: attachment.file_kind(), |
| 267 | name: attachment.name.clone().unwrap_or_default(), |
| 268 | content_type: attachment.content_type.clone(), |
| 269 | size: attachment.size, |
| 270 | open_url: attachment.url.clone().unwrap_or_default(), |
| 271 | }) |
| 272 | .collect() |
| 273 | } |
| 274 | |
| 275 | /// Text for the clipboard: body and card text, mentions as `@name`, no markup. |
| 276 | pub fn copy_text(record: &MessageRecord) -> String { |
| 277 | let mut text = String::new(); |
| 278 | push_plain(&mut text, &message_spans(record)); |
| 279 | text.trim().to_owned() |
| 280 | } |
| 281 | |
| 282 | fn push_plain(text: &mut String, spans: &[Span]) { |
| 283 | for span in spans { |
| 284 | match span { |
| 285 | Span::Text(value) | Span::Code(value) => text.push_str(value), |
| 286 | Span::CodeBlock { code, .. } => { |
| 287 | text.push_str(code); |
| 288 | text.push('\n'); |
| 289 | } |
| 290 | Span::Bold(children) |
| 291 | | Span::Italic(children) |
| 292 | | Span::Strike(children) |
| 293 | | Span::Underline(children) |
| 294 | | Span::Colored { children, .. } |
| 295 | | Span::Quote(children) |
| 296 | | Span::BlockQuote(children) |
| 297 | | Span::Link { children, .. } => push_plain(text, children), |
| 298 | Span::Heading { children, .. } => { |
| 299 | push_plain(text, children); |
| 300 | text.push('\n'); |
| 301 | } |
| 302 | Span::Mention { name } => { |
| 303 | text.push('@'); |
| 304 | text.push_str(name); |
| 305 | } |
| 306 | Span::LineBreak | Span::Rule => text.push('\n'), |
| 307 | Span::List { |
| 308 | ordered, |
| 309 | start, |
| 310 | items, |
| 311 | } => { |
| 312 | for (offset, item) in items.iter().enumerate() { |
| 313 | if *ordered { |
| 314 | text.push_str(&format!("{}. ", *start as usize + offset)); |
| 315 | } else { |
| 316 | text.push_str("- "); |
| 317 | } |
| 318 | push_plain(text, item); |
| 319 | text.push('\n'); |
| 320 | } |
| 321 | } |
| 322 | Span::Table { rows, .. } => { |
| 323 | for row in rows { |
| 324 | for (index, cell) in row.iter().enumerate() { |
| 325 | if index > 0 { |
| 326 | text.push('\t'); |
| 327 | } |
| 328 | push_plain(text, cell); |
| 329 | } |
| 330 | text.push('\n'); |
| 331 | } |
| 332 | } |
| 333 | Span::Image { .. } => {} |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /// Own, not deleted. Whether the conversation supports the write is the engine's call. |
| 339 | pub fn can_edit(record: &MessageRecord, my_user_id: &str) -> bool { |
| 340 | !record.deleted && record.sender_id.as_deref() == Some(my_user_id) |
| 341 | } |
| 342 | |
| 343 | pub fn can_delete(record: &MessageRecord, my_user_id: &str) -> bool { |
| 344 | can_edit(record, my_user_id) |
| 345 | } |
| 346 | |
| 347 | fn image_id(url: &str) -> String { |
| 348 | let path = url.split(['?', '#']).next().unwrap_or(url); |
| 349 | let segments: Vec<&str> = path |
| 350 | .split('/') |
| 351 | .filter(|segment| !segment.is_empty()) |
| 352 | .collect(); |
| 353 | segments |
| 354 | .iter() |
| 355 | .position(|segment| matches!(*segment, "hostedContents" | "objects")) |
| 356 | .and_then(|index| segments.get(index + 1)) |
| 357 | .or(segments.last()) |
| 358 | .map_or_else(String::new, |segment| (*segment).to_owned()) |
| 359 | } |
| 360 | |
| 361 | fn dimension(attribute: Option<&str>, style: &str, property: &str) -> Option<u32> { |
| 362 | let from_style = || { |
| 363 | style.split(';').find_map(|declaration| { |
| 364 | let (name, value) = declaration.split_once(':')?; |
| 365 | (name.trim() == property).then(|| leading_number(value))? |
| 366 | }) |
| 367 | }; |
| 368 | attribute |
| 369 | .and_then(leading_number) |
| 370 | .or_else(from_style) |
| 371 | .filter(|pixels| *pixels > 0) |
| 372 | } |
| 373 | |
| 374 | fn leading_number(text: &str) -> Option<u32> { |
| 375 | let digits: String = text |
| 376 | .trim() |
| 377 | .chars() |
| 378 | .take_while(char::is_ascii_digit) |
| 379 | .collect(); |
| 380 | digits.parse().ok() |
| 381 | } |
| 382 | |
| 383 | /// Body spans followed by the flattened text of adaptive-card attachments. |
| 384 | pub fn message_spans(record: &MessageRecord) -> Vec<Span> { |
| 385 | let mut spans = html_to_spans(&record.body_html); |
| 386 | for card_text in attachments(record) |
| 387 | .iter() |
| 388 | .filter(|attachment| attachment.is_card()) |
| 389 | .filter_map(|attachment| attachment.text.as_deref()) |
| 390 | { |
| 391 | if !spans.is_empty() { |
| 392 | spans.push(Span::LineBreak); |
| 393 | } |
| 394 | for (index, line) in card_text.lines().enumerate() { |
| 395 | if index > 0 { |
| 396 | spans.push(Span::LineBreak); |
| 397 | } |
| 398 | spans.push(Span::Text(line.to_owned())); |
| 399 | } |
| 400 | } |
| 401 | spans |
| 402 | } |
| 403 | |
| 404 | #[cfg(test)] |
| 405 | mod tests { |
| 406 | use chrono::Utc; |
| 407 | |
| 408 | use super::*; |
| 409 | |
| 410 | fn record(body_html: &str, attachments_json: &str) -> MessageRecord { |
| 411 | MessageRecord { |
| 412 | conversation_id: "c".to_owned(), |
| 413 | message_id: "m".to_owned(), |
| 414 | reply_to_id: None, |
| 415 | sender_id: Some("u".to_owned()), |
| 416 | sender_name: None, |
| 417 | created_at: Utc::now(), |
| 418 | edited_at: None, |
| 419 | deleted: false, |
| 420 | body_html: body_html.to_owned(), |
| 421 | attachments_json: attachments_json.to_owned(), |
| 422 | reactions_json: "[]".to_owned(), |
| 423 | mentions_json: "[]".to_owned(), |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | #[test] |
| 428 | fn images_cover_hosted_contents_and_ams_and_skip_emoji() { |
| 429 | let html = concat!( |
| 430 | r#"<p>a</p><img src="https://graph.microsoft.com/v1.0/chats/c/messages/1/hostedContents/aWQ9/$value" style="width:300px; height: 120px">"#, |
| 431 | r#"<img itemtype="http://schema.skype.com/AMSImage" src="https://eu-api.asm.skype.com/v1/objects/0-weu-d1-abc/views/imgo" width="64" height="auto">"#, |
| 432 | r#"<img itemtype="http://schema.skype.com/Emoji" src="https://x/e.png" alt="x">"#, |
| 433 | r#"<img src="https://graph.microsoft.com/v1.0/chats/c/messages/1/hostedContents/aWQ9/$value">"#, |
| 434 | ); |
| 435 | let found = images(&record(html, "[]")); |
| 436 | assert_eq!(found.len(), 2); |
| 437 | assert_eq!( |
| 438 | (found[0].id.as_str(), found[0].width, found[0].height), |
| 439 | ("aWQ9", Some(300), Some(120)) |
| 440 | ); |
| 441 | assert_eq!( |
| 442 | (found[1].id.as_str(), found[1].width, found[1].height), |
| 443 | ("0-weu-d1-abc", Some(64), None) |
| 444 | ); |
| 445 | } |
| 446 | |
| 447 | #[test] |
| 448 | fn deleted_messages_have_no_images() { |
| 449 | let mut deleted = record(r#"<img src="https://x/y">"#, "[]"); |
| 450 | deleted.deleted = true; |
| 451 | assert!(images(&deleted).is_empty()); |
| 452 | } |
| 453 | |
| 454 | #[test] |
| 455 | fn edits_keep_images_and_match_files_to_their_attachment_tags() { |
| 456 | let html = "<p>text</p><p><img src=\"https://graph.microsoft.com/v1.0/chats/c/messages/1/hostedContents/9/$value\"></p><attachment id=\"G1\"></attachment>"; |
| 457 | let attachments = r#"[{"content_type":"reference","name":"a.pdf","url":"https://x/a.pdf","text":null,"id":"G1"}]"#; |
| 458 | let preserved = edit_preserved(&record(html, attachments)); |
| 459 | assert!( |
| 460 | preserved |
| 461 | .images_html |
| 462 | .starts_with("<p><img src=\"https://graph.microsoft.com/") |
| 463 | ); |
| 464 | assert!(!preserved.images_html.contains("text")); |
| 465 | assert_eq!(preserved.files.len(), 1); |
| 466 | assert_eq!(preserved.files[0].attachment_id, "G1"); |
| 467 | assert_eq!(preserved.files[0].content_url, "https://x/a.pdf"); |
| 468 | assert_eq!(preserved.files[0].name, "a.pdf"); |
| 469 | assert!(preserved.kept.is_empty()); |
| 470 | } |
| 471 | |
| 472 | const QUOTED_HTML: &str = |
| 473 | "<attachment id=\"Q1\"></attachment><p>answer</p><attachment id=\"F1\"></attachment>"; |
| 474 | |
| 475 | #[test] |
| 476 | fn a_quoted_reply_keeps_its_quote_and_pairs_the_file_by_id() { |
| 477 | let attachments = r#"[ |
| 478 | {"content_type":"messageReference","name":null,"url":null,"text":null,"id":"Q1","content":"{\"messageId\":\"Q1\",\"messagePreview\":\"hi\"}","quote":{"message_id":"Q1","preview":"hi","sender_name":null}}, |
| 479 | {"content_type":"reference","name":"a.pdf","url":"https://x/a.pdf","text":null,"id":"F1"} |
| 480 | ]"#; |
| 481 | let preserved = edit_preserved(&record(QUOTED_HTML, attachments)); |
| 482 | assert_eq!(preserved.files.len(), 1); |
| 483 | assert_eq!(preserved.files[0].attachment_id, "F1"); |
| 484 | assert_eq!(preserved.kept.len(), 1); |
| 485 | assert_eq!(preserved.kept[0].id, "Q1"); |
| 486 | assert_eq!( |
| 487 | preserved.kept[0].content_type.as_deref(), |
| 488 | Some("messageReference") |
| 489 | ); |
| 490 | assert!( |
| 491 | preserved.kept[0] |
| 492 | .content |
| 493 | .as_deref() |
| 494 | .unwrap() |
| 495 | .contains("messagePreview") |
| 496 | ); |
| 497 | } |
| 498 | |
| 499 | #[test] |
| 500 | fn records_cached_before_content_was_stored_leave_the_quote_out() { |
| 501 | let attachments = r#"[ |
| 502 | {"content_type":"messageReference","name":null,"url":null,"text":null,"quote":{"message_id":"Q1","preview":"hi","sender_name":null}}, |
| 503 | {"content_type":"reference","name":"a.pdf","url":"https://x/a.pdf","text":null} |
| 504 | ]"#; |
| 505 | let preserved = edit_preserved(&record(QUOTED_HTML, attachments)); |
| 506 | assert_eq!(preserved.files[0].attachment_id, "F1"); |
| 507 | assert!(preserved.kept.is_empty()); |
| 508 | } |
| 509 | |
| 510 | #[test] |
| 511 | fn file_kind_follows_the_extension_then_the_content_type() { |
| 512 | let kind = |name: Option<&str>, content_type: &str| { |
| 513 | AttachmentInfo { |
| 514 | content_type: Some(content_type.to_owned()), |
| 515 | name: name.map(str::to_owned), |
| 516 | url: None, |
| 517 | text: None, |
| 518 | size: None, |
| 519 | quote: None, |
| 520 | id: None, |
| 521 | content: None, |
| 522 | } |
| 523 | .file_kind() |
| 524 | }; |
| 525 | assert_eq!(kind(Some("Plan.DOCX"), "reference"), FileKind::Word); |
| 526 | assert_eq!(kind(Some("a.xlsx"), "reference"), FileKind::Excel); |
| 527 | assert_eq!(kind(Some("a.pptx"), "reference"), FileKind::PowerPoint); |
| 528 | assert_eq!(kind(Some("a.pdf"), "reference"), FileKind::Pdf); |
| 529 | assert_eq!(kind(Some("a.PNG"), "reference"), FileKind::Image); |
| 530 | assert_eq!(kind(Some("a.zip"), "reference"), FileKind::Archive); |
| 531 | assert_eq!(kind(Some("a.bin"), "reference"), FileKind::Other); |
| 532 | assert_eq!(kind(None, "image/png"), FileKind::Image); |
| 533 | assert_eq!(kind(Some("noextension"), "reference"), FileKind::Other); |
| 534 | } |
| 535 | |
| 536 | #[test] |
| 537 | fn files_expose_name_kind_and_open_url() { |
| 538 | let json = r#"[{"content_type":"reference","name":"Plan.docx","url":"https://x/Plan.docx","text":null},{"content_type":"messageReference","name":null,"url":null,"text":null}]"#; |
| 539 | let found = files(&record("", json)); |
| 540 | assert_eq!(found.len(), 1); |
| 541 | assert_eq!( |
| 542 | (found[0].name.as_str(), found[0].kind), |
| 543 | ("Plan.docx", FileKind::Word) |
| 544 | ); |
| 545 | assert_eq!(found[0].open_url, "https://x/Plan.docx"); |
| 546 | assert_eq!(found[0].size, None); |
| 547 | } |
| 548 | |
| 549 | #[test] |
| 550 | fn copy_text_flattens_markup_and_keeps_mentions_and_lines() { |
| 551 | let html = r#"<p>Hi <at id="0">Ada</at>, <b>look</b></p><p>second <a href="https://x">link</a></p>"#; |
| 552 | let text = copy_text(&record(html, "[]")); |
| 553 | assert!(text.starts_with("Hi @Ada, look"), "{text}"); |
| 554 | assert!(text.contains("second link"), "{text}"); |
| 555 | assert!(text.contains('\n')); |
| 556 | } |
| 557 | |
| 558 | #[test] |
| 559 | fn old_cached_attachments_still_parse() { |
| 560 | let parsed = attachments(&record( |
| 561 | "", |
| 562 | r#"[{"content_type":"reference","name":"a.pdf","url":"u","text":null}]"#, |
| 563 | )); |
| 564 | assert_eq!(parsed.len(), 1); |
| 565 | assert!(parsed[0].is_file()); |
| 566 | assert_eq!(parsed[0].size, None); |
| 567 | } |
| 568 | |
| 569 | #[test] |
| 570 | fn quote_references_are_extracted_from_the_message_reference_attachment() { |
| 571 | let attachment = graph::Attachment { |
| 572 | id: Some("1".to_owned()), |
| 573 | content_type: Some("messageReference".to_owned()), |
| 574 | content_url: None, |
| 575 | name: None, |
| 576 | content: Some( |
| 577 | r#"{"messageId":"1728","messagePreview":"Hello","messageSender":{"user":{"displayName":"Adele"}}}"#.to_owned(), |
| 578 | ), |
| 579 | }; |
| 580 | let info = crate::mapping::attachment_info(&attachment); |
| 581 | assert_eq!( |
| 582 | info.quote, |
| 583 | Some(QuoteInfo { |
| 584 | message_id: "1728".to_owned(), |
| 585 | preview: "Hello".to_owned(), |
| 586 | sender_name: Some("Adele".to_owned()) |
| 587 | }) |
| 588 | ); |
| 589 | let stored = record("", &serde_json::to_string(&[info]).unwrap()); |
| 590 | assert_eq!(quotes(&stored).len(), 1); |
| 591 | } |
| 592 | } |