Fix /user/{uid} endoints.

- ChzzkApis.kt moved to common
- ChzzkApis.kt response data is nullable data.
- if require {uid}'s data, getStreamInfo function called.
This commit is contained in:
dalbodeule
2024-08-04 20:27:54 +09:00
parent 514ab14c3c
commit 6da0662e2a
8 changed files with 47 additions and 138 deletions

View File

@@ -4,16 +4,8 @@ import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
import org.koin.java.KoinJavaComponent.inject
import space.mori.chzzk_bot.common.events.CoroutinesEventBus
import space.mori.chzzk_bot.common.events.GetUserEvents
import space.mori.chzzk_bot.common.events.GetUserType
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedQueue
import space.mori.chzzk_bot.common.utils.getStreamInfo
@Serializable
data class GetUserDTO(
@@ -23,33 +15,11 @@ data class GetUserDTO(
val avatarUrl: String,
)
fun GetUserEvents.toDTO(): GetUserDTO {
return GetUserDTO(
this.uid!!,
this.nickname!!,
this.isStreamOn!!,
this.avatarUrl!!
)
}
fun Routing.apiRoutes() {
val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
val callMap = ConcurrentHashMap<String, ConcurrentLinkedQueue<ApplicationCall>>()
fun addCall(uid: String, call: ApplicationCall) {
callMap.computeIfAbsent(uid) { ConcurrentLinkedQueue() }.add(call)
}
fun removeCall(uid: String, call: ApplicationCall) {
callMap[uid]?.remove(call)
if(callMap[uid]?.isEmpty() == true) {
callMap.remove(uid)
}
}
route("/") {
get {
call.respondText("Hello World!", status = HttpStatusCode.OK)
call.respondText("Hello World!", status =
HttpStatusCode.OK)
}
}
route("/health") {
@@ -57,38 +27,31 @@ fun Routing.apiRoutes() {
call.respondText("OK", status= HttpStatusCode.OK)
}
}
route("/user/{uid}") {
get {
val uid = call.parameters["uid"]
if(uid == null) {
call.respondText("Require UID", status = HttpStatusCode.NotFound)
return@get
}
val user = getStreamInfo(uid)
if(user.content == null) {
call.respondText("User not found", status = HttpStatusCode.NotFound)
return@get
} else {
call.respond(HttpStatusCode.OK, GetUserDTO(
user.content!!.channel.channelId,
user.content!!.channel.channelName,
user.content!!.status == "OPEN",
user.content!!.channel.channelImageUrl
))
}
}
}
route("/user") {
get {
call.respondText("Require UID", status = HttpStatusCode.NotFound)
}
get("{uid}") {
val uid = call.parameters["uid"]
if(uid != null) {
addCall(uid, call)
if(!callMap.containsKey(uid)) {
CoroutineScope(Dispatchers.Default).launch {
dispatcher.post(GetUserEvents(GetUserType.REQUEST, null, null, null, null))
}
}
}
}
}
dispatcher.subscribe(GetUserEvents::class) {
if(it.type == GetUserType.REQUEST) return@subscribe
CoroutineScope(Dispatchers.Default). launch {
if (it.type == GetUserType.NOTFOUND) {
callMap[it.uid]?.forEach { call ->
call.respondText("User not found", status = HttpStatusCode.NotFound)
removeCall(it.uid ?: "", call)
}
return@launch
}
callMap[it.uid]?.forEach { call ->
call.respond(HttpStatusCode.OK, it.toDTO())
removeCall(it.uid ?: "", call)
}
}
}
}