mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
commit
fd281b8271
@ -146,3 +146,22 @@ fun getStreamInfo(userId: String) : IData<IStreamInfo?> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getUserInfo(userId: String): IData<Channel?> {
|
||||
val url = "https://api.chzzk.naver.com/service/v1/channels/${userId}"
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.build()
|
||||
|
||||
client.newCall(request).execute().use { response ->
|
||||
try {
|
||||
if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}")
|
||||
val body = response.body?.string()
|
||||
val channel = gson.fromJson(body, object: TypeToken<IData<Channel?>>() {})
|
||||
|
||||
return channel
|
||||
} catch(e: Exception) {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -111,6 +111,7 @@ val server = embeddedServer(Netty, port = 8080, ) {
|
||||
apiRoutes()
|
||||
apiSongRoutes()
|
||||
apiCommandRoutes()
|
||||
apiTimerRoutes()
|
||||
|
||||
wsTimerRoutes()
|
||||
wsSongRoutes()
|
||||
|
@ -16,6 +16,7 @@ import space.mori.chzzk_bot.common.events.UserRegisterEvent
|
||||
import space.mori.chzzk_bot.common.services.SongConfigService
|
||||
import space.mori.chzzk_bot.common.services.UserService
|
||||
import space.mori.chzzk_bot.common.utils.getStreamInfo
|
||||
import space.mori.chzzk_bot.common.utils.getUserInfo
|
||||
import space.mori.chzzk_bot.webserver.UserSession
|
||||
|
||||
@Serializable
|
||||
@ -133,20 +134,20 @@ fun Routing.apiRoutes() {
|
||||
return@post
|
||||
}
|
||||
|
||||
val status = getStreamInfo(matchedChzzkId)
|
||||
val status = getUserInfo(matchedChzzkId)
|
||||
if (status.content == null) {
|
||||
call.respondText("Invalid chzzk ID", status = HttpStatusCode.BadRequest)
|
||||
return@post
|
||||
}
|
||||
UserService.updateUser(
|
||||
user,
|
||||
status.content!!.channel.channelId,
|
||||
status.content!!.channel.channelName
|
||||
status.content!!.channelId,
|
||||
status.content!!.channelName
|
||||
)
|
||||
call.respondText("Done!", status = HttpStatusCode.OK)
|
||||
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
dispatcher.post(UserRegisterEvent(status.content!!.channel.channelId))
|
||||
dispatcher.post(UserRegisterEvent(status.content!!.channelId))
|
||||
}
|
||||
return@post
|
||||
}
|
||||
|
@ -0,0 +1,63 @@
|
||||
package space.mori.chzzk_bot.webserver.routes
|
||||
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.sessions.*
|
||||
import kotlinx.serialization.Serializable
|
||||
import space.mori.chzzk_bot.common.events.TimerType
|
||||
import space.mori.chzzk_bot.common.services.TimerConfigService
|
||||
import space.mori.chzzk_bot.common.services.UserService
|
||||
import space.mori.chzzk_bot.webserver.UserSession
|
||||
|
||||
fun Routing.apiTimerRoutes() {
|
||||
route("/timerapi") {
|
||||
get("/{uid}") {
|
||||
val uid = call.parameters["uid"]
|
||||
val session = call.sessions.get<UserSession>()
|
||||
if(uid == null) {
|
||||
call.respond(HttpStatusCode.BadRequest, "UID is required")
|
||||
return@get
|
||||
}
|
||||
val user = UserService.getUser(uid)
|
||||
if(user == null || user.naverId != session?.id) {
|
||||
call.respond(HttpStatusCode.BadRequest, "User does not exist")
|
||||
return@get
|
||||
}
|
||||
|
||||
val timerConfig = TimerConfigService.getConfig(user)
|
||||
|
||||
call.respond(HttpStatusCode.OK, TimerResponseDTO(timerConfig?.option ?: 0))
|
||||
}
|
||||
|
||||
put("/{uid}") {
|
||||
val uid = call.parameters["uid"]
|
||||
val session = call.sessions.get<UserSession>()
|
||||
val request = call.receive<TimerRequestDTO>()
|
||||
if(uid == null) {
|
||||
call.respond(HttpStatusCode.BadRequest, "UID is required")
|
||||
return@put
|
||||
}
|
||||
val user = UserService.getUser(uid)
|
||||
if(user == null || user.naverId != session?.id) {
|
||||
call.respond(HttpStatusCode.BadRequest, "User does not exist")
|
||||
return@put
|
||||
}
|
||||
|
||||
TimerConfigService.saveOrUpdateConfig(user, TimerType.entries[request.option])
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class TimerRequestDTO(
|
||||
val option: Int
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class TimerResponseDTO(
|
||||
val option: Int
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user