Fix CalDAV/CardDAV multiget loading entire collections into memory

A REPORT multiget for a handful of resources previously listed the
whole calendar/address book (every row incl. ical_data/vcard) and
filtered by href in Rust with an O(N×M) substring scan. Large
collections paid full-table latency, RAM and DB CPU on every sync.

- Add find_events_by_ical_uids / get_contacts_by_uids through every
  layer (domain repo trait → PG repo → storage port/adapter → use case
  → handler) using one indexed `= ANY($2)` query, mirroring the
  existing single-UID lookups.
- Extract UIDs from multiget hrefs with a shared, tested
  uid_from_multiget_href helper (percent-decoding, case-insensitive
  extension strip, collection hrefs rejected). Exact UID matching also
  removes the false positives the old substring filter allowed.
- CalDAV PROPFIND single-event path now uses the existing indexed
  get_event_by_ical_uid instead of listing all events.
- Honour the already-documented limit/offset query params on
  GET /api/address-books/{id}/contacts: optional ListQuery fields
  thread through ContactUseCase::list_contacts to a paginated repo
  query. Omitted params keep returning the full book (frontend and
  DAV listing/sync paths unchanged).

https://claude.ai/code/session_0193Hff42gaA962wThxMGSd1
This commit is contained in:
Claude
2026-06-11 09:21:35 +00:00
parent f678ff414e
commit 6a27c742d4
14 changed files with 431 additions and 49 deletions
@@ -422,6 +422,26 @@ impl CalendarStoragePort for CalendarStorageAdapter {
Ok(event.map(CalendarEventDto::from))
}
async fn find_events_by_ical_uids(
&self,
calendar_id: &str,
ical_uids: &[String],
) -> Result<Vec<CalendarEventDto>, DomainError> {
let uuid = Uuid::parse_str(calendar_id).map_err(|_| {
DomainError::new(
ErrorKind::InvalidInput,
"Calendar",
"Invalid calendar ID format",
)
})?;
let events = self
.event_repository
.find_events_by_ical_uids(&uuid, ical_uids)
.await?;
Ok(events.into_iter().map(CalendarEventDto::from).collect())
}
async fn list_events_by_calendar(
&self,
calendar_id: &str,
@@ -730,9 +730,10 @@ impl ContactUseCase for ContactStorageAdapter {
Ok(contact.map(ContactDto::from))
}
async fn list_contacts(
async fn get_contacts_by_uids(
&self,
address_book_id: &str,
uids: &[String],
user_id: Uuid,
) -> Result<Vec<ContactDto>, DomainError> {
let uuid = Self::parse_uuid(address_book_id, "AddressBook")?;
@@ -740,13 +741,43 @@ impl ContactUseCase for ContactStorageAdapter {
// Check read access
self.check_address_book_access(&uuid, user_id).await?;
if uids.is_empty() {
return Ok(Vec::new());
}
let contacts = self
.contact_repository
.get_contacts_by_address_book(&uuid)
.get_contacts_by_uids(&uuid, uids)
.await?;
Ok(contacts.into_iter().map(ContactDto::from).collect())
}
async fn list_contacts(
&self,
address_book_id: &str,
limit: Option<i64>,
offset: Option<i64>,
user_id: Uuid,
) -> Result<Vec<ContactDto>, DomainError> {
let uuid = Self::parse_uuid(address_book_id, "AddressBook")?;
// Check read access
self.check_address_book_access(&uuid, user_id).await?;
let contacts = if limit.is_some() || offset.is_some() {
let limit = limit.unwrap_or(100);
let offset = offset.unwrap_or(0);
self.contact_repository
.get_contacts_by_address_book_paginated(&uuid, limit, offset)
.await?
} else {
self.contact_repository
.get_contacts_by_address_book(&uuid)
.await?
};
Ok(contacts.into_iter().map(ContactDto::from).collect())
}
async fn search_contacts(
&self,
address_book_id: &str,