feat: Delegate homeserver

Now if matrix homeserver on matrix.example.com, example.com will be correct too
This commit is contained in:
2025-03-03 18:42:57 +03:00
parent e70049f1f5
commit cab56d6329
4 changed files with 32 additions and 22 deletions
@@ -1,7 +1,7 @@
/*
* Created by sweetbread on 22.02.2025, 17:28
* Created by sweetbread
* Copyright (c) 2025. All rights reserved.
* Last modified 22.02.2025, 17:28
* Last modified 03.03.2025, 15:40
*/
package ru.risdeveau.pixeldragon.api
@@ -13,19 +13,24 @@ import org.json.JSONObject
import ru.risdeveau.pixeldragon.client
import ru.risdeveau.pixeldragon.homeserver
suspend fun isMatrixServer(url: String): Boolean {
suspend fun getHomeserver(url: String): String? {
val r = try { client.get("https://$url/.well-known/matrix/client") }
catch (_: Exception) { return false }
catch (_: Exception) { return null }
try { JSONObject(r.bodyAsText()) }
catch (_: JSONException) { return false }
val json = try { JSONObject(r.bodyAsText()) }
catch (_: JSONException) { return null }
return true
if (!json.has("m.homeserver")) return null
var homeserver = json.getJSONObject("m.homeserver").getString("base_url")
if (homeserver.endsWith("/")) homeserver = homeserver.dropLast(1)
return homeserver
}
fun mxcToUrl(mxc: String): String {
val pattern = Regex("mxc://([-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6})/([a-zA-Z]+)")
val match = pattern.find(mxc)
return "https://$homeserver/_matrix/client/v1/media/download/${match?.groupValues[1]}/${match?.groupValues[2]}"
return "$homeserver/_matrix/client/v1/media/download/${match?.groupValues[1]}/${match?.groupValues[2]}"
}