caldav: Make supported-calendar-component-set configurable

This commit is contained in:
Lennart
2025-01-19 13:19:46 +01:00
parent 79edfcaa25
commit 3b99508065
14 changed files with 252 additions and 71 deletions

View File

@@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "SELECT principal, id, synctoken, \"order\", displayname, description, color, timezone, timezone_id, deleted_at, subscription_url, push_topic\n FROM calendars\n WHERE (principal, id) = (?, ?)", "query": "SELECT *\n FROM calendars\n WHERE (principal, id) = (?, ?)",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -18,21 +18,21 @@
"ordinal": 2, "ordinal": 2,
"type_info": "Integer" "type_info": "Integer"
}, },
{
"name": "order",
"ordinal": 3,
"type_info": "Integer"
},
{ {
"name": "displayname", "name": "displayname",
"ordinal": 4, "ordinal": 3,
"type_info": "Text" "type_info": "Text"
}, },
{ {
"name": "description", "name": "description",
"ordinal": 5, "ordinal": 4,
"type_info": "Text" "type_info": "Text"
}, },
{
"name": "order",
"ordinal": 5,
"type_info": "Integer"
},
{ {
"name": "color", "name": "color",
"ordinal": 6, "ordinal": 6,
@@ -62,6 +62,21 @@
"name": "push_topic", "name": "push_topic",
"ordinal": 11, "ordinal": 11,
"type_info": "Text" "type_info": "Text"
},
{
"name": "comp_event",
"ordinal": 12,
"type_info": "Bool"
},
{
"name": "comp_todo",
"ordinal": 13,
"type_info": "Bool"
},
{
"name": "comp_journal",
"ordinal": 14,
"type_info": "Bool"
} }
], ],
"parameters": { "parameters": {
@@ -71,16 +86,19 @@
false, false,
false, false,
false, false,
true,
true,
false, false,
true, true,
true, true,
true, true,
true, true,
true, true,
true, false,
true, false,
false,
false false
] ]
}, },
"hash": "23bc337c3f74466be1d136218cf9780e4f32af800dfb75d1442f3e68dd3412de" "hash": "9f930775043a6d4571a8ffd5a981cadf7c51f3f11a189f8461505abec31076e6"
} }

View File

@@ -1,12 +0,0 @@
{
"db_name": "SQLite",
"query": "INSERT INTO calendars (principal, id, displayname, description, \"order\", color, timezone, timezone_id, push_topic)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
"describe": {
"columns": [],
"parameters": {
"Right": 9
},
"nullable": []
},
"hash": "c0d889d0738797185b10ec3e5134b804579ad4300fd8227b0d6f307281f25111"
}

View File

@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO calendars (principal, id, displayname, description, \"order\", color, timezone, timezone_id, push_topic, comp_event, comp_todo, comp_journal)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
"describe": {
"columns": [],
"parameters": {
"Right": 12
},
"nullable": []
},
"hash": "c4134652b1efb1dda36fb59827bf9cfee6be5bddfd352f1da4e37c6b6aa0fa7a"
}

View File

