| 1 | //! Read-only ICS subscriptions (holidays, Proton share links). |
| 2 | |
| 3 | use chrono::{DateTime, Utc}; |
| 4 | use store::{CalendarAccounts, IcsSubscription}; |
| 5 | use uuid::Uuid; |
| 6 | |
| 7 | use chatsvc::{CalendarEvent, CalendarSource, expand_events, parse_calendar}; |
| 8 | |
| 9 | pub async fn add_ics_subscription( |
| 10 | accounts: &mut CalendarAccounts, |
| 11 | url: &str, |
| 12 | summary: &str, |
| 13 | ) -> Result<String, String> { |
| 14 | let url = url.trim().to_owned(); |
| 15 | if url.is_empty() { |
| 16 | return Err("ICS URL is empty".into()); |
| 17 | } |
| 18 | // Smoke-fetch once so a bad URL fails at connect time. |
| 19 | let _ = fetch_one(&url, "tmp", summary, Utc::now(), Utc::now() + chrono::Duration::days(1)) |
| 20 | .await?; |
| 21 | let id = Uuid::new_v4().to_string(); |
| 22 | accounts.ics_subscriptions.push(IcsSubscription { |
| 23 | id: id.clone(), |
| 24 | url, |
| 25 | summary: if summary.trim().is_empty() { |
| 26 | "ICS".into() |
| 27 | } else { |
| 28 | summary.to_owned() |
| 29 | }, |
| 30 | selected: true, |
| 31 | }); |
| 32 | Ok(id) |
| 33 | } |
| 34 | |
| 35 | pub async fn fetch_ics_events( |
| 36 | accounts: &CalendarAccounts, |
| 37 | start: DateTime<Utc>, |
| 38 | end: DateTime<Utc>, |
| 39 | ) -> Result<Vec<CalendarEvent>, String> { |
| 40 | let mut all = Vec::new(); |
| 41 | for subscription in accounts |
| 42 | .ics_subscriptions |
| 43 | .iter() |
| 44 | .filter(|subscription| subscription.selected) |
| 45 | { |
| 46 | match fetch_one( |
| 47 | &subscription.url, |
| 48 | &subscription.id, |
| 49 | &subscription.summary, |
| 50 | start, |
| 51 | end, |
| 52 | ) |
| 53 | .await |
| 54 | { |
| 55 | Ok(events) => all.extend(events), |
| 56 | Err(error) => return Err(format!("ICS {}: {error}", subscription.summary)), |
| 57 | } |
| 58 | } |
| 59 | Ok(all) |
| 60 | } |
| 61 | |
| 62 | async fn fetch_one( |
| 63 | url: &str, |
| 64 | account_id: &str, |
| 65 | calendar_name: &str, |
| 66 | start: DateTime<Utc>, |
| 67 | end: DateTime<Utc>, |
| 68 | ) -> Result<Vec<CalendarEvent>, String> { |
| 69 | let client = reqwest::Client::new(); |
| 70 | let response = client |
| 71 | .get(url) |
| 72 | .send() |
| 73 | .await |
| 74 | .map_err(|e| e.to_string())?; |
| 75 | if !response.status().is_success() { |
| 76 | return Err(format!("HTTP {}", response.status())); |
| 77 | } |
| 78 | let body = response.text().await.map_err(|e| e.to_string())?; |
| 79 | let parsed = parse_calendar(&body); |
| 80 | let expanded = expand_events(parsed, start, end); |
| 81 | Ok(expanded |
| 82 | .into_iter() |
| 83 | .map(|item| CalendarEvent { |
| 84 | object_id: format!("ics:{}:{}", account_id, item.uid), |
| 85 | subject: item.summary, |
| 86 | start: item.start, |
| 87 | end: item.end, |
| 88 | all_day: item.all_day, |
| 89 | cancelled: item.cancelled, |
| 90 | online: item.join_url.is_some(), |
| 91 | join_url: item.join_url, |
| 92 | ical_uid: Some(item.uid), |
| 93 | source: CalendarSource::Ics, |
| 94 | account_id: Some(account_id.to_owned()), |
| 95 | calendar_id: Some(url.to_owned()), |
| 96 | sources: vec![CalendarSource::Ics], |
| 97 | location: item.location, |
| 98 | body_preview: item.description.clone(), |
| 99 | body_html: item.description, |
| 100 | reminders: item.reminders_minutes, |
| 101 | calendar_name: Some(calendar_name.to_owned()), |
| 102 | ..Default::default() |
| 103 | }) |
| 104 | .collect()) |
| 105 | } |