| 1 | use chrono::{Datelike, Timelike}; |
| 2 | use serde_json::{Value, json}; |
| 3 | |
| 4 | use crate::calendar::{Attendee, NewEvent, Repeat, Reply}; |
| 5 | |
| 6 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 7 | pub struct GoogleEventDraft { |
| 8 | pub subject: String, |
| 9 | pub start: chrono::DateTime<chrono::Utc>, |
| 10 | pub end: chrono::DateTime<chrono::Utc>, |
| 11 | pub attendees: Vec<Attendee>, |
| 12 | pub details: String, |
| 13 | pub join_url: Option<String>, |
| 14 | pub repeat: Repeat, |
| 15 | } |
| 16 | |
| 17 | impl From<&NewEvent> for GoogleEventDraft { |
| 18 | fn from(event: &NewEvent) -> Self { |
| 19 | GoogleEventDraft { |
| 20 | subject: event.subject.clone(), |
| 21 | start: event.start, |
| 22 | end: event.end, |
| 23 | attendees: event.attendees.clone(), |
| 24 | details: event.details.clone(), |
| 25 | join_url: None, |
| 26 | repeat: event.repeat, |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | pub fn rrule_for_repeat(repeat: Repeat, start: chrono::DateTime<chrono::Utc>) -> Option<String> { |
| 32 | match repeat { |
| 33 | Repeat::Never => None, |
| 34 | Repeat::Daily => Some("RRULE:FREQ=DAILY".to_owned()), |
| 35 | Repeat::Weekdays => Some("RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR".to_owned()), |
| 36 | Repeat::Weekly => { |
| 37 | let day = match start.weekday() { |
| 38 | chrono::Weekday::Mon => "MO", |
| 39 | chrono::Weekday::Tue => "TU", |
| 40 | chrono::Weekday::Wed => "WE", |
| 41 | chrono::Weekday::Thu => "TH", |
| 42 | chrono::Weekday::Fri => "FR", |
| 43 | chrono::Weekday::Sat => "SA", |
| 44 | chrono::Weekday::Sun => "SU", |
| 45 | }; |
| 46 | Some(format!("RRULE:FREQ=WEEKLY;BYDAY={day}")) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | pub fn google_event_body(draft: &GoogleEventDraft) -> Value { |
| 52 | let mut description = draft.details.clone(); |
| 53 | if let Some(join_url) = &draft.join_url { |
| 54 | if !description.is_empty() { |
| 55 | description.push_str("\n\n"); |
| 56 | } |
| 57 | description.push_str(join_url); |
| 58 | } |
| 59 | let mut body = json!({ |
| 60 | "summary": draft.subject, |
| 61 | "start": { |
| 62 | "dateTime": draft.start.to_rfc3339_opts(chrono::SecondsFormat::Secs, true), |
| 63 | "timeZone": "UTC", |
| 64 | }, |
| 65 | "end": { |
| 66 | "dateTime": draft.end.to_rfc3339_opts(chrono::SecondsFormat::Secs, true), |
| 67 | "timeZone": "UTC", |
| 68 | }, |
| 69 | "attendees": draft.attendees.iter().map(|attendee| json!({ |
| 70 | "email": attendee.address, |
| 71 | "displayName": attendee.name, |
| 72 | })).collect::<Vec<_>>(), |
| 73 | }); |
| 74 | if let Some(join_url) = &draft.join_url { |
| 75 | body["location"] = json!(join_url); |
| 76 | } |
| 77 | if !description.is_empty() { |
| 78 | body["description"] = json!(description); |
| 79 | } |
| 80 | if let Some(rrule) = rrule_for_repeat(draft.repeat, draft.start) { |
| 81 | body["recurrence"] = json!([rrule]); |
| 82 | } |
| 83 | let _ = (draft.start.hour(), draft.start.minute()); |
| 84 | body |
| 85 | } |
| 86 | |
| 87 | pub fn google_rsvp_status(reply: Reply) -> &'static str { |
| 88 | match reply { |
| 89 | Reply::Accept => "accepted", |
| 90 | Reply::Tentative => "tentative", |
| 91 | Reply::Decline => "declined", |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | pub fn google_rsvp_query(send_updates: bool) -> String { |
| 96 | format!("sendUpdates={}", if send_updates { "all" } else { "none" }) |
| 97 | } |
| 98 | |
| 99 | #[cfg(test)] |
| 100 | mod tests { |
| 101 | use chrono::TimeZone; |
| 102 | |
| 103 | use super::*; |
| 104 | |
| 105 | fn draft(repeat: Repeat) -> GoogleEventDraft { |
| 106 | GoogleEventDraft { |
| 107 | subject: "Sync".into(), |
| 108 | start: chrono::Utc.with_ymd_and_hms(2026, 11, 11, 9, 0, 0).unwrap(), |
| 109 | end: chrono::Utc |
| 110 | .with_ymd_and_hms(2026, 11, 11, 9, 30, 0) |
| 111 | .unwrap(), |
| 112 | attendees: vec![Attendee { |
| 113 | name: "Mara".into(), |
| 114 | address: "mara@example.com".into(), |
| 115 | }], |
| 116 | details: "Notes".into(), |
| 117 | join_url: Some("https://teams.microsoft.com/l/meetup-join/x".into()), |
| 118 | repeat, |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn body_puts_join_link_in_location_and_description() { |
| 124 | let body = google_event_body(&draft(Repeat::Never)); |
| 125 | assert_eq!( |
| 126 | body["location"], |
| 127 | "https://teams.microsoft.com/l/meetup-join/x" |
| 128 | ); |
| 129 | assert!( |
| 130 | body["description"] |
| 131 | .as_str() |
| 132 | .unwrap() |
| 133 | .contains("teams.microsoft.com") |
| 134 | ); |
| 135 | assert!(body.get("recurrence").is_none()); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn weekdays_rrule_and_rsvp_query() { |
| 140 | assert_eq!( |
| 141 | rrule_for_repeat( |
| 142 | Repeat::Weekdays, |
| 143 | chrono::Utc.with_ymd_and_hms(2026, 11, 11, 9, 0, 0).unwrap() |
| 144 | ) |
| 145 | .as_deref(), |
| 146 | Some("RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR") |
| 147 | ); |
| 148 | assert_eq!(google_rsvp_status(Reply::Decline), "declined"); |
| 149 | assert_eq!(google_rsvp_query(true), "sendUpdates=all"); |
| 150 | assert_eq!(google_rsvp_query(false), "sendUpdates=none"); |
| 151 | } |
| 152 | } |