Re-implement calendar imports

This commit is contained in:
Lennart K
2026-01-08 23:17:39 +01:00
parent 43fff63008
commit 276fdcacf5
3 changed files with 70 additions and 68 deletions

2
Cargo.lock generated
View File

@@ -1771,7 +1771,7 @@ dependencies = [
[[package]] [[package]]
name = "ical" name = "ical"
version = "0.11.0" version = "0.11.0"
source = "git+https://github.com/lennart-k/ical-rs?branch=dev#dd310bbb9866dd2b51d3e83e2b3572e4a3c7f7c9" source = "git+https://github.com/lennart-k/ical-rs?branch=dev#a64b2f6b2920c238f0aee6dd02bafcda5ca76040"
dependencies = [ dependencies = [
"chrono", "chrono",
"chrono-tz", "chrono-tz",

View File

@@ -5,13 +5,13 @@ use axum::{
response::{IntoResponse, Response}, response::{IntoResponse, Response},
}; };
use http::StatusCode; use http::StatusCode;
use ical::{generator::Emitter, parser::Component}; use ical::parser::{Component, ComponentMut};
use rustical_dav::header::Overwrite; use rustical_dav::header::Overwrite;
use rustical_ical::{CalendarObject, CalendarObjectType}; use rustical_ical::CalendarObjectType;
use rustical_store::{ use rustical_store::{
Calendar, CalendarMetadata, CalendarStore, SubscriptionStore, auth::Principal, Calendar, CalendarMetadata, CalendarStore, SubscriptionStore, auth::Principal,
}; };
use std::io::BufReader; use std::{collections::HashMap, io::BufReader};
use tracing::instrument; use tracing::instrument;
#[instrument(skip(resource_service))] #[instrument(skip(resource_service))]
@@ -26,18 +26,11 @@ pub async fn route_import<C: CalendarStore, S: SubscriptionStore>(
return Err(Error::Unauthorized); return Err(Error::Unauthorized);
} }
let mut parser = ical::IcalParser::new(BufReader::new(body.as_bytes())); let parser = ical::IcalParser::new(BufReader::new(body.as_bytes()));
let mut cal = parser let mut cal = parser
.next() .expect_one()
.expect("input must contain calendar") .map_err(rustical_ical::Error::ParserError)?
.unwrap()
.mutable(); .mutable();
if parser.next().is_some() {
return Err(rustical_ical::Error::InvalidData(
"multiple calendars, only one allowed".to_owned(),
)
.into());
}
// Extract calendar metadata // Extract calendar metadata
let displayname = cal let displayname = cal
@@ -53,59 +46,58 @@ pub async fn route_import<C: CalendarStore, S: SubscriptionStore>(
.get_property("X-WR-TIMEZONE") .get_property("X-WR-TIMEZONE")
.and_then(|prop| prop.value.clone()); .and_then(|prop| prop.value.clone());
// These properties should not appear in the expanded calendar objects // These properties should not appear in the expanded calendar objects
todo!(); cal.remove_property("X-WR-CALNAME");
// cal.remove_property("X-WR-CALNAME"); cal.remove_property("X-WR-CALDESC");
// cal.remove_property("X-WR-CALDESC"); cal.remove_property("X-WR-CALCOLOR");
// cal.remove_property("X-WR-TIMEZONE"); cal.remove_property("X-WR-TIMEZONE");
// let cal = cal.verify().unwrap(); let cal = cal.build(&HashMap::new()).unwrap();
// // Make sure timezone is valid
// if let Some(timezone_id) = timezone_id.as_ref() { // Make sure timezone is valid
// assert!( if let Some(timezone_id) = timezone_id.as_ref() {
// vtimezones_rs::VTIMEZONES.contains_key(timezone_id), assert!(
// "Invalid calendar timezone id" vtimezones_rs::VTIMEZONES.contains_key(timezone_id),
// ); "Invalid calendar timezone id"
// } );
// }
// // Extract necessary component types // // Extract necessary component types
// let mut cal_components = vec![]; let mut cal_components = vec![];
// if !cal.events.is_empty() { if !cal.events.is_empty() {
// cal_components.push(CalendarObjectType::Event); cal_components.push(CalendarObjectType::Event);
// } }
// if !cal.journals.is_empty() { if !cal.journals.is_empty() {
// cal_components.push(CalendarObjectType::Journal); cal_components.push(CalendarObjectType::Journal);
// } }
// if !cal.todos.is_empty() { if !cal.todos.is_empty() {
// cal_components.push(CalendarObjectType::Todo); cal_components.push(CalendarObjectType::Todo);
// } }
//
// let expanded_cals = cal.expand_calendar(); let objects = cal
// // Janky way to convert between IcalCalendar and CalendarObject .into_objects()
// let objects = expanded_cals .map_err(rustical_ical::Error::ParserError)?
// .into_iter() .into_iter()
// .map(|cal| cal.generate()) .map(Into::into)
// .map(|ics| CalendarObject::from_ics(ics, None)) .collect();
// .collect::<Result<Vec<_>, _>>()?; let new_cal = Calendar {
// let new_cal = Calendar { principal,
// principal, id: cal_id,
// id: cal_id, meta: CalendarMetadata {
// meta: CalendarMetadata { displayname,
// displayname, order: 0,
// order: 0, description,
// description, color,
// color: None, },
// }, timezone_id,
// timezone_id, deleted_at: None,
// deleted_at: None, synctoken: 0,
// synctoken: 0, subscription_url: None,
// subscription_url: None, push_topic: uuid::Uuid::new_v4().to_string(),
// push_topic: uuid::Uuid::new_v4().to_string(), components: cal_components,
// components: cal_components, };
// };
// let cal_store = resource_service.cal_store;
// let cal_store = resource_service.cal_store; cal_store
// cal_store .import_calendar(new_cal, objects, overwrite)
// .import_calendar(new_cal, objects, overwrite) .await?;
// .await?;
// Ok(StatusCode::OK.into_response())
// Ok(StatusCode::OK.into_response())
} }

View File

@@ -2,6 +2,7 @@ use crate::Error;
use derive_more::Display; use derive_more::Display;
use ical::component::CalendarInnerData; use ical::component::CalendarInnerData;
use ical::component::IcalCalendarObject; use ical::component::IcalCalendarObject;
use ical::generator::Emitter;
use ical::parser::ComponentParser; use ical::parser::ComponentParser;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
@@ -108,3 +109,12 @@ impl From<CalendarObject> for IcalCalendarObject {
value.inner value.inner
} }
} }
impl From<IcalCalendarObject> for CalendarObject {
fn from(value: IcalCalendarObject) -> Self {
Self {
ics: value.generate(),
inner: value,
}
}
}