mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
commit
ae5e520dea
@ -295,7 +295,6 @@ class UserHandler(
|
|||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -271,10 +271,7 @@ class MessageHandler(
|
|||||||
SongType.ADD,
|
SongType.ADD,
|
||||||
msg.userId,
|
msg.userId,
|
||||||
msg.profile?.nickname ?: "",
|
msg.profile?.nickname ?: "",
|
||||||
video.name,
|
video,
|
||||||
video.author,
|
|
||||||
video.length,
|
|
||||||
video.url
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package space.mori.chzzk_bot.common.events
|
package space.mori.chzzk_bot.common.events
|
||||||
|
|
||||||
|
import space.mori.chzzk_bot.common.utils.YoutubeVideo
|
||||||
|
|
||||||
enum class SongType(var value: Int) {
|
enum class SongType(var value: Int) {
|
||||||
ADD(0),
|
ADD(0),
|
||||||
REMOVE(1),
|
REMOVE(1),
|
||||||
@ -13,10 +15,9 @@ class SongEvent(
|
|||||||
val type: SongType,
|
val type: SongType,
|
||||||
val reqUid: String?,
|
val reqUid: String?,
|
||||||
val reqName: String?,
|
val reqName: String?,
|
||||||
val name: String?,
|
val current: YoutubeVideo? = null,
|
||||||
val author: String?,
|
val next: YoutubeVideo? = null,
|
||||||
val time: Int?,
|
val delUrl: String? = null,
|
||||||
val url: String?
|
|
||||||
): Event {
|
): Event {
|
||||||
var TAG = javaClass.simpleName
|
var TAG = javaClass.simpleName
|
||||||
}
|
}
|
@ -8,6 +8,8 @@ import kotlinx.serialization.Serializable
|
|||||||
import space.mori.chzzk_bot.common.models.SongList
|
import space.mori.chzzk_bot.common.models.SongList
|
||||||
import space.mori.chzzk_bot.common.services.SongListService
|
import space.mori.chzzk_bot.common.services.SongListService
|
||||||
import space.mori.chzzk_bot.common.services.UserService
|
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
|
@Serializable
|
||||||
data class SongsDTO(
|
data class SongsDTO(
|
||||||
@ -18,6 +20,11 @@ data class SongsDTO(
|
|||||||
val reqName: String
|
val reqName: String
|
||||||
)
|
)
|
||||||
|
|
||||||
|
data class SongsResponseDTO(
|
||||||
|
val current: SongsDTO? = null,
|
||||||
|
val next: List<SongsDTO> = emptyList()
|
||||||
|
)
|
||||||
|
|
||||||
fun SongList.toDTO(): SongsDTO = SongsDTO(
|
fun SongList.toDTO(): SongsDTO = SongsDTO(
|
||||||
this.url,
|
this.url,
|
||||||
this.name,
|
this.name,
|
||||||
@ -26,6 +33,14 @@ fun SongList.toDTO(): SongsDTO = SongsDTO(
|
|||||||
this.reqName
|
this.reqName
|
||||||
)
|
)
|
||||||
|
|
||||||
|
fun YoutubeVideo.toDTO(): SongsDTO = SongsDTO(
|
||||||
|
this.url,
|
||||||
|
this.name,
|
||||||
|
this.author,
|
||||||
|
this.length,
|
||||||
|
""
|
||||||
|
)
|
||||||
|
|
||||||
fun Routing.apiSongRoutes() {
|
fun Routing.apiSongRoutes() {
|
||||||
route("/songs/{uid}") {
|
route("/songs/{uid}") {
|
||||||
get {
|
get {
|
||||||
@ -37,7 +52,12 @@ fun Routing.apiSongRoutes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val songs = SongListService.getSong(user)
|
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") {
|
route("/songs") {
|
||||||
|
@ -14,11 +14,14 @@ import kotlinx.serialization.json.Json
|
|||||||
import org.koin.java.KoinJavaComponent.inject
|
import org.koin.java.KoinJavaComponent.inject
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.common.events.*
|
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.SongConfigService
|
||||||
import space.mori.chzzk_bot.common.services.SongListService
|
import space.mori.chzzk_bot.common.services.SongListService
|
||||||
import space.mori.chzzk_bot.common.services.UserService
|
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.common.utils.getYoutubeVideo
|
||||||
import space.mori.chzzk_bot.webserver.UserSession
|
import space.mori.chzzk_bot.webserver.UserSession
|
||||||
|
import space.mori.chzzk_bot.webserver.utils.CurrentSong
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
||||||
fun Routing.wsSongListRoutes() {
|
fun Routing.wsSongListRoutes() {
|
||||||
@ -90,8 +93,6 @@ fun Routing.wsSongListRoutes() {
|
|||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
|
||||||
null,
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
removeSession(uid)
|
removeSession(uid)
|
||||||
@ -140,10 +141,8 @@ fun Routing.wsSongListRoutes() {
|
|||||||
SongType.ADD,
|
SongType.ADD,
|
||||||
user.token,
|
user.token,
|
||||||
user.username,
|
user.username,
|
||||||
youtubeVideo.name,
|
null,
|
||||||
youtubeVideo.author,
|
youtubeVideo
|
||||||
youtubeVideo.length,
|
|
||||||
youtubeVideo.url
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -160,29 +159,38 @@ fun Routing.wsSongListRoutes() {
|
|||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
0,
|
|
||||||
data.url
|
data.url
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
} else if (data.type == SongType.NEXT.value) {
|
} else if (data.type == SongType.NEXT.value) {
|
||||||
val songList = SongListService.getSong(user)
|
val songList = SongListService.getSong(user)
|
||||||
|
var song: SongList? = null
|
||||||
|
var youtubeVideo: YoutubeVideo? = null
|
||||||
|
|
||||||
if (songList.isNotEmpty()) {
|
if (songList.isNotEmpty()) {
|
||||||
val song = songList[0]
|
song = songList[0]
|
||||||
SongListService.deleteSong(user, song.uid, song.name)
|
SongListService.deleteSong(user, song.uid, song.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
song?.let {
|
||||||
|
youtubeVideo = YoutubeVideo(
|
||||||
|
song.url,
|
||||||
|
song.name,
|
||||||
|
song.author,
|
||||||
|
song.time
|
||||||
|
)
|
||||||
|
}
|
||||||
dispatcher.post(
|
dispatcher.post(
|
||||||
SongEvent(
|
SongEvent(
|
||||||
user.token!!,
|
user.token!!,
|
||||||
SongType.NEXT,
|
SongType.NEXT,
|
||||||
null,
|
song?.uid,
|
||||||
null,
|
song?.reqName,
|
||||||
null,
|
youtubeVideo
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
CurrentSong.setSong(user, youtubeVideo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -200,7 +208,7 @@ fun Routing.wsSongListRoutes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dispatcher.subscribe(SongEvent::class) {
|
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 {
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
val user = UserService.getUser(it.uid)
|
val user = UserService.getUser(it.uid)
|
||||||
if(user != null) {
|
if(user != null) {
|
||||||
@ -210,10 +218,8 @@ fun Routing.wsSongListRoutes() {
|
|||||||
it.type.value,
|
it.type.value,
|
||||||
it.uid,
|
it.uid,
|
||||||
it.reqUid,
|
it.reqUid,
|
||||||
it.name,
|
it.current?.toSerializable(),
|
||||||
it.author,
|
it.next?.toSerializable()
|
||||||
it.time,
|
|
||||||
it.url
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -232,8 +238,6 @@ fun Routing.wsSongListRoutes() {
|
|||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
|
||||||
null
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import org.koin.java.KoinJavaComponent.inject
|
|||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.common.events.*
|
import space.mori.chzzk_bot.common.events.*
|
||||||
import space.mori.chzzk_bot.common.services.UserService
|
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.ConcurrentHashMap
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue
|
import java.util.concurrent.ConcurrentLinkedQueue
|
||||||
|
|
||||||
@ -89,8 +90,6 @@ fun Routing.wsSongRoutes() {
|
|||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
|
||||||
null
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -119,16 +118,14 @@ fun Routing.wsSongRoutes() {
|
|||||||
val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
|
|
||||||
dispatcher.subscribe(SongEvent::class) {
|
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 {
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
broadcastMessage(it.uid, SongResponse(
|
broadcastMessage(it.uid, SongResponse(
|
||||||
it.type.value,
|
it.type.value,
|
||||||
it.uid,
|
it.uid,
|
||||||
it.reqUid,
|
it.reqUid,
|
||||||
it.name,
|
it.current?.toSerializable(),
|
||||||
it.author,
|
it.next?.toSerializable()
|
||||||
it.time,
|
|
||||||
it.url
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -140,8 +137,6 @@ fun Routing.wsSongRoutes() {
|
|||||||
it.uid,
|
it.uid,
|
||||||
null,
|
null,
|
||||||
null,
|
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
|
@Serializable
|
||||||
data class SongResponse(
|
data class SongResponse(
|
||||||
val type: Int,
|
val type: Int,
|
||||||
val uid: String,
|
val uid: String,
|
||||||
val reqUid: String?,
|
val reqUid: String?,
|
||||||
val name: String?,
|
val current: SerializableYoutubeVideo? = null,
|
||||||
val author: String?,
|
val next: SerializableYoutubeVideo? = null,
|
||||||
val time: Int?,
|
|
||||||
val url: String?
|
|
||||||
)
|
)
|
||||||
|
@ -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 ?: ""]
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user