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