wip: Migrate to Trixnity
This commit is contained in:
@@ -1,97 +1,4 @@
|
||||
/*
|
||||
* Created by sweetbread
|
||||
* Copyright (c) 2025. All rights reserved.
|
||||
* Copyright (c) 2026. All rights reserved.
|
||||
*/
|
||||
|
||||
package ru.risdeveau.pixeldragon.api
|
||||
|
||||
import io.ktor.client.request.bearerAuth
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import org.json.JSONObject
|
||||
import ru.risdeveau.pixeldragon.baseUrl
|
||||
import ru.risdeveau.pixeldragon.client
|
||||
import ru.risdeveau.pixeldragon.repo.Room
|
||||
import ru.risdeveau.pixeldragon.repo.User
|
||||
import ru.risdeveau.pixeldragon.token
|
||||
|
||||
//fun getRooms(): List<Room> {
|
||||
// return db.roomDoa().getAllJoined()
|
||||
//}
|
||||
//
|
||||
//fun updateRooms(): List<Room> {
|
||||
//
|
||||
//}
|
||||
|
||||
suspend fun getJoinedRooms(): List<Room> {
|
||||
val r = client.get("$baseUrl/joined_rooms")
|
||||
{ bearerAuth(token) }
|
||||
val rooms = JSONObject(r.bodyAsText()).getJSONArray("joined_rooms")
|
||||
return List(
|
||||
rooms.length()
|
||||
) { i -> getRoom(rooms.getString(i), true) }
|
||||
}
|
||||
|
||||
suspend fun isJoined(id: String): Boolean {
|
||||
val r = client.get("$baseUrl/joined_rooms")
|
||||
{ bearerAuth(token) }
|
||||
val rooms = JSONObject(r.bodyAsText()).getJSONArray("joined_rooms")
|
||||
return List<String>(
|
||||
rooms.length()
|
||||
) { i -> rooms.getString(i) }.contains(id)
|
||||
}
|
||||
|
||||
suspend fun getRoom(rid: String, joined: Boolean? = null): Room {
|
||||
val direct = getAccountData(getMe()!!.userId, "m.direct")
|
||||
var directWith = ""
|
||||
direct?.let {
|
||||
for (user in direct.keys()) {
|
||||
val roomsWithUser = direct.getJSONArray(user)
|
||||
for (i in 0 until roomsWithUser.length()) {
|
||||
if (rid == roomsWithUser.getString(i)) {
|
||||
directWith = user
|
||||
break
|
||||
}
|
||||
}
|
||||
if (directWith.isNotEmpty()) break
|
||||
}
|
||||
}
|
||||
|
||||
return coroutineScope {
|
||||
val name = async { getState(rid, "m.room.name", "name") }
|
||||
val type = async { getState(rid, "m.room.create", "type") ?: "m.room" }
|
||||
val creator = async { getState(rid, "m.room.create", "creator") }
|
||||
val avatar = async { getState(rid, "m.room.avatar", "url") }
|
||||
val joined = async { joined ?: isJoined(rid) }
|
||||
val direct = async { if (directWith.isNotEmpty()) User.getById(directWith) else null }
|
||||
|
||||
Room(
|
||||
rid,
|
||||
name.await(),
|
||||
type.await(),
|
||||
creator.await(),
|
||||
null,
|
||||
avatar.await(),
|
||||
null,
|
||||
joined.await(),
|
||||
direct.await()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun getState(rid: String, state: String, key: String): String? {
|
||||
val r = client.get("$baseUrl/rooms/$rid/state/$state") { bearerAuth(token) }
|
||||
if (r.status != HttpStatusCode.OK) return null
|
||||
val json = JSONObject(r.bodyAsText())
|
||||
if (!json.has(key)) return null
|
||||
return json.getString(key)
|
||||
}
|
||||
|
||||
suspend fun getAccountData(user: String, room: String, state: String): JSONObject? {
|
||||
val r = client.get("$baseUrl/user/$user/rooms/$room/account_data/$state") { bearerAuth(token) }
|
||||
if (r.status != HttpStatusCode.OK) return null
|
||||
return JSONObject(r.bodyAsText())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user