feat: Delegate homeserver
Now if matrix homeserver on matrix.example.com, example.com will be correct too
This commit is contained in:
parent
e70049f1f5
commit
cab56d6329
@ -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:32
|
||||
*/
|
||||
|
||||
package ru.risdeveau.pixeldragon
|
||||
@ -42,9 +42,12 @@ suspend fun initCheck(): Boolean {
|
||||
if (!accountData.contains("token")) return false
|
||||
if (!accountData.contains("homeserver")) return false
|
||||
|
||||
|
||||
token = accountData.getString("token", "").toString()
|
||||
homeserver = accountData.getString("homeserver", "").toString()
|
||||
baseUrl = "https://$homeserver/_matrix/client/v3"
|
||||
baseUrl = "$homeserver/_matrix/client/v3"
|
||||
|
||||
Log.d("initCheck", "homeserver: $homeserver")
|
||||
|
||||
return getMe() != null
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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:27
|
||||
*/
|
||||
|
||||
package ru.risdeveau.pixeldragon.ui.activity
|
||||
@ -38,7 +38,7 @@ import androidx.compose.ui.unit.dp
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import ru.risdeveau.pixeldragon.api.isMatrixServer
|
||||
import ru.risdeveau.pixeldragon.api.getHomeserver
|
||||
import ru.risdeveau.pixeldragon.api.login
|
||||
import ru.risdeveau.pixeldragon.initCheck
|
||||
import ru.risdeveau.pixeldragon.ui.theme.PixelDragonTheme
|
||||
@ -102,7 +102,7 @@ fun LoginField(modifier: Modifier = Modifier, ok: () -> Unit, err: () -> Unit) {
|
||||
var login by remember { mutableStateOf("") }
|
||||
var pass by remember { mutableStateOf("") }
|
||||
|
||||
var hmsValid by remember { mutableStateOf(false) }
|
||||
var hmsValid: Boolean by remember { mutableStateOf(false) }
|
||||
|
||||
OutlinedTextField(
|
||||
modifier = Modifier.padding(4.dp),
|
||||
@ -142,7 +142,7 @@ fun LoginField(modifier: Modifier = Modifier, ok: () -> Unit, err: () -> Unit) {
|
||||
}
|
||||
|
||||
LaunchedEffect(homeserver) {
|
||||
scope.launch { hmsValid = isMatrixServer(homeserver) }
|
||||
scope.launch { hmsValid = (getHomeserver(homeserver) != null) }
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user