mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
debug chzzk login 2
This commit is contained in:
parent
6bf80c309f
commit
9cf85a7bef
@ -1,22 +1,15 @@
|
|||||||
package space.mori.chzzk_bot.webserver.routes
|
package space.mori.chzzk_bot.webserver.routes
|
||||||
|
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import io.ktor.server.request.*
|
|
||||||
import io.ktor.server.response.*
|
import io.ktor.server.response.*
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
import io.ktor.server.sessions.*
|
import io.ktor.server.sessions.*
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
import org.koin.java.KoinJavaComponent.inject
|
import org.koin.java.KoinJavaComponent.inject
|
||||||
import space.mori.chzzk_bot.common.events.CoroutinesEventBus
|
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.SongConfigService
|
||||||
import space.mori.chzzk_bot.common.services.UserService
|
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
|
import space.mori.chzzk_bot.webserver.UserSession
|
||||||
import space.mori.chzzk_bot.webserver.utils.ChzzkUserCache
|
import space.mori.chzzk_bot.webserver.utils.ChzzkUserCache
|
||||||
|
|
||||||
@ -40,13 +33,7 @@ data class GetSessionDTO(
|
|||||||
val isDisabled: Boolean
|
val isDisabled: Boolean
|
||||||
)
|
)
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class RegisterChzzkUserDTO(
|
|
||||||
val chzzkUrl: String
|
|
||||||
)
|
|
||||||
|
|
||||||
fun Routing.apiRoutes() {
|
fun Routing.apiRoutes() {
|
||||||
val chzzkIDRegex = """(?:.+chzzk\.naver\.com\/)?([a-f0-9]{32})(?:.+)?""".toRegex()
|
|
||||||
val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
|
|
||||||
route("/") {
|
route("/") {
|
||||||
@ -95,9 +82,14 @@ fun Routing.apiRoutes() {
|
|||||||
user = UserService.saveUser("임시닉네임", session.id)
|
user = UserService.saveUser("임시닉네임", session.id)
|
||||||
}
|
}
|
||||||
val songConfig = SongConfigService.getConfig(user)
|
val songConfig = SongConfigService.getConfig(user)
|
||||||
val status = getStreamInfo(user.token)
|
val status = ChzzkUserCache.getCachedUser(session.id)
|
||||||
val returnUsers = mutableListOf<GetSessionDTO>()
|
val returnUsers = mutableListOf<GetSessionDTO>()
|
||||||
|
|
||||||
|
if(status == null) {
|
||||||
|
call.respondText("No user found", status = HttpStatusCode.NotFound)
|
||||||
|
return@get
|
||||||
|
}
|
||||||
|
|
||||||
if (user.username == "임시닉네임") {
|
if (user.username == "임시닉네임") {
|
||||||
status.content?.channel?.let { it1 -> UserService.updateUser(user, it1.channelId, it1.channelName) }
|
status.content?.channel?.let { it1 -> UserService.updateUser(user, it1.channelId, it1.channelName) }
|
||||||
}
|
}
|
||||||
@ -136,45 +128,5 @@ fun Routing.apiRoutes() {
|
|||||||
|
|
||||||
call.respond(HttpStatusCode.OK, returnUsers)
|
call.respond(HttpStatusCode.OK, returnUsers)
|
||||||
}
|
}
|
||||||
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.getUser(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 = getUserInfo(matchedChzzkId)
|
|
||||||
if (status.content == null) {
|
|
||||||
call.respondText("Invalid chzzk ID", status = HttpStatusCode.BadRequest)
|
|
||||||
return@post
|
|
||||||
}
|
|
||||||
UserService.updateUser(
|
|
||||||
user,
|
|
||||||
status.content!!.channelId,
|
|
||||||
status.content!!.channelName
|
|
||||||
)
|
|
||||||
call.respondText("Done!", status = HttpStatusCode.OK)
|
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.Default).launch {
|
|
||||||
dispatcher.post(UserRegisterEvent(status.content!!.channelId))
|
|
||||||
}
|
|
||||||
return@post
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory
|
|||||||
import space.mori.chzzk_bot.common.utils.IData
|
import space.mori.chzzk_bot.common.utils.IData
|
||||||
import space.mori.chzzk_bot.common.utils.IStreamInfo
|
import space.mori.chzzk_bot.common.utils.IStreamInfo
|
||||||
import space.mori.chzzk_bot.common.utils.getStreamInfo
|
import space.mori.chzzk_bot.common.utils.getStreamInfo
|
||||||
|
import space.mori.chzzk_bot.common.utils.getUserInfo
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
||||||
@ -21,9 +22,20 @@ object ChzzkUserCache {
|
|||||||
|
|
||||||
if(user == null || user.timestamp.plusSeconds(EXP_SECONDS).isBefore(now)) {
|
if(user == null || user.timestamp.plusSeconds(EXP_SECONDS).isBefore(now)) {
|
||||||
mutex.withLock {
|
mutex.withLock {
|
||||||
if(user == null || user?.timestamp?.plusSeconds(EXP_SECONDS)?.isBefore(now) != false) {
|
if(user == null || user.timestamp.plusSeconds(EXP_SECONDS)?.isBefore(now) != false) {
|
||||||
user = CachedUser(getStreamInfo(id))
|
var findUser = getStreamInfo(id)
|
||||||
user?.let { cache[id] = user!! }
|
if(findUser.content == null) {
|
||||||
|
val userInfo = getUserInfo(id)
|
||||||
|
|
||||||
|
if(userInfo.content == null) return null
|
||||||
|
|
||||||
|
findUser = IData(200, null, IStreamInfo(
|
||||||
|
channel = userInfo.content!!
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
user = CachedUser(findUser)
|
||||||
|
user.let { cache[id] = user }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user