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]}"
}
@@ -1,7 +1,7 @@
/*
* Created by sweetbread on 22.02.2025, 15:45
* Created by sweetbread
* Copyright (c) 2025. All rights reserved.
* Last modified 22.02.2025, 15:45
* Last modified 03.03.2025, 15:43
*/
package ru.risdeveau.pixeldragon.api
@@ -41,8 +41,10 @@ suspend fun getMe(): Me? {
}
@SuppressLint("ApplySharedPref")
suspend fun login(homeserver: String, login: String, pass: String): Boolean {
val pinfo = appCtx.packageManager.getPackageInfo(appCtx.packageName, 0);
suspend fun login(server: String, login: String, pass: String): Boolean {
val homeserver = getHomeserver(server)!!
val pinfo = appCtx.packageManager.getPackageInfo(appCtx.packageName, 0)
val pattern = """
{
@@ -58,7 +60,7 @@ suspend fun login(homeserver: String, login: String, pass: String): Boolean {
json.put("password", pass)
val r = try {
client.post("https://$homeserver/_matrix/client/v3/login") {
client.post("$homeserver/_matrix/client/v3/login") {
setBody(json.toString())
contentType(ContentType.Application.Json)
}
@@ -75,7 +77,7 @@ suspend fun login(homeserver: String, login: String, pass: String): Boolean {
val res = JSONObject(r.bodyAsText())
val editor = accountData.edit()
editor.putString("token", res.getString("access_token"))
editor.putString("homeserver", res.getString("home_server"))
editor.putString("homeserver", homeserver)
editor.commit()
return initCheck()