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