| 1 | use chrono::{DateTime, Utc}; |
| 2 | use store::{CalendarAccounts, GoogleTokenSet}; |
| 3 | |
| 4 | use chatsvc::{ |
| 5 | CalendarEvent, GoogleCalendar, GoogleEventDraft, GoogleOAuth, Reply, client_id_from_env, |
| 6 | google_rsvp_status, refresh_access_token, |
| 7 | }; |
| 8 | |
| 9 | /// Refresh tokens when needed and load events from every selected Google calendar. |
| 10 | pub async fn fetch_google_events( |
| 11 | accounts: &mut CalendarAccounts, |
| 12 | start: DateTime<Utc>, |
| 13 | end: DateTime<Utc>, |
| 14 | ) -> Result<Vec<CalendarEvent>, String> { |
| 15 | let Some(oauth) = client_id_from_env() else { |
| 16 | return Ok(Vec::new()); |
| 17 | }; |
| 18 | let mut all = Vec::new(); |
| 19 | for index in 0..accounts.google_accounts.len() { |
| 20 | if !ensure_access_token(accounts, index, &oauth).await? { |
| 21 | continue; |
| 22 | } |
| 23 | let account = &accounts.google_accounts[index]; |
| 24 | let access = account.tokens.access_token.clone(); |
| 25 | let account_id = account.id.clone(); |
| 26 | let calendars: Vec<_> = account |
| 27 | .calendars |
| 28 | .iter() |
| 29 | .filter(|calendar| calendar.selected) |
| 30 | .map(|calendar| calendar.id.clone()) |
| 31 | .collect(); |
| 32 | let calendars = if calendars.is_empty() { |
| 33 | vec!["primary".to_owned()] |
| 34 | } else { |
| 35 | calendars |
| 36 | }; |
| 37 | let client = GoogleCalendar::new(access); |
| 38 | for calendar_id in calendars { |
| 39 | match client.events(&calendar_id, start, end).await { |
| 40 | Ok(mut events) => { |
| 41 | for event in &mut events { |
| 42 | event.account_id = Some(account_id.clone()); |
| 43 | event.calendar_id = Some(calendar_id.clone()); |
| 44 | } |
| 45 | all.extend(events); |
| 46 | } |
| 47 | Err(error) => crate::diagnostics::record(format!( |
| 48 | "calls: google calendar {calendar_id} not loaded: {error}" |
| 49 | )), |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | Ok(all) |
| 54 | } |
| 55 | |
| 56 | pub async fn insert_google_event( |
| 57 | accounts: &mut CalendarAccounts, |
| 58 | account_id: &str, |
| 59 | calendar_id: &str, |
| 60 | draft: &GoogleEventDraft, |
| 61 | ) -> Result<(), String> { |
| 62 | let oauth = client_id_from_env().ok_or_else(|| missing_client_id())?; |
| 63 | let index = accounts |
| 64 | .google_accounts |
| 65 | .iter() |
| 66 | .position(|account| account.id == account_id) |
| 67 | .ok_or_else(|| format!("Google account {account_id} missing"))?; |
| 68 | if !ensure_access_token(accounts, index, &oauth).await? { |
| 69 | return Err("Google access token missing".into()); |
| 70 | } |
| 71 | let access = accounts.google_accounts[index].tokens.access_token.clone(); |
| 72 | GoogleCalendar::new(access) |
| 73 | .insert_event(calendar_id, draft) |
| 74 | .await |
| 75 | .map(|_| ()) |
| 76 | .map_err(|error| error.to_string()) |
| 77 | } |
| 78 | |
| 79 | pub async fn respond_google_event( |
| 80 | accounts: &mut CalendarAccounts, |
| 81 | account_id: &str, |
| 82 | calendar_id: &str, |
| 83 | event_id: &str, |
| 84 | reply: Reply, |
| 85 | send_updates: bool, |
| 86 | ) -> Result<(), String> { |
| 87 | let oauth = client_id_from_env().ok_or_else(|| missing_client_id())?; |
| 88 | let index = accounts |
| 89 | .google_accounts |
| 90 | .iter() |
| 91 | .position(|account| account.id == account_id) |
| 92 | .ok_or_else(|| format!("Google account {account_id} missing"))?; |
| 93 | if !ensure_access_token(accounts, index, &oauth).await? { |
| 94 | return Err("Google access token missing".into()); |
| 95 | } |
| 96 | let access = accounts.google_accounts[index].tokens.access_token.clone(); |
| 97 | GoogleCalendar::new(access) |
| 98 | .respond( |
| 99 | calendar_id, |
| 100 | event_id, |
| 101 | google_rsvp_status(reply), |
| 102 | send_updates, |
| 103 | ) |
| 104 | .await |
| 105 | .map_err(|error| error.to_string()) |
| 106 | } |
| 107 | |
| 108 | pub async fn delete_google_event( |
| 109 | accounts: &mut CalendarAccounts, |
| 110 | account_id: &str, |
| 111 | calendar_id: &str, |
| 112 | event_id: &str, |
| 113 | send_updates: bool, |
| 114 | ) -> Result<(), String> { |
| 115 | let oauth = client_id_from_env().ok_or_else(|| missing_client_id())?; |
| 116 | let index = accounts |
| 117 | .google_accounts |
| 118 | .iter() |
| 119 | .position(|account| account.id == account_id) |
| 120 | .ok_or_else(|| format!("Google account {account_id} missing"))?; |
| 121 | if !ensure_access_token(accounts, index, &oauth).await? { |
| 122 | return Err("Google access token missing".into()); |
| 123 | } |
| 124 | let access = accounts.google_accounts[index].tokens.access_token.clone(); |
| 125 | GoogleCalendar::new(access) |
| 126 | .delete_event(calendar_id, event_id, send_updates) |
| 127 | .await |
| 128 | .map_err(|error| error.to_string()) |
| 129 | } |
| 130 | |
| 131 | async fn ensure_access_token( |
| 132 | accounts: &mut CalendarAccounts, |
| 133 | index: usize, |
| 134 | oauth: &GoogleOAuth, |
| 135 | ) -> Result<bool, String> { |
| 136 | let now = Utc::now().timestamp(); |
| 137 | let account = &mut accounts.google_accounts[index]; |
| 138 | if account.tokens.refresh_token.is_empty() && account.tokens.access_token.is_empty() { |
| 139 | return Ok(false); |
| 140 | } |
| 141 | if account.tokens.expires_at <= now + 60 && !account.tokens.refresh_token.is_empty() { |
| 142 | match refresh_access_token(oauth, &account.tokens.refresh_token).await { |
| 143 | Ok(bundle) => { |
| 144 | account.tokens = GoogleTokenSet { |
| 145 | access_token: bundle.access_token, |
| 146 | refresh_token: if bundle.refresh_token.is_empty() { |
| 147 | account.tokens.refresh_token.clone() |
| 148 | } else { |
| 149 | bundle.refresh_token |
| 150 | }, |
| 151 | expires_at: now + bundle.expires_in.max(60), |
| 152 | }; |
| 153 | } |
| 154 | Err(error) => { |
| 155 | crate::diagnostics::record(format!("calls: google token refresh failed: {error}")); |
| 156 | return Ok(false); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | Ok(!account.tokens.access_token.is_empty()) |
| 161 | } |
| 162 | |
| 163 | fn missing_client_id() -> String { |
| 164 | "Set TEAMS_GOOGLE_CLIENT_ID to connect Google Calendar".into() |
| 165 | } |