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