| 1 | use graph::Message; |
| 2 | use serde_json::Value; |
| 3 | use store::{ChatPreview, MessageRecord}; |
| 4 | |
| 5 | use crate::call_event::{CallEvent, CallPhase, call_event}; |
| 6 | use crate::external_image::is_giphy_url; |
| 7 | use crate::forwarded::FORWARDED_CONTENT_TYPE; |
| 8 | use crate::roster_event::{RosterChange, roster_event}; |
| 9 | use crate::spans::{Span, html_to_spans}; |
| 10 | use crate::stored::{AttachmentInfo, LOOP_CARD_TYPE, attachments}; |
| 11 | |
| 12 | const MAX_PREVIEW_CHARS: usize = 200; |
| 13 | |
| 14 | pub const IMAGE_PREVIEW: &str = "Sent an image"; |
| 15 | pub const GIF_PREVIEW: &str = "\u{1F4F7} GIF"; |
| 16 | pub const VIDEO_PREVIEW: &str = "\u{1F3A5} Video"; |
| 17 | pub const FILE_PREVIEW: &str = "Sent a file"; |
| 18 | pub const CARD_PREVIEW: &str = "Sent a card"; |
| 19 | pub const FORWARD_PREVIEW: &str = "Forwarded a message"; |
| 20 | pub const AUDIO_PREVIEW: &str = "\u{1F399} Audio message"; |
| 21 | pub const LOOP_PREVIEW: &str = "Loop component"; |
| 22 | pub const RECORDING_STARTED_PREVIEW: &str = "Recording has started"; |
| 23 | pub const RECORDING_STOPPED_PREVIEW: &str = "Recording has stopped"; |
| 24 | pub const RECORDING_READY_PREVIEW: &str = "Recording is ready"; |
| 25 | pub const TRANSCRIPT_PREVIEW: &str = "Transcript is available"; |
| 26 | pub const USER_ADDED_PREVIEW: &str = "User added"; |
| 27 | pub const USER_JOINED_PREVIEW: &str = "User joined"; |
| 28 | const EVENT_PREVIEWS: [&str; 6] = [ |
| 29 | RECORDING_STARTED_PREVIEW, |
| 30 | RECORDING_STOPPED_PREVIEW, |
| 31 | RECORDING_READY_PREVIEW, |
| 32 | TRANSCRIPT_PREVIEW, |
| 33 | USER_ADDED_PREVIEW, |
| 34 | USER_JOINED_PREVIEW, |
| 35 | ]; |
| 36 | |
| 37 | const AUDIO_CARD_TYPE: &str = "application/vnd.microsoft.card.audio"; |
| 38 | const FILE_CONTENT_TYPE: &str = "reference"; |
| 39 | const RECORDING_TYPE: &str = "#microsoft.graph.callRecordingEventMessageDetail"; |
| 40 | const TRANSCRIPT_TYPE: &str = "#microsoft.graph.callTranscriptEventMessageDetail"; |
| 41 | const MEMBERS_ADDED_TYPE: &str = "#microsoft.graph.membersAddedEventMessageDetail"; |
| 42 | const MEMBERS_JOINED_TYPE: &str = "#microsoft.graph.membersJoinedEventMessageDetail"; |
| 43 | |
| 44 | #[derive(Default)] |
| 45 | struct BodyParts { |
| 46 | text: String, |
| 47 | images: usize, |
| 48 | gifs: usize, |
| 49 | } |
| 50 | |
| 51 | /// Plain text of a message body as the chat list shows it: emoji as glyphs, mentions by name, reply quotes and images dropped. |
| 52 | pub fn preview_text(html: &str) -> Option<String> { |
| 53 | let mut parts = BodyParts::default(); |
| 54 | collect(&html_to_spans(html), &mut parts); |
| 55 | collapse(&parts.text) |
| 56 | } |
| 57 | |
| 58 | pub fn is_event_preview(text: &str) -> bool { |
| 59 | EVENT_PREVIEWS.contains(&text) |
| 60 | } |
| 61 | |
| 62 | /// The chat list line for a message, with the Teams labels for bodies without text. |
| 63 | pub fn message_preview(html: &str, attachments: &[AttachmentInfo]) -> Option<String> { |
| 64 | if html.contains("<video") { |
| 65 | return Some(VIDEO_PREVIEW.to_owned()); |
| 66 | } |
| 67 | let mut parts = BodyParts::default(); |
| 68 | collect(&html_to_spans(html), &mut parts); |
| 69 | if let Some(text) = collapse(&parts.text) { |
| 70 | return Some(text); |
| 71 | } |
| 72 | if parts.gifs > 0 { |
| 73 | return Some(GIF_PREVIEW.to_owned()); |
| 74 | } |
| 75 | if parts.images > 0 { |
| 76 | return Some(IMAGE_PREVIEW.to_owned()); |
| 77 | } |
| 78 | attachment_label(attachments) |
| 79 | } |
| 80 | |
| 81 | fn attachment_label(attachments: &[AttachmentInfo]) -> Option<String> { |
| 82 | fixed_label(attachments).map(str::to_owned).or_else(|| { |
| 83 | let mut cards = attachments.iter().filter(|attachment| attachment.is_card()); |
| 84 | cards |
| 85 | .clone() |
| 86 | .find_map(|card| collapse(&card.card_text()?)) |
| 87 | .or_else(|| cards.next().map(|_| CARD_PREVIEW.to_owned())) |
| 88 | }) |
| 89 | } |
| 90 | |
| 91 | fn fixed_label(attachments: &[AttachmentInfo]) -> Option<&'static str> { |
| 92 | let has_type = |wanted: &str| { |
| 93 | attachments |
| 94 | .iter() |
| 95 | .any(|attachment| attachment.content_type.as_deref() == Some(wanted)) |
| 96 | }; |
| 97 | if has_type(FORWARDED_CONTENT_TYPE) { |
| 98 | Some(FORWARD_PREVIEW) |
| 99 | } else if has_type(FILE_CONTENT_TYPE) { |
| 100 | Some(FILE_PREVIEW) |
| 101 | } else if has_type(AUDIO_CARD_TYPE) { |
| 102 | Some(AUDIO_PREVIEW) |
| 103 | } else if has_type(LOOP_CARD_TYPE) { |
| 104 | Some(LOOP_PREVIEW) |
| 105 | } else { |
| 106 | None |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | fn collect(spans: &[Span], parts: &mut BodyParts) { |
| 111 | for span in spans { |
| 112 | match span { |
| 113 | Span::Text(value) | Span::Code(value) => parts.text.push_str(value), |
| 114 | Span::CodeBlock { code, .. } => { |
| 115 | parts.text.push(' '); |
| 116 | parts.text.push_str(code); |
| 117 | parts.text.push(' '); |
| 118 | } |
| 119 | Span::Mention { name, .. } => parts.text.push_str(name), |
| 120 | Span::CustomEmoji { name, .. } => { |
| 121 | parts.text.push(':'); |
| 122 | parts.text.push_str(name); |
| 123 | parts.text.push(':'); |
| 124 | } |
| 125 | Span::Quote { .. } => {} |
| 126 | Span::Image { hosted_content_url } if is_giphy_url(hosted_content_url) => { |
| 127 | parts.gifs += 1 |
| 128 | } |
| 129 | Span::Image { .. } => parts.images += 1, |
| 130 | Span::LineBreak | Span::Rule => parts.text.push(' '), |
| 131 | Span::Bold(children) |
| 132 | | Span::Italic(children) |
| 133 | | Span::Strike(children) |
| 134 | | Span::Superscript(children) |
| 135 | | Span::Subscript(children) |
| 136 | | Span::Sized(_, children) |
| 137 | | Span::Underline(children) |
| 138 | | Span::Colored { children, .. } |
| 139 | | Span::Indent { children, .. } |
| 140 | | Span::Link { children, .. } => collect(children, parts), |
| 141 | Span::Heading { children, .. } | Span::BlockQuote(children) => { |
| 142 | parts.text.push(' '); |
| 143 | collect(children, parts); |
| 144 | parts.text.push(' '); |
| 145 | } |
| 146 | Span::List { items, .. } => { |
| 147 | for item in items { |
| 148 | parts.text.push(' '); |
| 149 | collect(item, parts); |
| 150 | } |
| 151 | parts.text.push(' '); |
| 152 | } |
| 153 | Span::Table { rows, .. } => { |
| 154 | for cell in rows.iter().flatten() { |
| 155 | parts.text.push(' '); |
| 156 | collect(cell, parts); |
| 157 | } |
| 158 | parts.text.push(' '); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | fn collapse(text: &str) -> Option<String> { |
| 165 | let collapsed = text.split_whitespace().collect::<Vec<_>>().join(" "); |
| 166 | if collapsed.is_empty() { |
| 167 | return None; |
| 168 | } |
| 169 | Some(collapsed.chars().take(MAX_PREVIEW_CHARS).collect()) |
| 170 | } |
| 171 | |
| 172 | fn system_event_preview(detail: &Value) -> Option<&'static str> { |
| 173 | match detail.get("@odata.type")?.as_str()? { |
| 174 | RECORDING_TYPE => match detail.get("callRecordingStatus")?.as_str()? { |
| 175 | "initial" => Some(RECORDING_STARTED_PREVIEW), |
| 176 | "chunkFinished" => Some(RECORDING_STOPPED_PREVIEW), |
| 177 | "success" => Some(RECORDING_READY_PREVIEW), |
| 178 | _ => None, |
| 179 | }, |
| 180 | TRANSCRIPT_TYPE => Some(TRANSCRIPT_PREVIEW), |
| 181 | MEMBERS_JOINED_TYPE => Some(USER_JOINED_PREVIEW), |
| 182 | MEMBERS_ADDED_TYPE if added_only_self(detail) => Some(USER_JOINED_PREVIEW), |
| 183 | MEMBERS_ADDED_TYPE => Some(USER_ADDED_PREVIEW), |
| 184 | _ => None, |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | fn call_phase_preview(phase: CallPhase) -> Option<&'static str> { |
| 189 | match phase { |
| 190 | CallPhase::RecordingStarted => Some(RECORDING_STARTED_PREVIEW), |
| 191 | CallPhase::RecordingStopped => Some(RECORDING_STOPPED_PREVIEW), |
| 192 | CallPhase::RecordingSaved => Some(RECORDING_READY_PREVIEW), |
| 193 | CallPhase::TranscriptStarted => Some(TRANSCRIPT_PREVIEW), |
| 194 | CallPhase::Started | CallPhase::Ended => None, |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /// Teams leaves call start and end out of the chat list line; recordings and transcripts stay in it. |
| 199 | pub(crate) fn keeps_previous_line(record: &MessageRecord) -> bool { |
| 200 | call_event(record).is_some_and(|event| event.phase.is_call()) |
| 201 | } |
| 202 | |
| 203 | /// Teams keeps a chat in place when someone leaves it or it is renamed; only being added moves it. |
| 204 | pub(crate) fn moves_the_chat(record: &MessageRecord) -> bool { |
| 205 | roster_event(record).is_none_or(|roster| roster.change == RosterChange::Added) |
| 206 | } |
| 207 | |
| 208 | fn added_only_self(detail: &Value) -> bool { |
| 209 | let Some(initiator) = detail.pointer("/initiator/user/id").and_then(Value::as_str) else { |
| 210 | return false; |
| 211 | }; |
| 212 | let members = detail |
| 213 | .get("members") |
| 214 | .and_then(Value::as_array) |
| 215 | .map(Vec::as_slice) |
| 216 | .unwrap_or_default(); |
| 217 | !members.is_empty() |
| 218 | && members |
| 219 | .iter() |
| 220 | .all(|member| member.get("id").and_then(Value::as_str) == Some(initiator)) |
| 221 | } |
| 222 | |
| 223 | pub(crate) struct OwnedPreview { |
| 224 | pub text: Option<String>, |
| 225 | pub sender_id: Option<String>, |
| 226 | pub sender_name: Option<String>, |
| 227 | pub deleted: bool, |
| 228 | pub system: bool, |
| 229 | pub call: bool, |
| 230 | pub attachments_only: bool, |
| 231 | } |
| 232 | |
| 233 | impl OwnedPreview { |
| 234 | pub(crate) fn from_graph(message: &Message) -> Self { |
| 235 | let regular = message |
| 236 | .message_type |
| 237 | .as_deref() |
| 238 | .is_none_or(|kind| kind == "message"); |
| 239 | let deleted = message.is_deleted(); |
| 240 | let detail = message.event_detail.as_ref().filter(|_| !regular); |
| 241 | let call = detail |
| 242 | .and_then(CallEvent::from_graph) |
| 243 | .is_some_and(|event| event.phase.is_call()); |
| 244 | let html = message |
| 245 | .body |
| 246 | .as_ref() |
| 247 | .and_then(|body| body.content.as_deref()) |
| 248 | .filter(|_| regular && !deleted); |
| 249 | let text = if deleted { |
| 250 | None |
| 251 | } else if regular { |
| 252 | html.and_then(|html| message_preview(html, &[])) |
| 253 | } else { |
| 254 | detail.and_then(system_event_preview).map(str::to_owned) |
| 255 | }; |
| 256 | let attachments_only = |
| 257 | text.is_none() && html.is_some_and(|html| html.contains("<attachment")); |
| 258 | let sender = message.from.as_ref().filter(|_| regular); |
| 259 | OwnedPreview { |
| 260 | text, |
| 261 | sender_id: sender |
| 262 | .and_then(|sender| sender.user_id()) |
| 263 | .map(str::to_owned), |
| 264 | sender_name: sender |
| 265 | .and_then(|sender| sender.display_name()) |
| 266 | .map(str::to_owned), |
| 267 | deleted, |
| 268 | system: !regular, |
| 269 | call, |
| 270 | attachments_only, |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | pub(crate) fn from_record(record: &MessageRecord) -> Self { |
| 275 | let event = call_event(record); |
| 276 | let roster = roster_event(record); |
| 277 | let text = if record.deleted { |
| 278 | None |
| 279 | } else if let Some(event) = &event { |
| 280 | call_phase_preview(event.phase).map(str::to_owned) |
| 281 | } else if let Some(roster) = &roster { |
| 282 | (roster.change == RosterChange::Added).then(|| USER_ADDED_PREVIEW.to_owned()) |
| 283 | } else { |
| 284 | message_preview(&record.body_html, &attachments(record)) |
| 285 | }; |
| 286 | let sender = event.is_none() && roster.is_none(); |
| 287 | OwnedPreview { |
| 288 | text, |
| 289 | sender_id: record.sender_id.clone().filter(|_| sender), |
| 290 | sender_name: record.sender_name.clone().filter(|_| sender), |
| 291 | deleted: record.deleted, |
| 292 | system: !sender, |
| 293 | call: event.is_some_and(|event| event.phase.is_call()), |
| 294 | attachments_only: false, |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | pub(crate) fn as_store(&self) -> ChatPreview<'_> { |
| 299 | ChatPreview { |
| 300 | text: self.text.as_deref(), |
| 301 | sender_id: self.sender_id.as_deref(), |
| 302 | sender_name: self.sender_name.as_deref(), |
| 303 | deleted: self.deleted, |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | #[cfg(test)] |
| 309 | mod tests { |
| 310 | use super::*; |
| 311 | |
| 312 | fn attachment(content_type: &str) -> AttachmentInfo { |
| 313 | serde_json::from_value(serde_json::json!({ |
| 314 | "content_type": content_type, |
| 315 | "name": null, |
| 316 | "url": null, |
| 317 | "text": null, |
| 318 | })) |
| 319 | .unwrap() |
| 320 | } |
| 321 | |
| 322 | fn graph_message(value: serde_json::Value) -> Message { |
| 323 | serde_json::from_value(value).unwrap() |
| 324 | } |
| 325 | |
| 326 | #[test] |
| 327 | fn only_being_added_moves_a_chat() { |
| 328 | let roster = |change: &str| MessageRecord { |
| 329 | call_event_json: Some(format!( |
| 330 | r#"{{"roster":{{"change":"{change}","initiator_id":null,"initiator_name":null,"member_ids":["u"],"member_names":[null]}}}}"# |
| 331 | )), |
| 332 | ..MessageRecord::default() |
| 333 | }; |
| 334 | assert!(moves_the_chat(&roster("Added"))); |
| 335 | assert!(!moves_the_chat(&roster("Left"))); |
| 336 | assert!(!moves_the_chat(&roster("Removed"))); |
| 337 | assert!(moves_the_chat(&MessageRecord::default())); |
| 338 | } |
| 339 | |
| 340 | #[test] |
| 341 | fn strips_tags_and_collapses_whitespace() { |
| 342 | assert_eq!( |
| 343 | preview_text("<p>Hello <b>big</b>\n world</p><p>second</p>").as_deref(), |
| 344 | Some("Hello big world second") |
| 345 | ); |
| 346 | } |
| 347 | |
| 348 | #[test] |
| 349 | fn image_only_bodies_are_empty() { |
| 350 | assert_eq!(preview_text("<p><img src=\"x\"></p>"), None); |
| 351 | assert_eq!(preview_text(" "), None); |
| 352 | } |
| 353 | |
| 354 | #[test] |
| 355 | fn long_text_is_cut_to_the_limit() { |
| 356 | let long = "a ".repeat(400); |
| 357 | assert_eq!( |
| 358 | preview_text(&long).unwrap().chars().count(), |
| 359 | MAX_PREVIEW_CHARS |
| 360 | ); |
| 361 | } |
| 362 | |
| 363 | #[test] |
| 364 | fn plain_text_bodies_keep_entities_decoded() { |
| 365 | assert_eq!(preview_text("a & b").as_deref(), Some("a & b")); |
| 366 | } |
| 367 | |
| 368 | #[test] |
| 369 | fn emoji_and_mentions_read_like_teams() { |
| 370 | let html = r#"<p><at id="0">Robin Beck</at> kannst du? <emoji id="smile" alt="🙂" title="Smile"></emoji></p>"#; |
| 371 | assert_eq!( |
| 372 | preview_text(html).as_deref(), |
| 373 | Some("Robin Beck kannst du? 🙂") |
| 374 | ); |
| 375 | } |
| 376 | |
| 377 | #[test] |
| 378 | fn reply_quotes_are_left_out() { |
| 379 | let html = r#"<blockquote itemtype="http://schema.skype.com/Reply" itemid="1"><strong>Ada</strong><p>old</p></blockquote><p>new</p>"#; |
| 380 | assert_eq!(preview_text(html).as_deref(), Some("new")); |
| 381 | } |
| 382 | |
| 383 | #[test] |
| 384 | fn image_only_message_says_sent_an_image() { |
| 385 | let html = r#"<p><img src="https://graph.microsoft.com/v1.0/chats/19:x/messages/1/hostedContents/aWQ9/$value" alt="image" width="447" height="250" itemid="0-nde-d3-x"></p>"#; |
| 386 | assert_eq!(message_preview(html, &[]).as_deref(), Some(IMAGE_PREVIEW)); |
| 387 | } |
| 388 | |
| 389 | #[test] |
| 390 | fn text_with_an_image_keeps_only_the_text() { |
| 391 | let html = r#"<p>this is from QA:</p><p><img src="https://graph.microsoft.com/v1.0/x/$value"></p><p> </p>"#; |
| 392 | assert_eq!( |
| 393 | message_preview(html, &[]).as_deref(), |
| 394 | Some("this is from QA:") |
| 395 | ); |
| 396 | } |
| 397 | |
| 398 | #[test] |
| 399 | fn giphy_reads_as_gif() { |
| 400 | let html = r#"<p> </p><img alt="Happy Birthday Reaction GIF by The Office (GIF Image)" height="250" width="300" src="https://media2.giphy.com/media/g5R9dok94mrIvplmZd/giphy.gif?cid=x&ct=g"><p> </p>"#; |
| 401 | assert_eq!(message_preview(html, &[]).as_deref(), Some(GIF_PREVIEW)); |
| 402 | } |
| 403 | |
| 404 | #[test] |
| 405 | fn inline_video_reads_as_video() { |
| 406 | let html = r#"<p>look</p><video src="https://x/v.mp4"></video>"#; |
| 407 | assert_eq!(message_preview(html, &[]).as_deref(), Some(VIDEO_PREVIEW)); |
| 408 | } |
| 409 | |
| 410 | #[test] |
| 411 | fn attachment_only_bodies_get_a_label() { |
| 412 | let body = r#"<attachment id="1792149081221"></attachment>"#; |
| 413 | let cases = [ |
| 414 | (FORWARDED_CONTENT_TYPE, FORWARD_PREVIEW), |
| 415 | ("reference", FILE_PREVIEW), |
| 416 | ("application/vnd.microsoft.card.adaptive", CARD_PREVIEW), |
| 417 | ("application/vnd.microsoft.card.hero", CARD_PREVIEW), |
| 418 | (AUDIO_CARD_TYPE, AUDIO_PREVIEW), |
| 419 | (LOOP_CARD_TYPE, LOOP_PREVIEW), |
| 420 | ]; |
| 421 | for (content_type, expected) in cases { |
| 422 | assert_eq!( |
| 423 | message_preview(body, &[attachment(content_type)]).as_deref(), |
| 424 | Some(expected), |
| 425 | "{content_type}" |
| 426 | ); |
| 427 | } |
| 428 | assert_eq!(message_preview(body, &[]), None); |
| 429 | } |
| 430 | |
| 431 | #[test] |
| 432 | fn a_card_with_text_shows_the_card_text() { |
| 433 | let body = r#"<attachment id="1792149081221"></attachment>"#; |
| 434 | let mut card = attachment("application/vnd.microsoft.card.adaptive"); |
| 435 | card.content = Some( |
| 436 | serde_json::json!({ |
| 437 | "type": "AdaptiveCard", |
| 438 | "body": [ |
| 439 | {"type": "TextBlock", "text": "Ada Lovelace edited Engine Notes"}, |
| 440 | {"type": "TextBlock", "text": "Page updated"}, |
| 441 | ], |
| 442 | }) |
| 443 | .to_string(), |
| 444 | ); |
| 445 | assert_eq!( |
| 446 | message_preview(body, &[card]).as_deref(), |
| 447 | Some("Ada Lovelace edited Engine Notes Page updated") |
| 448 | ); |
| 449 | } |
| 450 | |
| 451 | #[test] |
| 452 | fn graph_attachment_only_bodies_keep_the_stored_line() { |
| 453 | let preview = |content: &str| { |
| 454 | OwnedPreview::from_graph(&graph_message(serde_json::json!({ |
| 455 | "id": "1", |
| 456 | "messageType": "message", |
| 457 | "body": {"contentType": "html", "content": content}, |
| 458 | }))) |
| 459 | }; |
| 460 | let card = preview(r#"<attachment id="1792149081221"></attachment>"#); |
| 461 | assert!(card.attachments_only); |
| 462 | assert_eq!(card.text, None); |
| 463 | assert!(!preview("<p>hi</p>").attachments_only); |
| 464 | } |
| 465 | |
| 466 | #[test] |
| 467 | fn a_file_with_text_shows_the_text() { |
| 468 | let body = r#"Hi Philipp,<br>Kann's dir zeigen.<attachment id="0cf57a46"></attachment>"#; |
| 469 | assert_eq!( |
| 470 | message_preview(body, &[attachment("reference")]).as_deref(), |
| 471 | Some("Hi Philipp, Kann's dir zeigen.") |
| 472 | ); |
| 473 | } |
| 474 | |
| 475 | #[test] |
| 476 | fn a_reply_shows_the_reply_text() { |
| 477 | let body = "<attachment id=\"1792136042729\"></attachment>\nwas war mit dependabot?"; |
| 478 | assert_eq!( |
| 479 | message_preview(body, &[attachment("messageReference")]).as_deref(), |
| 480 | Some("was war mit dependabot?") |
| 481 | ); |
| 482 | } |
| 483 | |
| 484 | #[test] |
| 485 | fn graph_recording_and_transcript_events_have_teams_text() { |
| 486 | let event = |detail: serde_json::Value| { |
| 487 | OwnedPreview::from_graph(&graph_message(serde_json::json!({ |
| 488 | "id": "1", |
| 489 | "messageType": "systemEventMessage", |
| 490 | "body": {"contentType": "html", "content": "<systemEventMessage/>"}, |
| 491 | "from": {"user": {"id": "u1", "displayName": "Ada Lovelace"}}, |
| 492 | "eventDetail": detail, |
| 493 | }))) |
| 494 | }; |
| 495 | let ready = event(serde_json::json!({ |
| 496 | "@odata.type": RECORDING_TYPE, |
| 497 | "callId": "a64b5e71", |
| 498 | "callRecordingDisplayName": "Call with Echo-20261015_120906-Meeting Recording.mp4", |
| 499 | "callRecordingStatus": "success", |
| 500 | })); |
| 501 | assert_eq!(ready.text.as_deref(), Some(RECORDING_READY_PREVIEW)); |
| 502 | assert!(ready.system && !ready.call); |
| 503 | assert_eq!(ready.sender_name, None); |
| 504 | let started = event(serde_json::json!({ |
| 505 | "@odata.type": RECORDING_TYPE, |
| 506 | "callRecordingStatus": "initial", |
| 507 | })); |
| 508 | assert_eq!(started.text.as_deref(), Some(RECORDING_STARTED_PREVIEW)); |
| 509 | let transcript = event(serde_json::json!({"@odata.type": TRANSCRIPT_TYPE})); |
| 510 | assert_eq!(transcript.text.as_deref(), Some(TRANSCRIPT_PREVIEW)); |
| 511 | } |
| 512 | |
| 513 | #[test] |
| 514 | fn graph_member_events_say_added_or_joined() { |
| 515 | let added = |members: serde_json::Value| { |
| 516 | OwnedPreview::from_graph(&graph_message(serde_json::json!({ |
| 517 | "id": "1", |
| 518 | "messageType": "systemEventMessage", |
| 519 | "eventDetail": { |
| 520 | "@odata.type": MEMBERS_ADDED_TYPE, |
| 521 | "members": members, |
| 522 | "initiator": {"user": {"id": "me"}}, |
| 523 | }, |
| 524 | }))) |
| 525 | .text |
| 526 | }; |
| 527 | assert_eq!( |
| 528 | added(serde_json::json!([{"id": "other"}])).as_deref(), |
| 529 | Some(USER_ADDED_PREVIEW) |
| 530 | ); |
| 531 | assert_eq!( |
| 532 | added(serde_json::json!([{"id": "me"}])).as_deref(), |
| 533 | Some(USER_JOINED_PREVIEW) |
| 534 | ); |
| 535 | } |
| 536 | |
| 537 | #[test] |
| 538 | fn graph_call_events_are_marked_to_keep_the_previous_line() { |
| 539 | let preview = OwnedPreview::from_graph(&graph_message(serde_json::json!({ |
| 540 | "id": "1", |
| 541 | "messageType": "systemEventMessage", |
| 542 | "eventDetail": { |
| 543 | "@odata.type": "#microsoft.graph.callEndedEventMessageDetail", |
| 544 | "callId": "c1", |
| 545 | "callDuration": "PT30S", |
| 546 | "callEventType": "call", |
| 547 | }, |
| 548 | }))); |
| 549 | assert!(preview.call); |
| 550 | assert_eq!(preview.text, None); |
| 551 | } |
| 552 | |
| 553 | #[test] |
| 554 | fn graph_deleted_flag_marks_the_preview_deleted() { |
| 555 | let preview = OwnedPreview::from_graph(&graph_message(serde_json::json!({ |
| 556 | "id": "1", |
| 557 | "messageType": "message", |
| 558 | "isDeleted": true, |
| 559 | "body": {"contentType": "html", "content": "secret"}, |
| 560 | }))); |
| 561 | assert!(preview.deleted); |
| 562 | assert_eq!(preview.text, None); |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn graph_image_preview_says_sent_an_image() { |
| 567 | let preview = OwnedPreview::from_graph(&graph_message(serde_json::json!({ |
| 568 | "id": "1", |
| 569 | "messageType": "message", |
| 570 | "body": {"contentType": "html", "content": "<p><img src=\"https://graph.microsoft.com/x/$value\"></p>"}, |
| 571 | "from": {"user": {"id": "u1", "displayName": "Lukas Berg"}}, |
| 572 | }))); |
| 573 | assert_eq!(preview.text.as_deref(), Some(IMAGE_PREVIEW)); |
| 574 | assert_eq!(preview.sender_name.as_deref(), Some("Lukas Berg")); |
| 575 | } |
| 576 | |
| 577 | fn call_record(phase: &str) -> MessageRecord { |
| 578 | MessageRecord { |
| 579 | conversation_id: "19:chat".into(), |
| 580 | message_id: "1".into(), |
| 581 | sender_id: Some("u1".into()), |
| 582 | sender_name: Some("Ada Lovelace".into()), |
| 583 | call_event_json: Some( |
| 584 | serde_json::json!({ |
| 585 | "phase": phase, |
| 586 | "call_type": "Call", |
| 587 | "call_id": "c1", |
| 588 | "duration_seconds": null, |
| 589 | "initiator_id": null, |
| 590 | "initiator_name": null, |
| 591 | "participant_ids": [], |
| 592 | }) |
| 593 | .to_string(), |
| 594 | ), |
| 595 | ..Default::default() |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | #[test] |
| 600 | fn stored_recordings_fill_the_line_and_calls_keep_the_previous_one() { |
| 601 | let saved = call_record("RecordingSaved"); |
| 602 | assert!(!keeps_previous_line(&saved)); |
| 603 | let preview = OwnedPreview::from_record(&saved); |
| 604 | assert_eq!(preview.text.as_deref(), Some(RECORDING_READY_PREVIEW)); |
| 605 | assert_eq!(preview.sender_name, None); |
| 606 | assert!(!preview.call); |
| 607 | let transcript = OwnedPreview::from_record(&call_record("TranscriptStarted")); |
| 608 | assert_eq!(transcript.text.as_deref(), Some(TRANSCRIPT_PREVIEW)); |
| 609 | let ended = call_record("Ended"); |
| 610 | assert!(keeps_previous_line(&ended)); |
| 611 | assert!(OwnedPreview::from_record(&ended).call); |
| 612 | } |
| 613 | } |