@@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "SELECT principal, id, synctoken, displayname, \"order\", description, color, timezone, timezone_id, deleted_at, subscription_url, push_topic\n FROM calendars\n WHERE principal = ? AND deleted_at IS NOT NULL", "query": "SELECT *\n FROM calendars\n WHERE principal = ? AND deleted_at IS NOT NULL",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -24,14 +24,14 @@
"type_info": "Text" "type_info": "Text"
}, },
{ {
"name": "order", "name": "description",
"ordinal": 4, "ordinal": 4,
"type_info": "Integer" "type_info": "Text"
}, },
{ {
"name": "description", "name": "order",
"ordinal": 5, "ordinal": 5,
"type_info": "Text" "type_info": "Integer"
}, },
{ {
"name": "color", "name": "color",
@@ -62,6 +62,21 @@
"name": "push_topic", "name": "push_topic",
"ordinal": 11, "ordinal": 11,
"type_info": "Text" "type_info": "Text"
},
{
"name": "comp_event",
"ordinal": 12,
"type_info": "Bool"
},
{
"name": "comp_todo",
"ordinal": 13,
"type_info": "Bool"
},
{
"name": "comp_journal",
"ordinal": 14,
"type_info": "Bool"
} }
], ],
"parameters": { "parameters": {
@@ -72,15 +87,18 @@
false, false,
false, false,
true, true,
true,
false, false,
true, true,
true, true,
true, true,
true, true,
true, true,
true, false,
false,
false,
false false
] ]
}, },
"hash": "ad596abc6b3826251bdf640ece558fde44fcbd4f7d27c283658de38330bef095" "hash": "cce62f7829bd688cd8c7928b587bc31f0e50865c214b1df113350bea2c254237"
} }

View File

@@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "SELECT principal, id, synctoken, displayname, \"order\", description, color, timezone, timezone_id, deleted_at, subscription_url, push_topic\n FROM calendars\n WHERE principal = ? AND deleted_at IS NULL", "query": "SELECT *\n FROM calendars\n WHERE principal = ? AND deleted_at IS NULL",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -24,14 +24,14 @@
"type_info": "Text" "type_info": "Text"
}, },
{ {
"name": "order", "name": "description",
"ordinal": 4, "ordinal": 4,
"type_info": "Integer" "type_info": "Text"
}, },
{ {
"name": "description", "name": "order",
"ordinal": 5, "ordinal": 5,
"type_info": "Text" "type_info": "Integer"
}, },
{ {
"name": "color", "name": "color",
@@ -62,6 +62,21 @@
"name": "push_topic", "name": "push_topic",
"ordinal": 11, "ordinal": 11,
"type_info": "Text" "type_info": "Text"
},
{
"name": "comp_event",
"ordinal": 12,
"type_info": "Bool"
},
{
"name": "comp_todo",
"ordinal": 13,
"type_info": "Bool"
},
{
"name": "comp_journal",
"ordinal": 14,
"type_info": "Bool"
} }
], ],
"parameters": { "parameters": {
@@ -72,15 +87,18 @@
false, false,
false, false,
true, true,
true,
false, false,
true, true,
true, true,
true, true,
true, true,
true, true,
true, false,
false,
false,
false false
] ]
}, },
"hash": "6d32ecdc8dbd73ba917f2e5f89df445ac48f7e438f1d99519dad49a127399971" "hash": "cedfb82b38fdd0c7681b9873b1008abee4a2f4ca16abad1b837f256d0bf416b1"
} }

View File

@@ -1,12 +1,12 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "UPDATE calendars SET principal = ?, id = ?, displayname = ?, description = ?, \"order\" = ?, color = ?, timezone = ?, timezone_id = ?, push_topic = ?\n WHERE (principal, id) = (?, ?)", "query": "UPDATE calendars SET principal = ?, id = ?, displayname = ?, description = ?, \"order\" = ?, color = ?, timezone = ?, timezone_id = ?, push_topic = ?, comp_event = ?, comp_todo = ?, comp_journal = ?\n WHERE (principal, id) = (?, ?)",
"describe": { "describe": {
"columns": [], "columns": [],
"parameters": { "parameters": {
"Right": 11 "Right": 14
}, },
"nullable": [] "nullable": []
}, },
"hash": "3ad4ff83b0317ee316f6e6d589d77a6f509d868de28d5b2b3724d66059223aaa" "hash": "d65c9c40606e59dd816a51b9b9ac60fd2ff81aaa358fcc038134e9a68ba45ad7"
} }

View File

