Keep each occurrence of a series sent without its master #36
No files matched your search
@@ -15,6 +15,11 @@ candidates before a series ships (`27.1-rc.1`). The tag is the version with a
|
||||
- `import carddav` takes a card that is in two address books into both.
|
||||
It used to fail on that card on every run. Taking it out of one book, or
|
||||
deleting that book, now leaves it in the other.
|
||||
- `import caldav` keeps every occurrence of a series someone was invited to
|
||||
one occurrence at a time, with no master event. It used to keep the first
|
||||
and drop the rest. `export` now tells such occurrences apart instead of
|
||||
writing them all onto one event, and `export-caldav` writes them as one
|
||||
resource, as CalDAV keeps them.
|
||||
|
||||
## [27.1-rc.3]
|
||||
|
||||
|
||||
@@ -110,6 +110,10 @@ inbuxa-migrate import caldav \
|
||||
```
|
||||
|
||||
Discovers the user's CalDAV principal (or accepts a URL pointing straight at a calendar-home or calendar), then imports calendars and events.
|
||||
A recurring event comes in with its changed occurrences. Someone invited to
|
||||
single occurrences of a series, without the series itself, has each of those
|
||||
occurrences as its own event, and `export`, `export-caldav` and
|
||||
`export-files` write them back the same way.
|
||||
|
||||
### CardDAV
|
||||
|
||||
|
||||
@@ -233,6 +233,31 @@ pub(crate) fn icalendar_text(
|
||||
} else {
|
||||
json!({ "@type": "Group", "entries": [value] })
|
||||
};
|
||||
group_text(group)
|
||||
}
|
||||
|
||||
/// Several events as one iCalendar object, each `(data, data type)`: the
|
||||
/// occurrences of one series that were sent on their own, which a CalDAV
|
||||
/// resource holds together under their UID.
|
||||
pub(crate) fn icalendar_entries_text(
|
||||
conn: &Connection,
|
||||
entries: &[(&str, &str)],
|
||||
) -> Result<String, String> {
|
||||
let mut values = Vec::with_capacity(entries.len());
|
||||
for (data, data_type) in entries {
|
||||
let mut value: Value = serde_json::from_str(data).map_err(|e| e.to_string())?;
|
||||
value
|
||||
.as_object_mut()
|
||||
.ok_or("not a JSON object")?
|
||||
.entry("@type")
|
||||
.or_insert_with(|| json!(data_type));
|
||||
inline_blobs(conn, &mut value, InlineShape::JsCalendarLink)?;
|
||||
values.push(value);
|
||||
}
|
||||
group_text(json!({ "@type": "Group", "entries": values }))
|
||||
}
|
||||
|
||||
fn group_text(group: Value) -> Result<String, String> {
|
||||
let json = group.to_string();
|
||||
let event = JSCalendar::<String, String>::parse(&json)?;
|
||||
Ok(event
|
||||
|
||||
+39
-15
@@ -26,6 +26,16 @@ fn target_uid(v: &Value) -> Option<String> {
|
||||
v.get("uid").and_then(Value::as_str).map(str::to_owned)
|
||||
}
|
||||
|
||||
/// What an object is matched by: its UID, and for one occurrence of a
|
||||
/// series sent on its own (no master event), which occurrence. Several
|
||||
/// such occurrences share a UID and are each their own object.
|
||||
fn match_key(uid: &str, v: &Value) -> String {
|
||||
match v.get("recurrenceId").and_then(Value::as_str) {
|
||||
Some(rid) => format!("{uid} {rid}"),
|
||||
None => uid.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
fn describe(ty: ObjectType, local: i64, uid: &str) -> String {
|
||||
let mut out = String::new();
|
||||
let _ = write!(out, "{} local {local}", ty.jmap_name());
|
||||
@@ -44,10 +54,10 @@ pub fn reconcile(
|
||||
logger: &Logger,
|
||||
) -> Result<Plan, Error> {
|
||||
let targets = target_query_get(net, ty, None).map_err(Error::from)?;
|
||||
let mut by_uid: HashMap<String, (String, &Value)> = HashMap::new();
|
||||
let mut by_key: HashMap<String, (String, &Value)> = HashMap::new();
|
||||
for t in &targets {
|
||||
if let (Some(uid), Some(id)) = (target_uid(t), jid(t)) {
|
||||
by_uid.entry(uid).or_insert((id, t));
|
||||
by_key.entry(match_key(&uid, t)).or_insert((id, t));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,28 +66,32 @@ pub fn reconcile(
|
||||
} else {
|
||||
CALENDAR_EVENT_SELECT
|
||||
};
|
||||
let rows: Vec<(i64, String)> = {
|
||||
// Each row as (local id, match key, uid).
|
||||
let rows: Vec<(i64, String, String)> = {
|
||||
let mut stmt = ctx
|
||||
.conn
|
||||
.prepare(select)
|
||||
.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
stmt.query_map([], |r| {
|
||||
if ty == ObjectType::ContactCard {
|
||||
Ok((r.get::<_, i64>(0)?, r.get::<_, String>(1)?))
|
||||
let uid: String = r.get(1)?;
|
||||
Ok((r.get::<_, i64>(0)?, uid.clone(), uid))
|
||||
} else {
|
||||
let data: String = r.get(4)?;
|
||||
let uid = serde_json::from_str::<Value>(&data)
|
||||
.ok()
|
||||
.and_then(|v| v.get("uid").and_then(Value::as_str).map(str::to_owned))
|
||||
.unwrap_or_default();
|
||||
Ok((r.get::<_, i64>(0)?, uid))
|
||||
let value = serde_json::from_str::<Value>(&data).unwrap_or(Value::Null);
|
||||
let uid = value
|
||||
.get("uid")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or_default()
|
||||
.to_owned();
|
||||
Ok((r.get::<_, i64>(0)?, match_key(&uid, &value), uid))
|
||||
}
|
||||
})
|
||||
.and_then(|m| m.collect::<Result<Vec<_>, _>>())
|
||||
.map_err(|e| Error::Partial(e.to_string()))?
|
||||
};
|
||||
|
||||
let mut matched_uids: HashSet<String> = HashSet::new();
|
||||
let mut matched_keys: HashSet<String> = HashSet::new();
|
||||
let mut updates: Vec<(String, Value)> = Vec::new();
|
||||
let blobs = Uploader::new(net, &ctx.conn);
|
||||
let mut progress = Progress::new(
|
||||
@@ -85,11 +99,11 @@ pub fn reconcile(
|
||||
rows.len() as u64,
|
||||
logger,
|
||||
);
|
||||
for (local, uid) in &rows {
|
||||
for (local, key, uid) in &rows {
|
||||
progress.add(1);
|
||||
if let Some((tid, existing)) = by_uid.get(uid) {
|
||||
if let Some((tid, existing)) = by_key.get(key) {
|
||||
maps.insert(ty, *local, crate::jmap::wire::JmapId(tid.clone()));
|
||||
matched_uids.insert(uid.clone());
|
||||
matched_keys.insert(key.clone());
|
||||
match build_wire(ctx, ty, *local, maps, &blobs) {
|
||||
Ok(wire) => match changed_properties(&wire, existing) {
|
||||
Some(patch) => updates.push((tid.clone(), patch)),
|
||||
@@ -149,10 +163,10 @@ pub fn reconcile(
|
||||
.iter()
|
||||
.filter_map(|t| {
|
||||
let id = jid(t)?;
|
||||
let uid = target_uid(t);
|
||||
let key = target_uid(t).map(|uid| match_key(&uid, t));
|
||||
Some(TargetObj {
|
||||
id: id.clone(),
|
||||
matched: uid.map(|u| matched_uids.contains(&u)).unwrap_or(false),
|
||||
matched: key.map(|k| matched_keys.contains(&k)).unwrap_or(false),
|
||||
protected: false,
|
||||
may_delete: true,
|
||||
parent: None,
|
||||
@@ -230,6 +244,16 @@ mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn occurrences_sharing_a_uid_are_matched_apart() {
|
||||
let week1 = json!({"uid": "s", "recurrenceId": "2024-03-04T09:00:00"});
|
||||
let week2 = json!({"uid": "s", "recurrenceId": "2024-03-11T09:00:00"});
|
||||
let series = json!({"uid": "s", "recurrenceRule": {"frequency": "weekly"}});
|
||||
assert_ne!(match_key("s", &week1), match_key("s", &week2));
|
||||
assert_ne!(match_key("s", &week1), match_key("s", &series));
|
||||
assert_eq!(match_key("s", &series), "s");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unchanged_object_needs_no_update() {
|
||||
let wire = json!({"uid": "u", "name": {"full": "Ann"}, "addressBookIds": {"A": true}});
|
||||
|
||||
+64
-2
@@ -21,7 +21,9 @@ use crate::dav::discover::{DavKind, discover_home};
|
||||
use crate::dav::href::{Href, absolute_url, normalise};
|
||||
use crate::dav::xml;
|
||||
use crate::error::Error;
|
||||
use crate::export_files::cards::{Collection, icalendar_text, read_collections, vcard_text};
|
||||
use crate::export_files::cards::{
|
||||
Collection, icalendar_entries_text, icalendar_text, read_collections, vcard_text,
|
||||
};
|
||||
use crate::jmap::error::JmapError;
|
||||
use crate::jmap::http::RetryPolicy;
|
||||
use crate::logging::{LEVEL_PROGRESS, Logger};
|
||||
@@ -77,6 +79,9 @@ struct Item {
|
||||
uid: String,
|
||||
text: String,
|
||||
collections: Vec<i64>,
|
||||
/// An event's `(data, data type)`, for writing it with the other
|
||||
/// occurrences that share its UID.
|
||||
event: Option<(String, String)>,
|
||||
}
|
||||
|
||||
/// id, uid (cards), collection ids, data, data type (events).
|
||||
@@ -121,10 +126,12 @@ fn read_items(
|
||||
counts.failed += 1;
|
||||
continue;
|
||||
};
|
||||
let event = data_type.map(|t| (data, t));
|
||||
out.push(Item {
|
||||
uid,
|
||||
text,
|
||||
collections: serde_json::from_str(&collections).unwrap_or_default(),
|
||||
event,
|
||||
});
|
||||
}
|
||||
Ok(out)
|
||||
@@ -412,7 +419,26 @@ pub fn run(common: CommonConfig, config: ExportDavConfig) -> Result<Summary, Err
|
||||
items: HashMap::new(),
|
||||
}
|
||||
};
|
||||
for item in mine {
|
||||
for group in by_uid(&mine) {
|
||||
let merged;
|
||||
let item = match group.as_slice() {
|
||||
[one] => *one,
|
||||
many => {
|
||||
merged = occurrences(&conn, many);
|
||||
match &merged {
|
||||
Ok(item) => item,
|
||||
Err(reason) => {
|
||||
logger.warn(&format!(
|
||||
"{} {}: can't be converted: {reason}",
|
||||
kind.item_type, many[0].uid
|
||||
));
|
||||
item_counts.failed += many.len() as u64;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
let before = item_counts.clone();
|
||||
write_item(
|
||||
&client,
|
||||
kind,
|
||||
@@ -422,6 +448,8 @@ pub fn run(common: CommonConfig, config: ExportDavConfig) -> Result<Summary, Err
|
||||
&mut item_counts,
|
||||
logger,
|
||||
)?;
|
||||
// Counted per row, as the archive holds them.
|
||||
item_counts.repeat_last(&before, group.len() as u64 - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -438,6 +466,40 @@ pub fn run(common: CommonConfig, config: ExportDavConfig) -> Result<Summary, Err
|
||||
})
|
||||
}
|
||||
|
||||
/// Items in archive order, those sharing a UID together: one CalDAV
|
||||
/// resource holds every event with its UID.
|
||||
fn by_uid<'a>(items: &[&'a Item]) -> Vec<Vec<&'a Item>> {
|
||||
let mut groups: Vec<Vec<&Item>> = Vec::new();
|
||||
let mut at: HashMap<&str, usize> = HashMap::new();
|
||||
for item in items {
|
||||
match at.get(item.uid.as_str()) {
|
||||
Some(&i) => groups[i].push(item),
|
||||
None => {
|
||||
at.insert(item.uid.as_str(), groups.len());
|
||||
groups.push(vec![item]);
|
||||
}
|
||||
}
|
||||
}
|
||||
groups
|
||||
}
|
||||
|
||||
/// Occurrences sharing a UID as one item.
|
||||
fn occurrences(conn: &Connection, many: &[&Item]) -> Result<Item, String> {
|
||||
let entries: Vec<(&str, &str)> = many
|
||||
.iter()
|
||||
.filter_map(|i| i.event.as_ref().map(|(d, t)| (d.as_str(), t.as_str())))
|
||||
.collect();
|
||||
if entries.len() != many.len() {
|
||||
return Err("two cards share a UID".to_owned());
|
||||
}
|
||||
Ok(Item {
|
||||
uid: many[0].uid.clone(),
|
||||
text: icalendar_entries_text(conn, &entries)?,
|
||||
collections: Vec::new(),
|
||||
event: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// A new collection under the home, named after the archive's, or `None`
|
||||
/// when the server won't make it.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
|
||||
+128
-49
@@ -226,12 +226,16 @@ fn reconcile_items_generic(
|
||||
ItemKind::ContactCard => tx
|
||||
.execute("DELETE FROM contact_cards WHERE id = ?1", params![local_id])
|
||||
.map(drop),
|
||||
ItemKind::CalendarEvent => tx
|
||||
.execute(
|
||||
"DELETE FROM calendar_events WHERE id = ?1",
|
||||
params![local_id],
|
||||
ItemKind::CalendarEvent => {
|
||||
occurrence_rows(&tx, pcc.run.source_id, pcc.container_local_id, local_id).and_then(
|
||||
|ids| {
|
||||
ids.iter().try_for_each(|id| {
|
||||
tx.execute("DELETE FROM calendar_events WHERE id = ?1", params![id])
|
||||
.map(drop)
|
||||
})
|
||||
},
|
||||
)
|
||||
.map(drop),
|
||||
}
|
||||
};
|
||||
removed.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
tx.commit().map_err(|e| Error::Partial(e.to_string()))?;
|
||||
@@ -483,6 +487,7 @@ fn insert_or_update_tx(
|
||||
let local = match pcc.kind {
|
||||
ItemKind::CalendarEvent => insert_or_update_event(
|
||||
tx,
|
||||
pcc.run.source_id,
|
||||
pcc.container_local_id,
|
||||
existing,
|
||||
raw,
|
||||
@@ -511,63 +516,137 @@ fn insert_or_update_tx(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// One event row as the archive keeps it.
|
||||
struct EventRow {
|
||||
is_draft: bool,
|
||||
use_default_alerts: bool,
|
||||
data: String,
|
||||
data_type: &'static str,
|
||||
}
|
||||
|
||||
/// One CalDAV resource holds one UID: a master event with its overrides,
|
||||
/// which is one row, or, when someone was invited to single occurrences of
|
||||
/// a series without the series itself, those occurrences, a row each. The
|
||||
/// first row is the one `sync_id_dav` names; the others are found by
|
||||
/// calendar and UID among rows no other server item names.
|
||||
fn insert_or_update_event(
|
||||
tx: &rusqlite::Transaction<'_>,
|
||||
source_id: i64,
|
||||
calendar_local: i64,
|
||||
existing: Option<i64>,
|
||||
raw: &str,
|
||||
item_href: &str,
|
||||
logger: Logger,
|
||||
) -> Result<i64, Error> {
|
||||
let entries = calcard::ical_to_jscalendar_entries(raw)
|
||||
let db = |e: rusqlite::Error| Error::Partial(e.to_string());
|
||||
let mut entries = calcard::ical_to_jscalendar_entries(raw)
|
||||
.map_err(|e| Error::Partial(format!("iCalendar parse: {e}")))?;
|
||||
if entries.len() > 1 && logger.enabled(crate::logging::LEVEL_DEFAULT) {
|
||||
logger.warn(&format!(
|
||||
"{item_href}: iCalendar resource has {} entries; storing the first ({}), \
|
||||
others dropped (recurrence overrides on the master are preserved)",
|
||||
entries.len(),
|
||||
entries[0].data_type.as_column(),
|
||||
));
|
||||
let uid_of = |v: &serde_json::Value| v.get("uid").and_then(|u| u.as_str()).map(str::to_owned);
|
||||
let occurrences_only = entries.len() > 1
|
||||
&& entries.iter().all(|e| e.data.get("recurrenceId").is_some())
|
||||
&& entries
|
||||
.iter()
|
||||
.all(|e| uid_of(&e.data) == uid_of(&entries[0].data));
|
||||
if entries.len() > 1 && !occurrences_only {
|
||||
if logger.enabled(crate::logging::LEVEL_DEFAULT) {
|
||||
logger.warn(&format!(
|
||||
"{item_href}: iCalendar resource has {} entries; storing the first ({}), \
|
||||
others dropped (recurrence overrides on the master are preserved)",
|
||||
entries.len(),
|
||||
entries[0].data_type.as_column(),
|
||||
));
|
||||
}
|
||||
entries.truncate(1);
|
||||
}
|
||||
let mut first = entries
|
||||
let rows: Vec<EventRow> = entries
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or_else(|| Error::Partial("iCalendar contained no parseable entries".to_owned()))?;
|
||||
let (is_draft, use_default_alerts, _uid) =
|
||||
calcard::strip_extracted_fields_from_event(&mut first.data);
|
||||
let data_type = first.data_type.as_column();
|
||||
.map(|mut entry| {
|
||||
let (is_draft, use_default_alerts, _uid) =
|
||||
calcard::strip_extracted_fields_from_event(&mut entry.data);
|
||||
EventRow {
|
||||
is_draft,
|
||||
use_default_alerts,
|
||||
data: entry.data.to_string(),
|
||||
data_type: entry.data_type.as_column(),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let calendar_ids = format!("[{calendar_local}]");
|
||||
if let Some(local) = existing {
|
||||
tx.execute(
|
||||
"UPDATE calendar_events SET calendar_ids = ?1, is_draft = ?2,
|
||||
use_default_alerts = ?3, data = ?4, data_type = ?5
|
||||
WHERE id = ?6",
|
||||
params![
|
||||
calendar_ids,
|
||||
is_draft as i64,
|
||||
use_default_alerts as i64,
|
||||
first.data.to_string(),
|
||||
data_type,
|
||||
local,
|
||||
],
|
||||
)
|
||||
.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
Ok(local)
|
||||
} else {
|
||||
tx.execute(
|
||||
"INSERT INTO calendar_events (calendar_ids, is_draft, use_default_alerts, data, data_type)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
params![
|
||||
calendar_ids,
|
||||
is_draft as i64,
|
||||
use_default_alerts as i64,
|
||||
first.data.to_string(),
|
||||
data_type,
|
||||
],
|
||||
)
|
||||
.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
Ok(tx.last_insert_rowid())
|
||||
let mut old = match existing {
|
||||
Some(local) => occurrence_rows(tx, source_id, calendar_local, local).map_err(db)?,
|
||||
None => Vec::new(),
|
||||
}
|
||||
.into_iter();
|
||||
let mut first = None;
|
||||
for row in &rows {
|
||||
let id = match old.next() {
|
||||
Some(local) => {
|
||||
tx.execute(
|
||||
"UPDATE calendar_events SET calendar_ids = ?1, is_draft = ?2,
|
||||
use_default_alerts = ?3, data = ?4, data_type = ?5
|
||||
WHERE id = ?6",
|
||||
params![
|
||||
calendar_ids,
|
||||
row.is_draft as i64,
|
||||
row.use_default_alerts as i64,
|
||||
row.data,
|
||||
row.data_type,
|
||||
local,
|
||||
],
|
||||
)
|
||||
.map_err(db)?;
|
||||
local
|
||||
}
|
||||
None => {
|
||||
tx.execute(
|
||||
"INSERT INTO calendar_events (calendar_ids, is_draft, use_default_alerts, data, data_type)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
params![
|
||||
calendar_ids,
|
||||
row.is_draft as i64,
|
||||
row.use_default_alerts as i64,
|
||||
row.data,
|
||||
row.data_type,
|
||||
],
|
||||
)
|
||||
.map_err(db)?;
|
||||
tx.last_insert_rowid()
|
||||
}
|
||||
};
|
||||
first.get_or_insert(id);
|
||||
}
|
||||
// Occurrences the resource no longer holds.
|
||||
for gone in old {
|
||||
tx.execute("DELETE FROM calendar_events WHERE id = ?1", params![gone])
|
||||
.map_err(db)?;
|
||||
}
|
||||
first.ok_or_else(|| Error::Partial("iCalendar contained no parseable entries".to_owned()))
|
||||
}
|
||||
|
||||
/// The rows one resource stands for: `local` first, then the other events
|
||||
/// in its calendar with its UID that no server item names.
|
||||
fn occurrence_rows(
|
||||
conn: &Connection,
|
||||
source_id: i64,
|
||||
calendar_local: i64,
|
||||
local: i64,
|
||||
) -> Result<Vec<i64>, rusqlite::Error> {
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT e.id FROM calendar_events e, calendar_events m
|
||||
WHERE m.id = ?3
|
||||
AND (e.id = ?3 OR (
|
||||
json_extract(e.data, '$.uid') = json_extract(m.data, '$.uid')
|
||||
AND EXISTS (SELECT 1 FROM json_each(e.calendar_ids) WHERE value = ?2)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM sync_id_dav s
|
||||
WHERE s.source_id = ?1 AND s.type_name = 'calendarevent'
|
||||
AND s.local_id = e.id)))
|
||||
ORDER BY e.id != ?3, e.id",
|
||||
)?;
|
||||
let ids = stmt
|
||||
.query_map(params![source_id, calendar_local, local], |r| r.get(0))?
|
||||
.collect::<Result<Vec<i64>, _>>()?;
|
||||
Ok(ids)
|
||||
}
|
||||
|
||||
fn insert_or_update_card(
|
||||
|
||||
@@ -88,6 +88,25 @@ pub struct TypeCounts {
|
||||
pub failed: u64,
|
||||
}
|
||||
|
||||
impl TypeCounts {
|
||||
/// Counts `extra` more of whatever moved since `before`: one write
|
||||
/// that stood for several rows.
|
||||
pub fn repeat_last(&mut self, before: &TypeCounts, extra: u64) {
|
||||
for (now, was) in [
|
||||
(&mut self.created, before.created),
|
||||
(&mut self.fetched, before.fetched),
|
||||
(&mut self.updated, before.updated),
|
||||
(&mut self.deleted, before.deleted),
|
||||
(&mut self.skipped, before.skipped),
|
||||
(&mut self.failed, before.failed),
|
||||
] {
|
||||
if *now > was {
|
||||
*now += extra;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct Summary {
|
||||
pub per_type: Vec<(&'static str, TypeCounts)>,
|
||||
|
||||
@@ -0,0 +1,219 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! Invitations to single occurrences of a series, with no master event:
|
||||
//! `import caldav` keeps every occurrence, follows the resource as it
|
||||
//! changes, and `export-caldav` writes them back as one resource.
|
||||
|
||||
mod fake_dav;
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use fake_dav::{Resource, Store, serve};
|
||||
use inbuxa_migrate::export_files::{self, ExportFilesConfig};
|
||||
use inbuxa_migrate::logging::Logger;
|
||||
use inbuxa_migrate::sync::export_dav::{self, ExportDavConfig};
|
||||
use inbuxa_migrate::sync::import_dav::{self, DavAuth, DavImportConfig, DavKindArg};
|
||||
use inbuxa_migrate::sync::{CommonConfig, Summary, TypeCounts};
|
||||
use rusqlite::Connection;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn occurrence(week: u32, title: &str) -> String {
|
||||
format!(
|
||||
"BEGIN:VEVENT\r\nUID:series-1\r\nRECURRENCE-ID:202403{d:02}T090000Z\r\n\
|
||||
DTSTAMP:20240101T000000Z\r\nDTSTART:202403{d:02}T090000Z\r\nDURATION:PT30M\r\n\
|
||||
SUMMARY:{title}\r\nEND:VEVENT\r\n",
|
||||
d = 4 + 7 * (week - 1)
|
||||
)
|
||||
}
|
||||
|
||||
fn calendar(components: &[String]) -> String {
|
||||
format!(
|
||||
"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:x\r\n{}END:VCALENDAR\r\n",
|
||||
components.concat()
|
||||
)
|
||||
}
|
||||
|
||||
const LUNCH: &str = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:x\r\nBEGIN:VEVENT\r\nUID:lunch\r\n\
|
||||
DTSTAMP:20240101T000000Z\r\nDTSTART:20240305T120000Z\r\nDURATION:PT1H\r\nSUMMARY:Lunch\r\n\
|
||||
END:VEVENT\r\nEND:VCALENDAR\r\n";
|
||||
|
||||
fn common(archive: &Path) -> CommonConfig {
|
||||
CommonConfig {
|
||||
archive: archive.to_path_buf(),
|
||||
threads: 1,
|
||||
dry_run: false,
|
||||
max_retries: 0,
|
||||
allow_invalid_certs: false,
|
||||
logger: Logger::from_flags(false, 0),
|
||||
}
|
||||
}
|
||||
|
||||
fn auth() -> DavAuth {
|
||||
DavAuth::Basic {
|
||||
user: "u".into(),
|
||||
password: "p".into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn import(url: &str, archive: &Path) -> Summary {
|
||||
let summary = import_dav::run(
|
||||
common(archive),
|
||||
DavImportConfig {
|
||||
kind: DavKindArg::Caldav,
|
||||
url: url.to_owned(),
|
||||
auth: auth(),
|
||||
allow_cleartext: true,
|
||||
dav_connections: 1,
|
||||
multiget_batch: 25,
|
||||
allow_source_change: false,
|
||||
},
|
||||
)
|
||||
.expect("import");
|
||||
assert!(!summary.any_failed(), "{summary:?}");
|
||||
summary
|
||||
}
|
||||
|
||||
fn events(summary: &Summary) -> &TypeCounts {
|
||||
&summary
|
||||
.per_type
|
||||
.iter()
|
||||
.find(|(t, _)| *t == "calendarevent")
|
||||
.unwrap()
|
||||
.1
|
||||
}
|
||||
|
||||
/// Each event as (uid, occurrence, title).
|
||||
fn rows(archive: &Path) -> Vec<(String, Option<String>, String)> {
|
||||
let conn = Connection::open(archive).unwrap();
|
||||
let mut stmt = conn
|
||||
.prepare(
|
||||
"SELECT json_extract(data, '$.uid'), json_extract(data, '$.recurrenceId'),
|
||||
json_extract(data, '$.title')
|
||||
FROM calendar_events ORDER BY 1, 2",
|
||||
)
|
||||
.unwrap();
|
||||
stmt.query_map([], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)))
|
||||
.unwrap()
|
||||
.map(Result::unwrap)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn row(uid: &str, rid: Option<&str>, title: &str) -> (String, Option<String>, String) {
|
||||
(uid.to_owned(), rid.map(str::to_owned), title.to_owned())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn occurrences_without_a_master_are_each_kept_and_written_back_together() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let archive = dir.path().join("archive.sqlite");
|
||||
let store = Arc::new(Mutex::new(Store::default()));
|
||||
{
|
||||
let mut s = store.lock().unwrap();
|
||||
s.collection("/cal/team/", "Team", true);
|
||||
s.put_item(
|
||||
"/cal/team/series.ics",
|
||||
&calendar(&[occurrence(1, "Week 1"), occurrence(2, "Week 2")]),
|
||||
);
|
||||
s.put_item("/cal/team/lunch.ics", LUNCH);
|
||||
}
|
||||
let url = serve(store.clone());
|
||||
|
||||
import(&url, &archive);
|
||||
let week1 = row("series-1", Some("2024-03-04T09:00:00"), "Week 1");
|
||||
let week2 = row("series-1", Some("2024-03-11T09:00:00"), "Week 2");
|
||||
let week3 = row("series-1", Some("2024-03-18T09:00:00"), "Week 3");
|
||||
let lunch = row("lunch", None, "Lunch");
|
||||
assert_eq!(
|
||||
rows(&archive),
|
||||
[lunch.clone(), week1.clone(), week2.clone()]
|
||||
);
|
||||
|
||||
let again = import(&url, &archive);
|
||||
assert_eq!(events(&again).fetched, 0, "{again:?}");
|
||||
assert_eq!(
|
||||
rows(&archive),
|
||||
[lunch.clone(), week1.clone(), week2.clone()]
|
||||
);
|
||||
|
||||
// A third invitation, then one withdrawn: the rows follow.
|
||||
let path = "/cal/team/series.ics";
|
||||
let three = calendar(&[
|
||||
occurrence(1, "Week 1"),
|
||||
occurrence(2, "Week 2"),
|
||||
occurrence(3, "Week 3"),
|
||||
]);
|
||||
store.lock().unwrap().put_item(path, &three);
|
||||
import(&url, &archive);
|
||||
assert_eq!(
|
||||
rows(&archive),
|
||||
[lunch.clone(), week1.clone(), week2.clone(), week3.clone()]
|
||||
);
|
||||
store
|
||||
.lock()
|
||||
.unwrap()
|
||||
.put_item(path, &calendar(&[occurrence(2, "Week 2")]));
|
||||
import(&url, &archive);
|
||||
assert_eq!(rows(&archive), [lunch.clone(), week2.clone()]);
|
||||
|
||||
store.lock().unwrap().put_item(path, &three);
|
||||
import(&url, &archive);
|
||||
|
||||
// Out again, into an empty server: one resource for the series.
|
||||
let target = Arc::new(Mutex::new(Store::default()));
|
||||
let target_url = serve(target.clone());
|
||||
let export = |archive: &Path| {
|
||||
export_dav::run(
|
||||
common(archive),
|
||||
ExportDavConfig {
|
||||
kind: DavKindArg::Caldav,
|
||||
url: target_url.clone(),
|
||||
auth: auth(),
|
||||
allow_cleartext: true,
|
||||
},
|
||||
)
|
||||
.expect("export")
|
||||
};
|
||||
let first = export(&archive);
|
||||
assert_eq!(events(&first).created, 4, "counted per row: {first:?}");
|
||||
{
|
||||
let t = target.lock().unwrap();
|
||||
let Some(Resource::Item { body, .. }) = t.resources.get("/cal/team/series-1.ics") else {
|
||||
panic!("no series: {:?}", t.resources.keys().collect::<Vec<_>>());
|
||||
};
|
||||
assert_eq!(body.matches("BEGIN:VEVENT").count(), 3, "{body}");
|
||||
assert_eq!(body.matches("RECURRENCE-ID").count(), 3, "{body}");
|
||||
}
|
||||
let second = export(&archive);
|
||||
let c = events(&second);
|
||||
assert_eq!((c.created, c.updated, c.skipped), (0, 0, 4), "{second:?}");
|
||||
|
||||
// As files: the calendar's .ics holds each occurrence.
|
||||
let out = dir.path().join("files");
|
||||
let files = export_files::run(ExportFilesConfig {
|
||||
archive: archive.clone(),
|
||||
dir: out.clone(),
|
||||
objects: None,
|
||||
dry_run: false,
|
||||
logger: Logger::from_flags(false, 0),
|
||||
})
|
||||
.expect("export-files");
|
||||
assert!(!files.any_failed(), "{files:?}");
|
||||
let ics = std::fs::read_to_string(out.join("calendars/Team.ics")).unwrap();
|
||||
assert_eq!(ics.matches("RECURRENCE-ID").count(), 3, "{ics}");
|
||||
assert_eq!(ics.matches("UID:series-1").count(), 3, "{ics}");
|
||||
|
||||
// And back in, from the exported copy: the same rows.
|
||||
let back = dir.path().join("back.sqlite");
|
||||
import(&target_url, &back);
|
||||
assert_eq!(rows(&back), rows(&archive));
|
||||
|
||||
// The resource deleted: every occurrence goes.
|
||||
store.lock().unwrap().remove(path);
|
||||
import(&url, &archive);
|
||||
assert_eq!(rows(&archive), [lunch]);
|
||||
}
|
||||
Reference in new issue
Block a user