Treat an unreadable home set as unable to hold lists, and keep a calendar home set beside event-only children (#180)

This commit is contained in:
makiolaj committed 2026-11-25 12:24:54 +01:00
1 parent eaa8f3ec74
commit 5f308b47c5
4 files changed
+50 -18

No files matched your search

@@ -2,7 +2,6 @@ package de.jeanlucmakiola.caldav
import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.Response
import at.bitfire.dav4jvm.property.ResourceType
import at.bitfire.dav4jvm.property.CalendarHomeSet
import at.bitfire.dav4jvm.exception.HttpException
import at.bitfire.dav4jvm.property.CurrentUserPrincipal
@@ -320,21 +319,21 @@ class CalDavDiscovery(
for (homeSet in homeSets.distinct()) {
var self: TaskCollection? = null
var hasMemberCalendars = false
var hasMemberLists = false
val result = runCatching {
DavResource(httpClient, homeSet).propfind(1, *collectionProperties) { response, relation ->
if (relation == Response.HrefRelation.SELF) {
if (self == null) self = CollectionClassifier.classify(response)
self = self ?: CollectionClassifier.classify(response)
return@propfind
}
if (response[ResourceType::class.java]?.types?.contains(ResourceType.CALENDAR) == true) {
hasMemberCalendars = true
CollectionClassifier.classify(response)?.let {
collections[it.url] = it
hasMemberLists = true
}
CollectionClassifier.classify(response)?.let { collections[it.url] = it }
}
// A home set that is itself a calendar with none inside it is the
// account's one list, as on all-inkl.com (#180).
self?.takeIf { !hasMemberCalendars }?.let { collections.putIfAbsent(it.url, it) }
// A home set that is itself a task calendar with none inside it is
// the account's one list, as on all-inkl.com (#180).
self?.takeIf { !hasMemberLists }?.let { collections.putIfAbsent(it.url, it) }
}
result.fold(
// A failed home set must not fail the account — the same rule the
@@ -123,7 +123,7 @@ class DavCollectionAdmin(
mkCalendar = "MKCALENDAR" in allow,
extendedMkCol = "EXTENDED-MKCOL" in dav,
)
}.takeIf { !it.canCreate || !isCalendar(homeSet) } ?: CollectionSupport.NONE
}.takeIf { !it.canCreate || isCalendar(homeSet) == false } ?: CollectionSupport.NONE
} catch (_: IOException) {
// Unknown reads as "no", which costs the user an affordance they can
// reach again on the next attempt — the other way round costs them a
@@ -131,17 +131,18 @@ class DavCollectionAdmin(
CollectionSupport.NONE
}
/** A home set that is itself a calendar, as on all-inkl.com (#180), cannot hold another. */
private fun isCalendar(homeSet: HttpUrl): Boolean {
var calendar = false
/**
* A home set that is itself a calendar, as on all-inkl.com (#180), cannot
* hold another. Null when the server did not say, which reads as no.
*/
private fun isCalendar(homeSet: HttpUrl): Boolean? {
var types: Set<Property.Name>? = null
runCatching {
DavResource(httpClient, homeSet).propfind(0, ResourceType.NAME) { response, _ ->
if (response[ResourceType::class.java]?.types?.contains(ResourceType.CALENDAR) == true) {
calendar = true
}
response[ResourceType::class.java]?.let { types = it.types }
}
}
return calendar
}.onFailure { return null }
return types?.contains(ResourceType.CALENDAR)
}
/**
@@ -302,6 +302,24 @@ class CalDavDiscoveryTest {
assertThat(found.collections.map { it.url.encodedPath }).containsExactly("/calendars/user/1/tasks/")
}
@Test
fun `a calendar home set holding only an events calendar is still offered`() {
server.enqueue(homeSetResponse("/calendars/user/1/"))
server.enqueue(
calendarAsHomeSet(
eventsAndTasks,
collection(
"/calendars/user/1/holidays/",
"<collection/><CAL:calendar/>",
"""<CAL:supported-calendar-component-set><CAL:comp name="VEVENT"/></CAL:supported-calendar-component-set>""",
),
),
)
val found = discovery.fromPrincipal(server.url("/calendars/principals/user/")) as CalDavDiscovery.Outcome.Found
assertThat(found.collections.map { it.url.encodedPath }).containsExactly("/calendars/user/1/")
}
@Test
fun `a calendar home set holding only non-calendar collections is still offered`() {
server.enqueue(homeSetResponse("/calendars/user/1/"))
@@ -63,6 +63,19 @@ class CollectionAdminTest {
assertThat(admin.support(homeSet())).isEqualTo(CollectionSupport.NONE)
}
@Test
fun `a home set whose type cannot be read offers nothing`() {
server.enqueue(
MockResponse()
.setResponseCode(200)
.setHeader("Allow", "OPTIONS, GET, PROPFIND, MKCALENDAR")
.setHeader("DAV", "1, 2, 3, calendar-access"),
)
server.enqueue(MockResponse().setResponseCode(500))
assertThat(admin.support(homeSet())).isEqualTo(CollectionSupport.NONE)
}
private fun resourceType(types: String) = MockResponse()
.setResponseCode(207)
.setHeader("Content-Type", "application/xml; charset=utf-8")
@@ -89,6 +102,7 @@ class CollectionAdminTest {
// affordance has to be gone rather than answering 405 at the end of a
// form the user has already filled in.
assertThat(admin.support(homeSet()).canCreate).isFalse()
assertThat(server.requestCount).isEqualTo(1)
}
@Test