@@ -1,7 +1,9 @@
use crate::calendar::prop::SupportedCalendarComponentSet;
use crate::Error; use crate::Error;
use actix_web::web::{Data, Path}; use actix_web::web::{Data, Path};
use actix_web::HttpResponse; use actix_web::HttpResponse;
use rustical_store::auth::User; use rustical_store::auth::User;
use rustical_store::calendar::CalendarObjectType;
use rustical_store::{Calendar, CalendarStore}; use rustical_store::{Calendar, CalendarStore};
use rustical_xml::{Unparsed, XmlDeserialize, XmlDocument, XmlRootTag}; use rustical_xml::{Unparsed, XmlDeserialize, XmlDocument, XmlRootTag};
use tracing::instrument; use tracing::instrument;
@@ -24,6 +26,8 @@ pub struct MkcolCalendarProp {
#[xml(ns = "rustical_dav::namespace::NS_DAV")] #[xml(ns = "rustical_dav::namespace::NS_DAV")]
#[allow(dead_code)] #[allow(dead_code)]
resourcetype: Unparsed, resourcetype: Unparsed,
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
supported_calendar_component_set: Option<SupportedCalendarComponentSet>,
} }
#[derive(XmlDeserialize, Clone, Debug)] #[derive(XmlDeserialize, Clone, Debug)]
@@ -69,6 +73,14 @@ pub async fn route_mkcalendar<C: CalendarStore>(
synctoken: 0, synctoken: 0,
subscription_url: None, subscription_url: None,
push_topic: uuid::Uuid::new_v4().to_string(), push_topic: uuid::Uuid::new_v4().to_string(),
components: request
.supported_calendar_component_set
.map(Into::into)
.unwrap_or(vec![
CalendarObjectType::Event,
CalendarObjectType::Todo,
CalendarObjectType::Journal,
]),
}; };
match store.insert_calendar(calendar).await { match store.insert_calendar(calendar).await {
@@ -103,6 +115,11 @@ mod tests {
<CAL:calendar-description>rggg</CAL:calendar-description> <CAL:calendar-description>rggg</CAL:calendar-description>
<n0:calendar-color xmlns:n0="http://apple.com/ns/ical/">#FFF8DCFF</n0:calendar-color> <n0:calendar-color xmlns:n0="http://apple.com/ns/ical/">#FFF8DCFF</n0:calendar-color>
<CAL:calendar-timezone-id>Europe/Berlin</CAL:calendar-timezone-id> <CAL:calendar-timezone-id>Europe/Berlin</CAL:calendar-timezone-id>
<CAL:supported-calendar-component-set>
<CAL:comp name="VEVENT"/>
<CAL:comp name="VTODO"/>
<CAL:comp name="VJOURNAL"/>
</CAL:supported-calendar-component-set>
<CAL:calendar-timezone>BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nTZID:Europe/Berlin\r\nLAST-MODIFIED:20240422T053450Z\r\nTZURL:https://www.tzurl.org/zoneinfo/Europe/Berlin\r\nX-LIC-LOCATION:Europe/Berlin\r\nX-PROLEPTIC-TZNAME:LMT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+005328\r\nTZOFFSETTO:+0100\r\nDTSTART:18930401T000632\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19160430T230000\r\nRDATE:19400401T020000\r\nRDATE:19430329T020000\r\nRDATE:19460414T020000\r\nRDATE:19470406T030000\r\nRDATE:19480418T020000\r\nRDATE:19490410T020000\r\nRDATE:19800406T020000\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19161001T010000\r\nRDATE:19421102T030000\r\nRDATE:19431004T030000\r\nRDATE:19441002T030000\r\nRDATE:19451118T030000\r\nRDATE:19461007T030000\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19170416T020000\r\nRRULE:FREQ=YEARLY;UNTIL=19180415T010000Z;BYMONTH=4;BYDAY=3MO\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19170917T030000\r\nRRULE:FREQ=YEARLY;UNTIL=19180916T010000Z;BYMONTH=9;BYDAY=3MO\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19440403T020000\r\nRRULE:FREQ=YEARLY;UNTIL=19450402T010000Z;BYMONTH=4;BYDAY=1MO\r\nEND:DAYLIGHT\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEMT\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0300\r\nDTSTART:19450524T000000\r\nRDATE:19470511T010000\r\nEND:DAYLIGHT\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0300\r\nTZOFFSETTO:+0200\r\nDTSTART:19450924T030000\r\nRDATE:19470629T030000\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0100\r\nDTSTART:19460101T000000\r\nRDATE:19800101T000000\r\nEND:STANDARD\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19471005T030000\r\nRRULE:FREQ=YEARLY;UNTIL=19491002T010000Z;BYMONTH=10;BYDAY=1SU\r\nEND:STANDARD\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19800928T030000\r\nRRULE:FREQ=YEARLY;UNTIL=19950924T010000Z;BYMONTH=9;BYDAY=-1SU\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19810329T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19961027T030000\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nEND:VCALENDAR\r\n</CAL:calendar-timezone> <CAL:calendar-timezone>BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nTZID:Europe/Berlin\r\nLAST-MODIFIED:20240422T053450Z\r\nTZURL:https://www.tzurl.org/zoneinfo/Europe/Berlin\r\nX-LIC-LOCATION:Europe/Berlin\r\nX-PROLEPTIC-TZNAME:LMT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+005328\r\nTZOFFSETTO:+0100\r\nDTSTART:18930401T000632\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19160430T230000\r\nRDATE:19400401T020000\r\nRDATE:19430329T020000\r\nRDATE:19460414T020000\r\nRDATE:19470406T030000\r\nRDATE:19480418T020000\r\nRDATE:19490410T020000\r\nRDATE:19800406T020000\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19161001T010000\r\nRDATE:19421102T030000\r\nRDATE:19431004T030000\r\nRDATE:19441002T030000\r\nRDATE:19451118T030000\r\nRDATE:19461007T030000\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19170416T020000\r\nRRULE:FREQ=YEARLY;UNTIL=19180415T010000Z;BYMONTH=4;BYDAY=3MO\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19170917T030000\r\nRRULE:FREQ=YEARLY;UNTIL=19180916T010000Z;BYMONTH=9;BYDAY=3MO\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19440403T020000\r\nRRULE:FREQ=YEARLY;UNTIL=19450402T010000Z;BYMONTH=4;BYDAY=1MO\r\nEND:DAYLIGHT\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEMT\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0300\r\nDTSTART:19450524T000000\r\nRDATE:19470511T010000\r\nEND:DAYLIGHT\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0300\r\nTZOFFSETTO:+0200\r\nDTSTART:19450924T030000\r\nRDATE:19470629T030000\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0100\r\nDTSTART:19460101T000000\r\nRDATE:19800101T000000\r\nEND:STANDARD\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19471005T030000\r\nRRULE:FREQ=YEARLY;UNTIL=19491002T010000Z;BYMONTH=10;BYDAY=1SU\r\nEND:STANDARD\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19800928T030000\r\nRRULE:FREQ=YEARLY;UNTIL=19950924T010000Z;BYMONTH=9;BYDAY=-1SU\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19810329T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19961027T030000\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nEND:VCALENDAR\r\n</CAL:calendar-timezone>
</prop> </prop>
</set> </set>

View File

@@ -1,26 +1,40 @@
use derive_more::derive::From; use derive_more::derive::{From, Into};
use rustical_xml::XmlSerialize; use rustical_store::calendar::CalendarObjectType;
use rustical_xml::{XmlDeserialize, XmlSerialize};
#[derive(Debug, Clone, XmlSerialize, PartialEq, From)] #[derive(Debug, Clone, XmlSerialize, XmlDeserialize, PartialEq, From, Into)]
pub struct SupportedCalendarComponent { pub struct SupportedCalendarComponent {
#[xml(ty = "attr")] #[xml(ty = "attr")]
pub name: &'static str, pub name: CalendarObjectType,
} }
#[derive(Debug, Clone, XmlSerialize, PartialEq)] #[derive(Debug, Clone, XmlSerialize, XmlDeserialize, PartialEq)]
pub struct SupportedCalendarComponentSet { pub struct SupportedCalendarComponentSet {
#[xml(ns = "rustical_dav::namespace::NS_CALDAV", flatten)] #[xml(ns = "rustical_dav::namespace::NS_CALDAV", flatten)]
pub comp: Vec<SupportedCalendarComponent>, pub comp: Vec<SupportedCalendarComponent>,
} }
impl Default for SupportedCalendarComponentSet { impl From<Vec<CalendarObjectType>> for SupportedCalendarComponentSet {
fn default() -> Self { fn from(value: Vec<CalendarObjectType>) -> Self {
Self { Self {
comp: vec!["VEVENT".into(), "VTODO".into(), "VJOURNAL".into()], comp: value
.into_iter()
.map(SupportedCalendarComponent::from)
.collect(),
} }
} }
} }
impl From<SupportedCalendarComponentSet> for Vec<CalendarObjectType> {
fn from(value: SupportedCalendarComponentSet) -> Self {
value
.comp
.into_iter()
.map(CalendarObjectType::from)
.collect()
}
}
#[derive(Debug, Clone, XmlSerialize, PartialEq)] #[derive(Debug, Clone, XmlSerialize, PartialEq)]
pub struct CalendarData { pub struct CalendarData {
#[xml(ty = "attr")] #[xml(ty = "attr")]

View File

@@ -145,9 +145,7 @@ impl Resource for CalendarResource {
CalendarProp::CalendarOrder(Some(self.cal.order)) CalendarProp::CalendarOrder(Some(self.cal.order))
} }
CalendarPropName::SupportedCalendarComponentSet => { CalendarPropName::SupportedCalendarComponentSet => {
CalendarProp::SupportedCalendarComponentSet( CalendarProp::SupportedCalendarComponentSet(self.cal.components.clone().into())
SupportedCalendarComponentSet::default(),
)
} }
CalendarPropName::SupportedCalendarData => { CalendarPropName::SupportedCalendarData => {
CalendarProp::SupportedCalendarData(SupportedCalendarData::default()) CalendarProp::SupportedCalendarData(SupportedCalendarData::default())
@@ -214,8 +212,9 @@ impl Resource for CalendarResource {
self.cal.order = order.unwrap_or_default(); self.cal.order = order.unwrap_or_default();
Ok(()) Ok(())
} }
CalendarProp::SupportedCalendarComponentSet(_) => { CalendarProp::SupportedCalendarComponentSet(comp_set) => {
Err(rustical_dav::Error::PropReadOnly) self.cal.components = comp_set.into();
Ok(())
} }
CalendarProp::SupportedCalendarData(_) => Err(rustical_dav::Error::PropReadOnly), CalendarProp::SupportedCalendarData(_) => Err(rustical_dav::Error::PropReadOnly),
CalendarProp::MaxResourceSize(_) => Err(rustical_dav::Error::PropReadOnly), CalendarProp::MaxResourceSize(_) => Err(rustical_dav::Error::PropReadOnly),

View File

@@ -1,3 +1,4 @@
use super::CalendarObjectType;
use crate::synctoken::format_synctoken; use crate::synctoken::format_synctoken;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde::Serialize; use serde::Serialize;
@@ -16,6 +17,7 @@ pub struct Calendar {
pub synctoken: i64, pub synctoken: i64,
pub subscription_url: Option<String>, pub subscription_url: Option<String>,
pub push_topic: String, pub push_topic: String,
pub components: Vec<CalendarObjectType>,
} }
impl Calendar { impl Calendar {

View File

@@ -2,10 +2,11 @@ use super::{CalDateTime, EventObject, JournalObject, TodoObject};
use crate::Error; use crate::Error;
use anyhow::Result; use anyhow::Result;
use ical::parser::{ical::component::IcalTimeZone, Component}; use ical::parser::{ical::component::IcalTimeZone, Component};
use serde::Serialize;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use std::{collections::HashMap, io::BufReader}; use std::{collections::HashMap, io::BufReader};
#[derive(Debug, Clone)] #[derive(Debug, Clone, Serialize, PartialEq, Eq)]
// specified in https://datatracker.ietf.org/doc/html/rfc5545#section-3.6 // specified in https://datatracker.ietf.org/doc/html/rfc5545#section-3.6
pub enum CalendarObjectType { pub enum CalendarObjectType {
Event = 0, Event = 0,
@@ -13,6 +14,31 @@ pub enum CalendarObjectType {
Journal = 2, Journal = 2,
} }
impl rustical_xml::ValueSerialize for CalendarObjectType {
fn serialize(&self) -> String {
match self {
CalendarObjectType::Event => "VEVENT",
CalendarObjectType::Todo => "VTODO",
CalendarObjectType::Journal => "VJOURNAL",
}
.to_owned()
}
}
impl rustical_xml::ValueDeserialize for CalendarObjectType {
fn deserialize(val: &str) -> std::result::Result<Self, rustical_xml::XmlError> {
match <String as rustical_xml::ValueDeserialize>::deserialize(val)?.as_str() {
"VEVENT" => Ok(Self::Event),
"VTODO" => Ok(Self::Todo),
"VJOURNAL" => Ok(Self::Journal),
_ => Err(rustical_xml::XmlError::Other(format!(
"Invalid value '{}', must be VEVENT, VTODO, or VJOURNAL",
val
))),
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum CalendarObjectComponent { pub enum CalendarObjectComponent {
Event(EventObject), Event(EventObject),

View File

@@ -1,7 +1,8 @@
use std::sync::Arc; use std::sync::Arc;
use crate::{ use crate::{
AddressObject, Addressbook, AddressbookStore, Calendar, CalendarObject, CalendarStore, Error, calendar::CalendarObjectType, AddressObject, Addressbook, AddressbookStore, Calendar,
CalendarObject, CalendarStore, Error,
}; };
use async_trait::async_trait; use async_trait::async_trait;
use derive_more::derive::Constructor; use derive_more::derive::Constructor;
@@ -31,6 +32,7 @@ fn birthday_calendar(addressbook: Addressbook) -> Calendar {
hasher.update(addressbook.push_topic); hasher.update(addressbook.push_topic);
format!("{:x}", hasher.finalize()) format!("{:x}", hasher.finalize())
}, },
components: vec![CalendarObjectType::Event],
} }
} }

View File

@@ -11,6 +11,9 @@ CREATE TABLE calendars (
deleted_at DATETIME, deleted_at DATETIME,
subscription_url TEXT, subscription_url TEXT,
push_topic TEXT UNIQUE NOT NULL, push_topic TEXT UNIQUE NOT NULL,
comp_event BOOLEAN NOT NULL,
comp_todo BOOLEAN NOT NULL,
comp_journal BOOLEAN NOT NULL,
PRIMARY KEY (principal, id) PRIMARY KEY (principal, id)
); );

View File

@@ -1,10 +1,11 @@
use super::ChangeOperation; use super::ChangeOperation;
use async_trait::async_trait; use async_trait::async_trait;
use derive_more::derive::Constructor; use derive_more::derive::Constructor;
use rustical_store::calendar::CalDateTime; use rustical_store::calendar::{CalDateTime, CalendarObjectType};
use rustical_store::synctoken::format_synctoken; use rustical_store::synctoken::format_synctoken;
use rustical_store::{Calendar, CalendarObject, CalendarStore, Error}; use rustical_store::{Calendar, CalendarObject, CalendarStore, Error};
use rustical_store::{CollectionOperation, CollectionOperationType}; use rustical_store::{CollectionOperation, CollectionOperationType};
use sqlx::types::chrono::NaiveDateTime;
use sqlx::{Acquire, Executor, Sqlite, SqlitePool, Transaction}; use sqlx::{Acquire, Executor, Sqlite, SqlitePool, Transaction};
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
use tracing::{error, instrument}; use tracing::{error, instrument};
@@ -23,6 +24,55 @@ impl TryFrom<CalendarObjectRow> for CalendarObject {
} }
} }
#[derive(Debug, Default, Clone)]
struct CalendarRow {
principal: String,
id: String,
displayname: Option<String>,
order: i64,
description: Option<String>,
color: Option<String>,
timezone: Option<String>,
timezone_id: Option<String>,
deleted_at: Option<NaiveDateTime>,
synctoken: i64,
subscription_url: Option<String>,
push_topic: String,
comp_event: bool,
comp_todo: bool,
comp_journal: bool,
}
impl From<CalendarRow> for Calendar {
fn from(value: CalendarRow) -> Self {
let mut components = vec![];
if value.comp_event {
components.push(CalendarObjectType::Event);
}
if value.comp_todo {
components.push(CalendarObjectType::Todo);
}
if value.comp_journal {
components.push(CalendarObjectType::Journal);
}
Self {
principal: value.principal,
id: value.id,
displayname: value.displayname,
order: value.order,
description: value.description,
color: value.color,
timezone: value.timezone,
timezone_id: value.timezone_id,
deleted_at: value.deleted_at,
synctoken: value.synctoken,
subscription_url: value.subscription_url,
push_topic: value.push_topic,
components,
}
}
}
#[derive(Debug, Constructor)] #[derive(Debug, Constructor)]
pub struct SqliteCalendarStore { pub struct SqliteCalendarStore {
db: SqlitePool, db: SqlitePool,
@@ -36,16 +86,17 @@ impl SqliteCalendarStore {
id: &str, id: &str,
) -> Result<Calendar, Error> { ) -> Result<Calendar, Error> {
let cal = sqlx::query_as!( let cal = sqlx::query_as!(
Calendar, CalendarRow,
r#"SELECT principal, id, synctoken, "order", displayname, description, color, timezone, timezone_id, deleted_at, subscription_url, push_topic r#"SELECT *
FROM calendars FROM calendars
WHERE (principal, id) = (?, ?)"#, WHERE (principal, id) = (?, ?)"#,
principal, principal,
id id
) )
.fetch_one(executor) .fetch_one(executor)
.await.map_err(crate::Error::from)?; .await
Ok(cal) .map_err(crate::Error::from)?;
Ok(cal.into())
} }
async fn _get_calendars<'e, E: Executor<'e, Database = Sqlite>>( async fn _get_calendars<'e, E: Executor<'e, Database = Sqlite>>(
@@ -53,15 +104,16 @@ impl SqliteCalendarStore {
principal: &str, principal: &str,
) -> Result<Vec<Calendar>, Error> { ) -> Result<Vec<Calendar>, Error> {
let cals = sqlx::query_as!( let cals = sqlx::query_as!(
Calendar, CalendarRow,
r#"SELECT principal, id, synctoken, displayname, "order", description, color, timezone, timezone_id, deleted_at, subscription_url, push_topic r#"SELECT *
FROM calendars FROM calendars
WHERE principal = ? AND deleted_at IS NULL"#, WHERE principal = ? AND deleted_at IS NULL"#,
principal principal
) )
.fetch_all(executor) .fetch_all(executor)
.await.map_err(crate::Error::from)?; .await
Ok(cals) .map_err(crate::Error::from)?;
Ok(cals.into_iter().map(Calendar::from).collect())
} }
async fn _get_deleted_calendars<'e, E: Executor<'e, Database = Sqlite>>( async fn _get_deleted_calendars<'e, E: Executor<'e, Database = Sqlite>>(
@@ -69,24 +121,29 @@ impl SqliteCalendarStore {
principal: &str, principal: &str,
) -> Result<Vec<Calendar>, Error> { ) -> Result<Vec<Calendar>, Error> {
let cals = sqlx::query_as!( let cals = sqlx::query_as!(
Calendar, CalendarRow,
r#"SELECT principal, id, synctoken, displayname, "order", description, color, timezone, timezone_id, deleted_at, subscription_url, push_topic r#"SELECT *
FROM calendars FROM calendars
WHERE principal = ? AND deleted_at IS NOT NULL"#, WHERE principal = ? AND deleted_at IS NOT NULL"#,
principal principal
) )
.fetch_all(executor) .fetch_all(executor)
.await.map_err(crate::Error::from)?; .await
Ok(cals) .map_err(crate::Error::from)?;
Ok(cals.into_iter().map(Calendar::from).collect())
} }
async fn _insert_calendar<'e, E: Executor<'e, Database = Sqlite>>( async fn _insert_calendar<'e, E: Executor<'e, Database = Sqlite>>(
executor: E, executor: E,
calendar: Calendar, calendar: Calendar,
) -> Result<(), Error> { ) -> Result<(), Error> {
let comp_event = calendar.components.contains(&CalendarObjectType::Event);
let comp_todo = calendar.components.contains(&CalendarObjectType::Todo);
let comp_journal = calendar.components.contains(&CalendarObjectType::Journal);
sqlx::query!( sqlx::query!(
r#"INSERT INTO calendars (principal, id, displayname, description, "order", color, timezone, timezone_id, push_topic) r#"INSERT INTO calendars (principal, id, displayname, description, "order", color, timezone, timezone_id, push_topic, comp_event, comp_todo, comp_journal)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"#, VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"#,
calendar.principal, calendar.principal,
calendar.id, calendar.id,
calendar.displayname, calendar.displayname,
@@ -96,9 +153,11 @@ impl SqliteCalendarStore {
calendar.timezone, calendar.timezone,
calendar.timezone_id, calendar.timezone_id,
calendar.push_topic, calendar.push_topic,
comp_event, comp_todo, comp_journal
) )
.execute(executor) .execute(executor)
.await.map_err(crate::Error::from)?; .await.map_err(crate::Error::from)?;
Ok(()) Ok(())
} }
@@ -108,8 +167,12 @@ impl SqliteCalendarStore {
id: String, id: String,
calendar: Calendar, calendar: Calendar,
) -> Result<(), Error> { ) -> Result<(), Error> {
let comp_event = calendar.components.contains(&CalendarObjectType::Event);
let comp_todo = calendar.components.contains(&CalendarObjectType::Todo);
let comp_journal = calendar.components.contains(&CalendarObjectType::Journal);
let result = sqlx::query!( let result = sqlx::query!(
r#"UPDATE calendars SET principal = ?, id = ?, displayname = ?, description = ?, "order" = ?, color = ?, timezone = ?, timezone_id = ?, push_topic = ? r#"UPDATE calendars SET principal = ?, id = ?, displayname = ?, description = ?, "order" = ?, color = ?, timezone = ?, timezone_id = ?, push_topic = ?, comp_event = ?, comp_todo = ?, comp_journal = ?
WHERE (principal, id) = (?, ?)"#, WHERE (principal, id) = (?, ?)"#,
calendar.principal, calendar.principal,
calendar.id, calendar.id,
@@ -120,6 +183,7 @@ impl SqliteCalendarStore {
calendar.timezone, calendar.timezone,
calendar.timezone_id, calendar.timezone_id,
calendar.push_topic, calendar.push_topic,
comp_event, comp_todo, comp_journal,
principal, principal,
id id
).execute(executor).await.map_err(crate::Error::from)?; ).execute(executor).await.map_err(crate::Error::from)?;