feat(shared): add KMP delete_note.php and delegate Android deleteNote

This commit is contained in:
2025-09-06 19:29:46 +02:00
parent d30b3a84c2
commit 3c432cde2f
2 changed files with 47 additions and 11 deletions

View File

@@ -417,22 +417,17 @@ suspend fun createNoteDoc(
)
}
/**
* Raderar (soft delete) en not du äger. Returnerar true om { ok: true }.
*/
suspend fun deleteNote(id: Long): Boolean {
if (Auth.idToken == null) return false
val payload = mapOf("id" to id)
val resp = try { Api.svc.deleteNote(payload) } catch (_: Throwable) { return false }
if (!resp.isSuccessful) return false
val okField = resp.body()?.get("ok")
return when (okField) {
is Boolean -> okField
is Number -> okField.toInt() != 0
is String -> okField.equals("true", true) || okField == "1" || okField.equals("ok", true)
else -> true
}
return KlottrApiShared.deleteNote(
baseUrl = ApiConfig.SERVER_API_BASE_URL,
apiKey = ApiConfig.SERVER_API_KEY,
bearerToken = Auth.idToken,
id = id
)
}

View File

@@ -77,6 +77,11 @@ object KlottrApiShared {
val clientRequestId: String? = null
)
@kotlinx.serialization.Serializable
private data class DeleteNoteBodyShared(
val id: Long
)
private fun ensureSlash(u: String): String = if (u.endsWith("/")) u else "$u/"
@@ -427,4 +432,40 @@ object KlottrApiShared {
val reason = obj["reason"]?.jsonPrimitive?.contentOrNull
return UsernameSetResp(ok = ok, username = uname, reason = reason)
}
/**
* KMP-variant av delete_note.php (POST JSON { id }). Returnerar true om { ok: true }.
*/
suspend fun deleteNote(
baseUrl: String,
apiKey: String,
bearerToken: String?,
id: Long
): Boolean {
val url = ensureSlash(baseUrl) + "delete_note.php"
val payload = DeleteNoteBodyShared(id)
val resp = try {
client.post(url) {
contentType(ContentType.Application.Json)
setBody(payload)
header("X-API-Key", apiKey)
if (bearerToken != null) header(HttpHeaders.Authorization, "Bearer $bearerToken")
}
} catch (_: Throwable) {
return false
}
if (resp.status.value != 200) return false
val obj = try { resp.body<JsonObject>() } catch (_: Throwable) { return false }
val okPrim = obj["ok"]?.jsonPrimitive
return when {
okPrim == null -> true
okPrim.booleanOrNull != null -> okPrim.boolean
okPrim.longOrNull != null -> okPrim.long != 0L
okPrim.contentOrNull != null -> okPrim.content.equals("true", true) || okPrim.content == "1" || okPrim.content.equals("ok", true)
else -> true
}
}
}