| 1 | /// Extract a Teams join URL from iCalendar fields (priority order from the CalDAV plan). |
| 2 | pub fn teams_join_url( |
| 3 | skype_url: Option<&str>, |
| 4 | url: Option<&str>, |
| 5 | location: Option<&str>, |
| 6 | description: Option<&str>, |
| 7 | ) -> Option<String> { |
| 8 | skype_url |
| 9 | .filter(|value| looks_like_teams(value)) |
| 10 | .map(str::to_owned) |
| 11 | .or_else(|| { |
| 12 | url.filter(|value| looks_like_teams(value)) |
| 13 | .map(str::to_owned) |
| 14 | }) |
| 15 | .or_else(|| { |
| 16 | location |
| 17 | .filter(|value| looks_like_teams(value)) |
| 18 | .map(str::to_owned) |
| 19 | }) |
| 20 | .or_else(|| description.and_then(find_in_text)) |
| 21 | } |
| 22 | |
| 23 | fn looks_like_teams(value: &str) -> bool { |
| 24 | value.contains("teams.microsoft.com") || value.contains("teams.live.com") |
| 25 | } |
| 26 | |
| 27 | fn find_in_text(text: &str) -> Option<String> { |
| 28 | text.split_whitespace() |
| 29 | .find(|token| looks_like_teams(token)) |
| 30 | .map(|token| { |
| 31 | token |
| 32 | .trim_end_matches(|c: char| matches!(c, '.' | ',' | ')' | ']' | '>' | '"')) |
| 33 | .to_owned() |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | #[cfg(test)] |
| 38 | mod tests { |
| 39 | use super::*; |
| 40 | |
| 41 | #[test] |
| 42 | fn prefers_skype_property() { |
| 43 | let url = teams_join_url( |
| 44 | Some("https://teams.microsoft.com/l/meetup-join/a"), |
| 45 | Some("https://example.com"), |
| 46 | None, |
| 47 | Some("https://teams.microsoft.com/l/meetup-join/b"), |
| 48 | ); |
| 49 | assert_eq!( |
| 50 | url.as_deref(), |
| 51 | Some("https://teams.microsoft.com/l/meetup-join/a") |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn finds_link_in_description() { |
| 57 | let url = teams_join_url( |
| 58 | None, |
| 59 | None, |
| 60 | None, |
| 61 | Some("Join: https://teams.microsoft.com/l/meetup-join/x?a=1."), |
| 62 | ); |
| 63 | assert_eq!( |
| 64 | url.as_deref(), |
| 65 | Some("https://teams.microsoft.com/l/meetup-join/x?a=1") |
| 66 | ); |
| 67 | } |
| 68 | } |