fix SongLists

- add currentSong object
- change some event, dto.
This commit is contained in:
dalbodeule 2024-08-24 13:31:50 +09:00
parent ef983cbe22
commit 36d0ed82d6
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
5 changed files with 86 additions and 39 deletions

View File

@ -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
}

View File

@ -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<SongsDTO> = 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") {

View File

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

View File

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

View File

@ -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<String, YoutubeVideo>()
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 ?: ""]
}