Merge pull request #110 from dalbodeule/develop

fix some endpoints.
This commit is contained in:
JinU Choi 2024-08-21 19:10:28 +09:00 committed by GitHub
commit 91e22107c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 21 deletions

View File

@ -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,
)

View File

@ -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 ?: "")
))
}
}
}