2025-02-22 18:45:55 +03:00
|
|
|
/*
|
2025-02-22 23:24:49 +03:00
|
|
|
* Created by sweetbread
|
2025-02-22 18:45:55 +03:00
|
|
|
* Copyright (c) 2025. 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
|
2025-11-05 01:23:35 +03:00
|
|
|
import kotlinx.coroutines.async
|
|
|
|
|
import kotlinx.coroutines.coroutineScope
|
2025-02-22 18:45:55 +03:00
|
|
|
import org.json.JSONObject
|
|
|
|
|
import ru.risdeveau.pixeldragon.baseUrl
|
|
|
|
|
import ru.risdeveau.pixeldragon.client
|
2025-11-04 20:30:22 +03:00
|
|
|
import ru.risdeveau.pixeldragon.repo.Room
|
|
|
|
|
import ru.risdeveau.pixeldragon.repo.User
|
2025-02-22 18:45:55 +03:00
|
|
|
import ru.risdeveau.pixeldragon.token
|
|
|
|
|
|
|
|
|
|
//fun getRooms(): List<Room> {
|
|
|
|
|
// return db.roomDoa().getAllJoined()
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//fun updateRooms(): List<Room> {
|
|
|
|
|
//
|
|
|
|
|
//}
|
|
|
|
|
|
2025-11-04 23:24:20 +03:00
|
|
|
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 {
|
2025-02-22 18:45:55 +03:00
|
|
|
val r = client.get("$baseUrl/joined_rooms")
|
|
|
|
|
{ bearerAuth(token) }
|
|
|
|
|
val rooms = JSONObject(r.bodyAsText()).getJSONArray("joined_rooms")
|
2025-02-22 20:56:15 +03:00
|
|
|
return List<String>(
|
2025-02-22 18:45:55 +03:00
|
|
|
rooms.length()
|
2025-11-04 23:24:20 +03:00
|
|
|
) { i -> rooms.getString(i) }.contains(id)
|
2025-02-22 18:45:55 +03:00
|
|
|
}
|
|
|
|
|
|
2025-11-04 23:24:20 +03:00
|
|
|
suspend fun getRoom(rid: String, joined: Boolean? = null): Room {
|
2025-11-04 16:01:14 +03:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 01:23:35 +03:00
|
|
|
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()
|
|
|
|
|
)
|
|
|
|
|
}
|
2025-02-22 18:45:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2025-03-03 23:22:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
}
|