| 1 | use chrono::{DateTime, Duration, Utc}; |
| 2 | use rmcp::schemars; |
| 3 | use serde::Deserialize; |
| 4 | |
| 5 | use crate::source::MeetingDraft; |
| 6 | |
| 7 | pub const MEETINGS_DISABLED: &str = |
| 8 | "organizing meetings is turned off (enable it in rusty-teams Settings > MCP)"; |
| 9 | const MAX_AVAILABILITY_PEOPLE: usize = 20; |
| 10 | const MAX_AVAILABILITY_DAYS: i64 = 14; |
| 11 | const MAX_ATTENDEES: usize = 50; |
| 12 | const MAX_SUBJECT_CHARACTERS: usize = 255; |
| 13 | const MAX_DESCRIPTION_CHARACTERS: usize = 20_000; |
| 14 | const MAX_MEETING_HOURS: i64 = 24; |
| 15 | const PAST_TOLERANCE_MINUTES: i64 = 5; |
| 16 | |
| 17 | #[derive(Debug, Deserialize, schemars::JsonSchema)] |
| 18 | pub struct FindAvailabilityParams { |
| 19 | /// Email addresses of the people to check, up to 20. The user's own address works too. |
| 20 | pub attendees: Vec<String>, |
| 21 | /// Start of the window, ISO 8601 with offset. |
| 22 | pub start: DateTime<Utc>, |
| 23 | /// End of the window, ISO 8601 with offset, at most 14 days after start. |
| 24 | pub end: DateTime<Utc>, |
| 25 | } |
| 26 | |
| 27 | #[derive(Debug, Deserialize, schemars::JsonSchema)] |
| 28 | pub struct CreateMeetingParams { |
| 29 | /// Meeting title. |
| 30 | pub subject: String, |
| 31 | /// Start, ISO 8601 with offset. |
| 32 | pub start: DateTime<Utc>, |
| 33 | /// End, ISO 8601 with offset, at most 24 hours after start. |
| 34 | pub end: DateTime<Utc>, |
| 35 | /// Email addresses of the people to invite, up to 50. |
| 36 | pub attendees: Vec<String>, |
| 37 | /// Agenda or description, plain text, shown in the invitation. |
| 38 | pub description: Option<String>, |
| 39 | /// Your session id, so the user can tell meetings of different sessions apart. |
| 40 | pub session_id: Option<String>, |
| 41 | /// Your name, shown to the user next to the meeting. |
| 42 | pub agent: Option<String>, |
| 43 | } |
| 44 | |
| 45 | fn valid_email(address: &str) -> bool { |
| 46 | let Some((local, domain)) = address.split_once('@') else { |
| 47 | return false; |
| 48 | }; |
| 49 | !local.is_empty() |
| 50 | && domain.contains('.') |
| 51 | && !domain.starts_with('.') |
| 52 | && !domain.ends_with('.') |
| 53 | && !address |
| 54 | .chars() |
| 55 | .any(|character| character.is_whitespace() || character == ',') |
| 56 | } |
| 57 | |
| 58 | fn clean_emails(addresses: &[String], limit: usize) -> Result<Vec<String>, String> { |
| 59 | let mut cleaned: Vec<String> = Vec::new(); |
| 60 | for address in addresses { |
| 61 | let address = address.trim().to_lowercase(); |
| 62 | if !valid_email(&address) { |
| 63 | return Err(format!("{address:?} is not an email address")); |
| 64 | } |
| 65 | if !cleaned.contains(&address) { |
| 66 | cleaned.push(address); |
| 67 | } |
| 68 | } |
| 69 | if cleaned.is_empty() { |
| 70 | return Err("name at least one attendee".to_owned()); |
| 71 | } |
| 72 | if cleaned.len() > limit { |
| 73 | return Err(format!("at most {limit} attendees")); |
| 74 | } |
| 75 | Ok(cleaned) |
| 76 | } |
| 77 | |
| 78 | pub fn availability_request( |
| 79 | params: &FindAvailabilityParams, |
| 80 | ) -> Result<(Vec<String>, DateTime<Utc>, DateTime<Utc>), String> { |
| 81 | if params.end <= params.start { |
| 82 | return Err("end must be after start".to_owned()); |
| 83 | } |
| 84 | if params.end - params.start > Duration::days(MAX_AVAILABILITY_DAYS) { |
| 85 | return Err(format!( |
| 86 | "the window may span at most {MAX_AVAILABILITY_DAYS} days" |
| 87 | )); |
| 88 | } |
| 89 | let emails = clean_emails(¶ms.attendees, MAX_AVAILABILITY_PEOPLE)?; |
| 90 | Ok((emails, params.start, params.end)) |
| 91 | } |
| 92 | |
| 93 | pub fn meeting_draft( |
| 94 | params: &CreateMeetingParams, |
| 95 | now: DateTime<Utc>, |
| 96 | ) -> Result<MeetingDraft, String> { |
| 97 | let subject = params.subject.trim(); |
| 98 | if subject.is_empty() { |
| 99 | return Err("subject is empty".to_owned()); |
| 100 | } |
| 101 | if subject.chars().count() > MAX_SUBJECT_CHARACTERS { |
| 102 | return Err(format!( |
| 103 | "subject is longer than {MAX_SUBJECT_CHARACTERS} characters" |
| 104 | )); |
| 105 | } |
| 106 | if params.end <= params.start { |
| 107 | return Err("end must be after start".to_owned()); |
| 108 | } |
| 109 | if params.end - params.start > Duration::hours(MAX_MEETING_HOURS) { |
| 110 | return Err(format!( |
| 111 | "a meeting may last at most {MAX_MEETING_HOURS} hours" |
| 112 | )); |
| 113 | } |
| 114 | if params.start < now - Duration::minutes(PAST_TOLERANCE_MINUTES) { |
| 115 | return Err("start lies in the past".to_owned()); |
| 116 | } |
| 117 | let description = params.description.clone().unwrap_or_default(); |
| 118 | if description.chars().count() > MAX_DESCRIPTION_CHARACTERS { |
| 119 | return Err(format!( |
| 120 | "description is longer than {MAX_DESCRIPTION_CHARACTERS} characters" |
| 121 | )); |
| 122 | } |
| 123 | Ok(MeetingDraft { |
| 124 | subject: subject.to_owned(), |
| 125 | start: params.start, |
| 126 | end: params.end, |
| 127 | attendees: clean_emails(¶ms.attendees, MAX_ATTENDEES)?, |
| 128 | description, |
| 129 | }) |
| 130 | } |
| 131 | |
| 132 | #[cfg(test)] |
| 133 | mod tests { |
| 134 | use chrono::{Duration, TimeZone, Utc}; |
| 135 | |
| 136 | use super::{CreateMeetingParams, FindAvailabilityParams, availability_request, meeting_draft}; |
| 137 | |
| 138 | fn params(start_hours: i64, length_hours: i64, attendees: &[&str]) -> CreateMeetingParams { |
| 139 | let start = |
| 140 | Utc.with_ymd_and_hms(2026, 11, 20, 9, 0, 0).unwrap() + Duration::hours(start_hours); |
| 141 | CreateMeetingParams { |
| 142 | subject: " Release review ".to_owned(), |
| 143 | start, |
| 144 | end: start + Duration::hours(length_hours), |
| 145 | attendees: attendees |
| 146 | .iter() |
| 147 | .map(|address| (*address).to_owned()) |
| 148 | .collect(), |
| 149 | description: Some("Agenda".to_owned()), |
| 150 | session_id: None, |
| 151 | agent: None, |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn a_meeting_draft_is_trimmed_and_deduplicated() { |
| 157 | let now = Utc.with_ymd_and_hms(2026, 11, 12, 9, 0, 0).unwrap(); |
| 158 | let draft = meeting_draft( |
| 159 | ¶ms( |
| 160 | 0, |
| 161 | 1, |
| 162 | &["Mara@Example.com", "mara@example.com", "jonas@example.com"], |
| 163 | ), |
| 164 | now, |
| 165 | ) |
| 166 | .unwrap(); |
| 167 | assert_eq!(draft.subject, "Release review"); |
| 168 | assert_eq!(draft.attendees, ["mara@example.com", "jonas@example.com"]); |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn bad_meetings_are_refused() { |
| 173 | let now = Utc.with_ymd_and_hms(2026, 11, 12, 9, 0, 0).unwrap(); |
| 174 | assert!(meeting_draft(¶ms(0, 0, &["a@example.com"]), now).is_err()); |
| 175 | assert!(meeting_draft(¶ms(0, 25, &["a@example.com"]), now).is_err()); |
| 176 | assert!(meeting_draft(¶ms(0, 1, &["not an address"]), now).is_err()); |
| 177 | assert!(meeting_draft(¶ms(0, 1, &[]), now).is_err()); |
| 178 | let late = Utc.with_ymd_and_hms(2026, 11, 21, 9, 0, 0).unwrap(); |
| 179 | assert!(meeting_draft(¶ms(0, 1, &["a@example.com"]), late).is_err()); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn an_availability_window_stays_short() { |
| 184 | let start = Utc.with_ymd_and_hms(2026, 11, 20, 0, 0, 0).unwrap(); |
| 185 | let request = |days| FindAvailabilityParams { |
| 186 | attendees: vec!["a@example.com".to_owned()], |
| 187 | start, |
| 188 | end: start + Duration::days(days), |
| 189 | }; |
| 190 | assert!(availability_request(&request(14)).is_ok()); |
| 191 | assert!(availability_request(&request(15)).is_err()); |
| 192 | } |
| 193 | } |