| 1 | use chrono::{DateTime, Duration, Utc}; |
| 2 | use serde::Deserialize; |
| 3 | use rmcp::schemars; |
| 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.chars().any(|character| character.is_whitespace() || character == ',') |
| 54 | } |
| 55 | |
| 56 | fn clean_emails(addresses: &[String], limit: usize) -> Result<Vec<String>, String> { |
| 57 | let mut cleaned: Vec<String> = Vec::new(); |
| 58 | for address in addresses { |
| 59 | let address = address.trim().to_lowercase(); |
| 60 | if !valid_email(&address) { |
| 61 | return Err(format!("{address:?} is not an email address")); |
| 62 | } |
| 63 | if !cleaned.contains(&address) { |
| 64 | cleaned.push(address); |
| 65 | } |
| 66 | } |
| 67 | if cleaned.is_empty() { |
| 68 | return Err("name at least one attendee".to_owned()); |
| 69 | } |
| 70 | if cleaned.len() > limit { |
| 71 | return Err(format!("at most {limit} attendees")); |
| 72 | } |
| 73 | Ok(cleaned) |
| 74 | } |
| 75 | |
| 76 | pub fn availability_request( |
| 77 | params: &FindAvailabilityParams, |
| 78 | ) -> Result<(Vec<String>, DateTime<Utc>, DateTime<Utc>), String> { |
| 79 | if params.end <= params.start { |
| 80 | return Err("end must be after start".to_owned()); |
| 81 | } |
| 82 | if params.end - params.start > Duration::days(MAX_AVAILABILITY_DAYS) { |
| 83 | return Err(format!("the window may span at most {MAX_AVAILABILITY_DAYS} days")); |
| 84 | } |
| 85 | let emails = clean_emails(¶ms.attendees, MAX_AVAILABILITY_PEOPLE)?; |
| 86 | Ok((emails, params.start, params.end)) |
| 87 | } |
| 88 | |
| 89 | pub fn meeting_draft(params: &CreateMeetingParams, now: DateTime<Utc>) -> Result<MeetingDraft, String> { |
| 90 | let subject = params.subject.trim(); |
| 91 | if subject.is_empty() { |
| 92 | return Err("subject is empty".to_owned()); |
| 93 | } |
| 94 | if subject.chars().count() > MAX_SUBJECT_CHARACTERS { |
| 95 | return Err(format!("subject is longer than {MAX_SUBJECT_CHARACTERS} characters")); |
| 96 | } |
| 97 | if params.end <= params.start { |
| 98 | return Err("end must be after start".to_owned()); |
| 99 | } |
| 100 | if params.end - params.start > Duration::hours(MAX_MEETING_HOURS) { |
| 101 | return Err(format!("a meeting may last at most {MAX_MEETING_HOURS} hours")); |
| 102 | } |
| 103 | if params.start < now - Duration::minutes(PAST_TOLERANCE_MINUTES) { |
| 104 | return Err("start lies in the past".to_owned()); |
| 105 | } |
| 106 | let description = params.description.clone().unwrap_or_default(); |
| 107 | if description.chars().count() > MAX_DESCRIPTION_CHARACTERS { |
| 108 | return Err(format!("description is longer than {MAX_DESCRIPTION_CHARACTERS} characters")); |
| 109 | } |
| 110 | Ok(MeetingDraft { |
| 111 | subject: subject.to_owned(), |
| 112 | start: params.start, |
| 113 | end: params.end, |
| 114 | attendees: clean_emails(¶ms.attendees, MAX_ATTENDEES)?, |
| 115 | description, |
| 116 | }) |
| 117 | } |
| 118 | |
| 119 | #[cfg(test)] |
| 120 | mod tests { |
| 121 | use chrono::{Duration, TimeZone, Utc}; |
| 122 | |
| 123 | use super::{CreateMeetingParams, FindAvailabilityParams, availability_request, meeting_draft}; |
| 124 | |
| 125 | fn params(start_hours: i64, length_hours: i64, attendees: &[&str]) -> CreateMeetingParams { |
| 126 | let start = Utc.with_ymd_and_hms(2026, 11, 20, 9, 0, 0).unwrap() + Duration::hours(start_hours); |
| 127 | CreateMeetingParams { |
| 128 | subject: " Release review ".to_owned(), |
| 129 | start, |
| 130 | end: start + Duration::hours(length_hours), |
| 131 | attendees: attendees.iter().map(|address| (*address).to_owned()).collect(), |
| 132 | description: Some("Agenda".to_owned()), |
| 133 | session_id: None, |
| 134 | agent: None, |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn a_meeting_draft_is_trimmed_and_deduplicated() { |
| 140 | let now = Utc.with_ymd_and_hms(2026, 11, 12, 9, 0, 0).unwrap(); |
| 141 | let draft = meeting_draft(¶ms(0, 1, &["Mara@Example.com", "mara@example.com", "jonas@example.com"]), now).unwrap(); |
| 142 | assert_eq!(draft.subject, "Release review"); |
| 143 | assert_eq!(draft.attendees, ["mara@example.com", "jonas@example.com"]); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn bad_meetings_are_refused() { |
| 148 | let now = Utc.with_ymd_and_hms(2026, 11, 12, 9, 0, 0).unwrap(); |
| 149 | assert!(meeting_draft(¶ms(0, 0, &["a@example.com"]), now).is_err()); |
| 150 | assert!(meeting_draft(¶ms(0, 25, &["a@example.com"]), now).is_err()); |
| 151 | assert!(meeting_draft(¶ms(0, 1, &["not an address"]), now).is_err()); |
| 152 | assert!(meeting_draft(¶ms(0, 1, &[]), now).is_err()); |
| 153 | let late = Utc.with_ymd_and_hms(2026, 11, 21, 9, 0, 0).unwrap(); |
| 154 | assert!(meeting_draft(¶ms(0, 1, &["a@example.com"]), late).is_err()); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn an_availability_window_stays_short() { |
| 159 | let start = Utc.with_ymd_and_hms(2026, 11, 20, 0, 0, 0).unwrap(); |
| 160 | let request = |days| FindAvailabilityParams { |
| 161 | attendees: vec!["a@example.com".to_owned()], |
| 162 | start, |
| 163 | end: start + Duration::days(days), |
| 164 | }; |
| 165 | assert!(availability_request(&request(14)).is_ok()); |
| 166 | assert!(availability_request(&request(15)).is_err()); |
| 167 | } |
| 168 | } |