diff --git a/caldav/src/main/kotlin/de/jeanlucmakiola/caldav/CollectionAdmin.kt b/caldav/src/main/kotlin/de/jeanlucmakiola/caldav/CollectionAdmin.kt index 2b7fff9..611c733 100644 --- a/caldav/src/main/kotlin/de/jeanlucmakiola/caldav/CollectionAdmin.kt +++ b/caldav/src/main/kotlin/de/jeanlucmakiola/caldav/CollectionAdmin.kt @@ -2,6 +2,7 @@ package de.jeanlucmakiola.caldav import at.bitfire.dav4jvm.DavResource import at.bitfire.dav4jvm.Property +import at.bitfire.dav4jvm.Response import at.bitfire.dav4jvm.XmlUtils import at.bitfire.dav4jvm.XmlUtils.insertTag import at.bitfire.dav4jvm.property.ResourceType @@ -69,7 +70,10 @@ sealed interface CollectionOutcome { */ interface CollectionAdmin { - /** What [homeSet] supports, as an OPTIONS rather than a guess. */ + /** + * What [homeSet] supports, as an OPTIONS rather than a guess, plus a + * PROPFIND ruling out a home set that is itself a calendar. + */ fun support(homeSet: HttpUrl): CollectionSupport /** @@ -123,7 +127,7 @@ class DavCollectionAdmin( mkCalendar = "MKCALENDAR" in allow, extendedMkCol = "EXTENDED-MKCOL" in dav, ) - }.takeIf { !it.canCreate || isCalendar(homeSet) == false } ?: CollectionSupport.NONE + }.takeIf { !it.canCreate || !isCalendar(homeSet) } ?: 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 @@ -133,16 +137,21 @@ class DavCollectionAdmin( /** * 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. + * hold another. A probe that fails leaves the OPTIONS answer standing, so + * other servers behave as before. */ - private fun isCalendar(homeSet: HttpUrl): Boolean? { - var types: Set? = null + private fun isCalendar(homeSet: HttpUrl): Boolean { + var calendar = false runCatching { - DavResource(httpClient, homeSet).propfind(0, ResourceType.NAME) { response, _ -> - response[ResourceType::class.java]?.let { types = it.types } + DavResource(httpClient, homeSet).propfind(0, ResourceType.NAME) { response, relation -> + if (relation == Response.HrefRelation.SELF && + response[ResourceType::class.java]?.types?.contains(ResourceType.CALENDAR) == true + ) { + calendar = true + } } - }.onFailure { return null } - return types?.contains(ResourceType.CALENDAR) + } + return calendar } /** diff --git a/caldav/src/test/kotlin/de/jeanlucmakiola/caldav/CollectionAdminTest.kt b/caldav/src/test/kotlin/de/jeanlucmakiola/caldav/CollectionAdminTest.kt index 1ef6059..0de75f9 100644 --- a/caldav/src/test/kotlin/de/jeanlucmakiola/caldav/CollectionAdminTest.kt +++ b/caldav/src/test/kotlin/de/jeanlucmakiola/caldav/CollectionAdminTest.kt @@ -64,7 +64,7 @@ class CollectionAdminTest { } @Test - fun `a home set whose type cannot be read offers nothing`() { + fun `a home set whose type cannot be read keeps the OPTIONS answer`() { server.enqueue( MockResponse() .setResponseCode(200) @@ -73,7 +73,7 @@ class CollectionAdminTest { ) server.enqueue(MockResponse().setResponseCode(500)) - assertThat(admin.support(homeSet())).isEqualTo(CollectionSupport.NONE) + assertThat(admin.support(homeSet()).mkCalendar).isTrue() } private fun resourceType(types: String) = MockResponse()