| 1 | use std::collections::{HashMap, HashSet}; |
| 2 | |
| 3 | use chrono::{DateTime, FixedOffset, NaiveDate, Utc}; |
| 4 | use store::MessageRecord; |
| 5 | use teams_core::roster_event::RosterEvent; |
| 6 | use teams_core::{CallEvent, CallPhase, CallType, ImageRef, format_duration}; |
| 7 | |
| 8 | use crate::format; |
| 9 | |
| 10 | const MAX_AVATARS: usize = 4; |
| 11 | const RECAP_FALLBACK_TITLE: &str = "Meeting"; |
| 12 | const RECAP_DATE_FORMAT: &str = "%A, %-d %B %Y %H:%M"; |
| 13 | const RECAP_TIME_FORMAT: &str = "%H:%M"; |
| 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 | |
| 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: Option<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: Some(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: FixedOffset, |
| 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 | slot.start |
| 326 | .with_timezone(&clock.offset) |
| 327 | .format(RECAP_DATE_FORMAT), |
| 328 | slot.end |
| 329 | .with_timezone(&clock.offset) |
| 330 | .format(RECAP_TIME_FORMAT) |
| 331 | ) |
| 332 | } |
| 333 | |
| 334 | /// Teams writes the weekday in full on these lines ("Thursday 11:45"), unlike message times. |
| 335 | fn notice_stamp(time: DateTime<Utc>, clock: &CallClock) -> String { |
| 336 | let local = time.with_timezone(&clock.offset); |
| 337 | let days_ago = (clock.today - local.date_naive()).num_days(); |
| 338 | if (2..=WEEKDAY_STAMP_DAYS).contains(&days_ago) { |
| 339 | local.format("%A %H:%M").to_string() |
| 340 | } else { |
| 341 | format::post_time_label(time, clock.today, clock.offset) |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | fn participants(event: &CallEvent, names: &CallNames) -> Vec<CallParticipant> { |
| 346 | event |
| 347 | .participant_ids |
| 348 | .iter() |
| 349 | .enumerate() |
| 350 | .filter_map(|(index, user_id)| { |
| 351 | let given = event.participant_names.get(index).map(String::as_str); |
| 352 | Some(CallParticipant { |
| 353 | user_id: user_id.clone(), |
| 354 | name: names.name(Some(user_id), given)?, |
| 355 | }) |
| 356 | }) |
| 357 | .collect() |
| 358 | } |
| 359 | |
| 360 | fn recording_card( |
| 361 | event: &CallEvent, |
| 362 | message_id: &str, |
| 363 | transcript: bool, |
| 364 | meeting: &MeetingInfo, |
| 365 | ) -> Option<RecordingCard> { |
| 366 | let recording = event.recording.as_ref()?; |
| 367 | Some(RecordingCard { |
| 368 | message_id: message_id.to_owned(), |
| 369 | duration: event |
| 370 | .duration_seconds |
| 371 | .filter(|seconds| *seconds > 0) |
| 372 | .map(format_duration), |
| 373 | url: recording.url.clone(), |
| 374 | thumbnail: recording.thumbnail_url().map(|url| ImageRef { |
| 375 | id: format!("recording-{message_id}"), |
| 376 | url, |
| 377 | width: None, |
| 378 | height: None, |
| 379 | alt: None, |
| 380 | }), |
| 381 | transcript, |
| 382 | expires_in_days: meeting.expiry_days.get(message_id).copied(), |
| 383 | }) |
| 384 | } |
| 385 | |
| 386 | fn day_count(days: i64) -> String { |
| 387 | if days == 1 { |
| 388 | "1 day".to_owned() |
| 389 | } else { |
| 390 | format!("{days} days") |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | struct NoticeSource<'a> { |
| 395 | event: &'a CallEvent, |
| 396 | message_id: &'a str, |
| 397 | stopped_by_meeting_end: bool, |
| 398 | } |
| 399 | |
| 400 | fn notice_text(source: NoticeSource, names: &CallNames, meeting: &MeetingInfo) -> String { |
| 401 | let NoticeSource { |
| 402 | event, |
| 403 | message_id, |
| 404 | stopped_by_meeting_end, |
| 405 | } = source; |
| 406 | let initiator = || { |
| 407 | names.name( |
| 408 | event.initiator_id.as_deref(), |
| 409 | event.initiator_name.as_deref(), |
| 410 | ) |
| 411 | }; |
| 412 | match event.phase { |
| 413 | CallPhase::Ended => format!("{}:", event.title(false)), |
| 414 | CallPhase::RecordingStarted => match initiator() { |
| 415 | Some(name) => format!("{name} started recording."), |
| 416 | None => event.title(false).to_owned(), |
| 417 | }, |
| 418 | CallPhase::RecordingStopped => match initiator().filter(|_| !stopped_by_meeting_end) { |
| 419 | Some(name) => format!("{name} stopped recording."), |
| 420 | None => format!("{}.", event.title(false)), |
| 421 | }, |
| 422 | CallPhase::RecordingSaved => { |
| 423 | let saved = saved_to(event, names); |
| 424 | match meeting.expiry_days.get(message_id) { |
| 425 | Some(days) => format!("{saved} and will expire in {}.", day_count(*days)), |
| 426 | None => format!("{saved}."), |
| 427 | } |
| 428 | } |
| 429 | _ => event.title(false).to_owned(), |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | fn saved_to(event: &CallEvent, names: &CallNames) -> String { |
| 434 | let organizer_id = event |
| 435 | .recording |
| 436 | .as_ref() |
| 437 | .and_then(|recording| recording.organizer_id.as_deref()); |
| 438 | if organizer_id.is_some() && organizer_id == names.my_user_id { |
| 439 | return "Recording has been saved to your OneDrive".to_owned(); |
| 440 | } |
| 441 | match names.name(organizer_id, None) { |
| 442 | Some(owner) => format!("Recording has been saved to {owner}'s OneDrive"), |
| 443 | None => "Recording has been saved to OneDrive".to_owned(), |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | fn title(event: &CallEvent, missed: bool, names: &CallNames) -> String { |
| 448 | match event.phase { |
| 449 | CallPhase::RecordingStarted => { |
| 450 | match names.name( |
| 451 | event.initiator_id.as_deref(), |
| 452 | event.initiator_name.as_deref(), |
| 453 | ) { |
| 454 | Some(name) => format!("{name} started recording"), |
| 455 | None => event.title(false).to_owned(), |
| 456 | } |
| 457 | } |
| 458 | CallPhase::RecordingSaved => saved_to(event, names), |
| 459 | _ => event.title(missed).to_owned(), |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /// Teams draws meeting and recording events as one-line notices; 1:1 and group calls keep the card. |
| 464 | pub fn is_notice(event: &CallEvent) -> bool { |
| 465 | event.call_type == CallType::Meeting || event.is_recording() |
| 466 | } |
| 467 | |
| 468 | pub struct CallSource<'a> { |
| 469 | pub event: &'a CallEvent, |
| 470 | pub message_id: &'a str, |
| 471 | pub created_at: DateTime<Utc>, |
| 472 | pub time: &'a str, |
| 473 | } |
| 474 | |
| 475 | pub fn call_card( |
| 476 | source: CallSource, |
| 477 | names: &CallNames, |
| 478 | clock: &CallClock, |
| 479 | timeline: &CallTimeline, |
| 480 | ) -> CallCard { |
| 481 | let CallSource { |
| 482 | event, |
| 483 | message_id, |
| 484 | created_at, |
| 485 | time, |
| 486 | } = source; |
| 487 | let missed = event.missed(names.my_user_id); |
| 488 | let detail = match (event.phase, event.duration_seconds) { |
| 489 | (CallPhase::Ended, Some(seconds)) if !missed => { |
| 490 | format!("{} \u{b7} {time}", format_duration(seconds)) |
| 491 | } |
| 492 | _ => time.to_owned(), |
| 493 | }; |
| 494 | let mut people = if event.phase == CallPhase::Ended && event.call_type == CallType::Meeting { |
| 495 | participants(event, names) |
| 496 | } else { |
| 497 | Vec::new() |
| 498 | }; |
| 499 | let extra_participants = people.len().saturating_sub(MAX_AVATARS); |
| 500 | people.truncate(MAX_AVATARS); |
| 501 | let notice = is_notice(event).then(|| CallNotice { |
| 502 | stamp: notice_stamp(created_at, clock), |
| 503 | text: notice_text( |
| 504 | NoticeSource { |
| 505 | event, |
| 506 | message_id, |
| 507 | stopped_by_meeting_end: timeline.stopped_by_meeting_end(event, created_at), |
| 508 | }, |
| 509 | names, |
| 510 | clock.meeting, |
| 511 | ), |
| 512 | emphasis: event |
| 513 | .duration_seconds |
| 514 | .filter(|_| event.phase == CallPhase::Ended) |
| 515 | .map(format_duration), |
| 516 | }); |
| 517 | CallCard { |
| 518 | title: title(event, missed, names), |
| 519 | detail, |
| 520 | icon: match (missed, event.phase, event.call_type) { |
| 521 | (true, _, _) => CallIcon::Missed, |
| 522 | (false, CallPhase::RecordingStarted, _) => CallIcon::Record, |
| 523 | (false, CallPhase::RecordingSaved, _) => CallIcon::Saved, |
| 524 | (false, CallPhase::RecordingStopped | CallPhase::TranscriptStarted, _) => { |
| 525 | CallIcon::RecordStop |
| 526 | } |
| 527 | (false, _, CallType::Meeting) => CallIcon::Video, |
| 528 | (false, _, CallType::Call) => CallIcon::Call, |
| 529 | }, |
| 530 | missed, |
| 531 | started: event.phase == CallPhase::Started, |
| 532 | participants: people, |
| 533 | extra_participants, |
| 534 | recap: timeline.recap(event, created_at, clock), |
| 535 | notice, |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | #[cfg(test)] |
| 540 | mod tests { |
| 541 | use chrono::TimeZone; |
| 542 | use teams_core::Recording; |
| 543 | |
| 544 | use super::*; |
| 545 | |
| 546 | fn event(phase: CallPhase, call_type: CallType, call_id: &str) -> CallEvent { |
| 547 | CallEvent { |
| 548 | phase, |
| 549 | call_type, |
| 550 | call_id: call_id.to_owned(), |
| 551 | duration_seconds: None, |
| 552 | initiator_id: Some("organizer".to_owned()), |
| 553 | initiator_name: None, |
| 554 | participant_ids: Vec::new(), |
| 555 | participant_names: Vec::new(), |
| 556 | recording: None, |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | fn fixture_records() -> Vec<MessageRecord> { |
| 561 | let path = format!( |
| 562 | "{}/../core/tests/fixtures/call_events/meeting_chat_recording.json", |
| 563 | env!("CARGO_MANIFEST_DIR") |
| 564 | ); |
| 565 | let page: serde_json::Value = |
| 566 | serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap(); |
| 567 | page["value"] |
| 568 | .as_array() |
| 569 | .unwrap() |
| 570 | .iter() |
| 571 | .rev() |
| 572 | .map(|message| { |
| 573 | let message: graph::Message = serde_json::from_value(message.clone()).unwrap(); |
| 574 | teams_core::message_record("meeting-chat", &message).unwrap() |
| 575 | }) |
| 576 | .collect() |
| 577 | } |
| 578 | |
| 579 | fn names() -> HashMap<String, String> { |
| 580 | HashMap::from([ |
| 581 | ( |
| 582 | "00000000-0000-4000-8000-000000000002".to_owned(), |
| 583 | "Vera Organizer".to_owned(), |
| 584 | ), |
| 585 | ( |
| 586 | "00000000-0000-4000-8000-000000000004".to_owned(), |
| 587 | "Sam Recorder".to_owned(), |
| 588 | ), |
| 589 | ]) |
| 590 | } |
| 591 | |
| 592 | fn cards(records: &[MessageRecord], meeting: &MeetingInfo) -> Vec<CallCard> { |
| 593 | let events: Vec<Option<CallEvent>> = records.iter().map(teams_core::call_event).collect(); |
| 594 | let timeline = CallTimeline::with_records(&events, &records.iter().collect::<Vec<_>>()); |
| 595 | let names = names(); |
| 596 | let lookup = CallNames { |
| 597 | my_user_id: Some("me"), |
| 598 | names: &names, |
| 599 | }; |
| 600 | let clock = CallClock { |
| 601 | offset: FixedOffset::east_opt(0).unwrap(), |
| 602 | today: NaiveDate::from_ymd_opt(2026, 10, 30).unwrap(), |
| 603 | meeting, |
| 604 | }; |
| 605 | events |
| 606 | .iter() |
| 607 | .zip(records) |
| 608 | .enumerate() |
| 609 | .filter(|(index, _)| !timeline.hides(*index)) |
| 610 | .map(|(_, (event, record))| { |
| 611 | call_card( |
| 612 | CallSource { |
| 613 | event: event.as_ref().unwrap(), |
| 614 | message_id: &record.message_id, |
| 615 | created_at: record.created_at, |
| 616 | time: "16:15", |
| 617 | }, |
| 618 | &lookup, |
| 619 | &clock, |
| 620 | &timeline, |
| 621 | ) |
| 622 | }) |
| 623 | .collect() |
| 624 | } |
| 625 | |
| 626 | fn notice_line(card: &CallCard) -> String { |
| 627 | let notice = card.notice.as_ref().unwrap(); |
| 628 | match ¬ice.emphasis { |
| 629 | Some(emphasis) => format!("{} {emphasis}", notice.text), |
| 630 | None => notice.text.clone(), |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | #[test] |
| 635 | fn recorded_meeting_shows_as_teams_notices() { |
| 636 | let shown = cards(&fixture_records(), &MeetingInfo::default()); |
| 637 | let lines: Vec<String> = shown.iter().map(notice_line).collect(); |
| 638 | assert_eq!( |
| 639 | lines, |
| 640 | [ |
| 641 | "Meeting started", |
| 642 | "Sam Recorder started recording.", |
| 643 | "Recording stopped.", |
| 644 | "Meeting ended: 16m 28s", |
| 645 | "Recording has been saved to Vera Organizer's OneDrive.", |
| 646 | ] |
| 647 | ); |
| 648 | let icons: Vec<CallIcon> = shown.iter().map(|card| card.icon).collect(); |
| 649 | assert_eq!( |
| 650 | icons, |
| 651 | [ |
| 652 | CallIcon::Video, |
| 653 | CallIcon::Record, |
| 654 | CallIcon::RecordStop, |
| 655 | CallIcon::Video, |
| 656 | CallIcon::Saved, |
| 657 | ] |
| 658 | ); |
| 659 | assert!( |
| 660 | shown |
| 661 | .iter() |
| 662 | .all(|card| card.recap.is_none() == (card.title != "Meeting ended")) |
| 663 | ); |
| 664 | } |
| 665 | |
| 666 | #[test] |
| 667 | fn a_recording_stopped_before_the_end_names_who_stopped_it() { |
| 668 | let mut records = fixture_records(); |
| 669 | for record in records.iter_mut() { |
| 670 | if teams_core::call_event(record) |
| 671 | .is_some_and(|event| event.phase == CallPhase::RecordingStopped) |
| 672 | { |
| 673 | record.created_at -= chrono::Duration::minutes(2); |
| 674 | } |
| 675 | } |
| 676 | let shown = cards(&records, &MeetingInfo::default()); |
| 677 | assert_eq!(notice_line(&shown[2]), "Sam Recorder stopped recording."); |
| 678 | } |
| 679 | |
| 680 | #[test] |
| 681 | fn ended_meeting_carries_the_recap_card_of_its_recording() { |
| 682 | let records = fixture_records(); |
| 683 | let saved_id = records.last().unwrap().message_id.clone(); |
| 684 | let meeting = MeetingInfo { |
| 685 | title: Some("Weekly sync".to_owned()), |
| 686 | slots: Vec::new(), |
| 687 | expiry_days: HashMap::from([(saved_id.clone(), 56)]), |
| 688 | }; |
| 689 | let shown = cards(&records, &meeting); |
| 690 | let ended = shown |
| 691 | .iter() |
| 692 | .find(|card| card.title == "Meeting ended") |
| 693 | .unwrap(); |
| 694 | let recap = ended.recap.as_ref().unwrap(); |
| 695 | assert_eq!(recap.title, "Weekly sync"); |
| 696 | assert_eq!(recap.recording.message_id, saved_id); |
| 697 | assert_eq!(recap.recording.duration.as_deref(), Some("13m 34s")); |
| 698 | assert_eq!(recap.recording.expires_in_days, Some(56)); |
| 699 | assert!(recap.recording.transcript); |
| 700 | assert!( |
| 701 | recap |
| 702 | .recording |
| 703 | .thumbnail |
| 704 | .as_ref() |
| 705 | .unwrap() |
| 706 | .url |
| 707 | .contains("/thumbnails/") |
| 708 | ); |
| 709 | assert!(shown.iter().filter(|card| card.recap.is_some()).count() == 1); |
| 710 | assert_eq!( |
| 711 | notice_line(shown.last().unwrap()), |
| 712 | "Recording has been saved to Vera Organizer's OneDrive and will expire in 56 days." |
| 713 | ); |
| 714 | } |
| 715 | |
| 716 | #[test] |
| 717 | fn recap_date_prefers_the_calendar_occurrence() { |
| 718 | let started_at = Utc.with_ymd_and_hms(2026, 10, 15, 9, 46, 0).unwrap(); |
| 719 | let ended_at = Utc.with_ymd_and_hms(2026, 10, 15, 9, 55, 0).unwrap(); |
| 720 | let mut meeting = MeetingInfo::default(); |
| 721 | let clock = |meeting: &MeetingInfo| { |
| 722 | recap_date_line( |
| 723 | started_at, |
| 724 | ended_at, |
| 725 | &CallClock { |
| 726 | offset: FixedOffset::east_opt(2 * 3600).unwrap(), |
| 727 | today: NaiveDate::from_ymd_opt(2026, 10, 17).unwrap(), |
| 728 | meeting, |
| 729 | }, |
| 730 | ) |
| 731 | }; |
| 732 | assert_eq!(clock(&meeting), "Thursday, 15 October 2026 11:46 - 11:55"); |
| 733 | meeting.slots = vec![ |
| 734 | MeetingSlot { |
| 735 | start: Utc.with_ymd_and_hms(2026, 10, 14, 9, 45, 0).unwrap(), |
| 736 | end: Utc.with_ymd_and_hms(2026, 10, 14, 10, 0, 0).unwrap(), |
| 737 | }, |
| 738 | MeetingSlot { |
| 739 | start: Utc.with_ymd_and_hms(2026, 10, 15, 9, 45, 0).unwrap(), |
| 740 | end: Utc.with_ymd_and_hms(2026, 10, 15, 10, 0, 0).unwrap(), |
| 741 | }, |
| 742 | ]; |
| 743 | assert_eq!(clock(&meeting), "Thursday, 15 October 2026 11:45 - 12:00"); |
| 744 | } |
| 745 | |
| 746 | #[test] |
| 747 | fn notice_stamps_spell_out_the_weekday_like_teams() { |
| 748 | let meeting = MeetingInfo::default(); |
| 749 | let clock = CallClock { |
| 750 | offset: FixedOffset::east_opt(0).unwrap(), |
| 751 | today: NaiveDate::from_ymd_opt(2026, 10, 17).unwrap(), |
| 752 | meeting: &meeting, |
| 753 | }; |
| 754 | let stamp = |day: u32| { |
| 755 | notice_stamp( |
| 756 | Utc.with_ymd_and_hms(2026, 10, day, 11, 45, 0).unwrap(), |
| 757 | &clock, |
| 758 | ) |
| 759 | }; |
| 760 | assert_eq!(stamp(17), "11:45"); |
| 761 | assert_eq!(stamp(16), "Yesterday 11:45"); |
| 762 | assert_eq!(stamp(15), "Thursday 11:45"); |
| 763 | } |
| 764 | |
| 765 | #[test] |
| 766 | fn ended_meeting_lists_named_participants_up_to_four() { |
| 767 | let shown = cards(&fixture_records(), &MeetingInfo::default()); |
| 768 | let ended = shown |
| 769 | .iter() |
| 770 | .find(|card| card.title == "Meeting ended") |
| 771 | .unwrap(); |
| 772 | assert_eq!(ended.detail, "16m 28s \u{b7} 16:15"); |
| 773 | let shown_names: Vec<&str> = ended |
| 774 | .participants |
| 775 | .iter() |
| 776 | .map(|person| person.name.as_str()) |
| 777 | .collect(); |
| 778 | assert_eq!( |
| 779 | shown_names, |
| 780 | ["Person 1", "Person 2", "Person 3", "Person 4"] |
| 781 | ); |
| 782 | assert_eq!(ended.extra_participants, 20); |
| 783 | } |
| 784 | |
| 785 | #[test] |
| 786 | fn meeting_start_stays_but_a_finished_call_start_hides() { |
| 787 | let events = vec![ |
| 788 | Some(event(CallPhase::Started, CallType::Meeting, "meeting")), |
| 789 | Some(event(CallPhase::Ended, CallType::Meeting, "meeting")), |
| 790 | Some(event(CallPhase::Started, CallType::Call, "call")), |
| 791 | Some(event(CallPhase::Ended, CallType::Call, "call")), |
| 792 | Some(event(CallPhase::Started, CallType::Meeting, "live")), |
| 793 | None, |
| 794 | ]; |
| 795 | let timeline = CallTimeline::new(&events); |
| 796 | assert!(!timeline.hides(0)); |
| 797 | assert!(timeline.hides(2)); |
| 798 | assert!(!timeline.hides(5)); |
| 799 | assert!(timeline.is_live_start(4)); |
| 800 | assert!(!timeline.is_live_start(0)); |
| 801 | assert!(timeline.finished("meeting")); |
| 802 | } |
| 803 | |
| 804 | #[test] |
| 805 | fn calls_keep_their_card_and_my_recording_is_in_my_onedrive() { |
| 806 | assert!(!is_notice(&event(CallPhase::Ended, CallType::Call, "c"))); |
| 807 | let mut saved = event(CallPhase::RecordingSaved, CallType::Call, "c"); |
| 808 | saved.recording = Some(Recording { |
| 809 | name: None, |
| 810 | url: Some("https://contoso.example/rec.mp4".to_owned()), |
| 811 | organizer_id: Some("me".to_owned()), |
| 812 | }); |
| 813 | let names = HashMap::new(); |
| 814 | let lookup = CallNames { |
| 815 | my_user_id: Some("me"), |
| 816 | names: &names, |
| 817 | }; |
| 818 | assert_eq!( |
| 819 | notice_text( |
| 820 | NoticeSource { |
| 821 | event: &saved, |
| 822 | message_id: "m", |
| 823 | stopped_by_meeting_end: false, |
| 824 | }, |
| 825 | &lookup, |
| 826 | &MeetingInfo::default() |
| 827 | ), |
| 828 | "Recording has been saved to your OneDrive." |
| 829 | ); |
| 830 | } |
| 831 | } |