From 36d0ed82d655352897b5898e9eb7f61dd0f68afe Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Sat, 24 Aug 2024 13:31:50 +0900 Subject: [PATCH 1/2] fix SongLists - add currentSong object - change some event, dto. --- .../chzzk_bot/common/events/SongEvents.kt | 9 ++-- .../webserver/routes/ApiSongRoutes.kt | 22 ++++++++- .../webserver/routes/WSSongListRoutes.kt | 46 ++++++++++--------- .../webserver/routes/WSSongRoutes.kt | 29 ++++++------ .../chzzk_bot/webserver/utils/CurrentSong.kt | 19 ++++++++ 5 files changed, 86 insertions(+), 39 deletions(-) create mode 100644 webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/utils/CurrentSong.kt diff --git a/common/src/main/kotlin/space/mori/chzzk_bot/common/events/SongEvents.kt b/common/src/main/kotlin/space/mori/chzzk_bot/common/events/SongEvents.kt index e2961e5..5e1f81c 100644 --- a/common/src/main/kotlin/space/mori/chzzk_bot/common/events/SongEvents.kt +++ b/common/src/main/kotlin/space/mori/chzzk_bot/common/events/SongEvents.kt @@ -1,5 +1,7 @@ package space.mori.chzzk_bot.common.events +import space.mori.chzzk_bot.common.utils.YoutubeVideo + enum class SongType(var value: Int) { ADD(0), REMOVE(1), @@ -13,10 +15,9 @@ class SongEvent( val type: SongType, val reqUid: String?, val reqName: String?, - val name: String?, - val author: String?, - val time: Int?, - val url: String? + val current: YoutubeVideo? = null, + val next: YoutubeVideo? = null, + val delUrl: String? = null, ): Event { var TAG = javaClass.simpleName } \ No newline at end of file diff --git a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiSongRoutes.kt b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiSongRoutes.kt index 694744d..c9e119f 100644 --- a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiSongRoutes.kt +++ b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/ApiSongRoutes.kt @@ -8,6 +8,8 @@ import kotlinx.serialization.Serializable import space.mori.chzzk_bot.common.models.SongList import space.mori.chzzk_bot.common.services.SongListService import space.mori.chzzk_bot.common.services.UserService +import space.mori.chzzk_bot.common.utils.YoutubeVideo +import space.mori.chzzk_bot.webserver.utils.CurrentSong @Serializable data class SongsDTO( @@ -18,6 +20,11 @@ data class SongsDTO( val reqName: String ) +data class SongsResponseDTO( + val current: SongsDTO? = null, + val next: List = emptyList() +) + fun SongList.toDTO(): SongsDTO = SongsDTO( this.url, this.name, @@ -26,6 +33,14 @@ fun SongList.toDTO(): SongsDTO = SongsDTO( this.reqName ) +fun YoutubeVideo.toDTO(): SongsDTO = SongsDTO( + this.url, + this.name, + this.author, + this.length, + "" +) + fun Routing.apiSongRoutes() { route("/songs/{uid}") { get { @@ -37,7 +52,12 @@ fun Routing.apiSongRoutes() { } val songs = SongListService.getSong(user) - call.respond(HttpStatusCode.OK, songs.map { it.toDTO() }) + call.respond(HttpStatusCode.OK, + SongsResponseDTO( + CurrentSong.getSong(user)?.toDTO(), + songs.map { it.toDTO() } + ) + ) } } route("/songs") { 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 63bcfa9..57792b4 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 @@ -14,11 +14,14 @@ import kotlinx.serialization.json.Json import org.koin.java.KoinJavaComponent.inject import org.slf4j.LoggerFactory import space.mori.chzzk_bot.common.events.* +import space.mori.chzzk_bot.common.models.SongList import space.mori.chzzk_bot.common.services.SongConfigService import space.mori.chzzk_bot.common.services.SongListService import space.mori.chzzk_bot.common.services.UserService +import space.mori.chzzk_bot.common.utils.YoutubeVideo import space.mori.chzzk_bot.common.utils.getYoutubeVideo import space.mori.chzzk_bot.webserver.UserSession +import space.mori.chzzk_bot.webserver.utils.CurrentSong import java.util.concurrent.ConcurrentHashMap fun Routing.wsSongListRoutes() { @@ -90,8 +93,6 @@ fun Routing.wsSongListRoutes() { null, null, null, - null, - null, )) } removeSession(uid) @@ -140,10 +141,8 @@ fun Routing.wsSongListRoutes() { SongType.ADD, user.token, user.username, - youtubeVideo.name, - youtubeVideo.author, - youtubeVideo.length, - youtubeVideo.url + null, + youtubeVideo ) ) } @@ -160,29 +159,38 @@ fun Routing.wsSongListRoutes() { null, null, null, - 0, data.url ) ) } else if (data.type == SongType.NEXT.value) { val songList = SongListService.getSong(user) + var song: SongList? = null + var youtubeVideo: YoutubeVideo? = null + if (songList.isNotEmpty()) { - val song = songList[0] + song = songList[0] SongListService.deleteSong(user, song.uid, song.name) } + song?.let { + youtubeVideo = YoutubeVideo( + song.url, + song.name, + song.author, + song.time + ) + } dispatcher.post( SongEvent( user.token!!, SongType.NEXT, - null, - null, - null, - null, - null, - null + song?.uid, + song?.reqName, + youtubeVideo ) ) + + CurrentSong.setSong(user, youtubeVideo) } } } @@ -200,7 +208,7 @@ fun Routing.wsSongListRoutes() { } dispatcher.subscribe(SongEvent::class) { - logger.debug("SongEvent: {} / {} {}", it.uid, it.type, it.name) + logger.debug("SongEvent: {} / {} {}", it.uid, it.type, it.current?.name) CoroutineScope(Dispatchers.Default).launch { val user = UserService.getUser(it.uid) if(user != null) { @@ -210,10 +218,8 @@ fun Routing.wsSongListRoutes() { it.type.value, it.uid, it.reqUid, - it.name, - it.author, - it.time, - it.url + it.current?.toSerializable(), + it.next?.toSerializable() )) } } @@ -232,8 +238,6 @@ fun Routing.wsSongListRoutes() { null, null, null, - null, - null )) } } diff --git a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/WSSongRoutes.kt b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/WSSongRoutes.kt index 9c36651..4a87751 100644 --- a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/WSSongRoutes.kt +++ b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/routes/WSSongRoutes.kt @@ -13,6 +13,7 @@ import org.koin.java.KoinJavaComponent.inject import org.slf4j.LoggerFactory import space.mori.chzzk_bot.common.events.* import space.mori.chzzk_bot.common.services.UserService +import space.mori.chzzk_bot.common.utils.YoutubeVideo import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentLinkedQueue @@ -89,8 +90,6 @@ fun Routing.wsSongRoutes() { null, null, null, - null, - null )) } } @@ -119,16 +118,14 @@ fun Routing.wsSongRoutes() { val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java) dispatcher.subscribe(SongEvent::class) { - logger.debug("SongEvent: {} / {} {}", it.uid, it.type, it.name) + logger.debug("SongEvent: {} / {} {}", it.uid, it.type, it.current?.name) CoroutineScope(Dispatchers.Default).launch { broadcastMessage(it.uid, SongResponse( it.type.value, it.uid, it.reqUid, - it.name, - it.author, - it.time, - it.url + it.current?.toSerializable(), + it.next?.toSerializable() )) } } @@ -140,8 +137,6 @@ fun Routing.wsSongRoutes() { it.uid, null, null, - null, - null, null )) } @@ -149,13 +144,21 @@ fun Routing.wsSongRoutes() { } } +@Serializable +data class SerializableYoutubeVideo( + val url: String, + val name: String, + val author: String, + val length: Int +) + +fun YoutubeVideo.toSerializable() = SerializableYoutubeVideo(url, name, author, length) + @Serializable data class SongResponse( val type: Int, val uid: String, val reqUid: String?, - val name: String?, - val author: String?, - val time: Int?, - val url: String? + val current: SerializableYoutubeVideo? = null, + val next: SerializableYoutubeVideo? = null, ) diff --git a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/utils/CurrentSong.kt b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/utils/CurrentSong.kt new file mode 100644 index 0000000..d600253 --- /dev/null +++ b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/utils/CurrentSong.kt @@ -0,0 +1,19 @@ +package space.mori.chzzk_bot.webserver.utils + +import space.mori.chzzk_bot.common.models.User +import space.mori.chzzk_bot.common.utils.YoutubeVideo +import java.util.concurrent.ConcurrentHashMap + +object CurrentSong { + private val currentSong = ConcurrentHashMap() + + fun setSong(user: User, song: YoutubeVideo?) { + if(song == null) { + currentSong.remove(user.token ?: "") + } else { + currentSong[user.token ?: ""] = song + } + } + + fun getSong(user: User) = currentSong[user.token ?: ""] +} \ No newline at end of file From 8bbd16345882899878408e70c96f42b2852c66c8 Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Sat, 24 Aug 2024 13:51:08 +0900 Subject: [PATCH 2/2] fix some event-emitter --- .../space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt | 1 - .../space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt | 5 +---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt index c7aa31e..38d0f92 100644 --- a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt +++ b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt @@ -295,7 +295,6 @@ class UserHandler( null, null, null, - null ) ) diff --git a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt index a11561c..4525585 100644 --- a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt +++ b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt @@ -271,10 +271,7 @@ class MessageHandler( SongType.ADD, msg.userId, msg.profile?.nickname ?: "", - video.name, - video.author, - video.length, - video.url + video, ) ) }