mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-08-08 05:11:12 +00:00
add POST /user endpoints.
- POST /user {"chzzkUrl": String} to add chzzk account to bots. - and it post event UserRegisterEvent
This commit is contained in:
@@ -2,10 +2,17 @@ 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.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.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
|
||||
@@ -30,7 +37,15 @@ data class GetSessionDTO(
|
||||
val isStreamerOnly: Boolean,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class RegisterChzzkUserDTO(
|
||||
val chzzkUrl: String
|
||||
)
|
||||
|
||||
fun Routing.apiRoutes() {
|
||||
val chzzkIDRegex = """(?:.+chzzk\.naver\.com/)?([a-f0-9]{32})?(?:/live)?${'$'}""".toRegex()
|
||||
val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||
|
||||
route("/") {
|
||||
get {
|
||||
call.respondText("Hello World!", status =
|
||||
@@ -69,18 +84,21 @@ fun Routing.apiRoutes() {
|
||||
val session = call.sessions.get<UserSession>()
|
||||
|
||||
if(session == null) {
|
||||
call.respondText("No session found", status = HttpStatusCode.NotFound)
|
||||
call.respondText("No session found", status = HttpStatusCode.Unauthorized)
|
||||
return@get
|
||||
}
|
||||
println(session)
|
||||
val user = UserService.getUserWithNaverId(session.id)
|
||||
var user = UserService.getUserWithNaverId(session.id)
|
||||
if(user == null) {
|
||||
call.respondText("No session found", status = HttpStatusCode.NotFound)
|
||||
return@get
|
||||
user = UserService.saveUser(session.nickname, session.id)
|
||||
}
|
||||
val songConfig = SongConfigService.getConfig(user)
|
||||
val status = getStreamInfo(user.token)
|
||||
|
||||
if(status.content == null) {
|
||||
call.respondText(user.naverId, status = HttpStatusCode.NotFound)
|
||||
}
|
||||
|
||||
call.respond(HttpStatusCode.OK, GetSessionDTO(
|
||||
status.content!!.channel.channelId,
|
||||
status.content!!.channel.channelName,
|
||||
@@ -91,5 +109,45 @@ fun Routing.apiRoutes() {
|
||||
songConfig.streamerOnly
|
||||
))
|
||||
}
|
||||
post {
|
||||
val session = call.sessions.get<UserSession>()
|
||||
if(session == null) {
|
||||
call.respondText("No session found", status = HttpStatusCode.Unauthorized)
|
||||
return@post
|
||||
}
|
||||
|
||||
val body: RegisterChzzkUserDTO = call.receive()
|
||||
|
||||
val user = UserService.getUserWithNaverId(session.id)
|
||||
if(user == null) {
|
||||
call.respondText("No session found", status = HttpStatusCode.Unauthorized)
|
||||
return@post
|
||||
}
|
||||
|
||||
val matchResult = chzzkIDRegex.find(body.chzzkUrl)
|
||||
val matchedChzzkId = matchResult?.groups?.get(1)?.value
|
||||
|
||||
if (matchedChzzkId == null) {
|
||||
call.respondText("Invalid chzzk ID", status = HttpStatusCode.BadRequest)
|
||||
return@post
|
||||
}
|
||||
|
||||
val status = getStreamInfo(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
|
||||
)
|
||||
call.respondText("Done!", status = HttpStatusCode.OK)
|
||||
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
dispatcher.post(UserRegisterEvent(status.content!!.channel.channelId))
|
||||
}
|
||||
return@post
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user