Ask the principal for its home sets before deleting a list, compare collection URLs by meaning (#180)

This commit is contained in:
makiolaj committed 2026-11-25 15:20:26 +01:00
1 parent 1c59c5e409
commit 91c6593ceb
9 files changed
+109 -9

No files matched your search

@@ -1,5 +1,6 @@
package de.jeanlucmakiola.agendula.data.sync
import de.jeanlucmakiola.agendula.data.CollectionHref
import de.jeanlucmakiola.agendula.data.di.IoDispatcher
import de.jeanlucmakiola.agendula.data.sync.RemoteLists.Outcome
import de.jeanlucmakiola.agendula.data.sync.push.PushStore
@@ -205,8 +206,15 @@ class RemoteListRepository @Inject constructor(
val url = list.href?.toHttpUrlOrNull() ?: return@withContext Outcome.NoAccount
val account = list.accountId?.let { database.accounts().account(it) }
?: return@withContext Outcome.NoAccount
if (isHomeSet(list.href, account.homeSetUrl)) return@withContext Outcome.AccountHome
if (CollectionHref.same(list.href, account.homeSetUrl)) return@withContext Outcome.AccountHome
val admin = adminFor(account) ?: return@withContext Outcome.NoAccount
// ⚠️ Asked again, not trusted from the row: only the first home set is
// stored, and deleting any of them takes the calendar's events too (#180).
val principal = account.principalUrl?.toHttpUrlOrNull() ?: return@withContext Outcome.NoAccount
val homeSets = admin.homeSets(principal) ?: return@withContext Outcome.Unreachable
if (homeSets.any { CollectionHref.same(it.toString(), list.href) }) {
return@withContext Outcome.AccountHome
}
when (val outcome = admin.delete(url)) {
CollectionOutcome.Updated -> {
@@ -0,0 +1,28 @@
package de.jeanlucmakiola.agendula.data
import java.net.URI
/** Compares collection URLs the way a server means them, not the way it spells them. */
object CollectionHref {
/** Whether [a] and [b] name the same collection: case, default port, escaping and a trailing slash aside. */
fun same(a: String?, b: String?): Boolean {
val left = a?.let(::key) ?: return false
return left == b?.let(::key)
}
private fun key(href: String): String? = runCatching {
val uri = URI(href)
val scheme = uri.scheme?.lowercase() ?: return null
val port = when {
uri.port != -1 -> uri.port
scheme == "https" -> HTTPS_PORT
scheme == "http" -> HTTP_PORT
else -> -1
}
"$scheme://${uri.host?.lowercase()}:$port${uri.path.orEmpty().trimEnd('/')}"
}.getOrNull()
private const val HTTPS_PORT = 443
private const val HTTP_PORT = 80
}
@@ -53,7 +53,3 @@ interface RemoteLists {
suspend fun delete(listId: Long): Outcome
}
/** Whether a list at [href] is the account's home set at [homeSetUrl] itself. */
fun isHomeSet(href: String?, homeSetUrl: String?): Boolean =
href != null && homeSetUrl != null && href.trimEnd('/') == homeSetUrl.trimEnd('/')
@@ -1,6 +1,6 @@
package de.jeanlucmakiola.agendula.data.tasks.room
import de.jeanlucmakiola.agendula.data.sync.isHomeSet
import de.jeanlucmakiola.agendula.data.CollectionHref
import de.jeanlucmakiola.agendula.domain.LocalAccount
import de.jeanlucmakiola.agendula.domain.Task
import de.jeanlucmakiola.agendula.domain.TaskList
@@ -30,7 +30,7 @@ object RoomTaskMapper {
owner = row.list.owner,
accountId = row.list.accountId,
isReadOnly = row.list.isReadOnly,
isAccountHome = isHomeSet(row.list.href, row.accountHomeSetUrl),
isAccountHome = CollectionHref.same(row.list.href, row.accountHomeSetUrl),
)
/**
@@ -186,7 +186,7 @@ fun ListEditorSheet(
if (deletable) {
Spacer(Modifier.height(24.dp))
DeleteRow(onClick = { confirmDelete = true })
} else if (initial?.isAccountHome == true) {
} else if (initial?.isAccountHome == true && !readOnly) {
Spacer(Modifier.height(24.dp))
ListNote(stringResource(R.string.list_delete_account_home))
}
@@ -429,7 +429,7 @@ fun TaskListScreen(
// owns it — and which the server would refuse anyway. Present for
// the account's own collections, where it now deletes on the server
// as well as here.
onDelete = if (list.isReadOnly || list.isAccountHome) {
onDelete = if (list.isReadOnly) {
null
} else {
{ viewModel.deleteList(list.id, list.accountId) }
@@ -0,0 +1,23 @@
package de.jeanlucmakiola.agendula.data
import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.Test
class CollectionHrefTest {
@Test
fun `spelling differences name the same collection`() {
val home = "https://dav.example.com/calendars/user%40example.com/1/"
assertThat(CollectionHref.same(home, "https://dav.example.com/calendars/user@example.com/1")).isTrue()
assertThat(CollectionHref.same(home, "https://DAV.example.com:443/calendars/user%40example.com/1/")).isTrue()
}
@Test
fun `a different path, port or a missing side is not the same`() {
val home = "https://dav.example.com/calendars/user/1/"
assertThat(CollectionHref.same(home, "https://dav.example.com/calendars/user/1/tasks/")).isFalse()
assertThat(CollectionHref.same(home, "https://dav.example.com:8443/calendars/user/1/")).isFalse()
assertThat(CollectionHref.same(home, null)).isFalse()
assertThat(CollectionHref.same(null, null)).isFalse()
}
}
@@ -5,6 +5,7 @@ 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.CalendarHomeSet
import at.bitfire.dav4jvm.property.ResourceType
import okhttp3.HttpUrl
import okhttp3.MediaType.Companion.toMediaType
@@ -95,6 +96,9 @@ interface CollectionAdmin {
/** DELETE. The tasks go with it, on the server and everywhere else. */
fun delete(url: HttpUrl): CollectionOutcome
/** The `calendar-home-set` hrefs [principal] reports now, or null when it did not answer. */
fun homeSets(principal: HttpUrl): List<HttpUrl>?
}
class DavCollectionAdmin(
@@ -135,6 +139,17 @@ class DavCollectionAdmin(
CollectionSupport.NONE
}
override fun homeSets(principal: HttpUrl): List<HttpUrl>? {
val resource = DavResource(httpClient, principal)
val hrefs = mutableListOf<String>()
runCatching {
resource.propfind(0, CalendarHomeSet.NAME) { response, _ ->
response[CalendarHomeSet::class.java]?.hrefs?.let(hrefs::addAll)
}
}.onFailure { return null }
return hrefs.mapNotNull { resource.location.resolve(it) }
}
/**
* A home set that is itself a calendar, as on all-inkl.com (#180), cannot
* hold another. A probe that fails leaves the OPTIONS answer standing, so
@@ -76,6 +76,36 @@ class CollectionAdminTest {
assertThat(admin.support(homeSet()).mkCalendar).isTrue()
}
@Test
fun `every home set the principal reports is returned, resolved`() {
server.enqueue(
MockResponse()
.setResponseCode(207)
.setHeader("Content-Type", "application/xml; charset=utf-8")
.setBody(
"""
<multistatus xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
<response><href>/principals/me/</href>
<propstat><prop><C:calendar-home-set>
<href>/calendars/me/1/</href><href>/calendars/me/2/</href>
</C:calendar-home-set></prop>
<status>HTTP/1.1 200 OK</status></propstat></response>
</multistatus>
""".trimIndent(),
),
)
assertThat(admin.homeSets(server.url("/principals/me/")))
.containsExactly(server.url("/calendars/me/1/"), server.url("/calendars/me/2/"))
}
@Test
fun `a principal that does not answer has no known home sets`() {
server.enqueue(MockResponse().setResponseCode(500))
assertThat(admin.homeSets(server.url("/principals/me/"))).isNull()
}
private fun resourceType(types: String) = MockResponse()
.setResponseCode(207)
.setHeader("Content-Type", "application/xml; charset=utf-8")