Ask the principal for its home sets before deleting a list, compare collection URLs by meaning (#180)
This commit is contained in:
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()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user