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