From a733f56d8cd39b0ef152e10994d93fa68c3c289a Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Wed, 21 Aug 2024 19:08:23 +0900 Subject: [PATCH] fix some endpoints. - In apiDiscordRoutes, guildId and channelId return string. - In wsSongListRoutes, retry 5 times, some logic changed. --- .../webserver/routes/ApiDiscordRoutes.kt | 10 +++--- .../webserver/routes/WSSongListRoutes.kt | 34 ++++++++++--------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiDiscordRoutes.kt b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiDiscordRoutes.kt index c0e464b..b961ab6 100644 --- a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiDiscordRoutes.kt +++ b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiDiscordRoutes.kt @@ -35,8 +35,8 @@ fun Route.apiDiscordRoutes() { return@get } call.respond(HttpStatusCode.OK, GuildSettings( - user.liveAlertGuild, - user.liveAlertChannel, + user.liveAlertGuild.toString(), + user.liveAlertChannel.toString(), user.liveAlertMessage )) return@get @@ -54,7 +54,7 @@ fun Route.apiDiscordRoutes() { call.respond(HttpStatusCode.BadRequest, "User does not exist") return@post } - UserService.updateLiveAlert(user, body.guildId ?: 0L, body.channelId ?: 0L, body.message) + UserService.updateLiveAlert(user, body.guildId?.toLong() ?: 0L, body.channelId?.toLong() ?: 0L, body.message) call.respond(HttpStatusCode.OK) } get("/guild/{gid}") { @@ -102,7 +102,7 @@ fun Route.apiDiscordRoutes() { @Serializable data class GuildSettings( - val guildId: Long?, - val channelId: Long?, + val guildId: String?, + val channelId: String?, val message: String? = null, ) \ No newline at end of file diff --git a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/WSSongListRoutes.kt b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/WSSongListRoutes.kt index 44745ab..63bcfa9 100644 --- a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/WSSongListRoutes.kt +++ b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/WSSongListRoutes.kt @@ -44,14 +44,15 @@ fun Routing.wsSongListRoutes() { sessions.remove(uid) } - suspend fun sendWithRetry(ws: WebSocketServerSession, res: SongResponse, maxRetries: Int, delayMillis: Long = 3000L) { + suspend fun sendWithRetry(uid: String, res: SongResponse, maxRetries: Int = 5, delayMillis: Long = 3000L) { var attempt = 0 var sentSuccessfully = false while (attempt < maxRetries && !sentSuccessfully) { + val ws = sessions[uid] try { // Attempt to send the message - ws.sendSerialized(res) + ws?.sendSerialized(res) sentSuccessfully = true // If no exception, mark as sent successfully logger.debug("Message sent successfully on attempt $attempt") } catch (e: Exception) { @@ -203,16 +204,17 @@ fun Routing.wsSongListRoutes() { CoroutineScope(Dispatchers.Default).launch { val user = UserService.getUser(it.uid) if(user != null) { - sessions[user.token ?: ""]?.let { ws -> - sendWithRetry(ws, SongResponse( - it.type.value, - it.uid, - it.reqUid, - it.name, - it.author, - it.time, - it.url - ), 3) + user.token?.let { token -> + sendWithRetry( + token, SongResponse( + it.type.value, + it.uid, + it.reqUid, + it.name, + it.author, + it.time, + it.url + )) } } } @@ -222,8 +224,9 @@ fun Routing.wsSongListRoutes() { CoroutineScope(Dispatchers.Default).launch { val user = UserService.getUser(it.uid) if(user != null) { - sessions[user.token ?: ""]?.let { ws -> - sendWithRetry(ws, SongResponse( + user.token?.let { token -> + sendWithRetry( + token, SongResponse( it.type.value, it.uid, null, @@ -231,8 +234,7 @@ fun Routing.wsSongListRoutes() { null, null, null - ), 3) - removeSession(user.token ?: "") + )) } } }