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

@@ -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
}
}
}