feat(#515): write RDATE into .ics backups and read it back

This commit is contained in:
makiolaj committed 2026-11-14 02:51:25 +01:00
1 parent 041f8ec2f7
commit 30af150197
13 files changed
+139 -40

No files matched your search

@@ -252,12 +252,7 @@ class CalendarRepositoryImpl @Inject constructor(
}
override suspend fun exportEvents(calendarIds: Set<Long>) = withContext(io) {
// A file can't carry RDATE: such a series is written as the one-off it reads as.
val rows = dataSource.calendarRows(calendarIds, allDayReminderTimeMinutes())
flattenOverrides(
rows.copy(series = rows.series.map { it.copy(event = it.event.copy(recurrenceDates = null)) }),
::formatExdateStamp,
)
flattenOverrides(dataSource.calendarRows(calendarIds, allDayReminderTimeMinutes()), ::formatExdateStamp)
}
override suspend fun copyEvents(
@@ -851,11 +851,12 @@ internal fun buildImportedEventValues(
put(CalendarContract.Events.TITLE, event.summary.trim())
put(CalendarContract.Events.ALL_DAY, if (event.isAllDay) 1 else 0)
put(CalendarContract.Events.DTSTART, startMillis)
if (event.recurrenceRule == null && event.recurrenceDates == null) {
if (event.recurrenceRule == null && event.recurrenceDates.isEmpty()) {
put(CalendarContract.Events.DTEND, endMillis)
} else {
event.recurrenceRule?.let { put(CalendarContract.Events.RRULE, it) }
event.recurrenceDates?.let { put(CalendarContract.Events.RDATE, it) }
event.recurrenceDates.takeIf { it.isNotEmpty() }
?.let { put(CalendarContract.Events.RDATE, it.joinToString(",")) }
put(
CalendarContract.Events.DURATION,
importDuration(startMillis, endMillis, event.isAllDay),
@@ -28,8 +28,9 @@ internal fun ColumnReader.toIcsEvent(
val eventId = getLong(EventExportProjection.IDX_ID)
val dtStart = getLong(EventExportProjection.IDX_DTSTART)
val rrule = getString(EventExportProjection.IDX_RRULE)?.takeIf { it.isNotBlank() }
val rdate = getString(EventExportProjection.IDX_RDATE)?.takeIf { it.isNotBlank() }
val isAllDay = getInt(EventExportProjection.IDX_ALL_DAY) != 0
val timezone = getString(EventExportProjection.IDX_EVENT_TIMEZONE)
val rdates = exportRecurrenceDates(getString(EventExportProjection.IDX_RDATE), isAllDay, timezone)
// Unclamped: the writer renders DTEND as stored.
val end = endMillis(dtStart, EventExportProjection.IDX_DTEND, EventExportProjection.IDX_DURATION)
@@ -43,14 +44,10 @@ internal fun ColumnReader.toIcsEvent(
zoneId = getString(EventExportProjection.IDX_EVENT_TIMEZONE)?.takeIf { it.isNotBlank() }
?: "UTC",
recurrenceRule = rrule,
exDates = if (rrule == null && rdate == null) {
exDates = if (rrule == null && rdates.isEmpty()) {
emptyList()
} else {
exportExDates(
getString(EventExportProjection.IDX_EXDATE),
isAllDay,
getString(EventExportProjection.IDX_EVENT_TIMEZONE),
)
exportExDates(getString(EventExportProjection.IDX_EXDATE), isAllDay, timezone)
},
location = getString(EventExportProjection.IDX_LOCATION),
description = getString(EventExportProjection.IDX_DESCRIPTION),
@@ -65,7 +62,7 @@ internal fun ColumnReader.toIcsEvent(
status = eventStatus(EventExportProjection.IDX_STATUS),
availability = mapAvailability(getInt(EventExportProjection.IDX_AVAILABILITY)),
calendarName = calendarName,
recurrenceDates = rdate,
recurrenceDates = rdates,
accessLevel = getIntOrNull(EventExportProjection.IDX_ACCESS_LEVEL)?.let(::mapAccessLevel) ?: AccessLevel.Default,
)
}
@@ -111,3 +108,25 @@ internal fun exportExDates(
}
.distinct()
}
/**
* The row's `RDATE` in the same shape [exportExDates] gives `EXDATE` — the list
* shares its `<zone>;<stamps>` form. A `PERIOD` entry (`start/end`) keeps only its
* start: the occurrence then runs the series' own length. Anything that still
* isn't a date or a UTC stamp is dropped, since a file can't name it.
*/
internal fun exportRecurrenceDates(
rdate: String?,
isAllDay: Boolean,
eventTimezone: String? = null,
): List<String> {
if (rdate.isNullOrBlank()) return emptyList()
// The zone prefix may hold a '/' of its own (Europe/Berlin), so only the stamps lose their period end.
val stamps = rdate.withoutExdateParameters()
val prefix = rdate.dropLast(stamps.length)
val starts = prefix + stamps.split(',').joinToString(",") { it.substringBefore('/') }
return exportExDates(starts, isAllDay, eventTimezone).filter { RECURRENCE_DATE.matches(it) }
}
private val RECURRENCE_DATE = Regex("""\d{8}(T\d{6}Z)?""")
@@ -44,7 +44,7 @@ fun IcsEvent.toParsedIcsEvent(): ParsedIcsEvent = ParsedIcsEvent(
isAllDay = isAllDay,
zoneId = zoneId,
recurrenceRule = recurrenceRule?.takeIf { it.isNotBlank() }?.removePrefix("RRULE:"),
recurrenceDates = recurrenceDates?.takeIf { it.isNotBlank() },
recurrenceDates = recurrenceDates.filter { it.isNotBlank() }.distinct(),
location = location,
description = description,
// The writer emits only lead times; a negative one would be an alarm after the start.
@@ -80,7 +80,7 @@ fun flattenOverrides(
detached += override.calendarId to override.event.copy(
uid = "$occurrence-${parent.event.uid}",
recurrenceRule = null,
recurrenceDates = null,
recurrenceDates = emptyList(),
exDates = emptyList(),
overrideOf = parent.event.uid,
overrideAt = override.originalInstance,
@@ -37,8 +37,8 @@ data class IcsEvent(
val availability: Availability = Availability.Busy,
/** Source calendar name, emitted as `X-CALENDULA-CALENDAR` so a combined backup can fan back out. */
val calendarName: String? = null,
/** The provider's raw `RDATE`; carried by a calendar copy, not written to a file. */
val recurrenceDates: String? = null,
/** Extra occurrences (`RDATE`), in the same shape as [exDates]. */
val recurrenceDates: List<String> = emptyList(),
/** Carried by a calendar copy, not written to a file. */
val accessLevel: AccessLevel = AccessLevel.Default,
/** The event's own colour, null when it shows its calendar's; carried by a calendar copy, not written to a file. */
@@ -54,7 +54,7 @@ data class IcsEvent(
/** Whether this is a series: by rule, or by listed dates alone. */
val IcsEvent.recurs: Boolean
get() = !recurrenceRule.isNullOrBlank() || !recurrenceDates.isNullOrBlank()
get() = !recurrenceRule.isNullOrBlank() || recurrenceDates.isNotEmpty()
/**
* The UID to export for a provider event. A row that already carries a UID
@@ -54,8 +54,8 @@ data class ParsedIcsEvent(
val overrideOf: String? = null,
/** The occurrence of [overrideOf] this event replaces (`X-CALENDULA-OVERRIDE-AT`). */
val overrideAt: Instant? = null,
/** The provider's raw `RDATE`, only set by a calendar copy. */
val recurrenceDates: String? = null,
/** Extra occurrences (`RDATE`), in the same shape as [exDates]. */
val recurrenceDates: List<String> = emptyList(),
val accessLevel: AccessLevel = AccessLevel.Default,
)
@@ -224,6 +224,7 @@ class IcsParser(private val deviceZone: TimeZone = TimeZone.currentSystemDefault
var calendarColor: Int? = null
val reminders = mutableListOf<Int>()
val exDateLines = mutableListOf<IcsContentLine>()
val rDateLines = mutableListOf<IcsContentLine>()
var skipAsOverride = false
var overrideOf: String? = null
var overrideAt: Instant? = null
@@ -247,6 +248,7 @@ class IcsParser(private val deviceZone: TimeZone = TimeZone.currentSystemDefault
"DURATION" -> duration = line.value.trim()
"RRULE" -> rrule = line.value.trim().ifEmpty { null }
"EXDATE" -> exDateLines.add(line)
"RDATE" -> rDateLines.add(line)
"LOCATION" -> location = unescapeText(line.value).ifEmpty { null }
"DESCRIPTION" -> description = unescapeText(line.value).ifEmpty { null }
"STATUS" -> status = mapIcsStatus(line.value)
@@ -295,6 +297,7 @@ class IcsParser(private val deviceZone: TimeZone = TimeZone.currentSystemDefault
val recurrence = sanitizeRrule(rrule)
if (recurrence.repaired) warnings.add(IcsParseWarning.RecurrenceRuleRepaired)
val end = resolveEnd(start, dtEnd, duration)
val recurrenceDates = normalizeExDates(rDateLines, start)
return ParsedIcsEvent(
uid = uid,
summary = summary,
@@ -310,11 +313,12 @@ class IcsParser(private val deviceZone: TimeZone = TimeZone.currentSystemDefault
availability = availability,
calendarName = calendarName ?: category,
color = eventColor ?: calendarColor,
exDates = if (recurrence.rule == null) {
exDates = if (recurrence.rule == null && recurrenceDates.isEmpty()) {
emptyList()
} else {
normalizeExDates(exDateLines, start)
},
recurrenceDates = recurrenceDates,
isTask = isTask,
overrideOf = overrideOf,
overrideAt = overrideAt,
@@ -352,13 +356,16 @@ class IcsParser(private val deviceZone: TimeZone = TimeZone.currentSystemDefault
* still means the day it spells out; resolving it to an instant first and
* reading *that* back in UTC would roll a midnight east of UTC onto the day
* before and exclude an occurrence that doesn't exist.
*
* `RDATE` shares the value form and is read here too.
*/
private fun normalizeExDates(lines: List<IcsContentLine>, start: IcsDateTime): List<String> {
if (lines.isEmpty()) return emptyList()
val startZone = zoneOrNull(start.zoneId) ?: deviceZone
val startTime = start.instant.toLocalDateTime(startZone).time
return lines
.flatMap { line -> line.value.split(',').map { line to it.trim() } }
// An RDATE PERIOD (`start/end`) counts by its start.
.flatMap { line -> line.value.split(',').map { line to it.trim().substringBefore('/') } }
.mapNotNull { (line, token) ->
when {
token.isEmpty() -> null
@@ -47,6 +47,7 @@ class IcsWriter(private val prodId: String = ICS_PROD_ID) {
appendTimes(event)
event.recurrenceRule?.takeIf { it.isNotBlank() }
?.let { add("RRULE:${it.removePrefix("RRULE:")}") }
appendRecurrenceDates(event)
appendExDates(event)
event.location?.takeIf { it.isNotBlank() }
?.let { add("LOCATION:${escapeText(it)}") }
@@ -71,7 +72,7 @@ class IcsWriter(private val prodId: String = ICS_PROD_ID) {
add("DTEND;VALUE=DATE:${event.end.toLocalDateTime(TimeZone.UTC).date.toBasic()}")
}
// Recurring: anchor to wall-clock in the event's own zone.
event.recurrenceRule?.isNotBlank() == true -> {
event.recurs -> {
val zone = zoneOrNull(event.zoneId)
if (zone != null) {
add("DTSTART;TZID=${event.zoneId}:${event.start.toLocalDateTime(zone).toBasic()}")
@@ -94,8 +95,16 @@ class IcsWriter(private val prodId: String = ICS_PROD_ID) {
* it the bare day codes read as an invalid DATE-TIME. Only written for a
* recurring event: an exclusion names an occurrence, and a one-off has none.
*/
/** Extra occurrences: the same UTC stamps / `VALUE=DATE` days as [appendExDates]. */
private fun MutableList<String>.appendRecurrenceDates(event: IcsEvent) {
val stamps = event.recurrenceDates.filter { it.isNotBlank() }.distinct()
if (stamps.isEmpty()) return
val prefix = if (event.isAllDay) "RDATE;VALUE=DATE:" else "RDATE:"
add(prefix + stamps.joinToString(","))
}
private fun MutableList<String>.appendExDates(event: IcsEvent) {
if (event.recurrenceRule.isNullOrBlank()) return
if (!event.recurs) return
val stamps = event.exDates.filter { it.isNotBlank() }.distinct()
if (stamps.isEmpty()) return
val prefix = if (event.isAllDay) "EXDATE;VALUE=DATE:" else "EXDATE:"
@@ -1079,22 +1079,27 @@ class CalendarRepositoryImplTest {
}
@Test
fun `a backup writes an RDATE-only series as the one-off a file can hold`(
fun `a backup keeps an RDATE-only series and its exceptions`(
@TempDir tempDir: Path,
) = runTest {
val start = Instant.parse("2026-01-05T09:00:00Z")
val event = IcsEvent("s", "S", start, Instant.parse("2026-01-05T10:00:00Z"), false, "UTC", recurrenceDates = "20260112T090000Z")
val other = Instant.parse("2026-01-12T09:00:00Z")
val event = IcsEvent(
"s", "S", start, Instant.parse("2026-01-05T10:00:00Z"), false, "UTC",
recurrenceDates = listOf("20260112T090000Z", "20260119T090000Z"),
)
val fake = FakeCalendarDataSource().apply {
calendarRowsResult = CalendarRows(
listOf(MasterRow(1, 1, null, event)),
listOf(OverrideRow(1, 1, null, start, event.copy(recurrenceDates = null))),
listOf(OverrideRow(1, 1, null, other, event.copy(recurrenceDates = emptyList(), status = EventStatus.Cancelled))),
)
}
val exported = testRepository(fake, tempDir, Dispatchers.Unconfined).exportEvents(setOf(1L))
val restored = IcsParser().parse(IcsWriter().writeCalendar(exported, start)).events.single()
assertThat(exported.map { it.uid }).containsExactly("s")
assertThat(exported.single().recurrenceDates).isNull()
assertThat(restored.recurrenceDates).containsExactly("20260112T090000Z", "20260119T090000Z")
assertThat(restored.exDates).containsExactly("20260112T090000Z")
}
private fun parsedEvent(uid: String?, summary: String = "E") =
@@ -294,4 +294,16 @@ class IcsExportMapperTest {
assertThat(event.exDates).isEmpty()
}
@Test
fun `a zoned RDATE list is written as UTC stamps, a period by its start`() {
assertThat(exportRecurrenceDates("Europe/Berlin;20260110T100000,20260111T100000/PT1H", isAllDay = false))
.containsExactly("20260110T090000Z", "20260111T090000Z").inOrder()
}
@Test
fun `an all-day RDATE keeps its days, junk is dropped`() {
assertThat(exportRecurrenceDates("20260110T000000Z,20260111,garbage", isAllDay = true))
.containsExactly("20260110", "20260111").inOrder()
}
}
@@ -127,7 +127,7 @@ class ImportedEventValuesTest {
@Test
fun `a copied RDATE-only series recurs by its dates`() {
val v = values(allDay().copy(recurrenceDates = "20260820", exDates = listOf("20260820")))
val v = values(allDay().copy(recurrenceDates = listOf("20260820"), exDates = listOf("20260820")))
assertThat(v).doesNotContainKey(CalendarContract.Events.DTEND)
assertThat(v).doesNotContainKey(CalendarContract.Events.RRULE)
assertThat(v[CalendarContract.Events.RDATE]).isEqualTo("20260820")
@@ -120,11 +120,11 @@ class CalendarRowsTest {
@Test
fun `an RDATE-only series keeps its dates and takes its exceptions`() {
val flat = flatten(
listOf(MasterRow(1, 1, null, event("s").copy(recurrenceDates = "20260112T090000Z"))),
listOf(MasterRow(1, 1, null, event("s").copy(recurrenceDates = listOf("20260112T090000Z")))),
listOf(OverrideRow(1, 1, null, occurrence(1), event("s", status = EventStatus.Cancelled))),
)
assertThat(flat.single().recurrenceDates).isEqualTo("20260112T090000Z")
assertThat(flat.single().recurrenceDates).containsExactly("20260112T090000Z")
assertThat(flat.single().exDates).containsExactly("20260112T090000Z")
}
@@ -229,4 +229,45 @@ class CalendarRowsTest {
assertThat(plan.map { it.uid }).containsExactly("s")
assertThat(plan.single().exDates).containsExactly("20260112T090000Z")
}
@Test
fun `RDATE and its exclusions survive the file`() {
val timed = event("t").copy(
recurrenceDates = listOf("20260112T090000Z", "20260120T090000Z"),
exDates = listOf("20260112T090000Z"),
)
val day = Instant.parse("2026-01-05T00:00:00Z")
val allDay = event("a", allDay = true, at = day).copy(
end = Instant.parse("2026-01-06T00:00:00Z"),
recurrenceDates = listOf("20260110"),
exDates = listOf("20260110"),
)
val text = IcsWriter().writeCalendar(listOf(timed, allDay), start)
assertThat(text).contains("RDATE;VALUE=DATE:20260110")
assertThat(text).contains("DTSTART;TZID=Europe/Berlin:20260105T100000")
val (t, a) = IcsParser().parse(text).events
assertThat(t.recurrenceDates).containsExactly("20260112T090000Z", "20260120T090000Z")
assertThat(t.exDates).containsExactly("20260112T090000Z")
assertThat(a.recurrenceDates).containsExactly("20260110")
assertThat(a.exDates).containsExactly("20260110")
}
@Test
fun `a foreign RDATE is read in its own zone, a period by its start`() {
val text = """
BEGIN:VCALENDAR
BEGIN:VEVENT
UID:f
DTSTART;TZID=Europe/Berlin:20260105T100000
DTEND;TZID=Europe/Berlin:20260105T110000
RDATE;TZID=America/New_York:20260110T090000
RDATE;VALUE=PERIOD:20260111T090000Z/PT1H
END:VEVENT
END:VCALENDAR
""".trimIndent().replace("\n", "\r\n")
assertThat(IcsParser().parse(text).events.single().recurrenceDates)
.containsExactly("20260110T140000Z", "20260111T090000Z")
}
}
+15 -5
View File
@@ -515,16 +515,26 @@ import inserts every such event ahead of the series, whatever order the file
lists them in.
Exceptions whose series isn't among the rows are left out, a cancelled
series' moved occurrences stay cancelled, and sync ids link only within their
own calendar. A file can't carry `RDATE`, so a backup treats a series that
repeats by listed dates alone as the one-off it writes it as. Backups made before this carry no exceptions
and restore exactly as before.
own calendar. Backups made before this carry no exceptions and restore exactly
as before.
**`RDATE` travels like `EXDATE` (#515).** A series can repeat by listed dates
alone. The provider stores `RDATE` in the same `<zone>;<stamps>` form as
`EXDATE`, so `exportRecurrenceDates` resolves it the same way to UTC stamps (or
bare days for an all-day series); a `PERIOD` entry keeps its start. `IcsWriter`
writes it as `RDATE:` / `RDATE;VALUE=DATE:`, anchors such a series to its zone
like an RRULE series, and writes its `EXDATE` too; `IcsParser` reads `RDATE`
through the same normalisation as `EXDATE`, so a foreign `TZID` or floating
value lands on the right instant. Older backups wrote such a series as a single
event and still restore as one.
**Copying a calendar.** Backup & restore → Copy events (#325) runs the source's
flattened rows straight into `importEvents`, so the UID dedup makes a re-run skip
what landed before. It skips the `.ics` text: the writer pins a timed one-off to
UTC and carries no colour, visibility or `RDATE`; the direct mapping keeps them.
UTC and carries no colour or visibility; the direct mapping keeps them.
Attendees and organizer are never read, and cancelled events and declined
invitations are left out (a declined occurrence counts as deleted) (an insert can't be cancelled; it would arrive live). A re-run is idempotent per
invitations are left out (an insert can't be cancelled; it would arrive live, and
a declined occurrence counts as deleted). A re-run is idempotent per
snapshot: a series already in the target keeps its overrides, so an occurrence
changed since keeps its earlier copy rather than gaining a second one.