fix(#515, #325): review round 3 - bare-day EXDATE/RDATE on timed series resolved when read, unaccepted occurrences of declined series not copied

This commit is contained in:
makiolaj committed 2026-11-14 03:22:45 +01:00
1 parent 05887d2ed0
commit 1e83000696
7 files changed
+63 -20

No files matched your search

@@ -798,8 +798,8 @@ class AndroidCalendarDataSource @Inject constructor(
it != calendar?.color || !getString(EventExportProjection.IDX_EVENT_COLOR_KEY).isNullOrEmpty()
}
val mapped = event(calendar?.displayName).copy(color = color)
val declined = getIntOrNull(EventExportProjection.IDX_SELF_ATTENDEE_STATUS) ==
CalendarContract.Attendees.ATTENDEE_STATUS_DECLINED
val selfStatus = getIntOrNull(EventExportProjection.IDX_SELF_ATTENDEE_STATUS)
val declined = selfStatus == CalendarContract.Attendees.ATTENDEE_STATUS_DECLINED
val originalId = getLongOrNull(EventExportProjection.IDX_ORIGINAL_ID)
val originalSyncId = getString(EventExportProjection.IDX_ORIGINAL_SYNC_ID)?.takeIf { it.isNotEmpty() }
// Without a link the provider shows the row as an event of its own.
@@ -822,6 +822,8 @@ class AndroidCalendarDataSource @Inject constructor(
originalInstance = originalInstance.toKotlinInstantFromEpochMillis(),
event = mapped,
declined = declined,
accepted = selfStatus == CalendarContract.Attendees.ATTENDEE_STATUS_ACCEPTED ||
selfStatus == CalendarContract.Attendees.ATTENDEE_STATUS_TENTATIVE,
)
}
}
@@ -4,7 +4,10 @@ import de.jeanlucmakiola.floret.time.toKotlinInstantFromEpochMillis
import de.jeanlucmakiola.calendula.domain.AccessLevel
import de.jeanlucmakiola.calendula.domain.ics.IcsEvent
import de.jeanlucmakiola.calendula.domain.ics.deriveIcsUid
import java.time.Instant
import java.time.LocalDate
import java.time.ZoneId
import java.time.format.DateTimeFormatter
/**
* Map one Events row (read through [EventExportProjection]) into an [IcsEvent]
@@ -30,7 +33,7 @@ internal fun ColumnReader.toIcsEvent(
val rrule = getString(EventExportProjection.IDX_RRULE)?.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)
val rdates = exportRecurrenceDates(getString(EventExportProjection.IDX_RDATE), isAllDay, timezone, dtStart)
// Unclamped: the writer renders DTEND as stored.
val end = endMillis(dtStart, EventExportProjection.IDX_DTEND, EventExportProjection.IDX_DURATION)
@@ -46,7 +49,7 @@ internal fun ColumnReader.toIcsEvent(
exDates = if (rrule == null && rdates.isEmpty()) {
emptyList()
} else {
exportExDates(getString(EventExportProjection.IDX_EXDATE), isAllDay, timezone)
exportExDates(getString(EventExportProjection.IDX_EXDATE), isAllDay, timezone, dtStart)
},
location = getString(EventExportProjection.IDX_LOCATION),
description = getString(EventExportProjection.IDX_DESCRIPTION),
@@ -94,21 +97,22 @@ internal fun exportExDates(
exdate: String?,
isAllDay: Boolean,
eventTimezone: String? = null,
seriesStartMillis: Long? = null,
): List<String> {
if (exdate.isNullOrBlank()) return emptyList()
// Several properties are stored one per line, each with its own zone prefix.
return exdate.lines().filter { it.isNotBlank() }.flatMap { line ->
val zone = exdateListZone(line, eventTimezone)
exdateStamps(line).map { stamp ->
if (isAllDay) {
stamp.substringBefore('T')
} else {
canonicalExdateStamp(stamp, isAllDay = false, listZone = zone) ?: stamp
exdateStamps(line).mapNotNull { stamp ->
when {
isAllDay -> stamp.substringBefore('T')
// A bare day on a timed series means the occurrence at the series' time of day.
seriesStartMillis != null && BARE_DAY.matches(stamp) -> atSeriesTime(stamp, seriesStartMillis, zone)
else -> canonicalExdateStamp(stamp, isAllDay = false, listZone = zone) ?: stamp
}
}
}
// A bare day on a timed series stays: a restore reads it at the series' time of day.
.filter { EXPORTED_STAMP.matches(it) }
.filter { (if (isAllDay) BARE_DAY else UTC_STAMP).matches(it) }
.distinct()
}
@@ -122,6 +126,7 @@ internal fun exportRecurrenceDates(
rdate: String?,
isAllDay: Boolean,
eventTimezone: String? = null,
seriesStartMillis: Long? = 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.
@@ -129,9 +134,16 @@ internal fun exportRecurrenceDates(
val stamps = line.withoutExdateParameters()
line.dropLast(stamps.length) + stamps.split(',').joinToString(",") { it.substringBefore('/') }
}
return exportExDates(starts, isAllDay, eventTimezone)
return exportExDates(starts, isAllDay, eventTimezone, seriesStartMillis)
}
/** What a file and a provider both read back: a day, or a UTC stamp. */
private val EXPORTED_STAMP = Regex("""\d{8}(T\d{6}Z)?""")
/** [day] (`yyyyMMdd`) at the wall-clock time the series starts at in [zone], as a UTC stamp. */
private fun atSeriesTime(day: String, seriesStartMillis: Long, zone: ZoneId): String? {
val date = runCatching { LocalDate.parse(day, DateTimeFormatter.BASIC_ISO_DATE) }.getOrNull() ?: return null
val time = Instant.ofEpochMilli(seriesStartMillis).atZone(zone).toLocalTime()
return formatExdateStamp(date.atTime(time).atZone(zone).toInstant().toEpochMilli(), isAllDay = false)
}
private val BARE_DAY = Regex("""\d{8}""")
private val UTC_STAMP = Regex("""\d{8}T\d{6}Z""")
@@ -18,15 +18,25 @@ fun planCalendarCopy(
rows: CalendarRows,
exdateStamp: (occurrenceMillis: Long, isAllDay: Boolean) -> String,
): List<ParsedIcsEvent> {
val declinedSeries = rows.series.filter { it.declined }
val declinedIds = declinedSeries.map { it.eventId }.toSet()
val declinedSyncIds = declinedSeries.filter { it.syncId != null }.map { it.calendarId to it.syncId }.toSet()
fun OverrideRow.ofDeclinedSeries() =
originalId in declinedIds || (originalSyncId != null && (calendarId to originalSyncId) in declinedSyncIds)
val live = CalendarRows(
series = rows.series.filterNot { it.event.status == EventStatus.Cancelled },
// A declined occurrence is as good as deleted here.
// A declined occurrence is as good as deleted here, and so is one of a
// declined series the user didn't accept on its own.
overrides = rows.overrides.map {
if (it.declined) it.copy(event = it.event.copy(status = EventStatus.Cancelled)) else it
if (it.declined || (it.ofDeclinedSeries() && !it.accepted)) {
it.copy(event = it.event.copy(status = EventStatus.Cancelled))
} else {
it
}
},
)
// A declined series still hands its accepted occurrences on before it goes.
val declined = rows.series.filter { it.declined }.map { it.event.uid }.toSet()
val declined = declinedSeries.map { it.event.uid }.toSet()
return flattenOverrides(live, exdateStamp)
.filterNot { it.overrideOf == null && it.uid in declined }
.map { it.toParsedIcsEvent() }
@@ -29,6 +29,8 @@ data class OverrideRow(
val event: IcsEvent,
/** The user declined this occurrence. */
val declined: Boolean = false,
/** The user accepted (or tentatively accepted) this occurrence on its own. */
val accepted: Boolean = false,
) {
val isCancelled: Boolean get() = event.status == EventStatus.Cancelled
}
@@ -318,4 +318,15 @@ class IcsExportMapperTest {
assertThat(exportExDates("Europe/Berlin;20260110T100000\nEurope/Berlin;20260117T100000,junk", isAllDay = false))
.containsExactly("20260110T090000Z", "20260117T090000Z").inOrder()
}
@Test
fun `a bare day on a timed series is read at the series' time of day`() {
val tenBerlin = java.time.ZonedDateTime.of(2026, 1, 5, 10, 0, 0, 0, java.time.ZoneId.of("Europe/Berlin"))
.toInstant().toEpochMilli()
assertThat(exportExDates("20260112", isAllDay = false, "Europe/Berlin", tenBerlin))
.containsExactly("20260112T090000Z")
assertThat(exportRecurrenceDates("20260712", isAllDay = false, "Europe/Berlin", tenBerlin))
.containsExactly("20260712T080000Z")
}
}
@@ -215,7 +215,10 @@ class CalendarRowsTest {
fun `a copy leaves declined invitations out but keeps an occurrence accepted on its own`() {
val rows = CalendarRows(
listOf(series("d").copy(declined = true), series("k", id = 2)),
listOf(OverrideRow(1, 1, null, occurrence(1), event("accepted"))),
listOf(
OverrideRow(1, 1, null, occurrence(1), event("accepted"), accepted = true),
OverrideRow(1, 1, null, occurrence(2), event("moved by the organizer")),
),
)
val plan = planCalendarCopy(rows, ::formatExdateStamp)
+5 -2
View File
@@ -522,7 +522,9 @@ as before.
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), line by line where several `RDATE`s were
stored; a `PERIOD` entry keeps its start. `IcsWriter`
stored; a `PERIOD` entry keeps its start, and a bare day on a timed series
becomes the occurrence at the series' time of day, so the copy (which never
goes through the parser) and a file get the same stamps. `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
@@ -535,7 +537,8 @@ what landed before. It skips the `.ics` text: the writer pins a timed one-off to
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 (an insert can't be cancelled; it would arrive live, and
a declined occurrence counts as deleted). A re-run is idempotent per
a declined occurrence counts as deleted, as does one of a declined series the
user didn't accept on its own). 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.