import carddav: a card in two address books is one card in both #35
No files matched your search
@@ -12,6 +12,9 @@ candidates before a series ships (`27.1-rc.1`). The tag is the version with a
|
||||
at a mistyped path and bring an older archive's layout up to date; now
|
||||
the first is an error and the second is refused, and reading a closed
|
||||
archive leaves no files beside it.
|
||||
- `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.
|
||||
|
||||
## [27.1-rc.3]
|
||||
|
||||
|
||||
+4
-1
@@ -121,7 +121,10 @@ inbuxa-migrate import carddav \
|
||||
<ARCHIVE>
|
||||
```
|
||||
|
||||
Same shape as `caldav`, but for address books and contacts.
|
||||
Same shape as `caldav`, but for address books and contacts. A card the
|
||||
server keeps in two address books, by the same UID, is one card in both. If
|
||||
it leaves one book, or that book is deleted, it stays in the other; if the
|
||||
copies differ, the one read last is kept.
|
||||
|
||||
### WebDAV
|
||||
|
||||
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! A contact card is one row however many address books hold it, with the
|
||||
//! books in `address_book_ids`. Sources that list a card once per book
|
||||
//! (CardDAV, Takeout) add each book they find it in to the same row.
|
||||
|
||||
use rusqlite::{Connection, params};
|
||||
|
||||
/// `address_book_ids` with `book` added, in the order first seen.
|
||||
pub fn with_book(ids_json: &str, book: i64) -> String {
|
||||
let mut ids: Vec<i64> = serde_json::from_str(ids_json).unwrap_or_default();
|
||||
if !ids.contains(&book) {
|
||||
ids.push(book);
|
||||
}
|
||||
serde_json::to_string(&ids).unwrap_or_else(|_| "[]".to_owned())
|
||||
}
|
||||
|
||||
/// Takes `book` off one card, leaving the card in its other books.
|
||||
pub fn leave_book(conn: &Connection, card: i64, book: i64) -> Result<(), rusqlite::Error> {
|
||||
conn.execute(
|
||||
"UPDATE contact_cards
|
||||
SET address_book_ids = (SELECT json_group_array(value) FROM json_each(address_book_ids)
|
||||
WHERE value != ?2)
|
||||
WHERE id = ?1",
|
||||
params![card, book],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// An address book going away: the cards only it held go with it, and the
|
||||
/// others stay in their other books.
|
||||
pub fn remove_book(conn: &Connection, book: i64) -> Result<(), rusqlite::Error> {
|
||||
conn.execute(
|
||||
"DELETE FROM contact_cards
|
||||
WHERE EXISTS (SELECT 1 FROM json_each(address_book_ids) WHERE value = ?1)
|
||||
AND NOT EXISTS (SELECT 1 FROM json_each(address_book_ids) WHERE value != ?1)",
|
||||
params![book],
|
||||
)?;
|
||||
conn.execute(
|
||||
"UPDATE contact_cards
|
||||
SET address_book_ids = (SELECT json_group_array(value) FROM json_each(address_book_ids)
|
||||
WHERE value != ?1)
|
||||
WHERE EXISTS (SELECT 1 FROM json_each(address_book_ids) WHERE value = ?1)",
|
||||
params![book],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::db::init;
|
||||
|
||||
fn books(conn: &Connection, uid: &str) -> Option<String> {
|
||||
conn.query_row(
|
||||
"SELECT address_book_ids FROM contact_cards WHERE uid = ?1",
|
||||
[uid],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.ok()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_book_is_added_once() {
|
||||
assert_eq!(with_book("[1]", 2), "[1,2]");
|
||||
assert_eq!(with_book("[1,2]", 1), "[1,2]");
|
||||
assert_eq!(with_book("not json", 3), "[3]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removing_a_book_keeps_cards_another_book_holds() {
|
||||
let conn = Connection::open_in_memory().unwrap();
|
||||
init::apply_schema(&conn).unwrap();
|
||||
conn.execute_batch(
|
||||
"INSERT INTO contact_cards (uid, address_book_ids, data) VALUES
|
||||
('only-1', '[1]', '{}'), ('both', '[1,2]', '{}'), ('only-2', '[2]', '{}');",
|
||||
)
|
||||
.unwrap();
|
||||
remove_book(&conn, 1).unwrap();
|
||||
assert_eq!(books(&conn, "only-1"), None);
|
||||
assert_eq!(books(&conn, "both").as_deref(), Some("[2]"));
|
||||
assert_eq!(books(&conn, "only-2").as_deref(), Some("[2]"));
|
||||
|
||||
let id: i64 = conn
|
||||
.query_row("SELECT id FROM contact_cards WHERE uid = 'both'", [], |r| {
|
||||
r.get(0)
|
||||
})
|
||||
.unwrap();
|
||||
conn.execute(
|
||||
"UPDATE contact_cards SET address_book_ids = '[2,3]' WHERE id = ?1",
|
||||
[id],
|
||||
)
|
||||
.unwrap();
|
||||
leave_book(&conn, id, 2).unwrap();
|
||||
assert_eq!(books(&conn, "both").as_deref(), Some("[3]"));
|
||||
}
|
||||
}
|
||||
+24
-26
@@ -121,6 +121,22 @@ pub fn collections_of_type(
|
||||
Ok(map)
|
||||
}
|
||||
|
||||
/// How many server items stand for one local row: a card in two address
|
||||
/// books has two.
|
||||
pub fn items_for_local(
|
||||
conn: &Connection,
|
||||
source_id: i64,
|
||||
type_name: &str,
|
||||
local_id: i64,
|
||||
) -> Result<i64, rusqlite::Error> {
|
||||
conn.query_row(
|
||||
"SELECT count(*) FROM sync_id_dav
|
||||
WHERE source_id = ?1 AND type_name = ?2 AND local_id = ?3",
|
||||
params![source_id, type_name, local_id],
|
||||
|row| row.get(0),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn delete_item(
|
||||
conn: &Connection,
|
||||
source_id: i64,
|
||||
@@ -305,32 +321,14 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unique_local_id_per_type_enforced() {
|
||||
fn two_items_can_stand_for_one_card() {
|
||||
let (conn, sid) = setup();
|
||||
insert(
|
||||
&conn,
|
||||
sid,
|
||||
CALENDAR_EVENT,
|
||||
"/cal/a/",
|
||||
"/cal/a/e1.ics",
|
||||
"v",
|
||||
1,
|
||||
)
|
||||
.unwrap();
|
||||
let err = insert(
|
||||
&conn,
|
||||
sid,
|
||||
CALENDAR_EVENT,
|
||||
"/cal/a/",
|
||||
"/cal/a/e2.ics",
|
||||
"v",
|
||||
1,
|
||||
)
|
||||
.unwrap_err();
|
||||
let msg = format!("{err}");
|
||||
assert!(
|
||||
msg.contains("UNIQUE"),
|
||||
"expected UNIQUE violation, got {msg}"
|
||||
);
|
||||
for (book, href) in [("/card/a/", "/card/a/x.vcf"), ("/card/b/", "/card/b/x.vcf")] {
|
||||
insert(&conn, sid, CONTACT_CARD, book, href, "v", 1).unwrap();
|
||||
}
|
||||
assert_eq!(items_for_local(&conn, sid, CONTACT_CARD, 1).unwrap(), 2);
|
||||
delete_item(&conn, sid, CONTACT_CARD, "/card/a/x.vcf").unwrap();
|
||||
assert_eq!(items_for_local(&conn, sid, CONTACT_CARD, 1).unwrap(), 1);
|
||||
assert_eq!(items_for_local(&conn, sid, CALENDAR_EVENT, 1).unwrap(), 0);
|
||||
}
|
||||
}
|
||||
@@ -123,6 +123,7 @@ pub fn apply_schema(conn: &Connection) -> Result<(), OpenError> {
|
||||
tx.execute_batch(SCHEMA_SQL)?;
|
||||
ensure_calendar_events_data_type(&tx)?;
|
||||
ensure_graph_ids_accept_file_nodes(&tx)?;
|
||||
ensure_dav_ids_share_local_rows(&tx)?;
|
||||
tx.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -175,6 +176,47 @@ fn ensure_graph_ids_accept_file_nodes(conn: &Connection) -> Result<(), OpenError
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A card in two address books is one archive row reached from two server
|
||||
/// items, so the DAV map no longer holds a local row to one item. An
|
||||
/// archive from before that is rebuilt without the constraint.
|
||||
fn ensure_dav_ids_share_local_rows(conn: &Connection) -> Result<(), OpenError> {
|
||||
let sql: Option<String> = conn
|
||||
.query_row(
|
||||
"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'sync_id_dav'",
|
||||
[],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.ok();
|
||||
let Some(sql) = sql else { return Ok(()) };
|
||||
if !sql.contains("UNIQUE (source_id, type_name, local_id)") {
|
||||
return Ok(());
|
||||
}
|
||||
conn.execute_batch(
|
||||
"DROP INDEX IF EXISTS sync_id_dav_collection_idx;
|
||||
DROP INDEX IF EXISTS sync_id_dav_local_idx;
|
||||
ALTER TABLE sync_id_dav RENAME TO sync_id_dav_old;
|
||||
CREATE TABLE sync_id_dav (
|
||||
source_id INTEGER NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
|
||||
type_name TEXT NOT NULL CHECK (type_name IN (
|
||||
'calendar','calendarevent',
|
||||
'addressbook','contactcard',
|
||||
'filenode')),
|
||||
collection_href TEXT NOT NULL,
|
||||
item_href TEXT NOT NULL,
|
||||
etag TEXT NOT NULL DEFAULT '',
|
||||
local_id INTEGER NOT NULL,
|
||||
PRIMARY KEY (source_id, type_name, item_href)
|
||||
);
|
||||
INSERT INTO sync_id_dav SELECT * FROM sync_id_dav_old;
|
||||
DROP TABLE sync_id_dav_old;
|
||||
CREATE INDEX sync_id_dav_collection_idx
|
||||
ON sync_id_dav (source_id, type_name, collection_href);
|
||||
CREATE INDEX sync_id_dav_local_idx
|
||||
ON sync_id_dav (source_id, type_name, local_id);",
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// An archive holds a whole mailbox, so it is created readable by its owner
|
||||
/// only. SQLite gives its `-wal` and `-shm` files the database file's mode,
|
||||
/// so they follow. An existing archive others can read is left as it is,
|
||||
@@ -419,3 +461,47 @@ mod permission_tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod migration_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn an_older_dav_map_is_rebuilt_to_share_local_rows() {
|
||||
let conn = Connection::open_in_memory().unwrap();
|
||||
conn.execute_batch(
|
||||
"CREATE TABLE sources (id INTEGER PRIMARY KEY);
|
||||
INSERT INTO sources (id) VALUES (1);
|
||||
CREATE TABLE sync_id_dav (
|
||||
source_id INTEGER NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
|
||||
type_name TEXT NOT NULL,
|
||||
collection_href TEXT NOT NULL,
|
||||
item_href TEXT NOT NULL,
|
||||
etag TEXT NOT NULL DEFAULT '',
|
||||
local_id INTEGER NOT NULL,
|
||||
PRIMARY KEY (source_id, type_name, item_href),
|
||||
UNIQUE (source_id, type_name, local_id)
|
||||
);
|
||||
INSERT INTO sync_id_dav VALUES (1, 'contactcard', '/a/', '/a/x.vcf', 'e1', 7);",
|
||||
)
|
||||
.unwrap();
|
||||
ensure_dav_ids_share_local_rows(&conn).unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO sync_id_dav VALUES (1, 'contactcard', '/b/', '/b/x.vcf', 'e2', 7)",
|
||||
[],
|
||||
)
|
||||
.expect("two items may stand for one card");
|
||||
let rows: i64 = conn
|
||||
.query_row(
|
||||
"SELECT count(*) FROM sync_id_dav WHERE local_id = 7",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(rows, 2, "the old row was kept");
|
||||
// A second pass finds nothing to do.
|
||||
ensure_dav_ids_share_local_rows(&conn).unwrap();
|
||||
// And a full schema pass over it is clean.
|
||||
apply_schema(&conn).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
pub mod blobs;
|
||||
pub mod cards;
|
||||
pub mod dav_ids;
|
||||
pub mod defaults;
|
||||
pub mod exchange_ews_ids;
|
||||
|
||||
+3
-2
@@ -68,12 +68,13 @@ CREATE TABLE IF NOT EXISTS sync_id_dav (
|
||||
item_href TEXT NOT NULL,
|
||||
etag TEXT NOT NULL DEFAULT '',
|
||||
local_id INTEGER NOT NULL,
|
||||
PRIMARY KEY (source_id, type_name, item_href),
|
||||
UNIQUE (source_id, type_name, local_id)
|
||||
PRIMARY KEY (source_id, type_name, item_href)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS sync_id_dav_collection_idx
|
||||
ON sync_id_dav (source_id, type_name, collection_href);
|
||||
CREATE INDEX IF NOT EXISTS sync_id_dav_local_idx
|
||||
ON sync_id_dav (source_id, type_name, local_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sync_id_managesieve (
|
||||
source_id INTEGER NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
|
||||
|
||||
@@ -231,13 +231,7 @@ fn upsert_address_book_row(
|
||||
}
|
||||
|
||||
fn delete_address_book_row(conn: &Connection, local_id: i64) -> Result<(), rusqlite::Error> {
|
||||
conn.execute(
|
||||
"DELETE FROM contact_cards
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM json_each(address_book_ids) WHERE value = ?1
|
||||
)",
|
||||
params![local_id],
|
||||
)?;
|
||||
crate::db::cards::remove_book(conn, local_id)?;
|
||||
conn.execute("DELETE FROM address_books WHERE id = ?1", params![local_id])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use rusqlite::{Connection, params};
|
||||
use rusqlite::{Connection, OptionalExtension, params};
|
||||
|
||||
use crate::dav::client::{DavClient, MultiStatus};
|
||||
use crate::dav::href::{Href, join_absolute, normalise};
|
||||
use crate::dav::parse::DavResponse;
|
||||
use crate::dav::xml;
|
||||
use crate::db::dav_ids;
|
||||
use crate::db::{cards, dav_ids};
|
||||
use crate::error::Error;
|
||||
use crate::jmap::error::JmapError;
|
||||
use crate::logging::{LEVEL_PROGRESS, Logger};
|
||||
@@ -214,17 +214,26 @@ fn reconcile_items_generic(
|
||||
let tx = conn
|
||||
.unchecked_transaction()
|
||||
.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
let table = match pcc.kind {
|
||||
ItemKind::CalendarEvent => "calendar_events",
|
||||
ItemKind::ContactCard => "contact_cards",
|
||||
};
|
||||
tx.execute(
|
||||
&format!("DELETE FROM {table} WHERE id = ?1"),
|
||||
params![local_id],
|
||||
)
|
||||
.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
dav_ids::delete_item(&tx, pcc.run.source_id, type_name, href)
|
||||
.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
let elsewhere = dav_ids::items_for_local(&tx, pcc.run.source_id, type_name, local_id)
|
||||
.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
let removed = match pcc.kind {
|
||||
// Still in another address book: only this one lets it go.
|
||||
ItemKind::ContactCard if elsewhere > 0 => {
|
||||
cards::leave_book(&tx, local_id, pcc.container_local_id)
|
||||
}
|
||||
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],
|
||||
)
|
||||
.map(drop),
|
||||
};
|
||||
removed.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
tx.commit().map_err(|e| Error::Partial(e.to_string()))?;
|
||||
counts.deleted += 1;
|
||||
}
|
||||
@@ -471,7 +480,7 @@ fn insert_or_update_tx(
|
||||
local_map: &HashMap<String, (String, i64)>,
|
||||
) -> Result<(), Error> {
|
||||
let existing = local_map.get(item_href).map(|(_, id)| *id);
|
||||
match pcc.kind {
|
||||
let local = match pcc.kind {
|
||||
ItemKind::CalendarEvent => insert_or_update_event(
|
||||
tx,
|
||||
pcc.container_local_id,
|
||||
@@ -485,7 +494,6 @@ fn insert_or_update_tx(
|
||||
}
|
||||
};
|
||||
if existing.is_none() {
|
||||
let new_local = tx.last_insert_rowid();
|
||||
dav_ids::insert(
|
||||
tx,
|
||||
pcc.run.source_id,
|
||||
@@ -493,7 +501,7 @@ fn insert_or_update_tx(
|
||||
pcc.collection_href,
|
||||
item_href,
|
||||
etag,
|
||||
new_local,
|
||||
local,
|
||||
)
|
||||
.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
} else {
|
||||
@@ -510,7 +518,7 @@ fn insert_or_update_event(
|
||||
raw: &str,
|
||||
item_href: &str,
|
||||
logger: Logger,
|
||||
) -> Result<(), Error> {
|
||||
) -> Result<i64, Error> {
|
||||
let 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) {
|
||||
@@ -544,6 +552,7 @@ fn insert_or_update_event(
|
||||
],
|
||||
)
|
||||
.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)
|
||||
@@ -557,8 +566,8 @@ fn insert_or_update_event(
|
||||
],
|
||||
)
|
||||
.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
Ok(tx.last_insert_rowid())
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert_or_update_card(
|
||||
@@ -567,25 +576,58 @@ fn insert_or_update_card(
|
||||
existing: Option<i64>,
|
||||
raw: &str,
|
||||
item_href: &str,
|
||||
) -> Result<(), Error> {
|
||||
) -> Result<i64, Error> {
|
||||
let card = calcard::vcard_to_jscontact(raw, item_href)
|
||||
.map_err(|e| Error::Partial(format!("vCard parse: {e}")))?;
|
||||
let address_book_ids = format!("[{address_book_local}]");
|
||||
if let Some(local) = existing {
|
||||
tx.execute(
|
||||
"UPDATE contact_cards SET uid = ?1, address_book_ids = ?2, data = ?3
|
||||
WHERE id = ?4",
|
||||
params![card.uid, address_book_ids, card.data.to_string(), local],
|
||||
)
|
||||
.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
} else {
|
||||
tx.execute(
|
||||
"INSERT INTO contact_cards (uid, address_book_ids, data) VALUES (?1, ?2, ?3)",
|
||||
params![card.uid, address_book_ids, card.data.to_string()],
|
||||
)
|
||||
.map_err(|e| Error::Partial(e.to_string()))?;
|
||||
let db = |e: rusqlite::Error| Error::Partial(e.to_string());
|
||||
// The same card met in another address book is the same row: this
|
||||
// book joins its others, and the copy read last is kept.
|
||||
let local = match existing {
|
||||
Some(local) => Some(local),
|
||||
None => tx
|
||||
.query_row(
|
||||
"SELECT id FROM contact_cards WHERE uid = ?1",
|
||||
params![card.uid],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.optional()
|
||||
.map_err(db)?,
|
||||
};
|
||||
match local {
|
||||
Some(local) => {
|
||||
let books: String = tx
|
||||
.query_row(
|
||||
"SELECT address_book_ids FROM contact_cards WHERE id = ?1",
|
||||
params![local],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.map_err(db)?;
|
||||
tx.execute(
|
||||
"UPDATE contact_cards SET uid = ?1, address_book_ids = ?2, data = ?3
|
||||
WHERE id = ?4",
|
||||
params![
|
||||
card.uid,
|
||||
cards::with_book(&books, address_book_local),
|
||||
card.data.to_string(),
|
||||
local
|
||||
],
|
||||
)
|
||||
.map_err(db)?;
|
||||
Ok(local)
|
||||
}
|
||||
None => {
|
||||
tx.execute(
|
||||
"INSERT INTO contact_cards (uid, address_book_ids, data) VALUES (?1, ?2, ?3)",
|
||||
params![
|
||||
card.uid,
|
||||
format!("[{address_book_local}]"),
|
||||
card.data.to_string()
|
||||
],
|
||||
)
|
||||
.map_err(db)?;
|
||||
Ok(tx.last_insert_rowid())
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn response_indicates_vanished(response: &DavResponse) -> bool {
|
||||
|
||||
@@ -0,0 +1,275 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! A small in-memory CalDAV and CardDAV server for tests: discovery, depth-1
|
||||
//! listings, multiget, GET, conditional PUT, MKCALENDAR, MKCOL and DELETE,
|
||||
//! one request per connection. The home sets are `/cal/` and `/card/`.
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::net::TcpListener;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
|
||||
/// What the server holds, by path: a collection's name, or an item's body
|
||||
/// and etag.
|
||||
#[derive(Clone)]
|
||||
pub enum Resource {
|
||||
Collection { name: String, calendar: bool },
|
||||
Item { body: String, etag: u32 },
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Store {
|
||||
pub resources: BTreeMap<String, Resource>,
|
||||
pub next_etag: u32,
|
||||
/// Each write as (method, path, condition header).
|
||||
pub writes: Vec<(String, String, String)>,
|
||||
/// Paths a PUT to is refused with 415.
|
||||
pub refuse: Vec<String>,
|
||||
}
|
||||
|
||||
impl Store {
|
||||
pub fn collection(&mut self, path: &str, name: &str, calendar: bool) {
|
||||
self.resources.insert(
|
||||
path.to_owned(),
|
||||
Resource::Collection {
|
||||
name: name.to_owned(),
|
||||
calendar,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn put_item(&mut self, path: &str, body: &str) {
|
||||
self.next_etag += 1;
|
||||
self.resources.insert(
|
||||
path.to_owned(),
|
||||
Resource::Item {
|
||||
body: body.to_owned(),
|
||||
etag: self.next_etag,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// A path and everything under it.
|
||||
pub fn remove(&mut self, path: &str) {
|
||||
self.resources.retain(|p, _| !p.starts_with(path));
|
||||
}
|
||||
}
|
||||
|
||||
fn escape(text: &str) -> String {
|
||||
text.replace('&', "&")
|
||||
.replace('<', "<")
|
||||
.replace('>', ">")
|
||||
}
|
||||
|
||||
fn props(path: &str, resource: Option<&Resource>, with_data: bool) -> String {
|
||||
let inner = match resource {
|
||||
None => "<d:resourcetype><d:collection/></d:resourcetype>".to_owned(),
|
||||
Some(Resource::Collection { name, calendar }) => format!(
|
||||
"<d:resourcetype><d:collection/>{}</d:resourcetype><d:displayname>{name}</d:displayname>",
|
||||
if *calendar {
|
||||
"<c:calendar/>"
|
||||
} else {
|
||||
"<card:addressbook/>"
|
||||
}
|
||||
),
|
||||
Some(Resource::Item { etag, body }) => {
|
||||
let data = match (with_data, path.starts_with("/cal/")) {
|
||||
(false, _) => String::new(),
|
||||
(true, true) => format!("<c:calendar-data>{}</c:calendar-data>", escape(body)),
|
||||
(true, false) => format!("<card:address-data>{}</card:address-data>", escape(body)),
|
||||
};
|
||||
format!("<d:resourcetype/><d:getetag>\"{etag}\"</d:getetag>{data}")
|
||||
}
|
||||
};
|
||||
format!(
|
||||
"<d:response><d:href>{path}</d:href><d:propstat><d:prop>{inner}</d:prop>\
|
||||
<d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>"
|
||||
)
|
||||
}
|
||||
|
||||
fn multistatus(responses: &str) -> String {
|
||||
format!(
|
||||
"<?xml version=\"1.0\"?><d:multistatus xmlns:d=\"DAV:\" \
|
||||
xmlns:c=\"urn:ietf:params:xml:ns:caldav\" \
|
||||
xmlns:card=\"urn:ietf:params:xml:ns:carddav\">{responses}</d:multistatus>"
|
||||
)
|
||||
}
|
||||
|
||||
fn displayname(body: &str) -> String {
|
||||
let start = body.find("displayname>").map(|i| i + "displayname>".len());
|
||||
start
|
||||
.and_then(|s| body[s..].find('<').map(|e| body[s..s + e].to_owned()))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Every `<…href>` in a request body.
|
||||
fn hrefs(body: &str) -> Vec<String> {
|
||||
let mut out = Vec::new();
|
||||
let mut rest = body;
|
||||
while let Some(i) = rest.find("href>") {
|
||||
rest = &rest[i + "href>".len()..];
|
||||
if let Some(end) = rest.find('<') {
|
||||
if !rest[..end].trim().is_empty() {
|
||||
out.push(rest[..end].trim().to_owned());
|
||||
}
|
||||
rest = &rest[end..];
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// One request against the store, as (status, etag, body).
|
||||
fn handle(
|
||||
store: &mut Store,
|
||||
method: &str,
|
||||
path: &str,
|
||||
headers: &BTreeMap<String, String>,
|
||||
body: &str,
|
||||
) -> (u16, Option<u32>, String) {
|
||||
match method {
|
||||
"PROPFIND" if headers.get("depth").map(String::as_str) == Some("0") => {
|
||||
let home = "<d:current-user-principal><d:href>/p/</d:href></d:current-user-principal>\
|
||||
<c:calendar-home-set><d:href>/cal/</d:href></c:calendar-home-set>\
|
||||
<card:addressbook-home-set><d:href>/card/</d:href></card:addressbook-home-set>";
|
||||
let response = format!(
|
||||
"<d:response><d:href>{path}</d:href><d:propstat><d:prop>{home}</d:prop>\
|
||||
<d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>"
|
||||
);
|
||||
(207, None, multistatus(&response))
|
||||
}
|
||||
"PROPFIND" => {
|
||||
let mut out = props(path, store.resources.get(path), false);
|
||||
for (child, resource) in store.resources.range(path.to_owned()..) {
|
||||
let Some(rest) = child.strip_prefix(path) else {
|
||||
break;
|
||||
};
|
||||
let direct = !rest.is_empty() && !rest.trim_end_matches('/').contains('/');
|
||||
if direct {
|
||||
out.push_str(&props(child, Some(resource), false));
|
||||
}
|
||||
}
|
||||
(207, None, multistatus(&out))
|
||||
}
|
||||
"REPORT" => {
|
||||
let mut out = String::new();
|
||||
for href in hrefs(body) {
|
||||
match store.resources.get(&href) {
|
||||
Some(item @ Resource::Item { .. }) => {
|
||||
out.push_str(&props(&href, Some(item), true))
|
||||
}
|
||||
_ => out.push_str(&format!(
|
||||
"<d:response><d:href>{href}</d:href>\
|
||||
<d:status>HTTP/1.1 404 Not Found</d:status></d:response>"
|
||||
)),
|
||||
}
|
||||
}
|
||||
(207, None, multistatus(&out))
|
||||
}
|
||||
"MKCALENDAR" | "MKCOL" => {
|
||||
store
|
||||
.writes
|
||||
.push((method.to_owned(), path.to_owned(), String::new()));
|
||||
if store.resources.contains_key(path) {
|
||||
return (405, None, String::new());
|
||||
}
|
||||
store.collection(path, &displayname(body), method == "MKCALENDAR");
|
||||
(201, None, String::new())
|
||||
}
|
||||
"GET" => match store.resources.get(path) {
|
||||
Some(Resource::Item { body, etag }) => (200, Some(*etag), body.clone()),
|
||||
_ => (404, None, String::new()),
|
||||
},
|
||||
"DELETE" => {
|
||||
store.remove(path);
|
||||
(204, None, String::new())
|
||||
}
|
||||
"PUT" => {
|
||||
let condition = headers
|
||||
.get("if-none-match")
|
||||
.map(|v| format!("If-None-Match: {v}"))
|
||||
.or_else(|| headers.get("if-match").map(|v| format!("If-Match: {v}")))
|
||||
.unwrap_or_default();
|
||||
store
|
||||
.writes
|
||||
.push(("PUT".to_owned(), path.to_owned(), condition));
|
||||
if store.refuse.iter().any(|r| r == path) {
|
||||
return (415, None, String::new());
|
||||
}
|
||||
let current = match store.resources.get(path) {
|
||||
Some(Resource::Item { etag, .. }) => Some(format!("\"{etag}\"")),
|
||||
_ => None,
|
||||
};
|
||||
let allowed = match (headers.get("if-none-match"), headers.get("if-match")) {
|
||||
(Some(_), _) => current.is_none(),
|
||||
(_, Some(want)) => current.as_deref() == Some(want.as_str()),
|
||||
_ => false,
|
||||
};
|
||||
if !allowed {
|
||||
return (412, None, String::new());
|
||||
}
|
||||
let status = if current.is_some() { 204 } else { 201 };
|
||||
store.put_item(path, body);
|
||||
(status, Some(store.next_etag), String::new())
|
||||
}
|
||||
_ => (405, None, String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Serves `store` on a local port; the URL of its root.
|
||||
pub fn serve(store: Arc<Mutex<Store>>) -> String {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let url = format!("http://{}/", listener.local_addr().unwrap());
|
||||
thread::spawn(move || {
|
||||
for stream in listener.incoming() {
|
||||
let Ok(mut stream) = stream else { break };
|
||||
let mut reader = BufReader::new(stream.try_clone().unwrap());
|
||||
let mut line = String::new();
|
||||
if reader.read_line(&mut line).unwrap_or(0) == 0 {
|
||||
continue;
|
||||
}
|
||||
let mut parts = line.split_whitespace();
|
||||
let method = parts.next().unwrap_or_default().to_owned();
|
||||
let path = parts.next().unwrap_or_default().to_owned();
|
||||
let mut headers = BTreeMap::new();
|
||||
loop {
|
||||
let mut h = String::new();
|
||||
if reader.read_line(&mut h).unwrap_or(0) == 0 || h.trim().is_empty() {
|
||||
break;
|
||||
}
|
||||
if let Some((k, v)) = h.split_once(':') {
|
||||
headers.insert(k.trim().to_ascii_lowercase(), v.trim().to_owned());
|
||||
}
|
||||
}
|
||||
let length: usize = headers
|
||||
.get("content-length")
|
||||
.and_then(|v| v.parse().ok())
|
||||
.unwrap_or(0);
|
||||
let mut body = vec![0; length];
|
||||
reader.read_exact(&mut body).unwrap();
|
||||
let body = String::from_utf8_lossy(&body);
|
||||
let (status, etag, out) =
|
||||
handle(&mut store.lock().unwrap(), &method, &path, &headers, &body);
|
||||
let etag = etag
|
||||
.map(|e| format!("ETag: \"{e}\"\r\n"))
|
||||
.unwrap_or_default();
|
||||
let kind = if status == 207 {
|
||||
"Content-Type: application/xml; charset=utf-8\r\n"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let _ = write!(
|
||||
stream,
|
||||
"HTTP/1.1 {status} X\r\n{etag}{kind}Content-Length: {}\r\nConnection: close\r\n\r\n{out}",
|
||||
out.len()
|
||||
);
|
||||
}
|
||||
});
|
||||
url
|
||||
}
|
||||
+6
-212
@@ -9,12 +9,12 @@
|
||||
//! replaced under its etag, a refused card is counted without stopping the
|
||||
//! rest, and a second run writes nothing.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::net::TcpListener;
|
||||
mod fake_dav;
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
|
||||
use fake_dav::{Resource, Store, serve};
|
||||
|
||||
use inbuxa_migrate::db::init;
|
||||
use inbuxa_migrate::logging::Logger;
|
||||
@@ -33,207 +33,6 @@ const EVENT: &str = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:x\r\nBEGIN:VEVENT\
|
||||
DTSTAMP:20240101T000000Z\r\nDTSTART:20240301T090000Z\r\nDURATION:PT15M\r\n\
|
||||
SUMMARY:Standup\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
|
||||
|
||||
/// What the server holds, by path: a collection's name, or an item's body
|
||||
/// and etag.
|
||||
#[derive(Clone)]
|
||||
enum Resource {
|
||||
Collection { name: String, calendar: bool },
|
||||
Item { body: String, etag: u32 },
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Store {
|
||||
resources: BTreeMap<String, Resource>,
|
||||
next_etag: u32,
|
||||
/// Each write as (method, path, condition header).
|
||||
writes: Vec<(String, String, String)>,
|
||||
}
|
||||
|
||||
impl Store {
|
||||
fn put_item(&mut self, path: &str, body: &str) {
|
||||
self.next_etag += 1;
|
||||
self.resources.insert(
|
||||
path.to_owned(),
|
||||
Resource::Item {
|
||||
body: body.to_owned(),
|
||||
etag: self.next_etag,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn props(path: &str, resource: Option<&Resource>) -> String {
|
||||
let inner = match resource {
|
||||
None => "<d:resourcetype><d:collection/></d:resourcetype>".to_owned(),
|
||||
Some(Resource::Collection { name, calendar }) => format!(
|
||||
"<d:resourcetype><d:collection/>{}</d:resourcetype><d:displayname>{name}</d:displayname>",
|
||||
if *calendar {
|
||||
"<c:calendar/>"
|
||||
} else {
|
||||
"<card:addressbook/>"
|
||||
}
|
||||
),
|
||||
Some(Resource::Item { etag, .. }) => {
|
||||
format!("<d:resourcetype/><d:getetag>\"{etag}\"</d:getetag>")
|
||||
}
|
||||
};
|
||||
format!(
|
||||
"<d:response><d:href>{path}</d:href><d:propstat><d:prop>{inner}</d:prop>\
|
||||
<d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>"
|
||||
)
|
||||
}
|
||||
|
||||
fn multistatus(responses: &str) -> String {
|
||||
format!(
|
||||
"<?xml version=\"1.0\"?><d:multistatus xmlns:d=\"DAV:\" \
|
||||
xmlns:c=\"urn:ietf:params:xml:ns:caldav\" \
|
||||
xmlns:card=\"urn:ietf:params:xml:ns:carddav\">{responses}</d:multistatus>"
|
||||
)
|
||||
}
|
||||
|
||||
fn displayname(body: &str) -> String {
|
||||
let start = body.find("displayname>").map(|i| i + "displayname>".len());
|
||||
start
|
||||
.and_then(|s| body[s..].find('<').map(|e| body[s..s + e].to_owned()))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// One request against the store, as (status, etag, body).
|
||||
fn handle(
|
||||
store: &mut Store,
|
||||
method: &str,
|
||||
path: &str,
|
||||
headers: &BTreeMap<String, String>,
|
||||
body: &str,
|
||||
) -> (u16, Option<u32>, String) {
|
||||
match method {
|
||||
"PROPFIND" if headers.get("depth").map(String::as_str) == Some("0") => {
|
||||
let home = "<d:current-user-principal><d:href>/p/</d:href></d:current-user-principal>\
|
||||
<c:calendar-home-set><d:href>/cal/</d:href></c:calendar-home-set>\
|
||||
<card:addressbook-home-set><d:href>/card/</d:href></card:addressbook-home-set>";
|
||||
let response = format!(
|
||||
"<d:response><d:href>{path}</d:href><d:propstat><d:prop>{home}</d:prop>\
|
||||
<d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response>"
|
||||
);
|
||||
(207, None, multistatus(&response))
|
||||
}
|
||||
"PROPFIND" => {
|
||||
let mut out = props(path, store.resources.get(path));
|
||||
for (child, resource) in store.resources.range(path.to_owned()..) {
|
||||
let Some(rest) = child.strip_prefix(path) else {
|
||||
break;
|
||||
};
|
||||
let direct = !rest.is_empty() && !rest.trim_end_matches('/').contains('/');
|
||||
if direct {
|
||||
out.push_str(&props(child, Some(resource)));
|
||||
}
|
||||
}
|
||||
(207, None, multistatus(&out))
|
||||
}
|
||||
"MKCALENDAR" | "MKCOL" => {
|
||||
store
|
||||
.writes
|
||||
.push((method.to_owned(), path.to_owned(), String::new()));
|
||||
if store.resources.contains_key(path) {
|
||||
return (405, None, String::new());
|
||||
}
|
||||
store.resources.insert(
|
||||
path.to_owned(),
|
||||
Resource::Collection {
|
||||
name: displayname(body),
|
||||
calendar: method == "MKCALENDAR",
|
||||
},
|
||||
);
|
||||
(201, None, String::new())
|
||||
}
|
||||
"GET" => match store.resources.get(path) {
|
||||
Some(Resource::Item { body, etag }) => (200, Some(*etag), body.clone()),
|
||||
_ => (404, None, String::new()),
|
||||
},
|
||||
"PUT" => {
|
||||
let condition = headers
|
||||
.get("if-none-match")
|
||||
.map(|v| format!("If-None-Match: {v}"))
|
||||
.or_else(|| headers.get("if-match").map(|v| format!("If-Match: {v}")))
|
||||
.unwrap_or_default();
|
||||
store
|
||||
.writes
|
||||
.push(("PUT".to_owned(), path.to_owned(), condition));
|
||||
if path.contains("card-carol") {
|
||||
return (415, None, String::new());
|
||||
}
|
||||
let current = match store.resources.get(path) {
|
||||
Some(Resource::Item { etag, .. }) => Some(format!("\"{etag}\"")),
|
||||
_ => None,
|
||||
};
|
||||
let allowed = match (headers.get("if-none-match"), headers.get("if-match")) {
|
||||
(Some(_), _) => current.is_none(),
|
||||
(_, Some(want)) => current.as_deref() == Some(want.as_str()),
|
||||
_ => false,
|
||||
};
|
||||
if !allowed {
|
||||
return (412, None, String::new());
|
||||
}
|
||||
let status = if current.is_some() { 204 } else { 201 };
|
||||
store.put_item(path, body);
|
||||
(status, Some(store.next_etag), String::new())
|
||||
}
|
||||
_ => (405, None, String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// A DAV server on a local port, one request per connection.
|
||||
fn serve(store: Arc<Mutex<Store>>) -> String {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let url = format!("http://{}/", listener.local_addr().unwrap());
|
||||
thread::spawn(move || {
|
||||
for stream in listener.incoming() {
|
||||
let Ok(mut stream) = stream else { break };
|
||||
let mut reader = BufReader::new(stream.try_clone().unwrap());
|
||||
let mut line = String::new();
|
||||
if reader.read_line(&mut line).unwrap_or(0) == 0 {
|
||||
continue;
|
||||
}
|
||||
let mut parts = line.split_whitespace();
|
||||
let method = parts.next().unwrap_or_default().to_owned();
|
||||
let path = parts.next().unwrap_or_default().to_owned();
|
||||
let mut headers = BTreeMap::new();
|
||||
loop {
|
||||
let mut h = String::new();
|
||||
if reader.read_line(&mut h).unwrap_or(0) == 0 || h.trim().is_empty() {
|
||||
break;
|
||||
}
|
||||
if let Some((k, v)) = h.split_once(':') {
|
||||
headers.insert(k.trim().to_ascii_lowercase(), v.trim().to_owned());
|
||||
}
|
||||
}
|
||||
let length: usize = headers
|
||||
.get("content-length")
|
||||
.and_then(|v| v.parse().ok())
|
||||
.unwrap_or(0);
|
||||
let mut body = vec![0; length];
|
||||
reader.read_exact(&mut body).unwrap();
|
||||
let body = String::from_utf8_lossy(&body);
|
||||
let (status, etag, out) =
|
||||
handle(&mut store.lock().unwrap(), &method, &path, &headers, &body);
|
||||
let etag = etag
|
||||
.map(|e| format!("ETag: \"{e}\"\r\n"))
|
||||
.unwrap_or_default();
|
||||
let kind = if status == 207 {
|
||||
"Content-Type: application/xml; charset=utf-8\r\n"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let _ = write!(
|
||||
stream,
|
||||
"HTTP/1.1 {status} X\r\n{etag}{kind}Content-Length: {}\r\nConnection: close\r\n\r\n{out}",
|
||||
out.len()
|
||||
);
|
||||
}
|
||||
});
|
||||
url
|
||||
}
|
||||
|
||||
fn common(archive: &Path) -> CommonConfig {
|
||||
CommonConfig {
|
||||
archive: archive.to_path_buf(),
|
||||
@@ -310,13 +109,8 @@ fn carddav_export_matches_by_name_replaces_under_etag_and_converges() {
|
||||
{
|
||||
// The target already has a "Personal" book holding an older Alice.
|
||||
let mut s = store.lock().unwrap();
|
||||
s.resources.insert(
|
||||
"/card/contacts/".into(),
|
||||
Resource::Collection {
|
||||
name: "Personal".into(),
|
||||
calendar: false,
|
||||
},
|
||||
);
|
||||
s.collection("/card/contacts/", "Personal", false);
|
||||
s.refuse.push("/card/work/card-carol.vcf".into());
|
||||
s.put_item(
|
||||
"/card/contacts/card-alice.vcf",
|
||||
"BEGIN:VCARD\r\nVERSION:4.0\r\nUID:card-alice\r\nFN:Alice Old\r\nEND:VCARD\r\n",
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*/
|
||||
|
||||
//! `import carddav` with one card in two address books: it is one card in
|
||||
//! both books, every run succeeds, and taking it out of one book, or
|
||||
//! deleting that book, leaves it in the other.
|
||||
|
||||
mod fake_dav;
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use fake_dav::{Store, serve};
|
||||
use inbuxa_migrate::logging::Logger;
|
||||
use inbuxa_migrate::sync::import_dav::{self, DavAuth, DavImportConfig, DavKindArg};
|
||||
use inbuxa_migrate::sync::{CommonConfig, Summary, TypeCounts};
|
||||
use rusqlite::Connection;
|
||||
use tempfile::TempDir;
|
||||
|
||||
const ALICE: &str =
|
||||
"BEGIN:VCARD\r\nVERSION:4.0\r\nUID:card-alice\r\nFN:Alice Smith\r\nEND:VCARD\r\n";
|
||||
const BOB: &str = "BEGIN:VCARD\r\nVERSION:4.0\r\nUID:card-bob\r\nFN:Bob Jones\r\nEND:VCARD\r\n";
|
||||
|
||||
fn import(url: &str, archive: &Path) -> Summary {
|
||||
let summary = import_dav::run(
|
||||
CommonConfig {
|
||||
archive: archive.to_path_buf(),
|
||||
threads: 1,
|
||||
dry_run: false,
|
||||
max_retries: 0,
|
||||
allow_invalid_certs: false,
|
||||
logger: Logger::from_flags(false, 0),
|
||||
},
|
||||
DavImportConfig {
|
||||
kind: DavKindArg::Carddav,
|
||||
url: url.to_owned(),
|
||||
auth: DavAuth::Basic {
|
||||
user: "u".into(),
|
||||
password: "p".into(),
|
||||
},
|
||||
allow_cleartext: true,
|
||||
dav_connections: 1,
|
||||
multiget_batch: 25,
|
||||
allow_source_change: false,
|
||||
},
|
||||
)
|
||||
.expect("import");
|
||||
assert!(!summary.any_failed(), "{summary:?}");
|
||||
summary
|
||||
}
|
||||
|
||||
fn cards(summary: &Summary) -> &TypeCounts {
|
||||
&summary
|
||||
.per_type
|
||||
.iter()
|
||||
.find(|(t, _)| *t == "contactcard")
|
||||
.unwrap()
|
||||
.1
|
||||
}
|
||||
|
||||
/// Each card's UID with the names of the books it is in.
|
||||
fn books(archive: &Path) -> Vec<(String, Vec<String>)> {
|
||||
let conn = Connection::open(archive).unwrap();
|
||||
let mut stmt = conn
|
||||
.prepare(
|
||||
"SELECT c.uid, coalesce((SELECT json_group_array(name) FROM
|
||||
(SELECT b.name FROM json_each(c.address_book_ids) j
|
||||
JOIN address_books b ON b.id = j.value ORDER BY b.name)), '[]')
|
||||
FROM contact_cards c ORDER BY c.uid",
|
||||
)
|
||||
.unwrap();
|
||||
stmt.query_map([], |r| {
|
||||
let names: String = r.get(1)?;
|
||||
Ok((r.get(0)?, serde_json::from_str(&names).unwrap()))
|
||||
})
|
||||
.unwrap()
|
||||
.map(Result::unwrap)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn names(list: &[(&str, &[&str])]) -> Vec<(String, Vec<String>)> {
|
||||
list.iter()
|
||||
.map(|(uid, books)| {
|
||||
(
|
||||
uid.to_string(),
|
||||
books.iter().map(|b| b.to_string()).collect(),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_card_in_two_address_books_is_one_card_in_both() {
|
||||
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("/card/personal/", "Personal", false);
|
||||
s.collection("/card/work/", "Work", false);
|
||||
s.put_item("/card/personal/alice.vcf", ALICE);
|
||||
s.put_item("/card/work/alice-copy.vcf", ALICE);
|
||||
s.put_item("/card/work/bob.vcf", BOB);
|
||||
}
|
||||
let url = serve(store.clone());
|
||||
|
||||
let first = import(&url, &archive);
|
||||
assert_eq!(cards(&first).fetched, 3, "{first:?}");
|
||||
let both = names(&[
|
||||
("card-alice", &["Personal", "Work"]),
|
||||
("card-bob", &["Work"]),
|
||||
]);
|
||||
assert_eq!(books(&archive), both);
|
||||
|
||||
let again = import(&url, &archive);
|
||||
assert_eq!(cards(&again).fetched, 0, "nothing changed: {again:?}");
|
||||
assert_eq!(books(&archive), both);
|
||||
|
||||
// Out of Work only: still in Personal.
|
||||
store.lock().unwrap().remove("/card/work/alice-copy.vcf");
|
||||
let left = import(&url, &archive);
|
||||
assert_eq!(cards(&left).deleted, 1, "{left:?}");
|
||||
assert_eq!(
|
||||
books(&archive),
|
||||
names(&[("card-alice", &["Personal"]), ("card-bob", &["Work"])])
|
||||
);
|
||||
|
||||
// Back in Work, then Personal deleted as a whole: still in Work.
|
||||
store
|
||||
.lock()
|
||||
.unwrap()
|
||||
.put_item("/card/work/alice-copy.vcf", ALICE);
|
||||
import(&url, &archive);
|
||||
assert_eq!(books(&archive), both);
|
||||
store.lock().unwrap().remove("/card/personal/");
|
||||
import(&url, &archive);
|
||||
assert_eq!(
|
||||
books(&archive),
|
||||
names(&[("card-alice", &["Work"]), ("card-bob", &["Work"])])
|
||||
);
|
||||
|
||||
// Gone from every book: gone.
|
||||
store.lock().unwrap().remove("/card/work/alice-copy.vcf");
|
||||
import(&url, &archive);
|
||||
assert_eq!(books(&archive), names(&[("card-bob", &["Work"])]));
|
||||
}
|
||||
Reference in new issue
Block a user