| 1 | use std::collections::{HashMap, HashSet}; |
| 2 | |
| 3 | use app_base::format::Zone; |
| 4 | use chrono::{DateTime, NaiveDate, Utc}; |
| 5 | use store::MessageRecord; |
| 6 | use teams_core::roster_event::RosterEvent; |
| 7 | use teams_core::{CallEvent, CallPhase, CallType, ImageRef, format_duration}; |
| 8 | |
| 9 | use crate::format; |
| 10 | |
| 11 | const MAX_AVATARS: usize = 4; |
| 12 | const RECAP_FALLBACK_TITLE: &str = "Meeting"; |
| 13 | const RECAP_DATE_FORMAT: &str = "%A, %-d %B %Y"; |
| 14 | const AUTO_STOP_SECONDS: i64 = 5; |
| 15 | const WEEKDAY_STAMP_DAYS: i64 = 6; |
| 16 | const OWN_NAME: &str = "You"; |
| 17 | const UNKNOWN_USER: &str = "Unknown User"; |
| 18 | const MISSED_CALL_NOTICE: &str = "Call missed"; |
| 19 | |
| 20 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 21 | pub enum CallIcon { |
| 22 | Call, |
| 23 | Missed, |
| 24 | Video, |
| 25 | Record, |
| 26 | RecordStop, |
| 27 | Saved, |
| 28 | Roster, |
| 29 | } |
| 30 | |
| 31 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 32 | pub struct CallParticipant { |
| 33 | pub user_id: String, |
| 34 | pub name: String, |
| 35 | } |
| 36 | |
| 37 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 38 | pub struct RecordingCard { |
| 39 | pub message_id: String, |
| 40 | pub duration: Option<String>, |
| 41 | pub url: Option<String>, |
| 42 | pub thumbnail: Option<ImageRef>, |
| 43 | pub transcript: bool, |
| 44 | pub expires_in_days: Option<i64>, |
| 45 | } |
| 46 | |
| 47 | /// The card Teams puts under the "Meeting ended" line of a meeting with a recording. |
| 48 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 49 | pub struct RecapCard { |
| 50 | pub title: String, |
| 51 | pub date_line: String, |
| 52 | pub recording: RecordingCard, |
| 53 | } |
| 54 | |
| 55 | /// A meeting or recording event as Teams draws it: one muted line, icon in the avatar column. |
| 56 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 57 | pub struct CallNotice { |
| 58 | pub stamp: String, |
| 59 | pub text: String, |
| 60 | pub emphasis: Option<String>, |
| 61 | } |
| 62 | |
| 63 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 64 | pub struct CallCard { |
| 65 | pub title: String, |
| 66 | pub detail: String, |
| 67 | pub icon: CallIcon, |
| 68 | pub missed: bool, |
| 69 | pub started: bool, |
| 70 | pub participants: Vec<CallParticipant>, |
| 71 | pub extra_participants: usize, |
| 72 | pub notice: CallNotice, |
| 73 | pub recap: Option<RecapCard>, |
| 74 | } |
| 75 | |
| 76 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 77 | pub struct MeetingSlot { |
| 78 | pub start: DateTime<Utc>, |
| 79 | pub end: DateTime<Utc>, |
| 80 | } |
| 81 | |
| 82 | /// What the meeting rows need beyond the messages: the chat topic, the calendar occurrences |
| 83 | /// and the recording expiry the Recap tab reads, keyed by the saved recording message id. |
| 84 | #[derive(Debug, Clone, Default, PartialEq)] |
| 85 | pub struct MeetingInfo { |
| 86 | pub title: Option<String>, |
| 87 | pub slots: Vec<MeetingSlot>, |
| 88 | pub expiry_days: HashMap<String, i64>, |
| 89 | } |
| 90 | |
| 91 | pub struct CallNames<'a> { |
| 92 | pub my_user_id: Option<&'a str>, |
| 93 | pub names: &'a HashMap<String, String>, |
| 94 | } |
| 95 | |
| 96 | impl CallNames<'_> { |
| 97 | fn name(&self, user_id: Option<&str>, given: Option<&str>) -> Option<String> { |
| 98 | given |
| 99 | .filter(|name| !name.is_empty()) |
| 100 | .map(str::to_owned) |
| 101 | .or_else(|| self.names.get(user_id?).cloned()) |
| 102 | } |
| 103 | |
| 104 | fn roster_name(&self, user_id: Option<&str>, given: Option<&str>) -> String { |
| 105 | if user_id.is_some() && user_id == self.my_user_id { |
| 106 | return OWN_NAME.to_owned(); |
| 107 | } |
| 108 | self.name(user_id, given) |
| 109 | .unwrap_or_else(|| UNKNOWN_USER.to_owned()) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// A member added, removed or leaving: one muted line with the people icon, like Teams. |
| 114 | pub fn roster_card( |
| 115 | event: &RosterEvent, |
| 116 | created_at: DateTime<Utc>, |
| 117 | names: &CallNames, |
| 118 | clock: &CallClock, |
| 119 | ) -> CallCard { |
| 120 | CallCard { |
| 121 | title: String::new(), |
| 122 | detail: String::new(), |
| 123 | icon: CallIcon::Roster, |
| 124 | missed: false, |
| 125 | started: false, |
| 126 | participants: Vec::new(), |
| 127 | extra_participants: 0, |
| 128 | notice: CallNotice { |
| 129 | stamp: notice_stamp(created_at, clock), |
| 130 | text: event.text(|user_id, given| names.roster_name(user_id, given)), |
| 131 | emphasis: None, |
| 132 | }, |
| 133 | recap: None, |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | pub struct CallClock<'a> { |
| 138 | pub offset: Zone, |
| 139 | pub today: NaiveDate, |
| 140 | pub meeting: &'a MeetingInfo, |
| 141 | } |
| 142 | |
| 143 | struct SavedRecording { |
| 144 | message_id: String, |
| 145 | event: CallEvent, |
| 146 | } |
| 147 | |
| 148 | /// Which call events of a timeline show as rows: Teams keeps one row per recording start and stop, |
| 149 | /// hides transcript notices and, for calls (not meetings), the start once the call ended. |
| 150 | pub struct CallTimeline { |
| 151 | hidden: Vec<bool>, |
| 152 | finished: HashSet<String>, |
| 153 | transcripts: HashSet<String>, |
| 154 | newest_open_start: Option<usize>, |
| 155 | started_at: HashMap<String, DateTime<Utc>>, |
| 156 | ended_at: HashMap<String, DateTime<Utc>>, |
| 157 | saved: HashMap<String, SavedRecording>, |
| 158 | } |
| 159 | |
| 160 | impl CallTimeline { |
| 161 | pub fn new(events: &[Option<CallEvent>]) -> Self { |
| 162 | Self::with_records(events, &[]) |
| 163 | } |
| 164 | |
| 165 | pub fn with_records(events: &[Option<CallEvent>], records: &[&MessageRecord]) -> Self { |
| 166 | let finished: HashSet<String> = events |
| 167 | .iter() |
| 168 | .flatten() |
| 169 | .filter(|event| event.phase == CallPhase::Ended) |
| 170 | .map(|event| event.call_id.clone()) |
| 171 | .collect(); |
| 172 | let transcripts: HashSet<String> = events |
| 173 | .iter() |
| 174 | .flatten() |
| 175 | .filter(|event| event.phase == CallPhase::TranscriptStarted) |
| 176 | .map(|event| event.call_id.clone()) |
| 177 | .collect(); |
| 178 | let mut shown_once: HashSet<(String, CallPhase)> = HashSet::new(); |
| 179 | let hidden = events |
| 180 | .iter() |
| 181 | .map(|event| { |
| 182 | let Some(event) = event else { |
| 183 | return false; |
| 184 | }; |
| 185 | match event.phase { |
| 186 | CallPhase::TranscriptStarted => true, |
| 187 | CallPhase::RecordingStarted | CallPhase::RecordingStopped => { |
| 188 | !shown_once.insert((event.call_id.clone(), event.phase)) |
| 189 | } |
| 190 | CallPhase::Started => { |
| 191 | event.call_type == CallType::Call && finished.contains(&event.call_id) |
| 192 | } |
| 193 | _ => false, |
| 194 | } |
| 195 | }) |
| 196 | .collect(); |
| 197 | let newest_open_start = events.iter().rposition(|event| { |
| 198 | event.as_ref().is_some_and(|event| { |
| 199 | event.phase == CallPhase::Started && !finished.contains(&event.call_id) |
| 200 | }) |
| 201 | }); |
| 202 | let mut started_at = HashMap::new(); |
| 203 | let mut ended_at = HashMap::new(); |
| 204 | let mut saved = HashMap::new(); |
| 205 | for (event, record) in events.iter().zip(records) { |
| 206 | let Some(event) = event else { |
| 207 | continue; |
| 208 | }; |
| 209 | match event.phase { |
| 210 | CallPhase::Started => { |
| 211 | started_at |
| 212 | .entry(event.call_id.clone()) |
| 213 | .or_insert(record.created_at); |
| 214 | } |
| 215 | CallPhase::Ended => { |
| 216 | ended_at.insert(event.call_id.clone(), record.created_at); |
| 217 | } |
| 218 | CallPhase::RecordingSaved => { |
| 219 | saved.insert( |
| 220 | event.call_id.clone(), |
| 221 | SavedRecording { |
| 222 | message_id: record.message_id.clone(), |
| 223 | event: event.clone(), |
| 224 | }, |
| 225 | ); |
| 226 | } |
| 227 | _ => {} |
| 228 | } |
| 229 | } |
| 230 | CallTimeline { |
| 231 | hidden, |
| 232 | finished, |
| 233 | transcripts, |
| 234 | newest_open_start, |
| 235 | started_at, |
| 236 | ended_at, |
| 237 | saved, |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | pub fn hides(&self, index: usize) -> bool { |
| 242 | self.hidden.get(index).copied().unwrap_or(false) |
| 243 | } |
| 244 | |
| 245 | pub fn is_live_start(&self, index: usize) -> bool { |
| 246 | self.newest_open_start == Some(index) |
| 247 | } |
| 248 | |
| 249 | pub fn finished(&self, call_id: &str) -> bool { |
| 250 | self.finished.contains(call_id) |
| 251 | } |
| 252 | |
| 253 | pub fn has_transcript(&self, call_id: &str) -> bool { |
| 254 | self.transcripts.contains(call_id) |
| 255 | } |
| 256 | |
| 257 | /// A recording that stops with the meeting end has no one who stopped it; Teams then says |
| 258 | /// "Recording stopped." instead of naming the recorder. |
| 259 | fn stopped_by_meeting_end(&self, event: &CallEvent, created_at: DateTime<Utc>) -> bool { |
| 260 | self.ended_at.get(&event.call_id).is_some_and(|ended_at| { |
| 261 | (*ended_at - created_at).num_seconds().abs() <= AUTO_STOP_SECONDS |
| 262 | }) |
| 263 | } |
| 264 | |
| 265 | fn recap( |
| 266 | &self, |
| 267 | ended: &CallEvent, |
| 268 | ended_at: DateTime<Utc>, |
| 269 | clock: &CallClock, |
| 270 | ) -> Option<RecapCard> { |
| 271 | if ended.phase != CallPhase::Ended || ended.call_type != CallType::Meeting { |
| 272 | return None; |
| 273 | } |
| 274 | let saved = self.saved.get(&ended.call_id)?; |
| 275 | let started_at = self |
| 276 | .started_at |
| 277 | .get(&ended.call_id) |
| 278 | .copied() |
| 279 | .or_else(|| { |
| 280 | ended |
| 281 | .duration_seconds |
| 282 | .map(|seconds| ended_at - chrono::Duration::seconds(seconds as i64)) |
| 283 | }) |
| 284 | .unwrap_or(ended_at); |
| 285 | let recording = recording_card( |
| 286 | &saved.event, |
| 287 | &saved.message_id, |
| 288 | self.has_transcript(&ended.call_id), |
| 289 | clock.meeting, |
| 290 | )?; |
| 291 | let title = clock |
| 292 | .meeting |
| 293 | .title |
| 294 | .clone() |
| 295 | .filter(|title| !title.trim().is_empty()) |
| 296 | .or_else(|| saved.event.recording.as_ref()?.title()) |
| 297 | .unwrap_or_else(|| RECAP_FALLBACK_TITLE.to_owned()); |
| 298 | Some(RecapCard { |
| 299 | title, |
| 300 | date_line: recap_date_line(started_at, ended_at, clock), |
| 301 | recording, |
| 302 | }) |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /// Teams dates the recap card with the scheduled occurrence; without a calendar match the call's |
| 307 | /// own start and end stand in. |
| 308 | fn recap_date_line( |
| 309 | started_at: DateTime<Utc>, |
| 310 | ended_at: DateTime<Utc>, |
| 311 | clock: &CallClock, |
| 312 | ) -> String { |
| 313 | let slot = clock |
| 314 | .meeting |
| 315 | .slots |
| 316 | .iter() |
| 317 | .filter(|slot| slot.start <= ended_at && slot.end >= started_at) |
| 318 | .min_by_key(|slot| (slot.start - started_at).num_seconds().abs()) |
| 319 | .copied() |
| 320 | .unwrap_or(MeetingSlot { |
| 321 | start: started_at, |
| 322 | end: ended_at, |
| 323 | }); |
| 324 | format!( |
| 325 | "{} - {}", |
| 326 | clock.offset.at(slot.start).format(&format!( |
| 327 | "{RECAP_DATE_FORMAT} {}", |
| 328 | app_base::format::clock_format() |
| 329 | )), |
| 330 | clock |
| 331 | .offset |
| 332 | .at(slot.end) |
| 333 | .format(app_base::format::clock_format()) |
| 334 | ) |
| 335 | } |
| 336 | |
| 337 | /// Teams writes the weekday in full on these lines ("Thursday 11:45"), unlike message times. |
| 338 | fn notice_stamp(time: DateTime<Utc>, clock: &CallClock) -> String { |
| 339 | let local = clock.offset.at(time); |
| 340 | let days_ago = (clock.today - local.date_naive()).num_days(); |
| 341 | if (2..=WEEKDAY_STAMP_DAYS).contains(&days_ago) { |
| 342 | format!( |
| 343 | "{} {}", |
| 344 | local.format("%A"), |
| 345 | local.format(app_base::format::clock_format()) |
| 346 | ) |
| 347 | } else { |
| 348 | format::post_time_label(time, clock.today, clock.offset) |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | fn participants(event: &CallEvent, names: &CallNames) -> Vec<CallParticipant> { |
| 353 | event |
| 354 | .participant_ids |
| 355 | .iter() |
| 356 | .enumerate() |
| 357 | .filter_map(|(index, user_id)| { |
| 358 | let given = event.participant_names.get(index).map(String::as_str); |
| 359 | Some(CallParticipant { |
| 360 | user_id: user_id.clone(), |
| 361 | name: names.name(Some(user_id), given)?, |
| 362 | }) |
| 363 | }) |
| 364 | .collect() |
| 365 | } |
| 366 | |
| 367 | fn recording_card( |
| 368 | event: &CallEvent, |
| 369 | message_id: &str, |
| 370 | transcript: bool, |
| 371 | meeting: &MeetingInfo, |
| 372 | ) -> Option<RecordingCard> { |
| 373 | let recording = event.recording.as_ref()?; |
| 374 | Some(RecordingCard { |
| 375 | message_id: message_id.to_owned(), |
| 376 | duration: event |
| 377 | .duration_seconds |
| 378 | .filter(|seconds| *seconds > 0) |
| 379 | .map(format_duration), |
| 380 | url: recording.url.clone(), |
| 381 | thumbnail: recording.thumbnail_url().map(|url| ImageRef { |
| 382 | id: format!("recording-{message_id}"), |
| 383 | url, |
| 384 | width: None, |
| 385 | height: None, |
| 386 | alt: None, |
| 387 | }), |
| 388 | transcript, |
| 389 | expires_in_days: meeting.expiry_days.get(message_id).copied(), |
| 390 | }) |
| 391 | } |
| 392 | |
| 393 | fn day_count(days: i64) -> String { |
| 394 | if days == 1 { |
| 395 | "1 day".to_owned() |
| 396 | } else { |
| 397 | format!("{days} days") |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | struct NoticeSource<'a> { |
| 402 | event: &'a CallEvent, |
| 403 | message_id: &'a str, |
| 404 | missed: bool, |
| 405 | stopped_by_meeting_end: bool, |
| 406 | } |
| 407 | |
| 408 | fn notice_text(source: NoticeSource, names: &CallNames, meeting: &MeetingInfo) -> String { |
| 409 | let NoticeSource { |
| 410 | event, |
| 411 | message_id, |
| 412 | missed, |
| 413 | stopped_by_meeting_end, |
| 414 | } = source; |
| 415 | let initiator = || { |
| 416 | names.name( |
| 417 | event.initiator_id.as_deref(), |
| 418 | event.initiator_name.as_deref(), |
| 419 | ) |
| 420 | }; |
| 421 | match (event.phase, event.call_type) { |
| 422 | _ if missed => MISSED_CALL_NOTICE.to_owned(), |
| 423 | (CallPhase::Ended, CallType::Meeting) => format!("{}:", event.title(false)), |
| 424 | (CallPhase::Ended, CallType::Call) => event.title(false).to_owned(), |
| 425 | (CallPhase::RecordingStarted, _) => match initiator() { |
| 426 | Some(name) => format!("{name} started recording."), |
| 427 | None => event.title(false).to_owned(), |
| 428 | }, |
| 429 | (CallPhase::RecordingStopped, _) => match initiator().filter(|_| !stopped_by_meeting_end) { |
| 430 | Some(name) => format!("{name} stopped recording."), |
| 431 | None => format!("{}.", event.title(false)), |
| 432 | }, |
| 433 | (CallPhase::RecordingSaved, _) => { |
| 434 | let saved = saved_to(event, names); |
| 435 | match meeting.expiry_days.get(message_id) { |
| 436 | Some(days) => format!("{saved} and will expire in {}.", day_count(*days)), |
| 437 | None => format!("{saved}."), |
| 438 | } |
| 439 | } |
| 440 | _ => event.title(false).to_owned(), |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | fn saved_to(event: &CallEvent, names: &CallNames) -> String { |
| 445 | let organizer_id = event |
| 446 | .recording |
| 447 | .as_ref() |
| 448 | .and_then(|recording| recording.organizer_id.as_deref()); |
| 449 | if organizer_id.is_some() && organizer_id == names.my_user_id { |
| 450 | return "Recording has been saved to your OneDrive".to_owned(); |
| 451 | } |
| 452 | match names.name(organizer_id, None) { |
| 453 | Some(owner) => format!("Recording has been saved to {owner}'s OneDrive"), |
| 454 | None => "Recording has been saved to OneDrive".to_owned(), |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | fn title(event: &CallEvent, missed: bool, names: &CallNames) -> String { |
| 459 | match event.phase { |
| 460 | CallPhase::RecordingStarted => { |
| 461 | match names.name( |
| 462 | event.initiator_id.as_deref(), |
| 463 | event.initiator_name.as_deref(), |
| 464 | ) { |
| 465 | Some(name) => format!("{name} started recording"), |
| 466 | None => event.title(false).to_owned(), |
| 467 | } |
| 468 | } |
| 469 | CallPhase::RecordingSaved => saved_to(event, names), |
| 470 | _ => event.title(missed).to_owned(), |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | pub struct CallSource<'a> { |
| 475 | pub event: &'a CallEvent, |
| 476 | pub message_id: &'a str, |
| 477 | pub created_at: DateTime<Utc>, |
| 478 | pub time: &'a str, |
| 479 | } |
| 480 | |
| 481 | pub fn call_card( |
| 482 | source: CallSource, |
| 483 | names: &CallNames, |
| 484 | clock: &CallClock, |
| 485 | timeline: &CallTimeline, |
| 486 | ) -> CallCard { |
| 487 | let CallSource { |
| 488 | event, |
| 489 | message_id, |
| 490 | created_at, |
| 491 | time, |
| 492 | } = source; |
| 493 | let missed = event.missed(names.my_user_id); |
| 494 | let detail = match (event.phase, event.duration_seconds) { |
| 495 | (CallPhase::Ended, Some(seconds)) if !missed => { |
| 496 | format!("{} \u{b7} {time}", format_duration(seconds)) |
| 497 | } |
| 498 | _ => time.to_owned(), |
| 499 | }; |
| 500 | let mut people = if event.phase == CallPhase::Ended && event.call_type == CallType::Meeting { |
| 501 | participants(event, names) |
| 502 | } else { |
| 503 | Vec::new() |
| 504 | }; |
| 505 | let extra_participants = people.len().saturating_sub(MAX_AVATARS); |
| 506 | people.truncate(MAX_AVATARS); |
| 507 | let notice = CallNotice { |
| 508 | stamp: notice_stamp(created_at, clock), |
| 509 | text: notice_text( |
| 510 | NoticeSource { |
| 511 | event, |
| 512 | message_id, |
| 513 | missed, |
| 514 | stopped_by_meeting_end: timeline.stopped_by_meeting_end(event, created_at), |
| 515 | }, |
| 516 | names, |
| 517 | clock.meeting, |
| 518 | ), |
| 519 | emphasis: event |
| 520 | .duration_seconds |
| 521 | .filter(|_| event.phase == CallPhase::Ended && !missed) |
| 522 | .map(format_duration), |
| 523 | }; |
| 524 | CallCard { |
| 525 | title: title(event, missed, names), |
| 526 | detail, |
| 527 | icon: match (missed, event.phase, event.call_type) { |
| 528 | (true, _, _) => CallIcon::Missed, |
| 529 | (false, CallPhase::RecordingStarted, _) => CallIcon::Record, |
| 530 | (false, CallPhase::RecordingSaved, _) => CallIcon::Saved, |
| 531 | (false, CallPhase::RecordingStopped | CallPhase::TranscriptStarted, _) => { |
| 532 | CallIcon::RecordStop |
| 533 | } |
| 534 | (false, _, CallType::Meeting) => CallIcon::Video, |
| 535 | (false, _, CallType::Call) => CallIcon::Call, |
| 536 | }, |
| 537 | missed, |
| 538 | started: event.phase == CallPhase::Started, |
| 539 | participants: people, |
| 540 | extra_participants, |
| 541 | recap: timeline.recap(event, created_at, clock), |
| 542 | notice, |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | #[cfg(test)] |
| 547 | mod tests { |
| 548 | use chrono::FixedOffset; |
| 549 | use chrono::TimeZone; |
| 550 | use teams_core::Recording; |
| 551 | |
| 552 | use super::*; |
| 553 | |
| 554 | fn event(phase: CallPhase, call_type: CallType, call_id: &str) -> CallEvent { |
| 555 | CallEvent { |
| 556 | phase, |
| 557 | call_type, |
| 558 | call_id: call_id.to_owned(), |
| 559 | duration_seconds: None, |
| 560 | initiator_id: Some("organizer".to_owned()), |
| 561 | initiator_name: None, |
| 562 | participant_ids: Vec::new(), |
| 563 | participant_names: Vec::new(), |
| 564 | recording: None, |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | fn fixture_records() -> Vec<MessageRecord> { |
| 569 | let path = format!( |
| 570 | "{}/../core/tests/fixtures/call_events/meeting_chat_recording.json", |
| 571 | env!("CARGO_MANIFEST_DIR") |
| 572 | ); |
| 573 | let page: serde_json::Value = |
| 574 | serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap(); |
| 575 | page["value"] |
| 576 | .as_array() |
| 577 | .unwrap() |
| 578 | .iter() |
| 579 | .rev() |
| 580 | .map(|message| { |
| 581 | let message: graph::Message = serde_json::from_value(message.clone()).unwrap(); |
| 582 | teams_core::message_record("meeting-chat", &message).unwrap() |
| 583 | }) |
| 584 | .collect() |
| 585 | } |
| 586 | |
| 587 | fn names() -> HashMap<String, String> { |
| 588 | HashMap::from([ |
| 589 | ( |
| 590 | "00000000-0000-4000-8000-000000000002".to_owned(), |
| 591 | "Vera Organizer".to_owned(), |
| 592 | ), |
| 593 | ( |
| 594 | "00000000-0000-4000-8000-000000000004".to_owned(), |
| 595 | "Sam Recorder".to_owned(), |
| 596 | ), |
| 597 | ]) |
| 598 | } |
| 599 | |
| 600 | fn cards(records: &[MessageRecord], meeting: &MeetingInfo) -> Vec<CallCard> { |
| 601 | let events: Vec<Option<CallEvent>> = records.iter().map(teams_core::call_event).collect(); |
| 602 | let timeline = CallTimeline::with_records(&events, &records.iter().collect::<Vec<_>>()); |
| 603 | let names = names(); |
| 604 | let lookup = CallNames { |
| 605 | my_user_id: Some("me"), |
| 606 | names: &names, |
| 607 | }; |
| 608 | let clock = CallClock { |
| 609 | offset: FixedOffset::east_opt(0).unwrap().into(), |
| 610 | today: NaiveDate::from_ymd_opt(2026, 10, 30).unwrap(), |
| 611 | meeting, |
| 612 | }; |
| 613 | events |
| 614 | .iter() |
| 615 | .zip(records) |
| 616 | .enumerate() |
| 617 | .filter(|(index, _)| !timeline.hides(*index)) |
| 618 | .map(|(_, (event, record))| { |
| 619 | call_card( |
| 620 | CallSource { |
| 621 | event: event.as_ref().unwrap(), |
| 622 | message_id: &record.message_id, |
| 623 | created_at: record.created_at, |
| 624 | time: "16:15", |
| 625 | }, |
| 626 | &lookup, |
| 627 | &clock, |
| 628 | &timeline, |
| 629 | ) |
| 630 | }) |
| 631 | .collect() |
| 632 | } |
| 633 | |
| 634 | fn notice_line(card: &CallCard) -> String { |
| 635 | let notice = &card.notice; |
| 636 | match ¬ice.emphasis { |
| 637 | Some(emphasis) => format!("{} {emphasis}", notice.text), |
| 638 | None => notice.text.clone(), |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | #[test] |
| 643 | fn recorded_meeting_shows_as_teams_notices() { |
| 644 | let shown = cards(&fixture_records(), &MeetingInfo::default()); |
| 645 | let lines: Vec<String> = shown.iter().map(notice_line).collect(); |
| 646 | assert_eq!( |
| 647 | lines, |
| 648 | [ |
| 649 | "Meeting started", |
| 650 | "Sam Recorder started recording.", |
| 651 | "Recording stopped.", |
| 652 | "Meeting ended: 16m 28s", |
| 653 | "Recording has been saved to Vera Organizer's OneDrive.", |
| 654 | ] |
| 655 | ); |
| 656 | let icons: Vec<CallIcon> = shown.iter().map(|card| card.icon).collect(); |
| 657 | assert_eq!( |
| 658 | icons, |
| 659 | [ |
| 660 | CallIcon::Video, |
| 661 | CallIcon::Record, |
| 662 | CallIcon::RecordStop, |
| 663 | CallIcon::Video, |
| 664 | CallIcon::Saved, |
| 665 | ] |
| 666 | ); |
| 667 | assert!( |
| 668 | shown |
| 669 | .iter() |
| 670 | .all(|card| card.recap.is_none() == (card.title != "Meeting ended")) |
| 671 | ); |
| 672 | } |
| 673 | |
| 674 | #[test] |
| 675 | fn a_recording_stopped_before_the_end_names_who_stopped_it() { |
| 676 | let mut records = fixture_records(); |
| 677 | for record in records.iter_mut() { |
| 678 | if teams_core::call_event(record) |
| 679 | .is_some_and(|event| event.phase == CallPhase::RecordingStopped) |
| 680 | { |
| 681 | record.created_at -= chrono::Duration::minutes(2); |
| 682 | } |
| 683 | } |
| 684 | let shown = cards(&records, &MeetingInfo::default()); |
| 685 | assert_eq!(notice_line(&shown[2]), "Sam Recorder stopped recording."); |
| 686 | } |
| 687 | |
| 688 | #[test] |
| 689 | fn ended_meeting_carries_the_recap_card_of_its_recording() { |
| 690 | let records = fixture_records(); |
| 691 | let saved_id = records.last().unwrap().message_id.clone(); |
| 692 | let meeting = MeetingInfo { |
| 693 | title: Some("Weekly sync".to_owned()), |
| 694 | slots: Vec::new(), |
| 695 | expiry_days: HashMap::from([(saved_id.clone(), 56)]), |
| 696 | }; |
| 697 | let shown = cards(&records, &meeting); |
| 698 | let ended = shown |
| 699 | .iter() |
| 700 | .find(|card| card.title == "Meeting ended") |
| 701 | .unwrap(); |
| 702 | let recap = ended.recap.as_ref().unwrap(); |
| 703 | assert_eq!(recap.title, "Weekly sync"); |
| 704 | assert_eq!(recap.recording.message_id, saved_id); |
| 705 | assert_eq!(recap.recording.duration.as_deref(), Some("13m 34s")); |
| 706 | assert_eq!(recap.recording.expires_in_days, Some(56)); |
| 707 | assert!(recap.recording.transcript); |
| 708 | assert!( |
| 709 | recap |
| 710 | .recording |
| 711 | .thumbnail |
| 712 | .as_ref() |
| 713 | .unwrap() |
| 714 | .url |
| 715 | .contains("/thumbnails/") |
| 716 | ); |
| 717 | assert!(shown.iter().filter(|card| card.recap.is_some()).count() == 1); |
| 718 | assert_eq!( |
| 719 | notice_line(shown.last().unwrap()), |
| 720 | "Recording has been saved to Vera Organizer's OneDrive and will expire in 56 days." |
| 721 | ); |
| 722 | } |
| 723 | |
| 724 | #[test] |
| 725 | fn recap_date_prefers_the_calendar_occurrence() { |
| 726 | let started_at = Utc.with_ymd_and_hms(2026, 10, 15, 9, 46, 0).unwrap(); |
| 727 | let ended_at = Utc.with_ymd_and_hms(2026, 10, 15, 9, 55, 0).unwrap(); |
| 728 | let mut meeting = MeetingInfo::default(); |
| 729 | let clock = |meeting: &MeetingInfo| { |
| 730 | recap_date_line( |
| 731 | started_at, |
| 732 | ended_at, |
| 733 | &CallClock { |
| 734 | offset: FixedOffset::east_opt(2 * 3600).unwrap().into(), |
| 735 | today: NaiveDate::from_ymd_opt(2026, 10, 17).unwrap(), |
| 736 | meeting, |
| 737 | }, |
| 738 | ) |
| 739 | }; |
| 740 | assert_eq!(clock(&meeting), "Thursday, 15 October 2026 11:46 - 11:55"); |
| 741 | meeting.slots = vec![ |
| 742 | MeetingSlot { |
| 743 | start: Utc.with_ymd_and_hms(2026, 10, 14, 9, 45, 0).unwrap(), |
| 744 | end: Utc.with_ymd_and_hms(2026, 10, 14, 10, 0, 0).unwrap(), |
| 745 | }, |
| 746 | MeetingSlot { |
| 747 | start: Utc.with_ymd_and_hms(2026, 10, 15, 9, 45, 0).unwrap(), |
| 748 | end: Utc.with_ymd_and_hms(2026, 10, 15, 10, 0, 0).unwrap(), |
| 749 | }, |
| 750 | ]; |
| 751 | assert_eq!(clock(&meeting), "Thursday, 15 October 2026 11:45 - 12:00"); |
| 752 | } |
| 753 | |
| 754 | #[test] |
| 755 | fn notice_stamps_spell_out_the_weekday_like_teams() { |
| 756 | let meeting = MeetingInfo::default(); |
| 757 | let clock = CallClock { |
| 758 | offset: FixedOffset::east_opt(0).unwrap().into(), |
| 759 | today: NaiveDate::from_ymd_opt(2026, 10, 17).unwrap(), |
| 760 | meeting: &meeting, |
| 761 | }; |
| 762 | let stamp = |day: u32| { |
| 763 | notice_stamp( |
| 764 | Utc.with_ymd_and_hms(2026, 10, day, 11, 45, 0).unwrap(), |
| 765 | &clock, |
| 766 | ) |
| 767 | }; |
| 768 | assert_eq!(stamp(17), "11:45"); |
| 769 | assert_eq!(stamp(16), "Yesterday 11:45"); |
| 770 | assert_eq!(stamp(15), "Thursday 11:45"); |
| 771 | } |
| 772 | |
| 773 | #[test] |
| 774 | fn ended_meeting_lists_named_participants_up_to_four() { |
| 775 | let shown = cards(&fixture_records(), &MeetingInfo::default()); |
| 776 | let ended = shown |
| 777 | .iter() |
| 778 | .find(|card| card.title == "Meeting ended") |
| 779 | .unwrap(); |
| 780 | assert_eq!(ended.detail, "16m 28s \u{b7} 16:15"); |
| 781 | let shown_names: Vec<&str> = ended |
| 782 | .participants |
| 783 | .iter() |
| 784 | .map(|person| person.name.as_str()) |
| 785 | .collect(); |
| 786 | assert_eq!( |
| 787 | shown_names, |
| 788 | ["Person 1", "Person 2", "Person 3", "Person 4"] |
| 789 | ); |
| 790 | assert_eq!(ended.extra_participants, 20); |
| 791 | } |
| 792 | |
| 793 | #[test] |
| 794 | fn meeting_start_stays_but_a_finished_call_start_hides() { |
| 795 | let events = vec![ |
| 796 | Some(event(CallPhase::Started, CallType::Meeting, "meeting")), |
| 797 | Some(event(CallPhase::Ended, CallType::Meeting, "meeting")), |
| 798 | Some(event(CallPhase::Started, CallType::Call, "call")), |
| 799 | Some(event(CallPhase::Ended, CallType::Call, "call")), |
| 800 | Some(event(CallPhase::Started, CallType::Meeting, "live")), |
| 801 | None, |
| 802 | ]; |
| 803 | let timeline = CallTimeline::new(&events); |
| 804 | assert!(!timeline.hides(0)); |
| 805 | assert!(timeline.hides(2)); |
| 806 | assert!(!timeline.hides(5)); |
| 807 | assert!(timeline.is_live_start(4)); |
| 808 | assert!(!timeline.is_live_start(0)); |
| 809 | assert!(timeline.finished("meeting")); |
| 810 | } |
| 811 | |
| 812 | #[test] |
| 813 | fn calls_read_like_teams_and_my_recording_is_in_my_onedrive() { |
| 814 | let names = HashMap::new(); |
| 815 | let lookup = CallNames { |
| 816 | my_user_id: Some("me"), |
| 817 | names: &names, |
| 818 | }; |
| 819 | let line = |event: &CallEvent, missed: bool| { |
| 820 | notice_text( |
| 821 | NoticeSource { |
| 822 | event, |
| 823 | message_id: "m", |
| 824 | missed, |
| 825 | stopped_by_meeting_end: false, |
| 826 | }, |
| 827 | &lookup, |
| 828 | &MeetingInfo::default(), |
| 829 | ) |
| 830 | }; |
| 831 | let ended = event(CallPhase::Ended, CallType::Call, "c"); |
| 832 | assert_eq!(line(&ended, false), "Call ended"); |
| 833 | assert_eq!(line(&ended, true), "Call missed"); |
| 834 | assert_eq!( |
| 835 | line(&event(CallPhase::Ended, CallType::Meeting, "c"), false), |
| 836 | "Meeting ended:" |
| 837 | ); |
| 838 | let mut saved = event(CallPhase::RecordingSaved, CallType::Call, "c"); |
| 839 | saved.recording = Some(Recording { |
| 840 | name: None, |
| 841 | url: Some("https://contoso.example/rec.mp4".to_owned()), |
| 842 | organizer_id: Some("me".to_owned()), |
| 843 | }); |
| 844 | assert_eq!( |
| 845 | line(&saved, false), |
| 846 | "Recording has been saved to your OneDrive." |
| 847 | ); |
| 848 | } |
| 849 | } |