100 lines
3.1 KiB
Kotlin
Executable File
100 lines
3.1 KiB
Kotlin
Executable File
/*
|
|
* Created by sweetbread
|
|
* Copyright (c) 2025. All rights reserved.
|
|
*/
|
|
|
|
package ru.risdeveau.pixeldragon.api
|
|
|
|
import android.util.Log
|
|
import io.ktor.client.request.bearerAuth
|
|
import io.ktor.client.request.get
|
|
import io.ktor.client.request.post
|
|
import io.ktor.client.request.setBody
|
|
import io.ktor.client.statement.bodyAsText
|
|
import io.ktor.http.ContentType
|
|
import io.ktor.http.HttpStatusCode
|
|
import io.ktor.http.contentType
|
|
import org.json.JSONObject
|
|
import ru.risdeveau.pixeldragon.AccountData
|
|
import ru.risdeveau.pixeldragon.baseUrl
|
|
import ru.risdeveau.pixeldragon.client
|
|
import ru.risdeveau.pixeldragon.initCheck
|
|
import ru.risdeveau.pixeldragon.token
|
|
import splitties.experimental.ExperimentalSplittiesApi
|
|
import splitties.init.appCtx
|
|
import splitties.preferences.edit
|
|
|
|
data class Me (val userId: String, val deviceId: String)
|
|
data class UserProfile (val displayName: String, val avatarUrl: String, val other: JSONObject)
|
|
|
|
/**
|
|
* This func is to validate the token
|
|
*/
|
|
suspend fun getMe(): Me? {
|
|
val r = client.get("$baseUrl/account/whoami") { bearerAuth(token) }
|
|
if (r.status != HttpStatusCode.OK) {
|
|
Log.e("getMe", r.bodyAsText())
|
|
return null
|
|
}
|
|
|
|
val json = JSONObject(r.bodyAsText())
|
|
return Me(json.getString("user_id"), json.getString("device_id"))
|
|
}
|
|
|
|
@OptIn(ExperimentalSplittiesApi::class)
|
|
suspend fun login(server: String, login: String, pass: String): Boolean {
|
|
val hs = getHomeserver(server)!!
|
|
|
|
val pinfo = appCtx.packageManager.getPackageInfo(appCtx.packageName, 0)
|
|
|
|
val pattern = """
|
|
{
|
|
"type": "m.login.password",
|
|
"identifier": {
|
|
"type": "m.id.user"
|
|
},
|
|
"initial_device_display_name": "PixelDragon Android v${pinfo.versionName}"
|
|
}
|
|
""".trimIndent()
|
|
val json = JSONObject(pattern)
|
|
json.getJSONObject("identifier").put("user", login)
|
|
json.put("password", pass)
|
|
|
|
val r = try {
|
|
client.post("$hs/_matrix/client/v3/login") {
|
|
setBody(json.toString())
|
|
contentType(ContentType.Application.Json)
|
|
}
|
|
} catch (e: Exception) {
|
|
Log.e("login", e.toString())
|
|
return false
|
|
}
|
|
|
|
if (r.status != HttpStatusCode.OK) {
|
|
Log.e("login", r.bodyAsText())
|
|
return false // TODO: Inform a user of error code
|
|
}
|
|
|
|
val res = JSONObject(r.bodyAsText())
|
|
AccountData.edit {
|
|
token = res.getString("access_token")
|
|
homeserver = hs
|
|
}
|
|
|
|
return initCheck()
|
|
}
|
|
|
|
suspend fun getAccountData(user: String, state: String): JSONObject? {
|
|
val r = client.get("$baseUrl/user/$user/account_data/$state") { bearerAuth(token) }
|
|
if (r.status != HttpStatusCode.OK) return null
|
|
return JSONObject(r.bodyAsText())
|
|
}
|
|
|
|
suspend fun getUserProfile(userId: String): UserProfile? {
|
|
val r = client.get("$baseUrl/profile/$userId") { bearerAuth(token) }
|
|
if (r.status != HttpStatusCode.OK) return null
|
|
val json = JSONObject(r.bodyAsText())
|
|
val name = json.optString("displayname", ""); json.remove("displayname")
|
|
val avatar = json.optString("avatar_url", ""); json.remove("avatar_url")
|
|
return UserProfile(name, avatar, json)
|
|
} |