diff --git a/.gitignore b/.gitignore index dbab0f4..a5b5571 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ site # Frontend **/node_modules **/.vite + +**/*.snap.new diff --git a/Cargo.lock b/Cargo.lock index 4e6e525..54945ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -573,9 +573,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.2.52" +version = "1.2.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd4932aefd12402b36c60956a4fe0035421f544799057659ff86f923657aada3" +checksum = "755d2fce177175ffca841e9a06afdb2c4ab0f593d53b4dee48147dfaade85932" dependencies = [ "find-msvc-tools", "shlex", @@ -1241,9 +1241,9 @@ dependencies = [ [[package]] name = "find-msvc-tools" -version = "0.1.7" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f449e6c6c08c865631d4890cfacf252b3d396c9bcc83adb6623cdb02a8336c41" +checksum = "8591b0bcc8a98a64310a2fae1bb3e9b8564dd10e381e6e28010fde8e8e8568db" [[package]] name = "flume" @@ -1768,25 +1768,10 @@ dependencies = [ "cc", ] -[[package]] -name = "ical" -version = "0.11.0" -source = "git+https://github.com/lennart-k/ical-rs?rev=7c2ab1f3#7c2ab1f3abdca768f22d8a36627eebbdd7947e29" -dependencies = [ - "chrono", - "chrono-tz", - "derive_more", - "itertools 0.14.0", - "lazy_static", - "regex", - "rrule", - "thiserror 2.0.17", -] - [[package]] name = "ical" version = "0.12.0-dev" -source = "git+https://github.com/lennart-k/ical-rs?branch=dev#d2226f6b92fa45dcc5a243adc57e6a07a67741a8" +source = "git+https://github.com/lennart-k/ical-rs?rev=f1ad6456fd6cbd1e6da095297febddd2cfe61422#f1ad6456fd6cbd1e6da095297febddd2cfe61422" dependencies = [ "chrono", "chrono-tz", @@ -1796,7 +1781,7 @@ dependencies = [ "phf 0.13.1", "regex", "rrule", - "thiserror 2.0.17", + "thiserror 2.0.18", ] [[package]] @@ -2133,7 +2118,7 @@ dependencies = [ "matchit 0.9.1", "percent-encoding", "serde", - "thiserror 2.0.17", + "thiserror 2.0.18", ] [[package]] @@ -2385,7 +2370,7 @@ dependencies = [ "futures-sink", "js-sys", "pin-project-lite", - "thiserror 2.0.17", + "thiserror 2.0.18", "tracing", ] @@ -2415,7 +2400,7 @@ dependencies = [ "opentelemetry_sdk", "prost", "reqwest", - "thiserror 2.0.17", + "thiserror 2.0.18", "tokio", "tonic", "tracing", @@ -2452,7 +2437,7 @@ dependencies = [ "opentelemetry", "percent-encoding", "rand 0.9.2", - "thiserror 2.0.17", + "thiserror 2.0.18", "tokio", "tokio-stream", ] @@ -2906,7 +2891,7 @@ dependencies = [ "rustc-hash", "rustls", "socket2", - "thiserror 2.0.17", + "thiserror 2.0.18", "tokio", "tracing", "web-time", @@ -2927,7 +2912,7 @@ dependencies = [ "rustls", "rustls-pki-types", "slab", - "thiserror 2.0.17", + "thiserror 2.0.18", "tinyvec", "tracing", "web-time", @@ -3202,7 +3187,7 @@ dependencies = [ "chrono-tz", "log", "regex", - "thiserror 2.0.17", + "thiserror 2.0.18", ] [[package]] @@ -3343,7 +3328,7 @@ dependencies = [ "figment", "headers", "http", - "ical 0.12.0-dev", + "ical", "insta", "opentelemetry", "opentelemetry-otlp", @@ -3364,6 +3349,7 @@ dependencies = [ "rustical_store", "rustical_store_sqlite", "serde", + "similar-asserts", "sqlx", "tokio", "toml 0.9.11+spec-1.1.0", @@ -3391,7 +3377,7 @@ dependencies = [ "futures-util", "headers", "http", - "ical 0.11.0", + "ical", "insta", "percent-encoding", "quick-xml", @@ -3408,7 +3394,7 @@ dependencies = [ "similar-asserts", "strum", "strum_macros", - "thiserror 2.0.17", + "thiserror 2.0.18", "tokio", "tower", "tower-http", @@ -3430,7 +3416,7 @@ dependencies = [ "derive_more", "futures-util", "http", - "ical 0.11.0", + "ical", "insta", "percent-encoding", "quick-xml", @@ -3443,7 +3429,7 @@ dependencies = [ "serde", "strum", "strum_macros", - "thiserror 2.0.17", + "thiserror 2.0.18", "tokio", "tower", "tower-http", @@ -3463,7 +3449,7 @@ dependencies = [ "futures-util", "headers", "http", - "ical 0.11.0", + "ical", "itertools 0.14.0", "log", "matchit 0.9.1", @@ -3472,7 +3458,7 @@ dependencies = [ "rustical_xml", "serde", "strum", - "thiserror 2.0.17", + "thiserror 2.0.18", "tokio", "tower", "tracing", @@ -3498,7 +3484,7 @@ dependencies = [ "rustical_store", "rustical_xml", "serde", - "thiserror 2.0.17", + "thiserror 2.0.18", "tokio", "tracing", ] @@ -3528,7 +3514,7 @@ dependencies = [ "rustical_store", "serde", "serde_json", - "thiserror 2.0.17", + "thiserror 2.0.18", "tokio", "tower", "tower-http", @@ -3547,7 +3533,7 @@ dependencies = [ "chrono", "chrono-tz", "derive_more", - "ical 0.11.0", + "ical", "regex", "rrule", "rstest", @@ -3555,7 +3541,7 @@ dependencies = [ "serde", "sha2", "similar-asserts", - "thiserror 2.0.17", + "thiserror 2.0.18", ] [[package]] @@ -3569,7 +3555,7 @@ dependencies = [ "openidconnect", "reqwest", "serde", - "thiserror 2.0.17", + "thiserror 2.0.18", "tower-sessions", "tracing", ] @@ -3588,7 +3574,7 @@ dependencies = [ "futures-core", "headers", "http", - "ical 0.11.0", + "ical", "regex", "rrule", "rstest", @@ -3599,7 +3585,7 @@ dependencies = [ "rustical_xml", "serde", "sha2", - "thiserror 2.0.17", + "thiserror 2.0.18", "tokio", "tower", "tower-sessions", @@ -3615,6 +3601,7 @@ dependencies = [ "chrono", "criterion", "derive_more", + "ical", "password-auth", "password-hash", "pbkdf2", @@ -3625,7 +3612,7 @@ dependencies = [ "serde", "sha2", "sqlx", - "thiserror 2.0.17", + "thiserror 2.0.18", "tokio", "tracing", "uuid", @@ -3636,7 +3623,7 @@ name = "rustical_xml" version = "0.11.17" dependencies = [ "quick-xml", - "thiserror 2.0.17", + "thiserror 2.0.18", "xml_derive", ] @@ -3669,9 +3656,9 @@ dependencies = [ [[package]] name = "rustls-pki-types" -version = "1.13.3" +version = "1.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4910321ebe4151be888e35fe062169554e74aad01beafed60410131420ceffbc" +checksum = "be040f8b0a225e40375822a563fa9524378b9d63112f53e19ffff34df5d33fdd" dependencies = [ "web-time", "zeroize", @@ -3679,9 +3666,9 @@ dependencies = [ [[package]] name = "rustls-webpki" -version = "0.103.8" +version = "0.103.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ffdfa2f5286e2247234e03f680868ac2815974dc39e00ea15adc445d0aafe52" +checksum = "d7df23109aa6c1567d1c575b9952556388da57401e4ace1d15f79eedad0d8f53" dependencies = [ "ring", "rustls-pki-types", @@ -4061,7 +4048,7 @@ dependencies = [ "serde_json", "sha2", "smallvec", - "thiserror 2.0.17", + "thiserror 2.0.18", "tokio", "tokio-stream", "tracing", @@ -4145,7 +4132,7 @@ dependencies = [ "smallvec", "sqlx-core", "stringprep", - "thiserror 2.0.17", + "thiserror 2.0.18", "tracing", "uuid", "whoami", @@ -4184,7 +4171,7 @@ dependencies = [ "smallvec", "sqlx-core", "stringprep", - "thiserror 2.0.17", + "thiserror 2.0.18", "tracing", "uuid", "whoami", @@ -4210,7 +4197,7 @@ dependencies = [ "serde", "serde_urlencoded", "sqlx-core", - "thiserror 2.0.17", + "thiserror 2.0.18", "tracing", "url", "uuid", @@ -4329,11 +4316,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.17" +version = "2.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f63587ca0f12b72a0600bcba1d40081f830876000bb46dd2337a3051618f4fc8" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" dependencies = [ - "thiserror-impl 2.0.17", + "thiserror-impl 2.0.18", ] [[package]] @@ -4349,9 +4336,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "2.0.17" +version = "2.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" dependencies = [ "proc-macro2", "quote", @@ -4733,7 +4720,7 @@ dependencies = [ "rand 0.8.5", "serde", "serde_json", - "thiserror 2.0.17", + "thiserror 2.0.18", "time", "tokio", "tracing", @@ -5576,6 +5563,6 @@ dependencies = [ [[package]] name = "zmij" -version = "1.0.14" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd8f3f50b848df28f887acb68e41201b5aea6bc8a8dacc00fb40635ff9a72fea" +checksum = "94f63c051f4fe3c1509da62131a678643c5b6fbdc9273b2b79d4378ebda003d2" diff --git a/Cargo.toml b/Cargo.toml index b73b07e..a6fab4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -107,7 +107,9 @@ strum = "0.27" strum_macros = "0.27" serde_json = { version = "1.0", features = ["raw_value"] } sqlx-sqlite = { version = "0.8", features = ["bundled"] } -ical = { git = "https://github.com/lennart-k/ical-rs", rev = "7c2ab1f3" } +ical = { git = "https://github.com/lennart-k/ical-rs", rev = "f1ad6456fd6cbd1e6da095297febddd2cfe61422", features = [ + "chrono-tz", +] } toml = "0.9" tower = "0.5" tower-http = { version = "0.6", features = [ @@ -151,6 +153,7 @@ criterion = { version = "0.8", features = ["async_tokio"] } rstest.workspace = true rustical_store_sqlite = { workspace = true, features = ["test"] } insta.workspace = true +similar-asserts.workspace = true [dependencies] rustical_store.workspace = true @@ -158,6 +161,7 @@ rustical_store_sqlite.workspace = true rustical_caldav.workspace = true rustical_carddav.workspace = true rustical_frontend.workspace = true +ical.workspace = true toml.workspace = true serde.workspace = true tokio.workspace = true @@ -199,7 +203,3 @@ tower-http.workspace = true axum-extra.workspace = true headers.workspace = true http.workspace = true -# TODO: Remove in next major release -ical_dev = { package = "ical", git = "https://github.com/lennart-k/ical-rs", branch = "dev", features = [ - "chrono-tz", -] } diff --git a/crates/caldav/src/calendar/methods/get.rs b/crates/caldav/src/calendar/methods/get.rs index f69843e..c00cf72 100644 --- a/crates/caldav/src/calendar/methods/get.rs +++ b/crates/caldav/src/calendar/methods/get.rs @@ -5,13 +5,11 @@ use axum::extract::State; use axum::{extract::Path, response::Response}; use headers::{ContentType, HeaderMapExt}; use http::{HeaderValue, Method, StatusCode, header}; -use ical::builder::calendar::IcalCalendarBuilder; +use ical::component::IcalCalendar; use ical::generator::Emitter; -use ical::property::Property; +use ical::property::ContentLine; use percent_encoding::{CONTROLS, utf8_percent_encode}; -use rustical_ical::{CalendarObjectComponent, EventObject}; use rustical_store::{CalendarStore, SubscriptionStore, auth::Principal}; -use std::collections::HashMap; use std::str::FromStr; use tracing::instrument; @@ -33,60 +31,45 @@ pub async fn route_get( return Err(crate::Error::Unauthorized); } - let mut vtimezones = HashMap::new(); - let objects = cal_store.get_objects(&principal, &calendar_id).await?; + let objects = cal_store + .get_objects(&principal, &calendar_id) + .await? + .into_iter() + .map(|(_, object)| object.into()) + .collect(); + + let mut props = vec![]; - let mut ical_calendar_builder = IcalCalendarBuilder::version("2.0") - .gregorian() - .prodid("RustiCal"); if let Some(displayname) = calendar.meta.displayname { - ical_calendar_builder = ical_calendar_builder.set(Property { + props.push(ContentLine { name: "X-WR-CALNAME".to_owned(), value: Some(displayname), - params: vec![], + params: vec![].into(), }); } if let Some(description) = calendar.meta.description { - ical_calendar_builder = ical_calendar_builder.set(Property { + props.push(ContentLine { name: "X-WR-CALDESC".to_owned(), value: Some(description), - params: vec![], + params: vec![].into(), + }); + } + if let Some(color) = calendar.meta.color { + props.push(ContentLine { + name: "X-WR-CALCOLOR".to_owned(), + value: Some(color), + params: vec![].into(), }); } if let Some(timezone_id) = calendar.timezone_id { - ical_calendar_builder = ical_calendar_builder.set(Property { + props.push(ContentLine { name: "X-WR-TIMEZONE".to_owned(), value: Some(timezone_id), - params: vec![], + params: vec![].into(), }); } - for object in &objects { - vtimezones.extend(object.get_vtimezones()); - match object.get_data() { - CalendarObjectComponent::Event(EventObject { event, .. }, overrides) => { - ical_calendar_builder = ical_calendar_builder - .add_event(event.clone()) - .add_events(overrides.iter().map(|ev| ev.event.clone())); - } - CalendarObjectComponent::Todo(todo, overrides) => { - ical_calendar_builder = ical_calendar_builder - .add_todo(todo.clone()) - .add_todos(overrides.iter().cloned()); - } - CalendarObjectComponent::Journal(journal, overrides) => { - ical_calendar_builder = ical_calendar_builder - .add_journal(journal.clone()) - .add_journals(overrides.iter().cloned()); - } - } - } - - ical_calendar_builder = ical_calendar_builder.add_timezones(vtimezones.into_values().cloned()); - - let ical_calendar = ical_calendar_builder - .build() - .map_err(|parser_error| Error::IcalError(parser_error.into()))?; + let export_calendar = IcalCalendar::from_objects("RustiCal Export".to_owned(), objects, props); let mut resp = Response::builder().status(StatusCode::OK); let hdrs = resp.headers_mut().unwrap(); @@ -104,6 +87,6 @@ pub async fn route_get( if matches!(method, Method::HEAD) { Ok(resp.body(Body::empty()).unwrap()) } else { - Ok(resp.body(Body::new(ical_calendar.generate())).unwrap()) + Ok(resp.body(Body::new(export_calendar.generate())).unwrap()) } } diff --git a/crates/caldav/src/calendar/methods/import.rs b/crates/caldav/src/calendar/methods/import.rs index deb3b05..d6370a3 100644 --- a/crates/caldav/src/calendar/methods/import.rs +++ b/crates/caldav/src/calendar/methods/import.rs @@ -5,16 +5,12 @@ use axum::{ response::{IntoResponse, Response}, }; use http::StatusCode; -use ical::{ - generator::Emitter, - parser::{Component, ComponentMut}, -}; +use ical::parser::{Component, ComponentMut}; use rustical_dav::header::Overwrite; -use rustical_ical::{CalendarObject, CalendarObjectType}; +use rustical_ical::CalendarObjectType; use rustical_store::{ Calendar, CalendarMetadata, CalendarStore, SubscriptionStore, auth::Principal, }; -use std::io::BufReader; use tracing::instrument; #[instrument(skip(resource_service))] @@ -29,18 +25,11 @@ pub async fn route_import( return Err(Error::Unauthorized); } - let mut parser = ical::IcalParser::new(BufReader::new(body.as_bytes())); - let mut cal = parser - .next() - .expect("input must contain calendar") - .unwrap() - .mutable(); - if parser.next().is_some() { - return Err(rustical_ical::Error::InvalidData( - "multiple calendars, only one allowed".to_owned(), - ) - .into()); - } + let parser = ical::IcalParser::from_slice(body.as_bytes()); + let mut cal = match parser.expect_one() { + Ok(cal) => cal.mutable(), + Err(err) => return Ok((StatusCode::BAD_REQUEST, err.to_string()).into_response()), + }; // Extract calendar metadata let displayname = cal @@ -49,14 +38,19 @@ pub async fn route_import( let description = cal .get_property("X-WR-CALDESC") .and_then(|prop| prop.value.clone()); + let color = cal + .get_property("X-WR-CALCOLOR") + .and_then(|prop| prop.value.clone()); let timezone_id = cal .get_property("X-WR-TIMEZONE") .and_then(|prop| prop.value.clone()); // These properties should not appear in the expanded calendar objects cal.remove_property("X-WR-CALNAME"); cal.remove_property("X-WR-CALDESC"); + cal.remove_property("X-WR-CALCOLOR"); cal.remove_property("X-WR-TIMEZONE"); - let cal = cal.verify().unwrap(); + let cal = cal.build(None).unwrap(); + // Make sure timezone is valid if let Some(timezone_id) = timezone_id.as_ref() { assert!( @@ -64,8 +58,7 @@ pub async fn route_import( "Invalid calendar timezone id" ); } - - // Extract necessary component types + // // Extract necessary component types let mut cal_components = vec![]; if !cal.events.is_empty() { cal_components.push(CalendarObjectType::Event); @@ -77,13 +70,10 @@ pub async fn route_import( cal_components.push(CalendarObjectType::Todo); } - let expanded_cals = cal.expand_calendar(); - // Janky way to convert between IcalCalendar and CalendarObject - let objects = expanded_cals - .into_iter() - .map(|cal| cal.generate()) - .map(|ics| CalendarObject::from_ics(ics, None)) - .collect::, _>>()?; + let objects = match cal.into_objects() { + Ok(objects) => objects.into_iter().map(Into::into).collect(), + Err(err) => return Ok((StatusCode::BAD_REQUEST, err.to_string()).into_response()), + }; let new_cal = Calendar { principal, id: cal_id, @@ -91,7 +81,7 @@ pub async fn route_import( displayname, order: 0, description, - color: None, + color, }, timezone_id, deleted_at: None, diff --git a/crates/caldav/src/calendar/methods/mkcalendar.rs b/crates/caldav/src/calendar/methods/mkcalendar.rs index 0847a57..d065231 100644 --- a/crates/caldav/src/calendar/methods/mkcalendar.rs +++ b/crates/caldav/src/calendar/methods/mkcalendar.rs @@ -87,15 +87,16 @@ pub async fn route_mkcalendar( Some(tzid) } else if let Some(tz) = request.calendar_timezone { // TODO: Proper error (calendar-timezone precondition) - let calendar = IcalParser::new(tz.as_bytes()) + let calendar = IcalParser::from_slice(tz.as_bytes()) .next() .ok_or_else(|| rustical_dav::Error::BadRequest("No timezone data provided".to_owned()))? .map_err(|_| rustical_dav::Error::BadRequest("Error parsing timezone".to_owned()))?; - let timezone = calendar.timezones.first().ok_or_else(|| { + let timezone = calendar.vtimezones.values().next().ok_or_else(|| { rustical_dav::Error::BadRequest("No timezone data provided".to_owned()) })?; - let timezone: chrono_tz::Tz = timezone.try_into().map_err(|_| { + let timezone: Option = timezone.into(); + let timezone = timezone.ok_or_else(|| { rustical_dav::Error::BadRequest("Cannot translate VTIMEZONE into IANA TZID".to_owned()) })?; diff --git a/crates/caldav/src/calendar/methods/report/calendar_multiget.rs b/crates/caldav/src/calendar/methods/report/calendar_multiget.rs index 2cf61ff..a16c6fa 100644 --- a/crates/caldav/src/calendar/methods/report/calendar_multiget.rs +++ b/crates/caldav/src/calendar/methods/report/calendar_multiget.rs @@ -21,7 +21,7 @@ pub async fn get_objects_calendar_multiget( principal: &str, cal_id: &str, store: &C, -) -> Result<(Vec, Vec), Error> { +) -> Result<(Vec<(String, CalendarObject)>, Vec), Error> { let mut result = vec![]; let mut not_found = vec![]; @@ -32,7 +32,7 @@ pub async fn get_objects_calendar_multiget( let filename = filename.trim_start_matches('/'); if let Some(object_id) = filename.strip_suffix(".ics") { match store.get_object(principal, cal_id, object_id, false).await { - Ok(object) => result.push(object), + Ok(object) => result.push((object_id.to_owned(), object)), Err(rustical_store::Error::NotFound) => not_found.push(href.to_string()), Err(err) => return Err(err.into()), } diff --git a/crates/caldav/src/calendar/methods/report/calendar_query/comp_filter.rs b/crates/caldav/src/calendar/methods/report/calendar_query/comp_filter.rs index cc34a88..57d08a6 100644 --- a/crates/caldav/src/calendar/methods/report/calendar_query/comp_filter.rs +++ b/crates/caldav/src/calendar/methods/report/calendar_query/comp_filter.rs @@ -2,8 +2,10 @@ use crate::calendar::methods::report::calendar_query::{ TimeRangeElement, prop_filter::{PropFilterElement, PropFilterable}, }; -use ical::parser::ical::component::IcalTimeZone; -use rustical_ical::{CalendarObject, CalendarObjectComponent, CalendarObjectType}; +use ical::{ + component::{CalendarInnerData, IcalAlarm, IcalCalendarObject, IcalEvent, IcalTodo}, + parser::{Component, ical::component::IcalTimeZone}, +}; use rustical_xml::XmlDeserialize; #[derive(XmlDeserialize, Clone, Debug, PartialEq)] @@ -68,9 +70,98 @@ pub trait CompFilterable: PropFilterable + Sized { } } -impl CompFilterable for CalendarObject { +impl CompFilterable for CalendarInnerData { fn get_comp_name(&self) -> &'static str { - "VCALENDAR" + match self { + Self::Event(main, _) => main.get_comp_name(), + Self::Journal(main, _) => main.get_comp_name(), + Self::Todo(main, _) => main.get_comp_name(), + } + } + + fn match_time_range(&self, time_range: &TimeRangeElement) -> bool { + if let Some(start) = &time_range.start + && let Some(last_end) = self.get_last_occurence() + && start.to_utc() > last_end.utc() + { + return false; + } + if let Some(end) = &time_range.end + && let Some(first_start) = self.get_first_occurence() + && end.to_utc() < first_start.utc() + { + return false; + } + true + } + + fn match_subcomponents(&self, comp_filter: &CompFilterElement) -> bool { + match self { + Self::Event(main, overrides) => std::iter::once(main) + .chain(overrides.iter()) + .flat_map(IcalEvent::get_alarms) + .any(|alarm| alarm.matches(comp_filter)), + Self::Todo(main, overrides) => std::iter::once(main) + .chain(overrides.iter()) + .flat_map(IcalTodo::get_alarms) + .any(|alarm| alarm.matches(comp_filter)), + // VJOURNAL has no subcomponents + Self::Journal(_, _) => comp_filter.is_not_defined.is_some(), + } + } +} + +impl PropFilterable for IcalAlarm { + fn get_named_properties<'a>( + &'a self, + name: &'a str, + ) -> impl Iterator { + Component::get_named_properties(self, name) + } +} + +impl CompFilterable for IcalAlarm { + fn get_comp_name(&self) -> &'static str { + Component::get_comp_name(self) + } + + fn match_time_range(&self, _time_range: &TimeRangeElement) -> bool { + true + } + + fn match_subcomponents(&self, comp_filter: &CompFilterElement) -> bool { + comp_filter.is_not_defined.is_some() + } +} + +impl PropFilterable for CalendarInnerData { + #[allow(refining_impl_trait)] + fn get_named_properties<'a>( + &'a self, + name: &'a str, + ) -> Box + 'a> { + // TODO: If we were pedantic, we would have to do recurrence expansion first + // and take into account the overrides :( + match self { + Self::Event(main, _) => Box::new(main.get_named_properties(name)), + Self::Todo(main, _) => Box::new(main.get_named_properties(name)), + Self::Journal(main, _) => Box::new(main.get_named_properties(name)), + } + } +} + +impl PropFilterable for IcalCalendarObject { + fn get_named_properties<'a>( + &'a self, + name: &'a str, + ) -> impl Iterator { + Component::get_named_properties(self, name) + } +} + +impl CompFilterable for IcalCalendarObject { + fn get_comp_name(&self) -> &'static str { + Component::get_comp_name(self) } fn match_time_range(&self, _time_range: &TimeRangeElement) -> bool { @@ -83,54 +174,36 @@ impl CompFilterable for CalendarObject { .get_vtimezones() .values() .map(|tz| tz.matches(comp_filter)) - .chain([self.get_data().matches(comp_filter)]); + .chain([self.get_inner().matches(comp_filter)]); if comp_filter.is_not_defined.is_some() { - matches.all(|x| x) + matches.all(|x| !x) } else { matches.any(|x| x) } } } +impl PropFilterable for IcalTimeZone { + fn get_named_properties<'a>( + &'a self, + name: &'a str, + ) -> impl Iterator { + Component::get_named_properties(self, name) + } +} + impl CompFilterable for IcalTimeZone { fn get_comp_name(&self) -> &'static str { - "VTIMEZONE" + Component::get_comp_name(self) } - fn match_time_range(&self, _time_range: &TimeRangeElement) -> bool { false } - fn match_subcomponents(&self, _comp_filter: &CompFilterElement) -> bool { - true - } -} - -impl CompFilterable for CalendarObjectComponent { - fn get_comp_name(&self) -> &'static str { - CalendarObjectType::from(self).as_str() - } - - fn match_time_range(&self, time_range: &TimeRangeElement) -> bool { - if let Some(start) = &time_range.start - && let Some(last_occurence) = self.get_last_occurence().unwrap_or(None) - && **start > last_occurence.utc() - { - return false; - } - if let Some(end) = &time_range.end - && let Some(first_occurence) = self.get_first_occurence().unwrap_or(None) - && **end < first_occurence.utc() - { - return false; - } - true - } - - fn match_subcomponents(&self, _comp_filter: &CompFilterElement) -> bool { - // TODO: Properly check subcomponents - true + fn match_subcomponents(&self, comp_filter: &CompFilterElement) -> bool { + // VTIMEZONE has no subcomponents + comp_filter.is_not_defined.is_some() } } @@ -147,6 +220,7 @@ mod tests { const ICS: &str = r"BEGIN:VCALENDAR CALSCALE:GREGORIAN VERSION:2.0 +PRODID:me BEGIN:VTIMEZONE TZID:Europe/Berlin X-LIC-LOCATION:Europe/Berlin @@ -166,7 +240,7 @@ END:VCALENDAR"; #[test] fn test_comp_filter_matching() { - let object = CalendarObject::from_ics(ICS.to_string(), None).unwrap(); + let object = CalendarObject::from_ics(ICS.to_string()).unwrap(); let comp_filter = CompFilterElement { is_not_defined: Some(()), @@ -175,7 +249,10 @@ END:VCALENDAR"; prop_filter: vec![], comp_filter: vec![], }; - assert!(!object.matches(&comp_filter), "filter: wants no VCALENDAR"); + assert!( + !object.get_inner().matches(&comp_filter), + "filter: wants no VCALENDAR" + ); let comp_filter = CompFilterElement { is_not_defined: None, @@ -190,7 +267,10 @@ END:VCALENDAR"; comp_filter: vec![], }], }; - assert!(!object.matches(&comp_filter), "filter matches VTODO"); + assert!( + !object.get_inner().matches(&comp_filter), + "filter matches VTODO" + ); let comp_filter = CompFilterElement { is_not_defined: None, @@ -205,7 +285,10 @@ END:VCALENDAR"; comp_filter: vec![], }], }; - assert!(object.matches(&comp_filter), "filter matches VEVENT"); + assert!( + object.get_inner().matches(&comp_filter), + "filter matches VEVENT" + ); let comp_filter = CompFilterElement { is_not_defined: None, @@ -252,13 +335,13 @@ END:VCALENDAR"; }], }; assert!( - object.matches(&comp_filter), + object.get_inner().matches(&comp_filter), "Some prop filters on VCALENDAR and VEVENT" ); } #[test] fn test_comp_filter_time_range() { - let object = CalendarObject::from_ics(ICS.to_string(), None).unwrap(); + let object = CalendarObject::from_ics(ICS.to_string()).unwrap(); let comp_filter = CompFilterElement { is_not_defined: None, @@ -281,7 +364,7 @@ END:VCALENDAR"; }], }; assert!( - object.matches(&comp_filter), + object.get_inner().matches(&comp_filter), "event should lie in time range" ); @@ -306,14 +389,14 @@ END:VCALENDAR"; }], }; assert!( - !object.matches(&comp_filter), + !object.get_inner().matches(&comp_filter), "event should not lie in time range" ); } #[test] fn test_match_timezone() { - let object = CalendarObject::from_ics(ICS.to_string(), None).unwrap(); + let object = CalendarObject::from_ics(ICS.to_string()).unwrap(); let comp_filter = CompFilterElement { is_not_defined: None, @@ -340,7 +423,7 @@ END:VCALENDAR"; }], }; assert!( - object.matches(&comp_filter), + object.get_inner().matches(&comp_filter), "Timezone should be Europe/Berlin" ); } diff --git a/crates/caldav/src/calendar/methods/report/calendar_query/elements.rs b/crates/caldav/src/calendar/methods/report/calendar_query/elements.rs index d9ca17b..48f1c59 100644 --- a/crates/caldav/src/calendar/methods/report/calendar_query/elements.rs +++ b/crates/caldav/src/calendar/methods/report/calendar_query/elements.rs @@ -1,8 +1,8 @@ use super::comp_filter::{CompFilterElement, CompFilterable}; use crate::calendar_object::CalendarObjectPropWrapperName; -use ical::property::Property; +use ical::{component::IcalCalendarObject, property::ContentLine}; use rustical_dav::xml::{PropfindType, TextMatchElement}; -use rustical_ical::{CalendarObject, UtcDateTime}; +use rustical_ical::UtcDateTime; use rustical_store::calendar_store::CalendarQuery; use rustical_xml::{XmlDeserialize, XmlRootTag}; @@ -30,8 +30,8 @@ pub struct ParamFilterElement { impl ParamFilterElement { #[must_use] - pub fn match_property(&self, prop: &Property) -> bool { - let Some(param) = prop.get_param(&self.name) else { + pub fn match_property(&self, prop: &ContentLine) -> bool { + let Some(param) = prop.params.get_param(&self.name) else { return self.is_not_defined.is_some(); }; if self.is_not_defined.is_some() { @@ -57,7 +57,7 @@ pub struct FilterElement { impl FilterElement { #[must_use] - pub fn matches(&self, cal_object: &CalendarObject) -> bool { + pub fn matches(&self, cal_object: &IcalCalendarObject) -> bool { cal_object.matches(&self.comp_filter) } } diff --git a/crates/caldav/src/calendar/methods/report/calendar_query/mod.rs b/crates/caldav/src/calendar/methods/report/calendar_query/mod.rs index d46d271..038b03c 100644 --- a/crates/caldav/src/calendar/methods/report/calendar_query/mod.rs +++ b/crates/caldav/src/calendar/methods/report/calendar_query/mod.rs @@ -11,19 +11,19 @@ mod tests; pub use comp_filter::{CompFilterElement, CompFilterable}; pub use elements::*; #[allow(unused_imports)] -pub use prop_filter::{PropFilterElement, PropFilterable}; +pub use prop_filter::PropFilterElement; pub async fn get_objects_calendar_query( cal_query: &CalendarQueryRequest, principal: &str, cal_id: &str, store: &C, -) -> Result, Error> { +) -> Result, Error> { let mut objects = store .calendar_query(principal, cal_id, cal_query.into()) .await?; if let Some(filter) = &cal_query.filter { - objects.retain(|object| filter.matches(object)); + objects.retain(|(_id, object)| filter.matches(object.get_inner())); } Ok(objects) } diff --git a/crates/caldav/src/calendar/methods/report/calendar_query/prop_filter.rs b/crates/caldav/src/calendar/methods/report/calendar_query/prop_filter.rs index 406166d..cb7a7f0 100644 --- a/crates/caldav/src/calendar/methods/report/calendar_query/prop_filter.rs +++ b/crates/caldav/src/calendar/methods/report/calendar_query/prop_filter.rs @@ -1,16 +1,8 @@ use super::{ParamFilterElement, TimeRangeElement}; -use ical::{ - generator::{IcalCalendar, IcalEvent}, - parser::{ - Component, - ical::component::{IcalJournal, IcalTimeZone, IcalTodo}, - }, - property::Property, -}; +use ical::{property::ContentLine, types::CalDateTime}; use rustical_dav::xml::TextMatchElement; -use rustical_ical::{CalDateTime, CalendarObject, CalendarObjectComponent, UtcDateTime}; +use rustical_ical::UtcDateTime; use rustical_xml::XmlDeserialize; -use std::collections::HashMap; #[derive(XmlDeserialize, Clone, Debug, PartialEq, Eq)] #[allow(dead_code)] @@ -29,12 +21,16 @@ pub struct PropFilterElement { pub(crate) name: String, } +pub trait PropFilterable { + fn get_named_properties<'a>(&'a self, name: &'a str) -> impl Iterator; +} + impl PropFilterElement { #[must_use] - pub fn match_property(&self, property: &Property) -> bool { + pub fn match_property(&self, property: &ContentLine) -> bool { if let Some(TimeRangeElement { start, end }) = &self.time_range { // TODO: Respect timezones - let Ok(timestamp) = CalDateTime::parse_prop(property, &HashMap::default()) else { + let Ok(timestamp) = CalDateTime::parse_prop(property, None) else { return false; }; let timestamp = timestamp.utc(); @@ -69,63 +65,13 @@ impl PropFilterElement { } pub fn match_component(&self, comp: &impl PropFilterable) -> bool { - let properties = comp.get_named_properties(&self.name); + let mut properties = comp.get_named_properties(&self.name); if self.is_not_defined.is_some() { - return properties.is_empty(); + return properties.next().is_none(); } // The filter matches when one property instance matches // Example where this matters: We have multiple attendees and want to match one - properties.iter().any(|prop| self.match_property(prop)) - } -} - -pub trait PropFilterable { - fn get_named_properties(&self, name: &str) -> Vec<&Property>; -} - -impl PropFilterable for CalendarObject { - fn get_named_properties(&self, name: &str) -> Vec<&Property> { - Self::get_named_properties(self, name) - } -} - -impl PropFilterable for IcalEvent { - fn get_named_properties(&self, name: &str) -> Vec<&Property> { - Component::get_named_properties(self, name) - } -} - -impl PropFilterable for IcalTodo { - fn get_named_properties(&self, name: &str) -> Vec<&Property> { - Component::get_named_properties(self, name) - } -} - -impl PropFilterable for IcalJournal { - fn get_named_properties(&self, name: &str) -> Vec<&Property> { - Component::get_named_properties(self, name) - } -} - -impl PropFilterable for IcalCalendar { - fn get_named_properties(&self, name: &str) -> Vec<&Property> { - Component::get_named_properties(self, name) - } -} - -impl PropFilterable for IcalTimeZone { - fn get_named_properties(&self, name: &str) -> Vec<&Property> { - Component::get_named_properties(self, name) - } -} - -impl PropFilterable for CalendarObjectComponent { - fn get_named_properties(&self, name: &str) -> Vec<&Property> { - match self { - Self::Event(event, _) => PropFilterable::get_named_properties(&event.event, name), - Self::Todo(todo, _) => PropFilterable::get_named_properties(todo, name), - Self::Journal(journal, _) => PropFilterable::get_named_properties(journal, name), - } + properties.any(|prop| self.match_property(prop)) } } diff --git a/crates/caldav/src/calendar/methods/report/calendar_query/tests.rs b/crates/caldav/src/calendar/methods/report/calendar_query/tests.rs index 9652c10..7f8119b 100644 --- a/crates/caldav/src/calendar/methods/report/calendar_query/tests.rs +++ b/crates/caldav/src/calendar/methods/report/calendar_query/tests.rs @@ -77,7 +77,7 @@ const FILTER_2: &str = r#" #[case(ICS_1, FILTER_1, true)] #[case(ICS_1, FILTER_2, false)] fn yeet(#[case] ics: &str, #[case] filter: &str, #[case] matches: bool) { - let obj = CalendarObject::from_ics(ics.to_owned(), None).unwrap(); + let obj = CalendarObject::from_ics(ics.to_owned()).unwrap(); let filter = FilterElement::parse_str(filter).unwrap(); - assert_eq!(matches, filter.matches(&obj)); + assert_eq!(matches, filter.matches(obj.get_inner())); } diff --git a/crates/caldav/src/calendar/methods/report/mod.rs b/crates/caldav/src/calendar/methods/report/mod.rs index b6b4bce..8d4e1da 100644 --- a/crates/caldav/src/calendar/methods/report/mod.rs +++ b/crates/caldav/src/calendar/methods/report/mod.rs @@ -51,7 +51,7 @@ impl ReportRequest { } fn objects_response( - objects: Vec, + objects: Vec<(String, CalendarObject)>, not_found: Vec, path: &str, principal: &str, @@ -60,11 +60,12 @@ fn objects_response( prop: &PropfindType, ) -> Result, Error> { let mut responses = Vec::new(); - for object in objects { - let path = format!("{}/{}.ics", path, object.get_id()); + for (object_id, object) in objects { + let path = format!("{path}/{object_id}.ics"); responses.push( CalendarObjectResource { object, + object_id, principal: principal.to_owned(), } .propfind(&path, prop, None, puri, user)?, diff --git a/crates/caldav/src/calendar/methods/report/sync_collection.rs b/crates/caldav/src/calendar/methods/report/sync_collection.rs index 7b70f20..97b6806 100644 --- a/crates/caldav/src/calendar/methods/report/sync_collection.rs +++ b/crates/caldav/src/calendar/methods/report/sync_collection.rs @@ -32,11 +32,12 @@ pub async fn handle_sync_collection( .await?; let mut responses = Vec::new(); - for object in new_objects { - let path = format!("{}/{}.ics", path, object.get_id()); + for (object_id, object) in new_objects { + let path = format!("{}/{}.ics", path, &object_id); responses.push( CalendarObjectResource { object, + object_id, principal: principal.to_owned(), } .propfind(&path, &sync_collection.prop, None, puri, user)?, diff --git a/crates/caldav/src/calendar/resource.rs b/crates/caldav/src/calendar/resource.rs index c1a59eb..5c8c54a 100644 --- a/crates/caldav/src/calendar/resource.rs +++ b/crates/caldav/src/calendar/resource.rs @@ -4,6 +4,7 @@ use crate::calendar::prop::{ReportMethod, SupportedCollationSet}; use chrono::{DateTime, Utc}; use derive_more::derive::{From, Into}; use ical::IcalParser; +use ical::types::CalDateTime; use rustical_dav::extensions::{ CommonPropertiesExtension, CommonPropertiesProp, SyncTokenExtension, SyncTokenExtensionProp, }; @@ -11,7 +12,6 @@ use rustical_dav::privileges::UserPrivilegeSet; use rustical_dav::resource::{PrincipalUri, Resource, ResourceName}; use rustical_dav::xml::{HrefElement, Resourcetype, ResourcetypeInner, SupportedReportSet}; use rustical_dav_push::{DavPushExtension, DavPushExtensionProp}; -use rustical_ical::CalDateTime; use rustical_store::Calendar; use rustical_store::auth::Principal; use rustical_xml::{EnumVariants, PropName}; @@ -202,7 +202,7 @@ impl Resource for CalendarResource { CalendarProp::CalendarTimezone(timezone) => { if let Some(tz) = timezone { // TODO: Proper error (calendar-timezone precondition) - let calendar = IcalParser::new(tz.as_bytes()) + let calendar = IcalParser::from_slice(tz.as_bytes()) .next() .ok_or_else(|| { rustical_dav::Error::BadRequest( @@ -215,13 +215,13 @@ impl Resource for CalendarResource { ) })?; - let timezone = calendar.timezones.first().ok_or_else(|| { + let timezone = calendar.vtimezones.values().next().ok_or_else(|| { rustical_dav::Error::BadRequest("No timezone data provided".to_owned()) })?; - let timezone: chrono_tz::Tz = timezone.try_into().map_err(|_| { + let timezone: Option = timezone.into(); + let timezone = timezone.ok_or_else(|| { rustical_dav::Error::BadRequest("No timezone data provided".to_owned()) })?; - self.cal.timezone_id = Some(timezone.name().to_owned()); } Ok(()) diff --git a/crates/caldav/src/calendar/service.rs b/crates/caldav/src/calendar/service.rs index ec6384d..2f35acc 100644 --- a/crates/caldav/src/calendar/service.rs +++ b/crates/caldav/src/calendar/service.rs @@ -78,8 +78,9 @@ impl ResourceService for CalendarResourc .get_objects(principal, cal_id) .await? .into_iter() - .map(|object| CalendarObjectResource { + .map(|(object_id, object)| CalendarObjectResource { object, + object_id, principal: principal.to_owned(), }) .collect()) @@ -91,7 +92,7 @@ impl ResourceService for CalendarResourc file: Self::Resource, ) -> Result<(), Self::Error> { self.cal_store - .update_calendar(principal.to_owned(), cal_id.to_owned(), file.into()) + .update_calendar(principal, cal_id, file.into()) .await?; Ok(()) } diff --git a/crates/caldav/src/calendar_object/methods.rs b/crates/caldav/src/calendar_object/methods.rs index 7e475ac..873bc59 100644 --- a/crates/caldav/src/calendar_object/methods.rs +++ b/crates/caldav/src/calendar_object/methods.rs @@ -11,7 +11,7 @@ use rustical_ical::CalendarObject; use rustical_store::CalendarStore; use rustical_store::auth::Principal; use std::str::FromStr; -use tracing::{debug, instrument}; +use tracing::{instrument, warn}; #[instrument(skip(cal_store))] pub async fn get_event( @@ -94,13 +94,17 @@ pub async fn put_event( true }; - let Ok(object) = CalendarObject::from_ics(body.clone(), Some(object_id)) else { - debug!("invalid calendar data:\n{body}"); - return Err(Error::PreconditionFailed(Precondition::ValidCalendarData)); + let object = match CalendarObject::from_ics(body.clone()) { + Ok(object) => object, + Err(err) => { + warn!("invalid calendar data:\n{body}"); + warn!("{err}"); + return Err(Error::PreconditionFailed(Precondition::ValidCalendarData)); + } }; let etag = object.get_etag(); cal_store - .put_object(principal, calendar_id, object, overwrite) + .put_object(&principal, &calendar_id, &object_id, object, overwrite) .await?; let mut headers = HeaderMap::new(); diff --git a/crates/caldav/src/calendar_object/resource.rs b/crates/caldav/src/calendar_object/resource.rs index 475f1fa..0bb74d7 100644 --- a/crates/caldav/src/calendar_object/resource.rs +++ b/crates/caldav/src/calendar_object/resource.rs @@ -1,11 +1,10 @@ -use std::borrow::Cow; - use super::prop::{ CalendarData, CalendarObjectProp, CalendarObjectPropName, CalendarObjectPropWrapper, CalendarObjectPropWrapperName, }; use crate::Error; use derive_more::derive::{From, Into}; +use ical::generator::Emitter; use rustical_dav::{ extensions::CommonPropertiesExtension, privileges::UserPrivilegeSet, @@ -14,16 +13,18 @@ use rustical_dav::{ }; use rustical_ical::CalendarObject; use rustical_store::auth::Principal; +use std::borrow::Cow; #[derive(Clone, From, Into)] pub struct CalendarObjectResource { pub object: CalendarObject, + pub object_id: String, pub principal: String, } impl ResourceName for CalendarObjectResource { fn get_name(&self) -> Cow<'_, str> { - Cow::from(format!("{}.ics", self.object.get_id())) + Cow::from(format!("{}.ics", self.object_id)) } } @@ -53,14 +54,18 @@ impl Resource for CalendarObjectResource { CalendarObjectProp::Getetag(self.object.get_etag()) } CalendarObjectPropName::CalendarData(CalendarData { expand, .. }) => { - CalendarObjectProp::CalendarData(if let Some(expand) = expand.as_ref() { - self.object.expand_recurrence( - Some(expand.start.to_utc()), - Some(expand.end.to_utc()), - )? - } else { - self.object.get_ics().to_owned() - }) + CalendarObjectProp::CalendarData(expand.as_ref().map_or_else( + || self.object.get_ics().to_owned(), + |expand| { + self.object + .get_inner() + .expand_recurrence( + Some(expand.start.to_utc()), + Some(expand.end.to_utc()), + ) + .generate() + }, + )) } CalendarObjectPropName::Getcontenttype => { CalendarObjectProp::Getcontenttype("text/calendar;charset=utf-8") diff --git a/crates/caldav/src/calendar_object/service.rs b/crates/caldav/src/calendar_object/service.rs index 8a63e12..97f1516 100644 --- a/crates/caldav/src/calendar_object/service.rs +++ b/crates/caldav/src/calendar_object/service.rs @@ -66,6 +66,7 @@ impl ResourceService for CalendarObjectResourceService { .await?; Ok(CalendarObjectResource { object, + object_id: object_id.to_owned(), principal: principal.to_owned(), }) } diff --git a/crates/caldav/src/error.rs b/crates/caldav/src/error.rs index 833db6c..3fc5530 100644 --- a/crates/caldav/src/error.rs +++ b/crates/caldav/src/error.rs @@ -52,9 +52,6 @@ pub enum Error { #[error(transparent)] XmlDecodeError(#[from] rustical_xml::XmlError), - #[error(transparent)] - IcalError(#[from] rustical_ical::Error), - #[error(transparent)] PreconditionFailed(Precondition), } @@ -75,7 +72,6 @@ impl Error { Self::XmlDecodeError(_) => StatusCode::BAD_REQUEST, Self::ChronoParseError(_) | Self::NotImplemented => StatusCode::INTERNAL_SERVER_ERROR, Self::NotFound => StatusCode::NOT_FOUND, - Self::IcalError(err) => err.status_code(), Self::PreconditionFailed(_err) => StatusCode::PRECONDITION_FAILED, } } @@ -83,6 +79,9 @@ impl Error { impl IntoResponse for Error { fn into_response(self) -> axum::response::Response { + if let Self::PreconditionFailed(precondition) = self { + return precondition.into_response(); + } if matches!( self.status_code(), StatusCode::INTERNAL_SERVER_ERROR | StatusCode::PRECONDITION_FAILED diff --git a/crates/carddav/src/address_object/methods.rs b/crates/carddav/src/address_object/methods.rs index a93c78d..9d3ab58 100644 --- a/crates/carddav/src/address_object/methods.rs +++ b/crates/carddav/src/address_object/methods.rs @@ -103,10 +103,13 @@ pub async fn put_object( true }; - let object = AddressObject::from_vcf(object_id, body)?; + let object = match AddressObject::from_vcf(body) { + Ok(object) => object, + Err(err) => return Ok((StatusCode::BAD_REQUEST, err.to_string()).into_response()), + }; let etag = object.get_etag(); addr_store - .put_object(principal, addressbook_id, object, overwrite) + .put_object(&principal, &addressbook_id, &object_id, object, overwrite) .await?; let mut headers = HeaderMap::new(); diff --git a/crates/carddav/src/address_object/resource.rs b/crates/carddav/src/address_object/resource.rs index cddcee3..56f5b89 100644 --- a/crates/carddav/src/address_object/resource.rs +++ b/crates/carddav/src/address_object/resource.rs @@ -8,6 +8,7 @@ use crate::{ }, }; use derive_more::derive::{From, Into}; +use ical::parser::VcardFNProperty; use rustical_dav::{ extensions::CommonPropertiesExtension, privileges::UserPrivilegeSet, @@ -21,11 +22,12 @@ use rustical_store::auth::Principal; pub struct AddressObjectResource { pub object: AddressObject, pub principal: String, + pub object_id: String, } impl ResourceName for AddressObjectResource { fn get_name(&self) -> Cow<'_, str> { - Cow::from(format!("{}.vcf", self.object.get_id())) + Cow::from(format!("{}.vcf", self.object_id)) } } @@ -69,7 +71,11 @@ impl Resource for AddressObjectResource { } fn get_displayname(&self) -> Option<&str> { - self.object.get_full_name() + self.object + .get_vcard() + .full_name + .first() + .map(|VcardFNProperty(name, _)| name.as_str()) } fn get_owner(&self) -> Option<&str> { diff --git a/crates/carddav/src/address_object/service.rs b/crates/carddav/src/address_object/service.rs index 1c6efdc..0070a2d 100644 --- a/crates/carddav/src/address_object/service.rs +++ b/crates/carddav/src/address_object/service.rs @@ -57,6 +57,7 @@ impl ResourceService for AddressObjectResourceService .await?; Ok(AddressObjectResource { object, + object_id: object_id.to_owned(), principal: principal.to_owned(), }) } diff --git a/crates/carddav/src/addressbook/methods/get.rs b/crates/carddav/src/addressbook/methods/get.rs index 3159243..8bd4d6e 100644 --- a/crates/carddav/src/addressbook/methods/get.rs +++ b/crates/carddav/src/addressbook/methods/get.rs @@ -9,7 +9,6 @@ use http::{HeaderValue, Method, StatusCode, header}; use percent_encoding::{CONTROLS, utf8_percent_encode}; use rustical_dav::privileges::UserPrivilege; use rustical_dav::resource::Resource; -use rustical_ical::AddressObject; use rustical_store::auth::Principal; use rustical_store::{AddressbookStore, SubscriptionStore}; use std::str::FromStr; @@ -40,7 +39,7 @@ pub async fn route_get( let objects = addr_store.get_objects(&principal, &addressbook_id).await?; let vcf = objects .iter() - .map(AddressObject::get_vcf) + .map(|(_id, obj)| obj.get_vcf()) .collect::>() .join("\r\n"); diff --git a/crates/carddav/src/addressbook/methods/import.rs b/crates/carddav/src/addressbook/methods/import.rs index ff7dff2..cb7bff2 100644 --- a/crates/carddav/src/addressbook/methods/import.rs +++ b/crates/carddav/src/addressbook/methods/import.rs @@ -1,5 +1,3 @@ -use std::io::BufReader; - use crate::Error; use crate::addressbook::AddressbookResourceService; use axum::{ @@ -9,7 +7,7 @@ use axum::{ use http::StatusCode; use ical::{ parser::{Component, ComponentMut, vcard}, - property::Property, + property::ContentLine, }; use rustical_store::{Addressbook, AddressbookStore, SubscriptionStore, auth::Principal}; use tracing::instrument; @@ -25,7 +23,7 @@ pub async fn route_import( return Err(Error::Unauthorized); } - let parser = vcard::VcardParser::new(BufReader::new(body.as_bytes())); + let parser = vcard::VcardParser::from_slice(body.as_bytes()); let mut objects = vec![]; for res in parser { @@ -33,15 +31,16 @@ pub async fn route_import( let uid = card.get_uid(); if uid.is_none() { let mut card_mut = card.mutable(); - card_mut.set_property(Property { + card_mut.add_content_line(ContentLine { name: "UID".to_owned(), value: Some(uuid::Uuid::new_v4().to_string()), - params: vec![], + params: vec![].into(), }); - card = card_mut.verify().unwrap(); + card = card_mut.build(None).unwrap(); } - - objects.push(card.try_into().unwrap()); + // TODO: Make nicer + let uid = card.get_uid().unwrap(); + objects.push((uid.to_owned(), card.into())); } if objects.is_empty() { diff --git a/crates/carddav/src/addressbook/methods/report/addressbook_multiget.rs b/crates/carddav/src/addressbook/methods/report/addressbook_multiget.rs index 53fff13..03fb11b 100644 --- a/crates/carddav/src/addressbook/methods/report/addressbook_multiget.rs +++ b/crates/carddav/src/addressbook/methods/report/addressbook_multiget.rs @@ -29,7 +29,7 @@ pub async fn get_objects_addressbook_multiget( principal: &str, addressbook_id: &str, store: &AS, -) -> Result<(Vec, Vec), Error> { +) -> Result<(Vec<(String, AddressObject)>, Vec), Error> { let mut result = vec![]; let mut not_found = vec![]; @@ -43,7 +43,7 @@ pub async fn get_objects_addressbook_multiget( .get_object(principal, addressbook_id, object_id, false) .await { - Ok(object) => result.push(object), + Ok(object) => result.push((object_id.to_owned(), object)), Err(rustical_store::Error::NotFound) => not_found.push(href.to_string()), Err(err) => return Err(err.into()), } @@ -74,11 +74,12 @@ pub async fn handle_addressbook_multiget( .await?; let mut responses = Vec::new(); - for object in objects { - let path = format!("{}/{}.vcf", path, object.get_id()); + for (object_id, object) in objects { + let path = format!("{path}/{object_id}.vcf"); responses.push( AddressObjectResource { object, + object_id, principal: principal.to_owned(), } .propfind(&path, prop, None, puri, user)?, diff --git a/crates/carddav/src/addressbook/methods/report/addressbook_query/elements.rs b/crates/carddav/src/addressbook/methods/report/addressbook_query/elements.rs index b294482..b89985b 100644 --- a/crates/carddav/src/addressbook/methods/report/addressbook_query/elements.rs +++ b/crates/carddav/src/addressbook/methods/report/addressbook_query/elements.rs @@ -3,7 +3,7 @@ use crate::{ addressbook::methods::report::addressbook_query::PropFilterElement, }; use derive_more::{From, Into}; -use ical::property::Property; +use ical::property::ContentLine; use rustical_dav::xml::{PropfindType, TextMatchElement}; use rustical_ical::{AddressObject, UtcDateTime}; use rustical_xml::{ValueDeserialize, XmlDeserialize, XmlRootTag}; @@ -32,8 +32,8 @@ pub struct ParamFilterElement { impl ParamFilterElement { #[must_use] - pub fn match_property(&self, prop: &Property) -> bool { - let Some(param) = prop.get_param(&self.name) else { + pub fn match_property(&self, prop: &ContentLine) -> bool { + let Some(param) = prop.params.get_param(&self.name) else { return self.is_not_defined.is_some(); }; if self.is_not_defined.is_some() { diff --git a/crates/carddav/src/addressbook/methods/report/addressbook_query/mod.rs b/crates/carddav/src/addressbook/methods/report/addressbook_query/mod.rs index 1e463fe..6302584 100644 --- a/crates/carddav/src/addressbook/methods/report/addressbook_query/mod.rs +++ b/crates/carddav/src/addressbook/methods/report/addressbook_query/mod.rs @@ -15,8 +15,8 @@ pub async fn get_objects_addressbook_query( principal: &str, addressbook_id: &str, store: &AS, -) -> Result, Error> { +) -> Result, Error> { let mut objects = store.get_objects(principal, addressbook_id).await?; - objects.retain(|object| addr_query.filter.matches(object)); + objects.retain(|(_id, object)| addr_query.filter.matches(object)); Ok(objects) } diff --git a/crates/carddav/src/addressbook/methods/report/addressbook_query/prop_filter.rs b/crates/carddav/src/addressbook/methods/report/addressbook_query/prop_filter.rs index e8de84d..b10b03c 100644 --- a/crates/carddav/src/addressbook/methods/report/addressbook_query/prop_filter.rs +++ b/crates/carddav/src/addressbook/methods/report/addressbook_query/prop_filter.rs @@ -1,5 +1,5 @@ use super::{Allof, ParamFilterElement}; -use ical::{parser::Component, property::Property}; +use ical::{parser::Component, property::ContentLine}; use rustical_dav::xml::TextMatchElement; use rustical_ical::AddressObject; use rustical_xml::XmlDeserialize; @@ -31,7 +31,7 @@ pub struct PropFilterElement { impl PropFilterElement { #[must_use] - pub fn match_property(&self, property: &Property) -> bool { + pub fn match_property(&self, property: &ContentLine) -> bool { if self.param_filter.is_empty() && self.text_match.is_empty() { // Filter empty return true; @@ -56,22 +56,22 @@ impl PropFilterElement { } pub fn match_component(&self, comp: &impl PropFilterable) -> bool { - let properties = comp.get_named_properties(&self.name); + let mut properties = comp.get_named_properties(&self.name); if self.is_not_defined.is_some() { - return properties.is_empty(); + return properties.next().is_none(); } // The filter matches when one property instance matches - properties.iter().any(|prop| self.match_property(prop)) + properties.any(|prop| self.match_property(prop)) } } pub trait PropFilterable { - fn get_named_properties(&self, name: &str) -> Vec<&Property>; + fn get_named_properties<'a>(&'a self, name: &'a str) -> impl Iterator; } impl PropFilterable for AddressObject { - fn get_named_properties(&self, name: &str) -> Vec<&Property> { + fn get_named_properties<'a>(&'a self, name: &'a str) -> impl Iterator { self.get_vcard().get_named_properties(name) } } diff --git a/crates/carddav/src/addressbook/methods/report/addressbook_query/tests.rs b/crates/carddav/src/addressbook/methods/report/addressbook_query/tests.rs index 5c84224..f292562 100644 --- a/crates/carddav/src/addressbook/methods/report/addressbook_query/tests.rs +++ b/crates/carddav/src/addressbook/methods/report/addressbook_query/tests.rs @@ -64,7 +64,7 @@ const FILTER_2: &str = r#" #[case(VCF_2, FILTER_2, true)] fn test_filter(#[case] vcf: &str, #[case] filter: &str, #[case] matches: bool) { dbg!(vcf); - let obj = AddressObject::from_vcf(String::new(), vcf.to_owned()).unwrap(); + let obj = AddressObject::from_vcf(vcf.to_owned()).unwrap(); let filter = FilterElement::parse_str(filter).unwrap(); assert_eq!(matches, filter.matches(&obj)); } diff --git a/crates/carddav/src/addressbook/methods/report/mod.rs b/crates/carddav/src/addressbook/methods/report/mod.rs index dfba5e0..8191b1a 100644 --- a/crates/carddav/src/addressbook/methods/report/mod.rs +++ b/crates/carddav/src/addressbook/methods/report/mod.rs @@ -55,7 +55,7 @@ impl ReportRequest { } fn objects_response( - objects: Vec, + objects: Vec<(String, AddressObject)>, not_found: Vec, path: &str, principal: &str, @@ -64,11 +64,12 @@ fn objects_response( prop: &PropfindType, ) -> Result, Error> { let mut responses = Vec::new(); - for object in objects { - let path = format!("{}/{}.vcf", path, object.get_id()); + for (object_id, object) in objects { + let path = format!("{}/{}.vcf", path, &object_id); responses.push( AddressObjectResource { object, + object_id, principal: principal.to_owned(), } .propfind(&path, prop, None, puri, user)?, diff --git a/crates/carddav/src/addressbook/methods/report/sync_collection.rs b/crates/carddav/src/addressbook/methods/report/sync_collection.rs index f27837b..e1ac852 100644 --- a/crates/carddav/src/addressbook/methods/report/sync_collection.rs +++ b/crates/carddav/src/addressbook/methods/report/sync_collection.rs @@ -32,11 +32,12 @@ pub async fn handle_sync_collection( .await?; let mut responses = Vec::new(); - for object in new_objects { - let path = format!("{}/{}.vcf", path.trim_end_matches('/'), object.get_id()); + for (object_id, object) in new_objects { + let path = format!("{}/{}.vcf", path.trim_end_matches('/'), object_id); responses.push( AddressObjectResource { object, + object_id, principal: principal.to_owned(), } .propfind(&path, &sync_collection.prop, None, puri, user)?, diff --git a/crates/carddav/src/addressbook/service.rs b/crates/carddav/src/addressbook/service.rs index 4ae19d6..0e838f1 100644 --- a/crates/carddav/src/addressbook/service.rs +++ b/crates/carddav/src/addressbook/service.rs @@ -78,7 +78,8 @@ impl ResourceService .get_objects(principal, addressbook_id) .await? .into_iter() - .map(|object| AddressObjectResource { + .map(|(object_id, object)| AddressObjectResource { + object_id, object, principal: principal.to_owned(), }) @@ -91,7 +92,7 @@ impl ResourceService file: Self::Resource, ) -> Result<(), Self::Error> { self.addr_store - .update_addressbook(principal.to_owned(), addressbook_id.to_owned(), file.into()) + .update_addressbook(principal, addressbook_id, file.into()) .await?; Ok(()) } diff --git a/crates/carddav/src/error.rs b/crates/carddav/src/error.rs index 66409b5..5439529 100644 --- a/crates/carddav/src/error.rs +++ b/crates/carddav/src/error.rs @@ -23,9 +23,6 @@ pub enum Error { #[error(transparent)] XmlDecodeError(#[from] rustical_xml::XmlError), - - #[error(transparent)] - IcalError(#[from] rustical_ical::Error), } impl Error { @@ -43,7 +40,6 @@ impl Error { Self::XmlDecodeError(_) => StatusCode::BAD_REQUEST, Self::ChronoParseError(_) | Self::NotImplemented => StatusCode::INTERNAL_SERVER_ERROR, Self::NotFound => StatusCode::NOT_FOUND, - Self::IcalError(err) => err.status_code(), } } } diff --git a/crates/dav/src/resource/methods/copy.rs b/crates/dav/src/resource/methods/copy.rs index bec1372..c0426c1 100644 --- a/crates/dav/src/resource/methods/copy.rs +++ b/crates/dav/src/resource/methods/copy.rs @@ -6,12 +6,15 @@ use axum::{ extract::{MatchedPath, Path, State}, response::{IntoResponse, Response}, }; +use axum_extra::TypedHeader; +use headers::Host; use http::{HeaderMap, StatusCode, Uri}; use matchit_serde::ParamsDeserializer; use serde::Deserialize; use tracing::instrument; #[instrument(skip(path, resource_service,))] +#[allow(clippy::too_many_arguments)] pub async fn axum_route_copy( Path(path): Path, State(resource_service): State, @@ -20,6 +23,7 @@ pub async fn axum_route_copy( Overwrite(overwrite): Overwrite, matched_path: MatchedPath, header_map: HeaderMap, + TypedHeader(host): TypedHeader, ) -> Result { let destination = header_map .get("Destination") @@ -27,7 +31,11 @@ pub async fn axum_route_copy( .to_str() .map_err(|_| crate::Error::Forbidden)?; let destination_uri: Uri = destination.parse().map_err(|_| crate::Error::Forbidden)?; - // TODO: Check that host also matches + if let Some(authority) = destination_uri.authority() + && host != authority.clone().into() + { + return Err(crate::Error::Forbidden.into()); + } let destination = destination_uri.path(); let mut router = matchit::Router::new(); diff --git a/crates/dav/src/resource/methods/mv.rs b/crates/dav/src/resource/methods/mv.rs index a10d5d8..8192565 100644 --- a/crates/dav/src/resource/methods/mv.rs +++ b/crates/dav/src/resource/methods/mv.rs @@ -6,12 +6,15 @@ use axum::{ extract::{MatchedPath, Path, State}, response::{IntoResponse, Response}, }; +use axum_extra::TypedHeader; +use headers::Host; use http::{HeaderMap, StatusCode, Uri}; use matchit_serde::ParamsDeserializer; use serde::Deserialize; use tracing::instrument; #[instrument(skip(path, resource_service,))] +#[allow(clippy::too_many_arguments)] pub async fn axum_route_move( Path(path): Path, State(resource_service): State, @@ -20,6 +23,7 @@ pub async fn axum_route_move( Overwrite(overwrite): Overwrite, matched_path: MatchedPath, header_map: HeaderMap, + TypedHeader(host): TypedHeader, ) -> Result { let destination = header_map .get("Destination") @@ -27,7 +31,11 @@ pub async fn axum_route_move( .to_str() .map_err(|_| crate::Error::Forbidden)?; let destination_uri: Uri = destination.parse().map_err(|_| crate::Error::Forbidden)?; - // TODO: Check that host also matches + if let Some(authority) = destination_uri.authority() + && host != authority.clone().into() + { + return Err(crate::Error::Forbidden.into()); + } let destination = destination_uri.path(); let mut router = matchit::Router::new(); diff --git a/crates/dav/src/xml/text_match.rs b/crates/dav/src/xml/text_match.rs index a8db120..2188fe5 100644 --- a/crates/dav/src/xml/text_match.rs +++ b/crates/dav/src/xml/text_match.rs @@ -1,4 +1,4 @@ -use ical::property::Property; +use ical::property::ContentLine; use rustical_xml::{ValueDeserialize, XmlDeserialize}; use std::borrow::Cow; @@ -128,7 +128,7 @@ impl TextMatchElement { negate_condition.0 ^ matches } #[must_use] - pub fn match_property(&self, property: &Property) -> bool { + pub fn match_property(&self, property: &ContentLine) -> bool { let text = property.value.as_deref().unwrap_or(""); self.match_text(text) } diff --git a/crates/ical/src/address_object.rs b/crates/ical/src/address_object.rs index b0c5450..ad1e943 100644 --- a/crates/ical/src/address_object.rs +++ b/crates/ical/src/address_object.rs @@ -1,59 +1,45 @@ -use crate::{CalDateTime, LOCAL_DATE}; use crate::{CalendarObject, Error}; -use chrono::Datelike; -use ical::generator::Emitter; -use ical::parser::{ - Component, - vcard::{self, component::VcardContact}, +use chrono::{NaiveDate, Utc}; +use ical::component::{ + CalendarInnerDataBuilder, IcalAlarmBuilder, IcalCalendarObjectBuilder, IcalEventBuilder, }; +use ical::generator::Emitter; +use ical::parser::vcard::{self, component::VcardContact}; +use ical::parser::{ + Calscale, ComponentMut, IcalCALSCALEProperty, IcalDTENDProperty, IcalDTSTAMPProperty, + IcalDTSTARTProperty, IcalPRODIDProperty, IcalRRULEProperty, IcalSUMMARYProperty, + IcalUIDProperty, IcalVERSIONProperty, IcalVersion, VcardANNIVERSARYProperty, VcardBDAYProperty, + VcardFNProperty, +}; +use ical::property::ContentLine; +use ical::types::{CalDate, PartialDate}; use sha2::{Digest, Sha256}; -use std::{collections::HashMap, io::BufReader}; +use std::collections::HashMap; +use std::str::FromStr; #[derive(Debug, Clone)] pub struct AddressObject { - id: String, vcf: String, vcard: VcardContact, } -impl TryFrom for AddressObject { - type Error = Error; - - fn try_from(vcard: VcardContact) -> Result { - let uid = vcard - .get_uid() - .ok_or_else(|| Error::InvalidData("missing UID".to_owned()))? - .to_owned(); +impl From for AddressObject { + fn from(vcard: VcardContact) -> Self { let vcf = vcard.generate(); - Ok(Self { - vcf, - vcard, - id: uid, - }) + Self { vcf, vcard } } } impl AddressObject { - pub fn from_vcf(id: String, vcf: String) -> Result { - let mut parser = vcard::VcardParser::new(BufReader::new(vcf.as_bytes())); - let vcard = parser.next().ok_or(Error::MissingContact)??; - if parser.next().is_some() { - return Err(Error::InvalidData( - "multiple vcards, only one allowed".to_owned(), - )); - } - Ok(Self { id, vcf, vcard }) - } - - #[must_use] - pub fn get_id(&self) -> &str { - &self.id + pub fn from_vcf(vcf: String) -> Result { + let parser = vcard::VcardParser::from_slice(vcf.as_bytes()); + let vcard = parser.expect_one()?; + Ok(Self { vcf, vcard }) } #[must_use] pub fn get_etag(&self) -> String { let mut hasher = Sha256::new(); - hasher.update(self.get_id()); hasher.update(self.get_vcf()); format!("\"{:x}\"", hasher.finalize()) } @@ -63,121 +49,115 @@ impl AddressObject { &self.vcf } - #[must_use] - pub fn get_anniversary(&self) -> Option<(CalDateTime, bool)> { - let prop = self.vcard.get_property("ANNIVERSARY")?.value.as_deref()?; - CalDateTime::parse_vcard(prop).ok() - } + fn get_significant_date_object( + &self, + date: &PartialDate, + summary_prefix: &str, + suffix: &str, + ) -> Result, Error> { + let Some(uid) = self.vcard.get_uid() else { + return Ok(None); + }; + let uid = format!("{uid}{suffix}"); + let year = date.get_year(); + let year_suffix = year.map(|year| format!(" {year}")).unwrap_or_default(); + let Some(month) = date.get_month() else { + return Ok(None); + }; + let Some(day) = date.get_day() else { + return Ok(None); + }; + let Some(dtstart) = NaiveDate::from_ymd_opt(year.unwrap_or(1900), month, day) else { + return Ok(None); + }; + let start_date = CalDate(dtstart, ical::types::Timezone::Local); + let Some(end_date) = start_date.succ_opt() else { + // start_date is MAX_DATE, this should never happen but FAPP also not raise an error + return Ok(None); + }; + let Some(VcardFNProperty(fullname, _)) = self.vcard.full_name.first() else { + return Ok(None); + }; + let summary = format!("{summary_prefix} {fullname}{year_suffix}"); - #[must_use] - pub fn get_birthday(&self) -> Option<(CalDateTime, bool)> { - let prop = self.vcard.get_property("BDAY")?.value.as_deref()?; - CalDateTime::parse_vcard(prop).ok() - } + let event = IcalEventBuilder { + properties: vec![ + IcalDTSTAMPProperty(Utc::now().into(), vec![].into()).into(), + IcalDTSTARTProperty(start_date.into(), vec![].into()).into(), + IcalDTENDProperty(end_date.into(), vec![].into()).into(), + IcalUIDProperty(uid, vec![].into()).into(), + IcalRRULEProperty( + rrule::RRule::from_str("FREQ=YEARLY").unwrap(), + vec![].into(), + ) + .into(), + IcalSUMMARYProperty(summary.clone(), vec![].into()).into(), + ContentLine { + name: "TRANSP".to_owned(), + value: Some("TRANSPARENT".to_owned()), + ..Default::default() + }, + ], + alarms: vec![IcalAlarmBuilder { + properties: vec![ + ContentLine { + name: "TRIGGER".to_owned(), + value: Some("-PT0M".to_owned()), + params: vec![("VALUE".to_owned(), vec!["DURATION".to_owned()])].into(), + }, + ContentLine { + name: "ACTION".to_owned(), + value: Some("DISPLAY".to_owned()), + ..Default::default() + }, + ContentLine { + name: "DESCRIPTION".to_owned(), + value: Some(summary), + ..Default::default() + }, + ], + }], + }; - #[must_use] - pub fn get_full_name(&self) -> Option<&str> { - let prop = self.vcard.get_property("FN")?; - prop.value.as_deref() + Ok(Some( + IcalCalendarObjectBuilder { + properties: vec![ + IcalVERSIONProperty(IcalVersion::Version2_0, vec![].into()).into(), + IcalCALSCALEProperty(Calscale::Gregorian, vec![].into()).into(), + IcalPRODIDProperty( + "-//github.com/lennart-k/rustical birthday calendar//EN".to_owned(), + vec![].into(), + ) + .into(), + ], + inner: Some(CalendarInnerDataBuilder::Event(vec![event])), + vtimezones: HashMap::default(), + } + .build(None)? + .into(), + )) } pub fn get_anniversary_object(&self) -> Result, Error> { - Ok( - if let Some((anniversary, contains_year)) = self.get_anniversary() { - let Some(fullname) = self.get_full_name() else { - return Ok(None); - }; - let anniversary = anniversary.date(); - let year = contains_year.then_some(anniversary.year()); - let anniversary_start = anniversary.format(LOCAL_DATE); - let anniversary_end = anniversary - .succ_opt() - .unwrap_or(anniversary) - .format(LOCAL_DATE); - let uid = format!("{}-anniversary", self.get_id()); + let Some(VcardANNIVERSARYProperty(anniversary, _)) = &self.vcard.anniversary else { + return Ok(None); + }; + let Some(date) = &anniversary.date else { + return Ok(None); + }; - let year_suffix = year.map(|year| format!(" ({year})")).unwrap_or_default(); - Some(CalendarObject::from_ics( - format!( - r"BEGIN:VCALENDAR -VERSION:2.0 -CALSCALE:GREGORIAN -PRODID:-//github.com/lennart-k/rustical birthday calendar//EN -BEGIN:VEVENT -DTSTART;VALUE=DATE:{anniversary_start} -DTEND;VALUE=DATE:{anniversary_end} -UID:{uid} -RRULE:FREQ=YEARLY -SUMMARY:💍 {fullname}{year_suffix} -TRANSP:TRANSPARENT -BEGIN:VALARM -TRIGGER;VALUE=DURATION:-PT0M -ACTION:DISPLAY -DESCRIPTION:💍 {fullname}{year_suffix} -END:VALARM -END:VEVENT -END:VCALENDAR", - ), - None, - )?) - } else { - None - }, - ) + self.get_significant_date_object(date, "💍", "-anniversary") } pub fn get_birthday_object(&self) -> Result, Error> { - Ok( - if let Some((birthday, contains_year)) = self.get_birthday() { - let Some(fullname) = self.get_full_name() else { - return Ok(None); - }; - let birthday = birthday.date(); - let year = contains_year.then_some(birthday.year()); - let birthday_start = birthday.format(LOCAL_DATE); - let birthday_end = birthday.succ_opt().unwrap_or(birthday).format(LOCAL_DATE); - let uid = format!("{}-birthday", self.get_id()); + let Some(VcardBDAYProperty(bday, _)) = &self.vcard.birthday else { + return Ok(None); + }; + let Some(date) = &bday.date else { + return Ok(None); + }; - let year_suffix = year.map(|year| format!(" ({year})")).unwrap_or_default(); - Some(CalendarObject::from_ics( - format!( - r"BEGIN:VCALENDAR -VERSION:2.0 -CALSCALE:GREGORIAN -PRODID:-//github.com/lennart-k/rustical birthday calendar//EN -BEGIN:VEVENT -DTSTART;VALUE=DATE:{birthday_start} -DTEND;VALUE=DATE:{birthday_end} -UID:{uid} -RRULE:FREQ=YEARLY -SUMMARY:🎂 {fullname}{year_suffix} -TRANSP:TRANSPARENT -BEGIN:VALARM -TRIGGER;VALUE=DURATION:-PT0M -ACTION:DISPLAY -DESCRIPTION:🎂 {fullname}{year_suffix} -END:VALARM -END:VEVENT -END:VCALENDAR", - ), - None, - )?) - } else { - None - }, - ) - } - - /// Get significant dates associated with this address object - pub fn get_significant_dates(&self) -> Result, Error> { - let mut out = HashMap::new(); - if let Some(birthday) = self.get_birthday_object()? { - out.insert("birthday", birthday); - } - if let Some(anniversary) = self.get_anniversary_object()? { - out.insert("anniversary", anniversary); - } - Ok(out) + self.get_significant_date_object(date, "🎂", "-birthday") } #[must_use] diff --git a/crates/ical/src/calendar_object.rs b/crates/ical/src/calendar_object.rs new file mode 100644 index 0000000..e0114f7 --- /dev/null +++ b/crates/ical/src/calendar_object.rs @@ -0,0 +1,120 @@ +use crate::Error; +use derive_more::Display; +use ical::IcalObjectParser; +use ical::component::CalendarInnerData; +use ical::component::IcalCalendarObject; +use ical::generator::Emitter; +use serde::Deserialize; +use serde::Serialize; +use sha2::{Digest, Sha256}; + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Display)] +// specified in https://datatracker.ietf.org/doc/html/rfc5545#section-3.6 +pub enum CalendarObjectType { + #[serde(rename = "VEVENT")] + Event = 0, + #[serde(rename = "VTODO")] + Todo = 1, + #[serde(rename = "VJOURNAL")] + Journal = 2, +} + +impl From<&IcalCalendarObject> for CalendarObjectType { + fn from(value: &IcalCalendarObject) -> Self { + match value.get_inner() { + CalendarInnerData::Event(_, _) => Self::Event, + CalendarInnerData::Todo(_, _) => Self::Todo, + CalendarInnerData::Journal(_, _) => Self::Journal, + } + } +} + +impl CalendarObjectType { + #[must_use] + pub const fn as_str(&self) -> &'static str { + match self { + Self::Event => "VEVENT", + Self::Todo => "VTODO", + Self::Journal => "VJOURNAL", + } + } +} + +impl rustical_xml::ValueSerialize for CalendarObjectType { + fn serialize(&self) -> String { + self.as_str().to_owned() + } +} + +impl rustical_xml::ValueDeserialize for CalendarObjectType { + fn deserialize(val: &str) -> std::result::Result { + match ::deserialize(val)?.as_str() { + "VEVENT" => Ok(Self::Event), + "VTODO" => Ok(Self::Todo), + "VJOURNAL" => Ok(Self::Journal), + _ => Err(rustical_xml::XmlError::InvalidValue( + rustical_xml::ParseValueError::Other(format!( + "Invalid value '{val}', must be VEVENT, VTODO, or VJOURNAL" + )), + )), + } + } +} + +#[derive(Debug, Clone)] +pub struct CalendarObject { + inner: IcalCalendarObject, + ics: String, +} + +impl CalendarObject { + pub fn from_ics(ics: String) -> Result { + let parser = IcalObjectParser::from_slice(ics.as_bytes()); + let inner = parser.expect_one()?; + + Ok(Self { inner, ics }) + } + + #[must_use] + pub const fn get_inner(&self) -> &IcalCalendarObject { + &self.inner + } + + #[must_use] + pub fn get_uid(&self) -> &str { + self.inner.get_uid() + } + + #[must_use] + pub fn get_etag(&self) -> String { + let mut hasher = Sha256::new(); + hasher.update(self.get_uid()); + hasher.update(self.get_ics()); + format!("\"{:x}\"", hasher.finalize()) + } + + #[must_use] + pub fn get_ics(&self) -> &str { + &self.ics + } + + #[must_use] + pub fn get_object_type(&self) -> CalendarObjectType { + (&self.inner).into() + } +} + +impl From for IcalCalendarObject { + fn from(value: CalendarObject) -> Self { + value.inner + } +} + +impl From for CalendarObject { + fn from(value: IcalCalendarObject) -> Self { + Self { + ics: value.generate(), + inner: value, + } + } +} diff --git a/crates/ical/src/error.rs b/crates/ical/src/error.rs deleted file mode 100644 index 94b41fe..0000000 --- a/crates/ical/src/error.rs +++ /dev/null @@ -1,42 +0,0 @@ -use axum::{http::StatusCode, response::IntoResponse}; - -use crate::CalDateTimeError; - -#[derive(Debug, thiserror::Error, PartialEq, Eq)] -pub enum Error { - #[error("Invalid ics/vcf input: {0}")] - InvalidData(String), - - #[error("Missing calendar")] - MissingCalendar, - - #[error("Missing contact")] - MissingContact, - - #[error(transparent)] - ParserError(#[from] ical::parser::ParserError), - - #[error(transparent)] - CalDateTimeError(#[from] CalDateTimeError), - - #[error(transparent)] - RRuleError(#[from] rrule::RRuleError), -} - -impl Error { - #[must_use] - pub const fn status_code(&self) -> StatusCode { - match self { - Self::InvalidData(_) | Self::MissingCalendar | Self::MissingContact => { - StatusCode::BAD_REQUEST - } - _ => StatusCode::INTERNAL_SERVER_ERROR, - } - } -} - -impl IntoResponse for Error { - fn into_response(self) -> axum::response::Response { - (self.status_code(), self.to_string()).into_response() - } -} diff --git a/crates/ical/src/icalendar/event.rs b/crates/ical/src/icalendar/event.rs deleted file mode 100644 index 2b2d929..0000000 --- a/crates/ical/src/icalendar/event.rs +++ /dev/null @@ -1,386 +0,0 @@ -use crate::CalDateTime; -use crate::Error; -use chrono::{DateTime, Duration, Utc}; -use ical::parser::ComponentMut; -use ical::{generator::IcalEvent, parser::Component, property::Property}; -use rrule::{RRule, RRuleSet}; -use std::{collections::HashMap, str::FromStr}; - -#[derive(Debug, Clone, Default)] -pub struct EventObject { - pub event: IcalEvent, - // If a timezone is None that means that in the VCALENDAR object there's a timezone defined - // with that name but its not from the Olson DB - pub timezones: HashMap>, -} - -impl EventObject { - #[must_use] - pub fn get_uid(&self) -> &str { - self.event.get_uid() - } - - pub fn get_dtstart(&self) -> Result, Error> { - if let Some(dtstart) = self.event.get_dtstart() { - Ok(Some(CalDateTime::parse_prop(dtstart, &self.timezones)?)) - } else { - Ok(None) - } - } - - pub fn get_dtend(&self) -> Result, Error> { - if let Some(dtend) = self.event.get_dtend() { - Ok(Some(CalDateTime::parse_prop(dtend, &self.timezones)?)) - } else { - Ok(None) - } - } - - pub fn get_last_occurence(&self) -> Result, Error> { - if self.event.get_rrule().is_some() { - // TODO: understand recurrence rules - return Ok(None); - } - - if let Some(dtend) = self.get_dtend()? { - return Ok(Some(dtend)); - } - - let duration = self.event.get_duration().unwrap_or(Duration::days(1)); - - let first_occurence = self.get_dtstart()?; - Ok(first_occurence.map(|first_occurence| first_occurence + duration)) - } - - pub fn recurrence_ruleset(&self) -> Result, Error> { - let dtstart: DateTime = if let Some(dtstart) = self.get_dtstart()? { - if let Some(dtend) = self.get_dtend()? { - // DTSTART and DTEND MUST have the same timezone - assert_eq!(dtstart.timezone(), dtend.timezone()); - } - - dtstart - .as_datetime() - .with_timezone(&dtstart.timezone().into()) - } else { - return Ok(None); - }; - - let mut rrule_set = RRuleSet::new(dtstart); - // TODO: Make nice, this is just a bodge to get correct behaviour - let mut empty = true; - - for prop in &self.event.properties { - rrule_set = match prop.name.as_str() { - "RRULE" => { - let rrule = RRule::from_str(prop.value.as_ref().ok_or_else(|| { - Error::RRuleError(rrule::ParseError::MissingDateGenerationRules.into()) - })?)? - .validate(dtstart) - .unwrap(); - empty = false; - rrule_set.rrule(rrule) - } - "RDATE" => { - let rdate = CalDateTime::parse_prop(prop, &self.timezones)?.into(); - empty = false; - rrule_set.rdate(rdate) - } - "EXDATE" => { - let exdate = CalDateTime::parse_prop(prop, &self.timezones)?.into(); - empty = false; - rrule_set.exdate(exdate) - } - _ => rrule_set, - } - } - if empty { - return Ok(None); - } - - Ok(Some(rrule_set)) - } - - // The returned calendar components MUST NOT use recurrence - // properties (i.e., EXDATE, EXRULE, RDATE, and RRULE) and MUST NOT - // have reference to or include VTIMEZONE components. Date and local - // time with reference to time zone information MUST be converted - // into date with UTC time. - pub fn expand_recurrence( - &self, - start: Option>, - end: Option>, - overrides: &[Self], - ) -> Result, Error> { - let mut events = vec![]; - let dtstart = self.get_dtstart()?.expect("We must have a DTSTART here"); - let computed_duration = self - .get_dtend()? - .map(|dtend| dtend.as_datetime().into_owned() - dtstart.as_datetime().as_ref()); - - let Some(mut rrule_set) = self.recurrence_ruleset()? else { - // If ruleset empty simply return main event AND all overrides - return Ok(std::iter::once(self.clone()) - .chain(overrides.iter().cloned()) - .map(|event| event.event) - .collect()); - }; - if let Some(start) = start { - rrule_set = rrule_set.after(start.with_timezone(&rrule::Tz::UTC)); - } - if let Some(end) = end { - rrule_set = rrule_set.before(end.with_timezone(&rrule::Tz::UTC)); - } - let dates = rrule_set.all(2048).dates; - 'recurrence: for date in dates { - let date = CalDateTime::from(date.to_utc()); - let recurrence_id = if dtstart.is_date() { - date.format_date() - } else { - date.format() - }; - - for ev_override in overrides { - if let Some(override_id) = &ev_override - .event - .get_recurrence_id() - .as_ref() - .expect("overrides have a recurrence id") - .value - && override_id == &recurrence_id - { - // We have an override for this occurence - // - events.push(ev_override.event.clone()); - continue 'recurrence; - } - } - - let mut ev = self.event.clone().mutable(); - ev.remove_property("RRULE"); - ev.remove_property("RDATE"); - ev.remove_property("EXDATE"); - ev.remove_property("EXRULE"); - let dtstart_prop = ev - .get_property("DTSTART") - .expect("We must have a DTSTART here") - .clone(); - ev.remove_property("DTSTART"); - ev.remove_property("DTEND"); - - ev.set_property(Property { - name: "RECURRENCE-ID".to_string(), - value: Some(recurrence_id.clone()), - params: vec![], - }); - ev.set_property(Property { - name: "DTSTART".to_string(), - value: Some(recurrence_id), - params: vec![], - }); - if let Some(duration) = computed_duration { - let dtend = date + duration; - let dtendformat = if dtstart.is_date() { - dtend.format_date() - } else { - dtend.format() - }; - ev.set_property(Property { - name: "DTEND".to_string(), - value: Some(dtendformat), - params: dtstart_prop.params, - }); - } - events.push(ev.verify()?); - } - Ok(events) - } -} - -#[cfg(test)] -mod tests { - use crate::{CalDateTime, CalendarObject}; - use chrono::{DateTime, Utc}; - use ical::generator::Emitter; - use rstest::rstest; - - const ICS_1: &str = r"BEGIN:VCALENDAR -CALSCALE:GREGORIAN -VERSION:2.0 -BEGIN:VTIMEZONE -TZID:Europe/Berlin -X-LIC-LOCATION:Europe/Berlin -END:VTIMEZONE - -BEGIN:VEVENT -UID:318ec6503573d9576818daf93dac07317058d95c -DTSTAMP:20250502T132758Z -DTSTART;TZID=Europe/Berlin:20250506T090000 -DTEND;TZID=Europe/Berlin:20250506T092500 -SEQUENCE:2 -SUMMARY:weekly stuff -TRANSP:OPAQUE -RRULE:FREQ=WEEKLY;COUNT=4;INTERVAL=2;BYDAY=TU,TH,SU -END:VEVENT -END:VCALENDAR"; - - const EXPANDED_1: &[&str] = &[ - "BEGIN:VEVENT\r -UID:318ec6503573d9576818daf93dac07317058d95c\r -DTSTAMP:20250502T132758Z\r -SEQUENCE:2\r -SUMMARY:weekly stuff\r -TRANSP:OPAQUE\r -RECURRENCE-ID:20250506T070000Z\r -DTSTART:20250506T070000Z\r -DTEND:20250506T072500Z\r -END:VEVENT\r\n", - "BEGIN:VEVENT\r -UID:318ec6503573d9576818daf93dac07317058d95c\r -DTSTAMP:20250502T132758Z\r -SEQUENCE:2\r -SUMMARY:weekly stuff\r -TRANSP:OPAQUE\r -RECURRENCE-ID:20250508T070000Z\r -DTSTART:20250508T070000Z\r -DTEND:20250508T072500Z\r -END:VEVENT\r\n", - "BEGIN:VEVENT\r -UID:318ec6503573d9576818daf93dac07317058d95c\r -DTSTAMP:20250502T132758Z\r -SEQUENCE:2\r -SUMMARY:weekly stuff\r -TRANSP:OPAQUE\r -RECURRENCE-ID:20250511T090000\r -DTSTART:20250511T070000Z\r -DTEND:20250511T072500Z\r -END:VEVENT\r\n", - "BEGIN:VEVENT\r -UID:318ec6503573d9576818daf93dac07317058d95c\r -DTSTAMP:20250502T132758Z\r -SEQUENCE:2\r -SUMMARY:weekly stuff\r -TRANSP:OPAQUE\r -RECURRENCE-ID:20250520T090000\r -DTSTA:20250520T070000Z\r -DTEND:20250520T072500Z\r -END:VEVENT\r\n", - ]; - - const ICS_2: &str = r"BEGIN:VCALENDAR -CALSCALE:GREGORIAN -VERSION:2.0 -BEGIN:VTIMEZONE -TZID:US/Eastern -END:VTIMEZONE -BEGIN:VEVENT -DTSTAMP:20060206T001121Z -DTSTART;TZID=US/Eastern:20060102T120000 -DURATION:PT1H -RRULE:FREQ=DAILY;COUNT=5 -SUMMARY:Event #2 -UID:abcd2 -END:VEVENT -BEGIN:VEVENT -DTSTAMP:20060206T001121Z -DTSTART;TZID=US/Eastern:20060104T140000 -DURATION:PT1H -RECURRENCE-ID;TZID=US/Eastern:20060104T120000 -SUMMARY:Event #2 bis -UID:abcd2 -END:VEVENT -END:VCALENDAR -"; - - const EXPANDED_2: &[&str] = &[ - "BEGIN:VEVENT\r -DTSTAMP:20060206T001121Z\r -DURATION:PT1H\r -SUMMARY:Event #2\r -UID:abcd2\r -RECURRENCE-ID:20060103T170000\r -DTSTART:20060103T170000\r -END:VEVENT\r\n", - "BEGIN:VEVENT\r -DTSTAMP:20060206T001121Z\r -DURATION:PT1H\r -SUMMARY:Event #2 bis\r -UID:abcd2\r -RECURRENCE-ID:20060104T170000\r -DTSTART:20060104T190000\r -END:VEVENT\r -END:VCALENDAR\r\n", - ]; - - const ICS_3: &str = r"BEGIN:VCALENDAR -CALSCALE:GREGORIAN -VERSION:2.0 -BEGIN:VTIMEZONE -TZID:US/Eastern -END:VTIMEZONE -BEGIN:VEVENT -ATTENDEE;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:cyrus@example.com -ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:lisa@example.com -DTSTAMP:20060206T001220Z -DTSTART;TZID=US/Eastern:20060104T100000 -DURATION:PT1H -LAST-MODIFIED:20060206T001330Z -ORGANIZER:mailto:cyrus@example.com -SEQUENCE:1 -STATUS:TENTATIVE -SUMMARY:Event #3 -UID:abcd3 -END:VEVENT -END:VCALENDAR -"; - - const EXPANDED_3: &[&str] = &["BEGIN:VEVENT -ATTENDEE;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:cyrus@example.com -ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:lisa@example.com -DTSTAMP:20060206T001220Z -DTSTART:20060104T150000 -DURATION:PT1H -LAST-MODIFIED:20060206T001330Z -ORGANIZER:mailto:cyrus@example.com -SEQUENCE:1 -STATUS:TENTATIVE -SUMMARY:Event #3 -UID:abcd3 -X-ABC-GUID:E1CX5Dr-0007ym-Hz@example.com -END:VEVENT"]; - - // The implementation never was entirely correct but will be fixed in v0.12 - // #[rstest] - // #[case(ICS_1, EXPANDED_1, None, None)] - // // from https://datatracker.ietf.org/doc/html/rfc4791#section-7.8.3 - // #[case(ICS_2, EXPANDED_2, - // Some(CalDateTime::parse("20060103T000000Z", Some(chrono_tz::US::Eastern)).unwrap().utc()), - // Some(CalDateTime::parse("20060105T000000Z", Some(chrono_tz::US::Eastern)).unwrap().utc()) - // )] - // #[case(ICS_3, EXPANDED_3, - // Some(CalDateTime::parse("20060103T000000Z", Some(chrono_tz::US::Eastern)).unwrap().utc()), - // Some(CalDateTime::parse("20060105T000000Z", Some(chrono_tz::US::Eastern)).unwrap().utc()) - // )] - // fn test_expand_recurrence( - // #[case] ics: &'static str, - // #[case] expanded: &[&str], - // #[case] from: Option>, - // #[case] to: Option>, - // ) { - // let event = CalendarObject::from_ics(ics.to_string(), None).unwrap(); - // let crate::CalendarObjectComponent::Event(event, overrides) = event.get_data() else { - // panic!() - // }; - // - // let events: Vec = event - // .expand_recurrence(from, to, overrides) - // .unwrap() - // .into_iter() - // .map(|event| Emitter::generate(&event)) - // .collect(); - // assert_eq!(events.len(), expanded.len()); - // for (output, reference) in events.iter().zip(expanded) { - // similar_asserts::assert_eq!(output, reference); - // } - // } -} diff --git a/crates/ical/src/icalendar/mod.rs b/crates/ical/src/icalendar/mod.rs deleted file mode 100644 index 4883949..0000000 --- a/crates/ical/src/icalendar/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod event; -mod object; - -pub use event::*; -pub use object::*; diff --git a/crates/ical/src/icalendar/object.rs b/crates/ical/src/icalendar/object.rs deleted file mode 100644 index fa99059..0000000 --- a/crates/ical/src/icalendar/object.rs +++ /dev/null @@ -1,366 +0,0 @@ -use super::EventObject; -use crate::CalDateTime; -use crate::Error; -use chrono::DateTime; -use chrono::Utc; -use derive_more::Display; -use ical::generator::{Emitter, IcalCalendar}; -use ical::parser::ical::component::IcalJournal; -use ical::parser::ical::component::IcalTimeZone; -use ical::parser::ical::component::IcalTodo; -use ical::property::Property; -use serde::Deserialize; -use serde::Serialize; -use sha2::{Digest, Sha256}; -use std::{collections::HashMap, io::BufReader}; - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Display)] -// specified in https://datatracker.ietf.org/doc/html/rfc5545#section-3.6 -pub enum CalendarObjectType { - #[serde(rename = "VEVENT")] - Event = 0, - #[serde(rename = "VTODO")] - Todo = 1, - #[serde(rename = "VJOURNAL")] - Journal = 2, -} - -impl CalendarObjectType { - #[must_use] - pub const fn as_str(&self) -> &'static str { - match self { - Self::Event => "VEVENT", - Self::Todo => "VTODO", - Self::Journal => "VJOURNAL", - } - } -} - -impl rustical_xml::ValueSerialize for CalendarObjectType { - fn serialize(&self) -> String { - self.as_str().to_owned() - } -} - -impl rustical_xml::ValueDeserialize for CalendarObjectType { - fn deserialize(val: &str) -> std::result::Result { - match ::deserialize(val)?.as_str() { - "VEVENT" => Ok(Self::Event), - "VTODO" => Ok(Self::Todo), - "VJOURNAL" => Ok(Self::Journal), - _ => Err(rustical_xml::XmlError::InvalidValue( - rustical_xml::ParseValueError::Other(format!( - "Invalid value '{val}', must be VEVENT, VTODO, or VJOURNAL" - )), - )), - } - } -} - -#[derive(Debug, Clone)] -pub enum CalendarObjectComponent { - Event(EventObject, Vec), - Todo(IcalTodo, Vec), - Journal(IcalJournal, Vec), -} - -impl CalendarObjectComponent { - #[must_use] - pub fn get_uid(&self) -> &str { - match &self { - // We've made sure before that the first component exists and all components share the - // same UID - Self::Todo(todo, _) => todo.get_uid(), - Self::Event(event, _) => event.event.get_uid(), - Self::Journal(journal, _) => journal.get_uid(), - } - } -} - -impl From<&CalendarObjectComponent> for CalendarObjectType { - fn from(value: &CalendarObjectComponent) -> Self { - match value { - CalendarObjectComponent::Event(..) => Self::Event, - CalendarObjectComponent::Todo(..) => Self::Todo, - CalendarObjectComponent::Journal(..) => Self::Journal, - } - } -} - -impl CalendarObjectComponent { - fn from_events(mut events: Vec) -> Result { - // A calendar object does not necessarily have to contain a main VOBJECT - if events.is_empty() { - return Err(Error::MissingCalendar); - } - #[allow(clippy::option_if_let_else)] - let main_event = if let Some(main) = events - .extract_if(.., |event| event.event.get_recurrence_id().is_none()) - .next() - { - main - } else { - events.remove(0) - }; - let overrides = events; - for event in &overrides { - if event.get_uid() != main_event.get_uid() { - return Err(Error::InvalidData( - "Calendar object contains multiple UIDs".to_owned(), - )); - } - if event.event.get_recurrence_id().is_none() { - return Err(Error::InvalidData( - "Calendar object can only contain one main component".to_owned(), - )); - } - } - Ok(Self::Event(main_event, overrides)) - } - fn from_todos(mut todos: Vec) -> Result { - // A calendar object does not necessarily have to contain a main VOBJECT - if todos.is_empty() { - return Err(Error::MissingCalendar); - } - #[allow(clippy::option_if_let_else)] - let main_todo = if let Some(main) = todos - .extract_if(.., |todo| todo.get_recurrence_id().is_none()) - .next() - { - main - } else { - todos.remove(0) - }; - let overrides = todos; - for todo in &overrides { - if todo.get_uid() != main_todo.get_uid() { - return Err(Error::InvalidData( - "Calendar object contains multiple UIDs".to_owned(), - )); - } - if todo.get_recurrence_id().is_none() { - return Err(Error::InvalidData( - "Calendar object can only contain one main component".to_owned(), - )); - } - } - Ok(Self::Todo(main_todo, overrides)) - } - fn from_journals(mut journals: Vec) -> Result { - // A calendar object does not necessarily have to contain a main VOBJECT - if journals.is_empty() { - return Err(Error::MissingCalendar); - } - #[allow(clippy::option_if_let_else)] - let main_journal = if let Some(main) = journals - .extract_if(.., |journal| journal.get_recurrence_id().is_none()) - .next() - { - main - } else { - journals.remove(0) - }; - let overrides = journals; - for journal in &overrides { - if journal.get_uid() != main_journal.get_uid() { - return Err(Error::InvalidData( - "Calendar object contains multiple UIDs".to_owned(), - )); - } - if journal.get_recurrence_id().is_none() { - return Err(Error::InvalidData( - "Calendar object can only contain one main component".to_owned(), - )); - } - } - Ok(Self::Journal(main_journal, overrides)) - } - - pub fn get_first_occurence(&self) -> Result, Error> { - match &self { - Self::Event(main_event, overrides) => Ok(overrides - .iter() - .chain(std::iter::once(main_event)) - .map(super::event::EventObject::get_dtstart) - .collect::, _>>()? - .into_iter() - .flatten() - .min()), - _ => Ok(None), - } - } - - pub fn get_last_occurence(&self) -> Result, Error> { - match &self { - Self::Event(main_event, overrides) => Ok(overrides - .iter() - .chain(std::iter::once(main_event)) - .map(super::event::EventObject::get_last_occurence) - .collect::, _>>()? - .into_iter() - .flatten() - .max()), - _ => Ok(None), - } - } -} - -#[derive(Debug, Clone)] -pub struct CalendarObject { - data: CalendarObjectComponent, - properties: Vec, - id: String, - ics: String, - vtimezones: HashMap, -} - -impl CalendarObject { - pub fn from_ics(ics: String, id: Option) -> Result { - let mut parser = ical::IcalParser::new(BufReader::new(ics.as_bytes())); - let cal = parser.next().ok_or(Error::MissingCalendar)??; - if parser.next().is_some() { - return Err(Error::InvalidData( - "multiple calendars, only one allowed".to_owned(), - )); - } - - if u8::from(!cal.events.is_empty()) - + u8::from(!cal.todos.is_empty()) - + u8::from(!cal.journals.is_empty()) - + u8::from(!cal.free_busys.is_empty()) - != 1 - { - // https://datatracker.ietf.org/doc/html/rfc4791#section-4.1 - return Err(Error::InvalidData( - "iCalendar object must have exactly one component type".to_owned(), - )); - } - - let timezones: HashMap> = cal - .timezones - .clone() - .into_iter() - .map(|timezone| (timezone.get_tzid().to_owned(), (&timezone).try_into().ok())) - .collect(); - - let vtimezones = cal - .timezones - .clone() - .into_iter() - .map(|timezone| (timezone.get_tzid().to_owned(), timezone)) - .collect(); - - let data = if !cal.events.is_empty() { - CalendarObjectComponent::from_events( - cal.events - .into_iter() - .map(|event| EventObject { - event, - timezones: timezones.clone(), - }) - .collect(), - )? - } else if !cal.todos.is_empty() { - CalendarObjectComponent::from_todos(cal.todos)? - } else if !cal.journals.is_empty() { - CalendarObjectComponent::from_journals(cal.journals)? - } else { - return Err(Error::InvalidData( - "iCalendar component type not supported :(".to_owned(), - )); - }; - - Ok(Self { - id: id.unwrap_or_else(|| data.get_uid().to_owned()), - data, - properties: cal.properties, - ics, - vtimezones, - }) - } - - #[must_use] - pub const fn get_vtimezones(&self) -> &HashMap { - &self.vtimezones - } - - #[must_use] - pub const fn get_data(&self) -> &CalendarObjectComponent { - &self.data - } - - #[must_use] - pub fn get_uid(&self) -> &str { - self.data.get_uid() - } - - #[must_use] - pub fn get_id(&self) -> &str { - &self.id - } - - #[must_use] - pub fn get_etag(&self) -> String { - let mut hasher = Sha256::new(); - hasher.update(self.get_uid()); - hasher.update(self.get_ics()); - format!("\"{:x}\"", hasher.finalize()) - } - - #[must_use] - pub fn get_ics(&self) -> &str { - &self.ics - } - - #[must_use] - pub fn get_component_name(&self) -> &str { - self.get_object_type().as_str() - } - - #[must_use] - pub fn get_object_type(&self) -> CalendarObjectType { - (&self.data).into() - } - - pub fn get_first_occurence(&self) -> Result, Error> { - self.data.get_first_occurence() - } - - pub fn get_last_occurence(&self) -> Result, Error> { - self.data.get_last_occurence() - } - - pub fn expand_recurrence( - &self, - start: Option>, - end: Option>, - ) -> Result { - // Only events can be expanded - match &self.data { - CalendarObjectComponent::Event(main_event, overrides) => { - let cal = IcalCalendar { - properties: self.properties.clone(), - events: main_event.expand_recurrence(start, end, overrides)?, - ..Default::default() - }; - Ok(cal.generate()) - } - _ => Ok(self.get_ics().to_string()), - } - } - - #[must_use] - pub fn get_property(&self, name: &str) -> Option<&Property> { - self.properties - .iter() - .find(|property| property.name == name) - } - - #[must_use] - pub fn get_named_properties(&self, name: &str) -> Vec<&Property> { - self.properties - .iter() - .filter(|property| property.name == name) - .collect() - } -} diff --git a/crates/ical/src/lib.rs b/crates/ical/src/lib.rs index 9c00d52..68782f3 100644 --- a/crates/ical/src/lib.rs +++ b/crates/ical/src/lib.rs @@ -1,15 +1,13 @@ #![warn(clippy::all, clippy::pedantic, clippy::nursery)] #![allow(clippy::missing_errors_doc, clippy::missing_panics_doc)] mod timestamp; -mod timezone; +use ical::parser::ParserError; pub use timestamp::*; -pub use timezone::*; -mod icalendar; -pub use icalendar::*; - -mod error; -pub use error::Error; +mod calendar_object; +pub use calendar_object::*; mod address_object; pub use address_object::AddressObject; + +pub type Error = ParserError; diff --git a/crates/ical/src/timestamp.rs b/crates/ical/src/timestamp.rs index 2dcf037..4fd5eb7 100644 --- a/crates/ical/src/timestamp.rs +++ b/crates/ical/src/timestamp.rs @@ -1,35 +1,8 @@ -use super::timezone::ICalTimezone; -use chrono::{DateTime, Datelike, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime, Utc}; -use chrono_tz::Tz; +use chrono::{DateTime, NaiveDateTime, Utc}; use derive_more::derive::Deref; -use ical::property::Property; use rustical_xml::{ValueDeserialize, ValueSerialize}; -use std::{borrow::Cow, collections::HashMap, ops::Add, sync::LazyLock}; -static RE_VCARD_DATE_MM_DD: LazyLock = - LazyLock::new(|| regex::Regex::new(r"^--(?\d{2})(?\d{2})$").unwrap()); - -const LOCAL_DATE_TIME: &str = "%Y%m%dT%H%M%S"; const UTC_DATE_TIME: &str = "%Y%m%dT%H%M%SZ"; -pub const LOCAL_DATE: &str = "%Y%m%d"; - -#[derive(Debug, thiserror::Error, PartialEq, Eq)] -pub enum CalDateTimeError { - #[error( - "Timezone has X-LIC-LOCATION property to specify a timezone from the Olson database, however its value {0} is invalid" - )] - InvalidOlson(String), - #[error("TZID {0} does not refer to a valid timezone")] - InvalidTZID(String), - #[error("Timestamp doesn't exist because of gap in local time")] - LocalTimeGap, - #[error("Datetime string {0} has an invalid format")] - InvalidDatetimeFormat(String), - #[error("Could not parse datetime {0}")] - ParseError(String), - #[error("Duration string {0} has an invalid format")] - InvalidDurationFormat(String), -} #[derive(Debug, Clone, Deref, PartialEq, Eq, Hash)] pub struct UtcDateTime(pub DateTime); @@ -54,375 +27,3 @@ impl ValueSerialize for UtcDateTime { format!("{}", self.0.format(UTC_DATE_TIME)) } } - -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum CalDateTime { - // Form 1, example: 19980118T230000 -> Local - // Form 2, example: 19980119T070000Z -> UTC - // Form 3, example: TZID=America/New_York:19980119T020000 -> Olson - // https://en.wikipedia.org/wiki/Tz_database - DateTime(DateTime), - Date(NaiveDate, ICalTimezone), -} - -impl From for DateTime { - fn from(value: CalDateTime) -> Self { - value - .as_datetime() - .into_owned() - .with_timezone(&value.timezone().into()) - } -} - -impl From> for CalDateTime { - fn from(value: DateTime) -> Self { - Self::DateTime(value.with_timezone(&value.timezone().into())) - } -} - -impl PartialOrd for CalDateTime { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -impl Ord for CalDateTime { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - match (&self, &other) { - (Self::DateTime(a), Self::DateTime(b)) => a.cmp(b), - (Self::DateTime(a), Self::Date(..)) => a.cmp(&other.as_datetime()), - (Self::Date(..), Self::DateTime(b)) => self.as_datetime().as_ref().cmp(b), - (Self::Date(..), Self::Date(..)) => self.as_datetime().cmp(&other.as_datetime()), - } - } -} - -impl From> for CalDateTime { - fn from(value: DateTime) -> Self { - Self::DateTime(value.with_timezone(&ICalTimezone::Local)) - } -} - -impl From> for CalDateTime { - fn from(value: DateTime) -> Self { - Self::DateTime(value.with_timezone(&ICalTimezone::Olson(chrono_tz::UTC))) - } -} - -impl Add for CalDateTime { - type Output = Self; - - fn add(self, duration: Duration) -> Self::Output { - match self { - Self::DateTime(datetime) => Self::DateTime(datetime + duration), - Self::Date(date, tz) => Self::DateTime( - date.and_time(NaiveTime::default()) - .and_local_timezone(tz) - .earliest() - .expect("Local timezone has constant offset") - + duration, - ), - } - } -} - -impl CalDateTime { - pub fn parse_prop( - prop: &Property, - timezones: &HashMap>, - ) -> Result { - let prop_value = prop - .value - .as_ref() - .ok_or_else(|| CalDateTimeError::InvalidDatetimeFormat("empty property".into()))?; - - let timezone = if let Some(tzid) = prop.get_param("TZID") { - if let Some(timezone) = timezones.get(tzid) { - timezone.to_owned() - } else { - // TZID refers to timezone that does not exist - return Err(CalDateTimeError::InvalidTZID(tzid.to_string())); - } - } else { - // No explicit timezone specified. - // This is valid and will be localtime or UTC depending on the value - // We will stick to this default as documented in https://github.com/lennart-k/rustical/issues/102 - None - }; - - Self::parse(prop_value, timezone) - } - - #[must_use] - pub fn format(&self) -> String { - match self { - Self::DateTime(datetime) => match datetime.timezone() { - ICalTimezone::Olson(chrono_tz::UTC) => datetime.format(UTC_DATE_TIME).to_string(), - _ => datetime.format(LOCAL_DATE_TIME).to_string(), - }, - Self::Date(date, _) => date.format(LOCAL_DATE).to_string(), - } - } - - #[must_use] - pub fn format_date(&self) -> String { - match self { - Self::DateTime(datetime) => datetime.format(LOCAL_DATE).to_string(), - Self::Date(date, _) => date.format(LOCAL_DATE).to_string(), - } - } - - #[must_use] - pub fn date(&self) -> NaiveDate { - match self { - Self::DateTime(datetime) => datetime.date_naive(), - Self::Date(date, _) => date.to_owned(), - } - } - - #[must_use] - pub const fn is_date(&self) -> bool { - matches!(&self, Self::Date(_, _)) - } - - #[must_use] - pub fn as_datetime(&self) -> Cow<'_, DateTime> { - match self { - Self::DateTime(datetime) => Cow::Borrowed(datetime), - Self::Date(date, tz) => Cow::Owned( - date.and_time(NaiveTime::default()) - .and_local_timezone(tz.to_owned()) - .earliest() - .expect("Midnight always exists"), - ), - } - } - - #[must_use] - pub fn with_timezone(&self, tz: &ICalTimezone) -> Self { - match self { - Self::DateTime(datetime) => Self::DateTime(datetime.with_timezone(tz)), - Self::Date(date, _) => Self::Date(date.to_owned(), tz.to_owned()), - } - } - - pub fn parse(value: &str, timezone: Option) -> Result { - if let Ok(datetime) = NaiveDateTime::parse_from_str(value, LOCAL_DATE_TIME) { - if let Some(timezone) = timezone { - return Ok(Self::DateTime( - datetime - .and_local_timezone(timezone.into()) - .earliest() - .ok_or(CalDateTimeError::LocalTimeGap)?, - )); - } - return Ok(Self::DateTime( - datetime - .and_local_timezone(ICalTimezone::Local) - .earliest() - .ok_or(CalDateTimeError::LocalTimeGap)?, - )); - } - - if let Ok(datetime) = NaiveDateTime::parse_from_str(value, UTC_DATE_TIME) { - return Ok(datetime.and_utc().into()); - } - let timezone = timezone.map_or(ICalTimezone::Local, ICalTimezone::Olson); - if let Ok(date) = NaiveDate::parse_from_str(value, LOCAL_DATE) { - return Ok(Self::Date(date, timezone)); - } - - if let Ok(date) = NaiveDate::parse_from_str(value, "%Y-%m-%d") { - return Ok(Self::Date(date, timezone)); - } - if let Ok(date) = NaiveDate::parse_from_str(value, "%Y%m%d") { - return Ok(Self::Date(date, timezone)); - } - - Err(CalDateTimeError::InvalidDatetimeFormat(value.to_string())) - } - - // Also returns whether the date contains a year - pub fn parse_vcard(value: &str) -> Result<(Self, bool), CalDateTimeError> { - if let Ok(datetime) = Self::parse(value, None) { - return Ok((datetime, true)); - } - - if let Some(captures) = RE_VCARD_DATE_MM_DD.captures(value) { - // Because 1972 is a leap year - let year = 1972; - // Cannot fail because of the regex - let month = captures.name("m").unwrap().as_str().parse().ok().unwrap(); - let day = captures.name("d").unwrap().as_str().parse().ok().unwrap(); - - return Ok(( - Self::Date( - NaiveDate::from_ymd_opt(year, month, day) - .ok_or_else(|| CalDateTimeError::ParseError(value.to_string()))?, - ICalTimezone::Local, - ), - false, - )); - } - Err(CalDateTimeError::InvalidDatetimeFormat(value.to_string())) - } - - #[must_use] - pub fn utc(&self) -> DateTime { - self.as_datetime().to_utc() - } - - #[must_use] - pub fn timezone(&self) -> ICalTimezone { - match &self { - Self::DateTime(datetime) => datetime.timezone(), - Self::Date(_, tz) => tz.to_owned(), - } - } -} - -impl From for DateTime { - fn from(value: CalDateTime) -> Self { - value.utc() - } -} - -impl Datelike for CalDateTime { - fn year(&self) -> i32 { - match &self { - Self::DateTime(datetime) => datetime.year(), - Self::Date(date, _) => date.year(), - } - } - fn month(&self) -> u32 { - match &self { - Self::DateTime(datetime) => datetime.month(), - Self::Date(date, _) => date.month(), - } - } - - fn month0(&self) -> u32 { - match &self { - Self::DateTime(datetime) => datetime.month0(), - Self::Date(date, _) => date.month0(), - } - } - fn day(&self) -> u32 { - match &self { - Self::DateTime(datetime) => datetime.day(), - Self::Date(date, _) => date.day(), - } - } - fn day0(&self) -> u32 { - match &self { - Self::DateTime(datetime) => datetime.day0(), - Self::Date(date, _) => date.day0(), - } - } - fn ordinal(&self) -> u32 { - match &self { - Self::DateTime(datetime) => datetime.ordinal(), - Self::Date(date, _) => date.ordinal(), - } - } - fn ordinal0(&self) -> u32 { - match &self { - Self::DateTime(datetime) => datetime.ordinal0(), - Self::Date(date, _) => date.ordinal0(), - } - } - fn weekday(&self) -> chrono::Weekday { - match &self { - Self::DateTime(datetime) => datetime.weekday(), - Self::Date(date, _) => date.weekday(), - } - } - fn iso_week(&self) -> chrono::IsoWeek { - match &self { - Self::DateTime(datetime) => datetime.iso_week(), - Self::Date(date, _) => date.iso_week(), - } - } - fn with_year(&self, year: i32) -> Option { - match &self { - Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_year(year)?)), - Self::Date(date, tz) => Some(Self::Date(date.with_year(year)?, tz.to_owned())), - } - } - fn with_month(&self, month: u32) -> Option { - match &self { - Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_month(month)?)), - Self::Date(date, tz) => Some(Self::Date(date.with_month(month)?, tz.to_owned())), - } - } - fn with_month0(&self, month0: u32) -> Option { - match &self { - Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_month0(month0)?)), - Self::Date(date, tz) => Some(Self::Date(date.with_month0(month0)?, tz.to_owned())), - } - } - fn with_day(&self, day: u32) -> Option { - match &self { - Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_day(day)?)), - Self::Date(date, tz) => Some(Self::Date(date.with_day(day)?, tz.to_owned())), - } - } - fn with_day0(&self, day0: u32) -> Option { - match &self { - Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_day0(day0)?)), - Self::Date(date, tz) => Some(Self::Date(date.with_day0(day0)?, tz.to_owned())), - } - } - fn with_ordinal(&self, ordinal: u32) -> Option { - match &self { - Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_ordinal(ordinal)?)), - Self::Date(date, tz) => Some(Self::Date(date.with_ordinal(ordinal)?, tz.to_owned())), - } - } - fn with_ordinal0(&self, ordinal0: u32) -> Option { - match &self { - Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_ordinal0(ordinal0)?)), - Self::Date(date, tz) => Some(Self::Date(date.with_ordinal0(ordinal0)?, tz.to_owned())), - } - } -} - -#[cfg(test)] -mod tests { - use crate::CalDateTime; - use chrono::NaiveDate; - - #[test] - fn test_vcard_date() { - assert_eq!( - CalDateTime::parse_vcard("19850412").unwrap(), - ( - CalDateTime::Date( - NaiveDate::from_ymd_opt(1985, 4, 12).unwrap(), - crate::ICalTimezone::Local - ), - true - ) - ); - assert_eq!( - CalDateTime::parse_vcard("1985-04-12").unwrap(), - ( - CalDateTime::Date( - NaiveDate::from_ymd_opt(1985, 4, 12).unwrap(), - crate::ICalTimezone::Local - ), - true - ) - ); - assert_eq!( - CalDateTime::parse_vcard("--0412").unwrap(), - ( - CalDateTime::Date( - NaiveDate::from_ymd_opt(1972, 4, 12).unwrap(), - crate::ICalTimezone::Local - ), - false - ) - ); - } -} diff --git a/crates/ical/src/timezone.rs b/crates/ical/src/timezone.rs deleted file mode 100644 index aea5c9e..0000000 --- a/crates/ical/src/timezone.rs +++ /dev/null @@ -1,92 +0,0 @@ -use chrono::{Local, NaiveDate, NaiveDateTime, TimeZone}; -use chrono_tz::Tz; -use derive_more::{Display, From}; - -#[derive(Debug, Clone, From, PartialEq, Eq)] -pub enum ICalTimezone { - Local, - Olson(Tz), -} - -impl From for rrule::Tz { - fn from(value: ICalTimezone) -> Self { - match value { - ICalTimezone::Local => Self::LOCAL, - ICalTimezone::Olson(tz) => Self::Tz(tz), - } - } -} - -impl From for ICalTimezone { - fn from(value: rrule::Tz) -> Self { - match value { - rrule::Tz::Local(_) => Self::Local, - rrule::Tz::Tz(tz) => Self::Olson(tz), - } - } -} - -#[derive(Debug, Clone, PartialEq, Eq, Display)] -pub enum CalTimezoneOffset { - Local(chrono::FixedOffset), - Olson(chrono_tz::TzOffset), -} - -impl chrono::Offset for CalTimezoneOffset { - fn fix(&self) -> chrono::FixedOffset { - match self { - Self::Local(local) => local.fix(), - Self::Olson(olson) => olson.fix(), - } - } -} - -impl TimeZone for ICalTimezone { - type Offset = CalTimezoneOffset; - - fn from_offset(offset: &Self::Offset) -> Self { - match offset { - CalTimezoneOffset::Local(_) => Self::Local, - CalTimezoneOffset::Olson(offset) => Self::Olson(Tz::from_offset(offset)), - } - } - - fn offset_from_local_date(&self, local: &NaiveDate) -> chrono::MappedLocalTime { - match self { - Self::Local => Local - .offset_from_local_date(local) - .map(CalTimezoneOffset::Local), - Self::Olson(tz) => tz - .offset_from_local_date(local) - .map(CalTimezoneOffset::Olson), - } - } - - fn offset_from_local_datetime( - &self, - local: &NaiveDateTime, - ) -> chrono::MappedLocalTime { - match self { - Self::Local => Local - .offset_from_local_datetime(local) - .map(CalTimezoneOffset::Local), - Self::Olson(tz) => tz - .offset_from_local_datetime(local) - .map(CalTimezoneOffset::Olson), - } - } - - fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> Self::Offset { - match self { - Self::Local => CalTimezoneOffset::Local(Local.offset_from_utc_datetime(utc)), - Self::Olson(tz) => CalTimezoneOffset::Olson(tz.offset_from_utc_datetime(utc)), - } - } - - fn offset_from_utc_date(&self, utc: &NaiveDate) -> Self::Offset { - match self { - Self::Local => CalTimezoneOffset::Local(Local.offset_from_utc_date(utc)), - Self::Olson(tz) => CalTimezoneOffset::Olson(tz.offset_from_utc_date(utc)), - } - } -} diff --git a/crates/ical/tests/test_cal_object.rs b/crates/ical/tests/test_cal_object.rs index 9fc4ffb..392affd 100644 --- a/crates/ical/tests/test_cal_object.rs +++ b/crates/ical/tests/test_cal_object.rs @@ -25,6 +25,6 @@ END:VCALENDAR #[test] fn parse_calendar_object() { - let object = CalendarObject::from_ics(MULTI_VEVENT.to_string(), None).unwrap(); - object.expand_recurrence(None, None).unwrap(); + let object = CalendarObject::from_ics(MULTI_VEVENT.to_string()).unwrap(); + object.get_inner().expand_recurrence(None, None); } diff --git a/crates/store/src/addressbook_store.rs b/crates/store/src/addressbook_store.rs index 325071b..c5bf2d6 100644 --- a/crates/store/src/addressbook_store.rs +++ b/crates/store/src/addressbook_store.rs @@ -15,8 +15,8 @@ pub trait AddressbookStore: Send + Sync + 'static { async fn update_addressbook( &self, - principal: String, - id: String, + principal: &str, + id: &str, addressbook: Addressbook, ) -> Result<(), Error>; async fn insert_addressbook(&self, addressbook: Addressbook) -> Result<(), Error>; @@ -33,7 +33,7 @@ pub trait AddressbookStore: Send + Sync + 'static { principal: &str, addressbook_id: &str, synctoken: i64, - ) -> Result<(Vec, Vec, i64), Error>; + ) -> Result<(Vec<(String, AddressObject)>, Vec, i64), Error>; async fn addressbook_metadata( &self, @@ -45,7 +45,7 @@ pub trait AddressbookStore: Send + Sync + 'static { &self, principal: &str, addressbook_id: &str, - ) -> Result, Error>; + ) -> Result, Error>; async fn get_object( &self, principal: &str, @@ -55,8 +55,9 @@ pub trait AddressbookStore: Send + Sync + 'static { ) -> Result; async fn put_object( &self, - principal: String, - addressbook_id: String, + principal: &str, + addressbook_id: &str, + object_id: &str, object: AddressObject, overwrite: bool, ) -> Result<(), Error>; @@ -77,7 +78,7 @@ pub trait AddressbookStore: Send + Sync + 'static { async fn import_addressbook( &self, addressbook: Addressbook, - objects: Vec, + objects: Vec<(String, AddressObject)>, merge_existing: bool, ) -> Result<(), Error>; } diff --git a/crates/store/src/calendar_store.rs b/crates/store/src/calendar_store.rs index 0c51909..ac9b311 100644 --- a/crates/store/src/calendar_store.rs +++ b/crates/store/src/calendar_store.rs @@ -22,8 +22,8 @@ pub trait CalendarStore: Send + Sync + 'static { async fn update_calendar( &self, - principal: String, - id: String, + principal: &str, + id: &str, calendar: Calendar, ) -> Result<(), Error>; async fn insert_calendar(&self, calendar: Calendar) -> Result<(), Error>; @@ -46,7 +46,7 @@ pub trait CalendarStore: Send + Sync + 'static { principal: &str, cal_id: &str, synctoken: i64, - ) -> Result<(Vec, Vec, i64), Error>; + ) -> Result<(Vec<(String, CalendarObject)>, Vec, i64), Error>; /// Since the rules are rather complex this function /// is only meant to do some prefiltering @@ -55,7 +55,7 @@ pub trait CalendarStore: Send + Sync + 'static { principal: &str, cal_id: &str, _query: CalendarQuery, - ) -> Result, Error> { + ) -> Result, Error> { self.get_objects(principal, cal_id).await } @@ -69,7 +69,7 @@ pub trait CalendarStore: Send + Sync + 'static { &self, principal: &str, cal_id: &str, - ) -> Result, Error>; + ) -> Result, Error>; async fn get_object( &self, principal: &str, @@ -79,20 +79,26 @@ pub trait CalendarStore: Send + Sync + 'static { ) -> Result; async fn put_objects( &self, - principal: String, - cal_id: String, - objects: Vec, + principal: &str, + cal_id: &str, + objects: Vec<(String, CalendarObject)>, overwrite: bool, ) -> Result<(), Error>; async fn put_object( &self, - principal: String, - cal_id: String, + principal: &str, + cal_id: &str, + object_id: &str, object: CalendarObject, overwrite: bool, ) -> Result<(), Error> { - self.put_objects(principal, cal_id, vec![object], overwrite) - .await + self.put_objects( + principal, + cal_id, + vec![(object_id.to_owned(), object)], + overwrite, + ) + .await } async fn delete_object( &self, diff --git a/crates/store/src/combined_calendar_store.rs b/crates/store/src/combined_calendar_store.rs index a83b7e4..e987918 100644 --- a/crates/store/src/combined_calendar_store.rs +++ b/crates/store/src/combined_calendar_store.rs @@ -1,5 +1,6 @@ -use crate::CalendarStore; +use crate::{Calendar, CalendarStore, calendar_store::CalendarQuery}; use async_trait::async_trait; +use rustical_ical::CalendarObject; use std::{collections::HashMap, sync::Arc}; pub trait PrefixedCalendarStore: CalendarStore { @@ -51,11 +52,11 @@ impl CalendarStore for CombinedCalendarStore { async fn update_calendar( &self, - principal: String, - id: String, - calendar: crate::Calendar, + principal: &str, + id: &str, + calendar: Calendar, ) -> Result<(), crate::Error> { - self.store_for_id(&id) + self.store_for_id(id) .update_calendar(principal, id, calendar) .await } @@ -88,7 +89,7 @@ impl CalendarStore for CombinedCalendarStore { principal: &str, cal_id: &str, synctoken: i64, - ) -> Result<(Vec, Vec, i64), crate::Error> { + ) -> Result<(Vec<(String, CalendarObject)>, Vec, i64), crate::Error> { self.store_for_id(cal_id) .sync_changes(principal, cal_id, synctoken) .await @@ -97,7 +98,7 @@ impl CalendarStore for CombinedCalendarStore { async fn import_calendar( &self, calendar: crate::Calendar, - objects: Vec, + objects: Vec, merge_existing: bool, ) -> Result<(), crate::Error> { self.store_for_id(&calendar.id) @@ -109,8 +110,8 @@ impl CalendarStore for CombinedCalendarStore { &self, principal: &str, cal_id: &str, - query: crate::calendar_store::CalendarQuery, - ) -> Result, crate::Error> { + query: CalendarQuery, + ) -> Result, crate::Error> { self.store_for_id(cal_id) .calendar_query(principal, cal_id, query) .await @@ -141,7 +142,7 @@ impl CalendarStore for CombinedCalendarStore { &self, principal: &str, cal_id: &str, - ) -> Result, crate::Error> { + ) -> Result, crate::Error> { self.store_for_id(cal_id) .get_objects(principal, cal_id) .await @@ -149,12 +150,12 @@ impl CalendarStore for CombinedCalendarStore { async fn put_objects( &self, - principal: String, - cal_id: String, - objects: Vec, + principal: &str, + cal_id: &str, + objects: Vec<(String, CalendarObject)>, overwrite: bool, ) -> Result<(), crate::Error> { - self.store_for_id(&cal_id) + self.store_for_id(cal_id) .put_objects(principal, cal_id, objects, overwrite) .await } diff --git a/crates/store/src/error.rs b/crates/store/src/error.rs index 9ace70f..259a9fa 100644 --- a/crates/store/src/error.rs +++ b/crates/store/src/error.rs @@ -26,7 +26,7 @@ pub enum Error { Other(#[from] anyhow::Error), #[error(transparent)] - IcalError(#[from] rustical_ical::Error), + IcalError(#[from] ical::parser::ParserError), } impl Error { @@ -36,7 +36,8 @@ impl Error { Self::NotFound => StatusCode::NOT_FOUND, Self::AlreadyExists => StatusCode::CONFLICT, Self::ReadOnly => StatusCode::FORBIDDEN, - Self::IcalError(err) => err.status_code(), + // TODO: Can also be Bad Request, depending on when this is raised + Self::IcalError(_err) => StatusCode::INTERNAL_SERVER_ERROR, Self::InvalidPrincipalType(_) => StatusCode::BAD_REQUEST, _ => StatusCode::INTERNAL_SERVER_ERROR, } diff --git a/crates/store_sqlite/Cargo.toml b/crates/store_sqlite/Cargo.toml index ae7a6d0..8c3b7fc 100644 --- a/crates/store_sqlite/Cargo.toml +++ b/crates/store_sqlite/Cargo.toml @@ -20,6 +20,7 @@ rstest.workspace = true criterion.workspace = true [dependencies] +ical.workspace = true tokio.workspace = true rustical_store.workspace = true async-trait.workspace = true diff --git a/crates/store_sqlite/benches/insert_calendar_object.rs b/crates/store_sqlite/benches/insert_calendar_object.rs index 02f0d42..4330720 100644 --- a/crates/store_sqlite/benches/insert_calendar_object.rs +++ b/crates/store_sqlite/benches/insert_calendar_object.rs @@ -34,16 +34,19 @@ fn benchmark(c: &mut Criterion) { cal_store }); - let object = CalendarObject::from_ics(include_str!("ical_event.ics").to_owned(), None).unwrap(); + let row = ( + "asd".to_owned(), + CalendarObject::from_ics(include_str!("ical_event.ics").to_owned()).unwrap(), + ); let batch_size = 1000; - let objects: Vec<_> = std::iter::repeat_n(object.clone(), batch_size).collect(); + let objects: Vec<_> = std::iter::repeat_n(row.clone(), batch_size).collect(); c.bench_function("put_batch", |b| { b.to_async(&runtime).iter(async || { // yeet cal_store - .put_objects("user".to_owned(), "okwow".to_owned(), objects.clone(), true) + .put_objects("user", "okwow", objects.clone(), true) .await .unwrap(); }); @@ -54,7 +57,7 @@ fn benchmark(c: &mut Criterion) { // yeet for _ in 0..1000 { cal_store - .put_object("user".to_owned(), "okwow".to_owned(), object.clone(), true) + .put_object("user", "okwow", &row.0, row.1.clone(), true) .await .unwrap(); } diff --git a/crates/store_sqlite/src/addressbook_store/birthday_calendar.rs b/crates/store_sqlite/src/addressbook_store/birthday_calendar.rs index 8a73824..2161d2c 100644 --- a/crates/store_sqlite/src/addressbook_store/birthday_calendar.rs +++ b/crates/store_sqlite/src/addressbook_store/birthday_calendar.rs @@ -1,14 +1,13 @@ use crate::addressbook_store::SqliteAddressbookStore; use async_trait::async_trait; use chrono::NaiveDateTime; -use rustical_ical::{AddressObject, CalendarObject, CalendarObjectType}; +use rustical_ical::{CalendarObject, CalendarObjectType}; use rustical_store::{ Addressbook, AddressbookStore, Calendar, CalendarMetadata, CalendarStore, CollectionMetadata, Error, PrefixedCalendarStore, }; use sha2::{Digest, Sha256}; use sqlx::{Executor, Sqlite}; -use std::collections::HashMap; use tracing::instrument; pub const BIRTHDAYS_PREFIX: &str = "_birthdays_"; @@ -269,17 +268,18 @@ impl CalendarStore for SqliteAddressbookStore { #[instrument] async fn update_calendar( &self, - principal: String, - id: String, + principal: &str, + id: &str, mut calendar: Calendar, ) -> Result<(), Error> { + assert_eq!(principal, calendar.principal); assert_eq!(id, calendar.id); calendar.id = calendar .id .strip_prefix(BIRTHDAYS_PREFIX) .ok_or(Error::NotFound)? .to_string(); - Self::_update_birthday_calendar(&self.db, &principal, &calendar).await + Self::_update_birthday_calendar(&self.db, principal, &calendar).await } #[instrument] @@ -324,19 +324,35 @@ impl CalendarStore for SqliteAddressbookStore { principal: &str, cal_id: &str, synctoken: i64, - ) -> Result<(Vec, Vec, i64), Error> { + ) -> Result<(Vec<(String, CalendarObject)>, Vec, i64), Error> { let cal_id = cal_id .strip_prefix(BIRTHDAYS_PREFIX) .ok_or(Error::NotFound)?; let (objects, deleted_objects, new_synctoken) = AddressbookStore::sync_changes(self, principal, cal_id, synctoken).await?; - let objects: Result>, rustical_ical::Error> = objects - .iter() - .map(AddressObject::get_birthday_object) - .collect(); - let objects = objects?.into_iter().flatten().collect(); - Ok((objects, deleted_objects, new_synctoken)) + let mut out_objects = vec![]; + + for (object_id, object) in objects { + if let Some(birthday) = object.get_birthday_object()? { + out_objects.push((format!("{object_id}-birthday"), birthday)); + } + if let Some(anniversary) = object.get_anniversary_object()? { + out_objects.push((format!("{object_id}-anniversayr"), anniversary)); + } + } + + let deleted_objects = deleted_objects + .into_iter() + .flat_map(|object_id| { + [ + format!("{object_id}-birthday"), + format!("{object_id}-anniversary"), + ] + }) + .collect(); + + Ok((out_objects, deleted_objects, new_synctoken)) } #[instrument] @@ -356,21 +372,19 @@ impl CalendarStore for SqliteAddressbookStore { &self, principal: &str, cal_id: &str, - ) -> Result, Error> { + ) -> Result, Error> { + let mut objects = vec![]; let cal_id = cal_id .strip_prefix(BIRTHDAYS_PREFIX) .ok_or(Error::NotFound)?; - let objects: Result>, rustical_ical::Error> = - AddressbookStore::get_objects(self, principal, cal_id) - .await? - .iter() - .map(AddressObject::get_significant_dates) - .collect(); - let objects = objects? - .into_iter() - .flat_map(HashMap::into_values) - .collect(); - + for (object_id, object) in AddressbookStore::get_objects(self, principal, cal_id).await? { + if let Some(birthday) = object.get_birthday_object()? { + objects.push((format!("{object_id}-birthday"), birthday)); + } + if let Some(anniversary) = object.get_anniversary_object()? { + objects.push((format!("{object_id}-anniversayr"), anniversary)); + } + } Ok(objects) } @@ -386,19 +400,22 @@ impl CalendarStore for SqliteAddressbookStore { .strip_prefix(BIRTHDAYS_PREFIX) .ok_or(Error::NotFound)?; let (addressobject_id, date_type) = object_id.rsplit_once('-').ok_or(Error::NotFound)?; - AddressbookStore::get_object(self, principal, cal_id, addressobject_id, show_deleted) - .await? - .get_significant_dates()? - .remove(date_type) - .ok_or(Error::NotFound) + let obj = + AddressbookStore::get_object(self, principal, cal_id, addressobject_id, show_deleted) + .await?; + match date_type { + "birthday" => Ok(obj.get_birthday_object()?.ok_or(Error::NotFound)?), + "anniversary" => Ok(obj.get_anniversary_object()?.ok_or(Error::NotFound)?), + _ => Err(Error::NotFound), + } } #[instrument] async fn put_objects( &self, - _principal: String, - _cal_id: String, - _objects: Vec, + _principal: &str, + _cal_id: &str, + _objects: Vec<(String, CalendarObject)>, _overwrite: bool, ) -> Result<(), Error> { Err(Error::ReadOnly) diff --git a/crates/store_sqlite/src/addressbook_store/mod.rs b/crates/store_sqlite/src/addressbook_store/mod.rs index b026c3d..be71439 100644 --- a/crates/store_sqlite/src/addressbook_store/mod.rs +++ b/crates/store_sqlite/src/addressbook_store/mod.rs @@ -2,6 +2,7 @@ use super::ChangeOperation; use crate::BEGIN_IMMEDIATE; use async_trait::async_trait; use derive_more::derive::Constructor; +use ical::parser::ParserError; use rustical_ical::AddressObject; use rustical_store::{ Addressbook, AddressbookStore, CollectionMetadata, CollectionOperation, @@ -9,7 +10,7 @@ use rustical_store::{ }; use sqlx::{Acquire, Executor, Sqlite, SqlitePool, Transaction}; use tokio::sync::mpsc::Sender; -use tracing::{error_span, instrument, warn}; +use tracing::{error, error_span, instrument, warn}; pub mod birthday_calendar; @@ -18,12 +19,18 @@ struct AddressObjectRow { id: String, vcf: String, } +impl From for (String, Result) { + fn from(row: AddressObjectRow) -> Self { + let result = AddressObject::from_vcf(row.vcf); + (row.id, result) + } +} -impl TryFrom for AddressObject { +impl TryFrom for (String, AddressObject) { type Error = rustical_store::Error; fn try_from(value: AddressObjectRow) -> Result { - Ok(Self::from_vcf(value.id, value.vcf)?) + Ok((value.id, AddressObject::from_vcf(value.vcf)?)) } } @@ -31,6 +38,7 @@ impl TryFrom for AddressObject { pub struct SqliteAddressbookStore { db: SqlitePool, sender: Sender, + skip_broken: bool, } impl SqliteAddressbookStore { @@ -88,6 +96,36 @@ impl SqliteAddressbookStore { Ok(()) } + #[allow(clippy::missing_panics_doc)] + pub async fn validate_objects(&self, principal: &str) -> Result<(), Error> { + let mut success = true; + for addressbook in self.get_addressbooks(principal).await? { + for (object_id, res) in Self::_get_objects(&self.db, principal, &addressbook.id).await? + { + if let Err(err) = res { + warn!( + "Invalid address object found at {principal}/{addr_id}/{object_id}.vcf. Error: {err}", + addr_id = addressbook.id + ); + success = false; + } + } + } + if !success { + if self.skip_broken { + error!( + "Not all address objects are valid. Since data_store.sqlite.skip_broken=true they will be hidden. You are still advised to manually remove or repair the object. If you need help feel free to open up an issue on GitHub." + ); + } else { + error!( + "Not all address objects are valid. Since data_store.sqlite.skip_broken=false this causes a panic. Remove or repair the broken objects manually or set data_store.sqlite.skip_broken=false as a temporary solution to ignore the error. If you need help feel free to open up an issue on GitHub." + ); + panic!(); + } + } + Ok(()) + } + // Logs an operation to an address object async fn log_object_operation( tx: &mut Transaction<'_, Sqlite>, @@ -134,7 +172,7 @@ impl SqliteAddressbookStore { if let Err(err) = self.sender.try_send(CollectionOperation { topic, data }) { error_span!( "Error trying to send addressbook update notification:", - err = format!("{err:?}"), + err = format!("{err}"), ); } } @@ -290,7 +328,7 @@ impl SqliteAddressbookStore { principal: &str, addressbook_id: &str, synctoken: i64, - ) -> Result<(Vec, Vec, i64), rustical_store::Error> { + ) -> Result<(Vec<(String, AddressObject)>, Vec, i64), rustical_store::Error> { struct Row { object_id: String, synctoken: i64, @@ -318,7 +356,7 @@ impl SqliteAddressbookStore { for Row { object_id, .. } in changes { match Self::_get_object(&mut *conn, principal, addressbook_id, &object_id, false).await { - Ok(object) => objects.push(object), + Ok(object) => objects.push((object_id, object)), Err(rustical_store::Error::NotFound) => deleted_objects.push(object_id), Err(err) => return Err(err), } @@ -353,8 +391,8 @@ impl SqliteAddressbookStore { executor: E, principal: &str, addressbook_id: &str, - ) -> Result, rustical_store::Error> { - sqlx::query_as!( + ) -> Result)>, Error> { + Ok(sqlx::query_as!( AddressObjectRow, "SELECT id, vcf FROM addressobjects WHERE principal = ? AND addressbook_id = ? AND deleted_at IS NULL", principal, @@ -363,8 +401,8 @@ impl SqliteAddressbookStore { .fetch_all(executor) .await.map_err(crate::Error::from)? .into_iter() - .map(std::convert::TryInto::try_into) - .collect() + .map(Into::into) + ) } async fn _get_object<'e, E: Executor<'e, Database = Sqlite>>( @@ -374,7 +412,7 @@ impl SqliteAddressbookStore { object_id: &str, show_deleted: bool, ) -> Result { - sqlx::query_as!( + let (id, object) = sqlx::query_as!( AddressObjectRow, "SELECT id, vcf FROM addressobjects WHERE (principal, addressbook_id, id) = (?, ?, ?) AND ((deleted_at IS NULL) OR ?)", principal, @@ -385,17 +423,20 @@ impl SqliteAddressbookStore { .fetch_one(executor) .await .map_err(crate::Error::from)? - .try_into() + .try_into()?; + assert_eq!(id, object_id); + Ok(object) } async fn _put_object<'e, E: Executor<'e, Database = Sqlite>>( executor: E, principal: &str, addressbook_id: &str, + object_id: &str, object: &AddressObject, overwrite: bool, ) -> Result<(), rustical_store::Error> { - let (object_id, vcf) = (object.get_id(), object.get_vcf()); + let vcf = object.get_vcf(); (if overwrite { sqlx::query!( @@ -500,11 +541,13 @@ impl AddressbookStore for SqliteAddressbookStore { #[instrument] async fn update_addressbook( &self, - principal: String, - id: String, + principal: &str, + id: &str, addressbook: Addressbook, ) -> Result<(), rustical_store::Error> { - Self::_update_addressbook(&self.db, &principal, &id, &addressbook).await + assert_eq!(principal, &addressbook.principal); + assert_eq!(id, &addressbook.id); + Self::_update_addressbook(&self.db, principal, id, &addressbook).await } #[instrument] @@ -569,7 +612,7 @@ impl AddressbookStore for SqliteAddressbookStore { principal: &str, addressbook_id: &str, synctoken: i64, - ) -> Result<(Vec, Vec, i64), rustical_store::Error> { + ) -> Result<(Vec<(String, AddressObject)>, Vec, i64), rustical_store::Error> { Self::_sync_changes(&self.db, principal, addressbook_id, synctoken).await } @@ -601,8 +644,17 @@ impl AddressbookStore for SqliteAddressbookStore { &self, principal: &str, addressbook_id: &str, - ) -> Result, rustical_store::Error> { - Self::_get_objects(&self.db, principal, addressbook_id).await + ) -> Result, rustical_store::Error> { + let objects = Self::_get_objects(&self.db, principal, addressbook_id).await?; + if self.skip_broken { + Ok(objects + .filter_map(|(id, res)| Some((id, res.ok()?))) + .collect()) + } else { + Ok(objects + .map(|(id, res)| res.map(|obj| (id, obj))) + .collect::, _>>()?) + } } #[instrument] @@ -619,8 +671,9 @@ impl AddressbookStore for SqliteAddressbookStore { #[instrument] async fn put_object( &self, - principal: String, - addressbook_id: String, + principal: &str, + addressbook_id: &str, + object_id: &str, object: AddressObject, overwrite: bool, ) -> Result<(), rustical_store::Error> { @@ -630,15 +683,21 @@ impl AddressbookStore for SqliteAddressbookStore { .await .map_err(crate::Error::from)?; - let object_id = object.get_id().to_owned(); - - Self::_put_object(&mut *tx, &principal, &addressbook_id, &object, overwrite).await?; + Self::_put_object( + &mut *tx, + principal, + addressbook_id, + object_id, + &object, + overwrite, + ) + .await?; let sync_token = Self::log_object_operation( &mut tx, - &principal, - &addressbook_id, - &object_id, + principal, + addressbook_id, + object_id, ChangeOperation::Add, ) .await @@ -648,7 +707,7 @@ impl AddressbookStore for SqliteAddressbookStore { self.send_push_notification( CollectionOperationInfo::Content { sync_token }, - self.get_addressbook(&principal, &addressbook_id, false) + self.get_addressbook(principal, addressbook_id, false) .await? .push_topic, ); @@ -733,7 +792,7 @@ impl AddressbookStore for SqliteAddressbookStore { async fn import_addressbook( &self, addressbook: Addressbook, - objects: Vec, + objects: Vec<(String, AddressObject)>, merge_existing: bool, ) -> Result<(), Error> { let mut tx = self @@ -758,11 +817,12 @@ impl AddressbookStore for SqliteAddressbookStore { } let mut sync_token = None; - for object in objects { + for (object_id, object) in objects { Self::_put_object( &mut *tx, &addressbook.principal, &addressbook.id, + &object_id, &object, false, ) @@ -773,7 +833,7 @@ impl AddressbookStore for SqliteAddressbookStore { &mut tx, &addressbook.principal, &addressbook.id, - object.get_id(), + &object_id, ChangeOperation::Add, ) .await?, diff --git a/crates/store_sqlite/src/calendar_store.rs b/crates/store_sqlite/src/calendar_store.rs index 5f1b130..7e84236 100644 --- a/crates/store_sqlite/src/calendar_store.rs +++ b/crates/store_sqlite/src/calendar_store.rs @@ -3,8 +3,10 @@ use crate::BEGIN_IMMEDIATE; use async_trait::async_trait; use chrono::TimeDelta; use derive_more::derive::Constructor; +use ical::parser::ParserError; +use ical::types::CalDateTime; use regex::Regex; -use rustical_ical::{CalDateTime, CalendarObject, CalendarObjectType}; +use rustical_ical::{CalendarObject, CalendarObjectType}; use rustical_store::calendar_store::CalendarQuery; use rustical_store::synctoken::format_synctoken; use rustical_store::{Calendar, CalendarMetadata, CalendarStore, CollectionMetadata, Error}; @@ -12,7 +14,7 @@ use rustical_store::{CollectionOperation, CollectionOperationInfo}; use sqlx::types::chrono::NaiveDateTime; use sqlx::{Acquire, Executor, Sqlite, SqlitePool, Transaction}; use tokio::sync::mpsc::Sender; -use tracing::{error_span, instrument, warn}; +use tracing::{error, error_span, instrument, warn}; #[derive(Debug, Clone)] struct CalendarObjectRow { @@ -21,21 +23,37 @@ struct CalendarObjectRow { uid: String, } -impl TryFrom for CalendarObject { +impl From for (String, Result) { + fn from(row: CalendarObjectRow) -> Self { + let result = CalendarObject::from_ics(row.ics).inspect(|object| { + if object.get_uid() != row.uid { + warn!( + "Calendar object {}.ics: UID={} and row uid={} do not match", + row.id, + object.get_uid(), + row.uid + ); + } + }); + + (row.id, result) + } +} + +impl TryFrom for (String, CalendarObject) { type Error = rustical_store::Error; - fn try_from(value: CalendarObjectRow) -> Result { - let object = Self::from_ics(value.ics, Some(value.id))?; - if object.get_uid() != value.uid { - return Err(rustical_store::Error::IcalError( - rustical_ical::Error::InvalidData(format!( - "uid={} and UID={} don't match", - value.uid, - object.get_uid() - )), - )); + fn try_from(row: CalendarObjectRow) -> Result { + let object = CalendarObject::from_ics(row.ics)?; + if object.get_uid() != row.uid { + warn!( + "Calendar object {}.ics: UID={} and row uid={} do not match", + row.id, + object.get_uid(), + row.uid + ); } - Ok(object) + Ok((row.id, object)) } } @@ -92,6 +110,7 @@ impl From for Calendar { pub struct SqliteCalendarStore { db: SqlitePool, sender: Sender, + skip_broken: bool, } impl SqliteCalendarStore { @@ -141,11 +160,40 @@ impl SqliteCalendarStore { if let Err(err) = self.sender.try_send(CollectionOperation { topic, data }) { error_span!( "Error trying to send calendar update notification:", - err = format!("{err:?}"), + err = format!("{err}"), ); } } + #[allow(clippy::missing_panics_doc)] + pub async fn validate_objects(&self, principal: &str) -> Result<(), Error> { + let mut success = true; + for calendar in self.get_calendars(principal).await? { + for (object_id, res) in Self::_get_objects(&self.db, principal, &calendar.id).await? { + if let Err(err) = res { + warn!( + "Invalid calendar object found at {principal}/{cal_id}/{object_id}.ics. Error: {err}", + cal_id = calendar.id + ); + success = false; + } + } + } + if !success { + if self.skip_broken { + error!( + "Not all calendar objects are valid. Since data_store.sqlite.skip_broken=true they will be hidden. You are still advised to manually remove or repair the object. If you need help feel free to open up an issue on GitHub." + ); + } else { + error!( + "Not all calendar objects are valid. Since data_store.sqlite.skip_broken=false this causes a panic. Remove or repair the broken objects manually or set data_store.sqlite.skip_broken=false as a temporary solution to ignore the error. If you need help feel free to open up an issue on GitHub." + ); + panic!(); + } + } + Ok(()) + } + /// In the past exports generated objects with invalid VERSION:4.0 /// This repair sets them to VERSION:2.0 #[allow(clippy::missing_panics_doc)] @@ -357,8 +405,8 @@ impl SqliteCalendarStore { async fn _update_calendar<'e, E: Executor<'e, Database = Sqlite>>( executor: E, - principal: String, - id: String, + principal: &str, + id: &str, calendar: Calendar, ) -> Result<(), Error> { let comp_event = calendar.components.contains(&CalendarObjectType::Event); @@ -456,8 +504,8 @@ impl SqliteCalendarStore { executor: E, principal: &str, cal_id: &str, - ) -> Result, Error> { - sqlx::query_as!( + ) -> Result)>, Error> { + Ok(sqlx::query_as!( CalendarObjectRow, "SELECT id, uid, ics FROM calendarobjects WHERE principal = ? AND cal_id = ? AND deleted_at IS NULL", principal, @@ -466,8 +514,8 @@ impl SqliteCalendarStore { .fetch_all(executor) .await.map_err(crate::Error::from)? .into_iter() - .map(std::convert::TryInto::try_into) - .collect() + .map(Into::into) + ) } async fn _calendar_query<'e, E: Executor<'e, Database = Sqlite>>( @@ -475,14 +523,14 @@ impl SqliteCalendarStore { principal: &str, cal_id: &str, query: CalendarQuery, - ) -> Result, Error> { + ) -> Result)>, Error> { // We extend our query interval by one day in each direction since we really don't want to // miss any objects because of timezone differences // I've previously tried NaiveDate::MIN,MAX, but it seems like sqlite cannot handle these let start = query.time_start.map(|start| start - TimeDelta::days(1)); let end = query.time_end.map(|end| end + TimeDelta::days(1)); - sqlx::query_as!( + Ok(sqlx::query_as!( CalendarObjectRow, r"SELECT id, uid, ics FROM calendarobjects WHERE principal = ? AND cal_id = ? AND deleted_at IS NULL @@ -500,8 +548,7 @@ impl SqliteCalendarStore { .await .map_err(crate::Error::from)? .into_iter() - .map(std::convert::TryInto::try_into) - .collect() + .map(Into::into)) } async fn _get_object<'e, E: Executor<'e, Database = Sqlite>>( @@ -511,7 +558,7 @@ impl SqliteCalendarStore { object_id: &str, show_deleted: bool, ) -> Result { - sqlx::query_as!( + let (row_id, object) = sqlx::query_as!( CalendarObjectRow, "SELECT id, uid, ics FROM calendarobjects WHERE (principal, cal_id, id) = (?, ?, ?) AND ((deleted_at IS NULL) OR ?)", principal, @@ -522,7 +569,9 @@ impl SqliteCalendarStore { .fetch_one(executor) .await .map_err(crate::Error::from)? - .try_into() + .try_into()?; + assert_eq!(object_id, row_id); + Ok(object) } #[instrument] @@ -530,23 +579,24 @@ impl SqliteCalendarStore { executor: E, principal: &str, cal_id: &str, + object_id: &str, object: &CalendarObject, overwrite: bool, ) -> Result<(), Error> { - let (object_id, uid, ics) = (object.get_id(), object.get_uid(), object.get_ics()); + let (uid, ics) = (object.get_uid(), object.get_ics()); let first_occurence = object + .get_inner() + .get_inner() .get_first_occurence() - .ok() - .flatten() .as_ref() - .map(CalDateTime::date); + .map(CalDateTime::date_floor); let last_occurence = object + .get_inner() + .get_inner() .get_last_occurence() - .ok() - .flatten() .as_ref() - .map(CalDateTime::date); + .map(CalDateTime::date_ceil); let etag = object.get_etag(); let object_type = object.get_object_type() as u8; @@ -638,7 +688,8 @@ impl SqliteCalendarStore { principal: &str, cal_id: &str, synctoken: i64, - ) -> Result<(Vec, Vec, i64), Error> { + skip_broken: bool, + ) -> Result<(Vec<(String, CalendarObject)>, Vec, i64), Error> { struct Row { object_id: String, synctoken: i64, @@ -665,8 +716,10 @@ impl SqliteCalendarStore { for Row { object_id, .. } in changes { match Self::_get_object(&mut *conn, principal, cal_id, &object_id, false).await { - Ok(object) => objects.push(object), + Ok(object) => objects.push((object_id, object)), Err(rustical_store::Error::NotFound) => deleted_objects.push(object_id), + // Skip broken object + Err(rustical_store::Error::IcalError(_)) if skip_broken => (), Err(err) => return Err(err), } } @@ -705,8 +758,8 @@ impl CalendarStore for SqliteCalendarStore { #[instrument] async fn update_calendar( &self, - principal: String, - id: String, + principal: &str, + id: &str, calendar: Calendar, ) -> Result<(), Error> { Self::_update_calendar(&self.db, principal, id, calendar).await @@ -774,14 +827,23 @@ impl CalendarStore for SqliteCalendarStore { let mut sync_token = None; for object in objects { - Self::_put_object(&mut *tx, &calendar.principal, &calendar.id, &object, false).await?; + let object_id = object.get_uid(); + Self::_put_object( + &mut *tx, + &calendar.principal, + &calendar.id, + object_id, + &object, + false, + ) + .await?; sync_token = Some( Self::log_object_operation( &mut tx, &calendar.principal, &calendar.id, - object.get_id(), + object_id, ChangeOperation::Add, ) .await?, @@ -807,8 +869,17 @@ impl CalendarStore for SqliteCalendarStore { principal: &str, cal_id: &str, query: CalendarQuery, - ) -> Result, Error> { - Self::_calendar_query(&self.db, principal, cal_id, query).await + ) -> Result, Error> { + let objects = Self::_calendar_query(&self.db, principal, cal_id, query).await?; + if self.skip_broken { + Ok(objects + .filter_map(|(id, res)| Some((id, res.ok()?))) + .collect()) + } else { + Ok(objects + .map(|(id, res)| res.map(|obj| (id, obj))) + .collect::, _>>()?) + } } async fn calendar_metadata( @@ -838,8 +909,17 @@ impl CalendarStore for SqliteCalendarStore { &self, principal: &str, cal_id: &str, - ) -> Result, Error> { - Self::_get_objects(&self.db, principal, cal_id).await + ) -> Result, Error> { + let objects = Self::_get_objects(&self.db, principal, cal_id).await?; + if self.skip_broken { + Ok(objects + .filter_map(|(id, res)| Some((id, res.ok()?))) + .collect()) + } else { + Ok(objects + .map(|(id, res)| res.map(|obj| (id, obj))) + .collect::, _>>()?) + } } #[instrument] @@ -856,9 +936,9 @@ impl CalendarStore for SqliteCalendarStore { #[instrument] async fn put_objects( &self, - principal: String, - cal_id: String, - objects: Vec, + principal: &str, + cal_id: &str, + objects: Vec<(String, CalendarObject)>, overwrite: bool, ) -> Result<(), Error> { let mut tx = self @@ -867,25 +947,25 @@ impl CalendarStore for SqliteCalendarStore { .await .map_err(crate::Error::from)?; - let calendar = Self::_get_calendar(&mut *tx, &principal, &cal_id, true).await?; + let calendar = Self::_get_calendar(&mut *tx, principal, cal_id, true).await?; if calendar.subscription_url.is_some() { // We cannot commit an object to a subscription calendar return Err(Error::ReadOnly); } let mut sync_token = None; - for object in objects { + for (object_id, object) in objects { sync_token = Some( Self::log_object_operation( &mut tx, - &principal, - &cal_id, - object.get_id(), + principal, + cal_id, + &object_id, ChangeOperation::Add, ) .await?, ); - Self::_put_object(&mut *tx, &principal, &cal_id, &object, overwrite).await?; + Self::_put_object(&mut *tx, principal, cal_id, &object_id, &object, overwrite).await?; } tx.commit().await.map_err(crate::Error::from)?; @@ -893,9 +973,7 @@ impl CalendarStore for SqliteCalendarStore { if let Some(sync_token) = sync_token { self.send_push_notification( CollectionOperationInfo::Content { sync_token }, - self.get_calendar(&principal, &cal_id, true) - .await? - .push_topic, + self.get_calendar(principal, cal_id, true).await?.push_topic, ); } Ok(()) @@ -963,8 +1041,8 @@ impl CalendarStore for SqliteCalendarStore { principal: &str, cal_id: &str, synctoken: i64, - ) -> Result<(Vec, Vec, i64), Error> { - Self::_sync_changes(&self.db, principal, cal_id, synctoken).await + ) -> Result<(Vec<(String, CalendarObject)>, Vec, i64), Error> { + Self::_sync_changes(&self.db, principal, cal_id, synctoken, self.skip_broken).await } fn is_read_only(&self, _cal_id: &str) -> bool { diff --git a/crates/store_sqlite/src/error.rs b/crates/store_sqlite/src/error.rs index 38e2af8..96e598a 100644 --- a/crates/store_sqlite/src/error.rs +++ b/crates/store_sqlite/src/error.rs @@ -18,7 +18,7 @@ impl From for Error { sqlx::Error::RowNotFound => Self::StoreError(rustical_store::Error::NotFound), sqlx::Error::Database(err) => { if err.is_unique_violation() { - warn!("{err:?}"); + warn!("{err}"); Self::StoreError(rustical_store::Error::AlreadyExists) } else { Self::SqlxError(sqlx::Error::Database(err)) diff --git a/crates/store_sqlite/src/tests/mod.rs b/crates/store_sqlite/src/tests/mod.rs index aee4f21..2f33ead 100644 --- a/crates/store_sqlite/src/tests/mod.rs +++ b/crates/store_sqlite/src/tests/mod.rs @@ -52,8 +52,8 @@ pub async fn test_store_context() -> TestStoreContext { let db = get_test_db().await; TestStoreContext { db: db.clone(), - addr_store: SqliteAddressbookStore::new(db.clone(), send_addr), - cal_store: SqliteCalendarStore::new(db.clone(), send_cal), + addr_store: SqliteAddressbookStore::new(db.clone(), send_addr, false), + cal_store: SqliteCalendarStore::new(db.clone(), send_cal, false), principal_store: SqlitePrincipalStore::new(db.clone()), sub_store: SqliteStore::new(db), } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 4f22346..7614232 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -18,6 +18,7 @@ pub fn cmd_gen_config(_args: GenConfigArgs) -> anyhow::Result<()> { data_store: DataStoreConfig::Sqlite(SqliteDataStoreConfig { db_url: "/var/lib/rustical/db.sqlite3".to_owned(), run_repairs: true, + skip_broken: true, }), tracing: TracingConfig::default(), frontend: FrontendConfig { diff --git a/src/config.rs b/src/config.rs index b559f8b..3a23f66 100644 --- a/src/config.rs +++ b/src/config.rs @@ -28,6 +28,8 @@ pub struct SqliteDataStoreConfig { pub db_url: String, #[serde(default = "default_true")] pub run_repairs: bool, + #[serde(default = "default_true")] + pub skip_broken: bool, } #[derive(Debug, Deserialize, Serialize)] diff --git a/src/integration_tests/caldav/calendar.rs b/src/integration_tests/caldav/calendar.rs index 475fe1d..ae3e005 100644 --- a/src/integration_tests/caldav/calendar.rs +++ b/src/integration_tests/caldav/calendar.rs @@ -8,7 +8,7 @@ use rustical_store::{CalendarMetadata, CalendarStore}; use rustical_store_sqlite::tests::{TestStoreContext, test_store_context}; use tower::ServiceExt; -fn mkcalendar_template( +pub fn mkcalendar_template( CalendarMetadata { displayname, order: _order, diff --git a/src/integration_tests/caldav/calendar_put.rs b/src/integration_tests/caldav/calendar_put.rs new file mode 100644 index 0000000..b99f274 --- /dev/null +++ b/src/integration_tests/caldav/calendar_put.rs @@ -0,0 +1,77 @@ +use axum::body::Body; +use headers::{Authorization, HeaderMapExt}; +use http::{Request, StatusCode}; +use rstest::rstest; +use rustical_store::CalendarMetadata; +use rustical_store_sqlite::tests::{TestStoreContext, test_store_context}; +use tower::ServiceExt; + +use crate::integration_tests::{ + ResponseExtractString, caldav::calendar::mkcalendar_template, get_app, +}; + +#[rstest] +#[tokio::test] +async fn test_put_invalid( + #[from(test_store_context)] + #[future] + context: TestStoreContext, +) { + let context = context.await; + let app = get_app(context.clone()); + + let calendar_meta = CalendarMetadata { + displayname: Some("Calendar".to_string()), + description: Some("Description".to_string()), + color: Some("#00FF00".to_string()), + order: 0, + }; + let (principal, cal_id) = ("user", "calendar"); + let url = format!("/caldav/principal/{principal}/{cal_id}"); + + let mut request = Request::builder() + .method("MKCALENDAR") + .uri(&url) + .body(Body::from(mkcalendar_template(&calendar_meta))) + .unwrap(); + request + .headers_mut() + .typed_insert(Authorization::basic("user", "pass")); + let response = app.clone().oneshot(request).await.unwrap(); + assert_eq!(response.status(), StatusCode::CREATED); + + // Invalid calendar data + let ical = r"BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Example Corp.//CalDAV Client//EN +BEGIN:VEVENT +UID:20010712T182145Z-123401@example.com +DTSTAMP:20060712T182145Z +DTSTART:20060714T170000Z +RRULE:UNTIL=123 +DTEND:20060715T040000Z +SUMMARY:Bastille Day Party +END:VEVENT +END:VCALENDAR"; + + let mut request = Request::builder() + .method("PUT") + .uri(format!("{url}/qwue23489.ics")) + .header("If-None-Match", "*") + .header("Content-Type", "text/calendar") + .body(Body::from(ical)) + .unwrap(); + request + .headers_mut() + .typed_insert(Authorization::basic("user", "pass")); + + let response = app.clone().oneshot(request).await.unwrap(); + assert_eq!(response.status(), StatusCode::PRECONDITION_FAILED); + let body = response.extract_string().await; + insta::assert_snapshot!(body, @r#" + + + + + "#); +} diff --git a/src/integration_tests/caldav/calendar_report.rs b/src/integration_tests/caldav/calendar_report.rs index 879e6f3..02974ff 100644 --- a/src/integration_tests/caldav/calendar_report.rs +++ b/src/integration_tests/caldav/calendar_report.rs @@ -87,70 +87,79 @@ const REPORT_7_8_3: &str = r#" "#; -const OUTPUT_7_8_3: &str = r#" - - http://cal.example.com/bernard/work/abcd2.ics - - - "fffff-abcd2" - BEGIN:VCALENDAR - VERSION:2.0 - PRODID:-//Example Corp.//CalDAV Client//EN - BEGIN:VEVENT - DTSTAMP:20060206T001121Z - DTSTART:20060103T170000 - DURATION:PT1H - RECURRENCE-ID:20060103T170000 - SUMMARY:Event #2 - UID:00959BC664CA650E933C892C@example.com - END:VEVENT - BEGIN:VEVENT - DTSTAMP:20060206T001121Z - DTSTART:20060104T190000 - DURATION:PT1H - RECURRENCE-ID:20060104T170000 - SUMMARY:Event #2 bis - UID:00959BC664CA650E933C892C@example.com - END:VEVENT - END:VCALENDAR - - - HTTP/1.1 200 OK - - - - http://cal.example.com/bernard/work/abcd3.ics - - - "fffff-abcd3" - BEGIN:VCALENDAR - VERSION:2.0 - PRODID:-//Example Corp.//CalDAV Client//EN - BEGIN:VEVENT - ATTENDEE;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:cyrus@example.com - ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:lisa@example.com - DTSTAMP:20060206T001220Z - DTSTART:20060104T150000 - DURATION:PT1H - LAST-MODIFIED:20060206T001330Z - ORGANIZER:mailto:cyrus@example.com - SEQUENCE:1 - STATUS:TENTATIVE - SUMMARY:Event #3 - UID:DC6C50A017428C5216A2F1CD@example.com - X-ABC-GUID:E1CX5Dr-0007ym-Hz@example.com - END:VEVENT - END:VCALENDAR - - - HTTP/1.1 200 OK - -"#; +// Adapted from Example 7.8.3 of RFC 4791 +// In the RFC the output is wrong since it returns DTSTART in UTC as local time, e.g. +// DTSTART:20060103T170000 +// instead of +// DTSTART:20060103T170000Z +// In https://datatracker.ietf.org/doc/html/rfc4791#section-9.6.5 +// it is clearly stated that times with timezone information MUST be returned in UTC. +// Also, the RECURRENCE-ID needs to include the TIMEZONE, which is fixed here by converting it to +// UTC +const OUTPUT_7_8_3: &str = r#" + + + /caldav/principal/user/calendar/abcd2.ics + + + BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Example Corp.//CalDAV Client//EN +BEGIN:VEVENT +DTSTAMP:20060206T001121Z +DTSTART:20060103T170000Z +DURATION:PT1H +SUMMARY:Event #2 +UID:abcd2 +RECURRENCE-ID:20060103T170000Z +END:VEVENT +BEGIN:VEVENT +DTSTAMP:20060206T001121Z +DTSTART:20060104T190000Z +DURATION:PT1H +RECURRENCE-ID:20060104T170000Z +SUMMARY:Event #2 bis +UID:abcd2 +END:VEVENT +END:VCALENDAR + + + HTTP/1.1 200 OK + + + + /caldav/principal/user/calendar/abcd3.ics + + + BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Example Corp.//CalDAV Client//EN +BEGIN:VEVENT +ATTENDEE;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:cyrus@example.com +ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:lisa@example.com +DTSTAMP:20060206T001220Z +DTSTART:20060104T150000Z +DURATION:PT1H +LAST-MODIFIED:20060206T001330Z +ORGANIZER:mailto:cyrus@example.com +SEQUENCE:1 +STATUS:TENTATIVE +SUMMARY:Event #3 +UID:abcd3 +X-ABC-GUID:E1CX5Dr-0007ym-Hz@example.com +END:VEVENT +END:VCALENDAR + + + HTTP/1.1 200 OK + + +"#; #[rstest] -#[case(0, ICS_1, REPORT_7_8_1)] -#[case(1, ICS_1, REPORT_7_8_2)] -#[case(2, ICS_1, REPORT_7_8_3)] +#[case(0, ICS_1, REPORT_7_8_1, None)] +#[case(1, ICS_1, REPORT_7_8_2, None)] +#[case(2, ICS_1, REPORT_7_8_3, Some(OUTPUT_7_8_3))] #[tokio::test] async fn test_report( #[from(test_store_context)] @@ -159,6 +168,7 @@ async fn test_report( #[case] case: usize, #[case] ics: &'static str, #[case] report: &'static str, + #[case] output: Option<&'static str>, ) { let context = context.await; let app = get_app(context.clone()); @@ -193,4 +203,7 @@ async fn test_report( assert_eq!(response.status(), StatusCode::MULTI_STATUS); let body = response.extract_string().await; insta::assert_snapshot!(format!("{case}_report_body"), body); + if let Some(output) = output { + similar_asserts::assert_eq!(output, body.replace('\r', "")); + } } diff --git a/src/integration_tests/caldav/mod.rs b/src/integration_tests/caldav/mod.rs index 4a91cd7..5d7d532 100644 --- a/src/integration_tests/caldav/mod.rs +++ b/src/integration_tests/caldav/mod.rs @@ -9,6 +9,7 @@ use tower::ServiceExt; mod calendar; mod calendar_import; +mod calendar_put; mod calendar_report; #[rstest] diff --git a/src/integration_tests/caldav/resources/rfc4791_appb.ics b/src/integration_tests/caldav/resources/rfc4791_appb.ics index ea74a96..4ab2a03 100644 --- a/src/integration_tests/caldav/resources/rfc4791_appb.ics +++ b/src/integration_tests/caldav/resources/rfc4791_appb.ics @@ -55,6 +55,7 @@ SEQUENCE:1 STATUS:TENTATIVE SUMMARY:Event #3 UID:abcd3 +X-ABC-GUID:E1CX5Dr-0007ym-Hz@example.com END:VEVENT BEGIN:VTODO DTSTAMP:20060205T235335Z diff --git a/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar__get_body.snap b/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar__get_body.snap index d0fc17b..b336d9c 100644 --- a/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar__get_body.snap +++ b/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar__get_body.snap @@ -4,9 +4,10 @@ expression: body --- BEGIN:VCALENDAR VERSION:2.0 +PRODID:RustiCal Export CALSCALE:GREGORIAN -PRODID:RustiCal X-WR-CALNAME:Calendar X-WR-CALDESC:Description +X-WR-CALCOLOR:#00FF00 X-WR-TIMEZONE:US/Eastern END:VCALENDAR diff --git a/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_import__0_get_body.snap b/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_import__0_get_body.snap index 7dc2838..c031a57 100644 --- a/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_import__0_get_body.snap +++ b/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_import__0_get_body.snap @@ -4,8 +4,8 @@ expression: body --- BEGIN:VCALENDAR VERSION:2.0 +PRODID:RustiCal Export CALSCALE:GREGORIAN -PRODID:RustiCal BEGIN:VEVENT UID:[UID] SUMMARY:One-off Meeting diff --git a/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_import__1_get_body.snap b/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_import__1_get_body.snap index 8b15580..059168d 100644 --- a/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_import__1_get_body.snap +++ b/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_import__1_get_body.snap @@ -4,8 +4,8 @@ expression: body --- BEGIN:VCALENDAR VERSION:2.0 +PRODID:RustiCal Export CALSCALE:GREGORIAN -PRODID:RustiCal BEGIN:VTIMEZONE LAST-MODIFIED:20040110T032845Z TZID:US/Eastern @@ -29,7 +29,7 @@ DTSTAMP:20060206T001102Z DTSTART;TZID=US/Eastern:20060102T100000 DURATION:PT1H SUMMARY:Event #1 -Description:Go Steelers! +DESCRIPTION:Go Steelers! UID:[UID] END:VEVENT BEGIN:VEVENT @@ -60,6 +60,7 @@ SEQUENCE:1 STATUS:TENTATIVE SUMMARY:Event #3 UID:[UID] +X-ABC-GUID:[UID] END:VEVENT BEGIN:VTODO DTSTAMP:20060205T235335Z diff --git a/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_report__0_report_body.snap b/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_report__0_report_body.snap index b179127..79834cc 100644 --- a/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_report__0_report_body.snap +++ b/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_report__0_report_body.snap @@ -56,7 +56,7 @@ END:VCALENDAR /caldav/principal/user/calendar/abcd3.ics - "c6a5b1cf6985805686df99e7f2e1cf286567dcb3383fc6fa1b12ce42d3fbc01c" + "a84fd022dfc742bf8f17ac04fca3aad687e9ae724180185e8e0df11e432dae30" BEGIN:VCALENDAR VERSION:2.0 PRODID:-//Example Corp.//CalDAV Client//EN @@ -90,6 +90,7 @@ SEQUENCE:1 STATUS:TENTATIVE SUMMARY:Event #3 UID:abcd3 +X-ABC-GUID:E1CX5Dr-0007ym-Hz@example.com END:VEVENT END:VCALENDAR diff --git a/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_report__1_report_body.snap b/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_report__1_report_body.snap index f3e413b..09add03 100644 --- a/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_report__1_report_body.snap +++ b/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_report__1_report_body.snap @@ -88,6 +88,7 @@ SEQUENCE:1 STATUS:TENTATIVE SUMMARY:Event #3 UID:abcd3 +X-ABC-GUID:E1CX5Dr-0007ym-Hz@example.com END:VEVENT END:VCALENDAR diff --git a/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_report__2_report_body.snap b/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_report__2_report_body.snap index 9801e29..0ae78e9 100644 --- a/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_report__2_report_body.snap +++ b/src/integration_tests/caldav/snapshots/rustical__integration_tests__caldav__calendar_report__2_report_body.snap @@ -13,19 +13,19 @@ VERSION:2.0 PRODID:-//Example Corp.//CalDAV Client//EN BEGIN:VEVENT DTSTAMP:20060206T001121Z +DTSTART:20060103T170000Z DURATION:PT1H SUMMARY:Event #2 UID:abcd2 RECURRENCE-ID:20060103T170000Z -DTSTART:20060103T170000Z END:VEVENT BEGIN:VEVENT DTSTAMP:20060206T001121Z +DTSTART:20060104T190000Z DURATION:PT1H -SUMMARY:Event #2 -UID:abcd2 RECURRENCE-ID:20060104T170000Z -DTSTART:20060104T170000Z +SUMMARY:Event #2 bis +UID:abcd2 END:VEVENT END:VCALENDAR @@ -44,7 +44,7 @@ BEGIN:VEVENT ATTENDEE;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:cyrus@example.com ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:lisa@example.com DTSTAMP:20060206T001220Z -DTSTART;TZID=US/Eastern:20060104T100000 +DTSTART:20060104T150000Z DURATION:PT1H LAST-MODIFIED:20060206T001330Z ORGANIZER:mailto:cyrus@example.com @@ -52,6 +52,7 @@ SEQUENCE:1 STATUS:TENTATIVE SUMMARY:Event #3 UID:abcd3 +X-ABC-GUID:E1CX5Dr-0007ym-Hz@example.com END:VEVENT END:VCALENDAR diff --git a/src/integration_tests/carddav/snapshots/rustical__integration_tests__carddav__addressbook__multiget_body.snap b/src/integration_tests/carddav/snapshots/rustical__integration_tests__carddav__addressbook__multiget_body.snap index 147559f..11e19dd 100644 --- a/src/integration_tests/carddav/snapshots/rustical__integration_tests__carddav__addressbook__multiget_body.snap +++ b/src/integration_tests/carddav/snapshots/rustical__integration_tests__carddav__addressbook__multiget_body.snap @@ -8,7 +8,7 @@ expression: body /carddav/principal/user/contacts/newcard.vcf - "24835b6c11816c864f9edadd4c7c296234c643892afcbbc5fbf5c9b7ac935cf8" + "ea0bf4a2ce7ef84606a4cf9235776dbc11b3e7ce351ddf35f27cbc0088acca7e" BEGIN:VCARD VERSION:3.0 FN:Cyrus Daboo diff --git a/src/main.rs b/src/main.rs index b29766a..ae7896d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,9 +34,6 @@ mod config; pub mod integration_tests; mod setup_tracing; -mod migration_0_12; -use migration_0_12::{validate_address_objects_0_12, validate_calendar_objects_0_12}; - #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] struct Args { @@ -73,13 +70,18 @@ async fn get_data_stores( DataStoreConfig::Sqlite(SqliteDataStoreConfig { db_url, run_repairs, + skip_broken, }) => { let db = create_db_pool(db_url, migrate).await?; // Channel to watch for changes (for DAV Push) let (send, recv) = tokio::sync::mpsc::channel(1000); - let addressbook_store = Arc::new(SqliteAddressbookStore::new(db.clone(), send.clone())); - let cal_store = Arc::new(SqliteCalendarStore::new(db.clone(), send)); + let addressbook_store = Arc::new(SqliteAddressbookStore::new( + db.clone(), + send.clone(), + *skip_broken, + )); + let cal_store = Arc::new(SqliteCalendarStore::new(db.clone(), send, *skip_broken)); if *run_repairs { info!("Running repair tasks"); addressbook_store.repair_orphans().await?; @@ -88,6 +90,13 @@ async fn get_data_stores( } let subscription_store = Arc::new(SqliteStore::new(db.clone())); let principal_store = Arc::new(SqlitePrincipalStore::new(db)); + + // Validate all calendar objects + for principal in principal_store.get_principals().await? { + cal_store.validate_objects(&principal.id).await?; + addressbook_store.validate_objects(&principal.id).await?; + } + ( addressbook_store, cal_store, @@ -125,14 +134,6 @@ async fn main() -> Result<()> { let (addr_store, cal_store, subscription_store, principal_store, update_recv) = get_data_stores(!args.no_migrations, &config.data_store).await?; - warn!( - "Validating calendar data against the next-version ical parser. -In the next major release these will be rejected and cause errors. -If any errors occur, please open an issue so they can be fixed before the next major release." - ); - validate_calendar_objects_0_12(principal_store.as_ref(), cal_store.as_ref()).await?; - validate_address_objects_0_12(principal_store.as_ref(), addr_store.as_ref()).await?; - let mut tasks = vec![]; if config.dav_push.enabled { diff --git a/src/migration_0_12.rs b/src/migration_0_12.rs deleted file mode 100644 index 2130220..0000000 --- a/src/migration_0_12.rs +++ /dev/null @@ -1,81 +0,0 @@ -use rustical_store::{AddressbookStore, CalendarStore, auth::AuthenticationProvider}; -use tracing::{error, info}; - -pub async fn validate_calendar_objects_0_12( - principal_store: &impl AuthenticationProvider, - cal_store: &impl CalendarStore, -) -> Result<(), rustical_store::Error> { - let mut success = true; - for principal in principal_store.get_principals().await? { - for calendar in cal_store.get_calendars(&principal.id).await? { - for object in cal_store - .get_objects(&calendar.principal, &calendar.id) - .await? - { - if let Err(err) = ical_dev::parser::ical::IcalObjectParser::from_slice( - object.get_ics().as_bytes(), - ) - .expect_one() - { - success = false; - error!( - "An error occured parsing a calendar object: principal={principal}, calendar={calendar}, object_id={object_id}: {err}", - principal = principal.id, - calendar = calendar.id, - object_id = object.get_id() - ); - println!("{}", object.get_ics()); - } - } - } - } - if success { - info!("Your calendar data seems to be valid in the next major version."); - } else { - error!( - "Not all calendar objects will be successfully parsed in the next major version (v0.12). -This will not cause issues in this version, but please comment under the tracking issue on GitHub: -https://github.com/lennart-k/rustical/issues/165" - ); - } - Ok(()) -} - -pub async fn validate_address_objects_0_12( - principal_store: &impl AuthenticationProvider, - addr_store: &impl AddressbookStore, -) -> Result<(), rustical_store::Error> { - let mut success = true; - for principal in principal_store.get_principals().await? { - for addressbook in addr_store.get_addressbooks(&principal.id).await? { - for object in addr_store - .get_objects(&addressbook.principal, &addressbook.id) - .await? - { - if let Err(err) = - ical_dev::parser::vcard::VcardParser::from_slice(object.get_vcf().as_bytes()) - .expect_one() - { - success = false; - error!( - "An error occured parsing an address object: principal={principal}, addressbook={addressbook}, object_id={object_id}: {err}", - principal = principal.id, - addressbook = addressbook.id, - object_id = object.get_id() - ); - println!("{}", object.get_vcf()); - } - } - } - } - if success { - info!("Your addressbook data seems to be valid in the next major version."); - } else { - error!( - "Not all address objects will be successfully parsed in the next major version (v0.12). -This will not cause issues in this version, but please comment under the tracking issue on GitHub: -https://github.com/lennart-k/rustical/issues/165" - ); - } - Ok(()) -}