some improve WSApis

- with WSSongListRoutes.kt, data receive logics.
- else WSAPI's logger name changed.
- MessageHandler.kt "!노래시작" command improved.
This commit is contained in:
dalbodeule 2024-08-05 13:25:28 +09:00
parent 0a4e8193bb
commit cc23ac03e7
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
4 changed files with 40 additions and 30 deletions

View File

@ -263,13 +263,13 @@ class MessageHandler(
SongConfigService.updateSession(user, session, password) SongConfigService.updateSession(user, session, password)
bot.getUserById(user.token)?.let { it ->
val channel = it.openPrivateChannel() bot.retrieveUserById(user.discord).queue { discordUser ->
channel.onSuccess { privateChannel -> discordUser?.openPrivateChannel()?.queue { channel ->
privateChannel.sendMessage("여기로 접속해주세요! https://nabot,mori.space/songlist/${session}.\n인증번호는 ||$password|| 입니다.").queue() channel.sendMessage("여기로 접속해주세요! https://nabot,mori.space/songlist/${session}.\n인증번호는 ||$password|| 입니다.")
.queue()
} }
} }
} }
internal fun handle(msg: ChatMessage, user: User) { internal fun handle(msg: ChatMessage, user: User) {

View File

@ -8,11 +8,13 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.ClosedReceiveChannelException import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
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.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.utils.getYoutubeVideo import space.mori.chzzk_bot.common.utils.getYoutubeVideo
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.ConcurrentLinkedQueue
@ -20,7 +22,7 @@ import java.util.concurrent.ConcurrentLinkedQueue
fun Routing.wsSongListRoutes() { fun Routing.wsSongListRoutes() {
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>() val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
val status = ConcurrentHashMap<String, SongType>() val status = ConcurrentHashMap<String, SongType>()
val logger = LoggerFactory.getLogger(this.javaClass.name) val logger = LoggerFactory.getLogger("WSSongListRoutes")
val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java) val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
@ -68,7 +70,7 @@ fun Routing.wsSongListRoutes() {
for (frame in incoming) { for (frame in incoming) {
when(frame) { when(frame) {
is Frame.Text -> { is Frame.Text -> {
val data = receiveDeserialized<SongRequest>() val data = frame.readText().let { Json.decodeFromString<SongRequest>(it) }
if(data.maxQueue != null && data.maxQueue > 0) SongConfigService.updateQueueLimit(user, data.maxQueue) if(data.maxQueue != null && data.maxQueue > 0) SongConfigService.updateQueueLimit(user, data.maxQueue)
if(data.maxUserLimit != null && data.maxUserLimit > 0) SongConfigService.updatePersonalLimit(user, data.maxUserLimit) if(data.maxUserLimit != null && data.maxUserLimit > 0) SongConfigService.updatePersonalLimit(user, data.maxUserLimit)
@ -76,18 +78,20 @@ fun Routing.wsSongListRoutes() {
if(data.url != null) { if(data.url != null) {
val youtubeVideo = getYoutubeVideo(data.url) val youtubeVideo = getYoutubeVideo(data.url)
if(youtubeVideo != null) {
dispatcher.post( CoroutineScope(Dispatchers.Default).launch {
SongEvent( SongListService.saveSong(user, user.token, data.url, youtubeVideo.name, youtubeVideo.author, youtubeVideo.length, user.username)
user.token, dispatcher.post(SongEvent(
SongType.ADD, user.token,
user.token, SongType.ADD,
user.username, user.token,
youtubeVideo?.name, user.username,
youtubeVideo?.author, youtubeVideo.name,
youtubeVideo?.length youtubeVideo.author,
) youtubeVideo.length
) ))
}
}
} }
if(data.remove != null && data.remove > 0) { if(data.remove != null && data.remove > 0) {
val songs = SongListService.getSong(user) val songs = SongListService.getSong(user)
@ -125,15 +129,21 @@ 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.name)
CoroutineScope(Dispatchers.Default).launch { CoroutineScope(Dispatchers.Default).launch {
sessions[it.uid]?.forEach { ws -> val user = UserService.getUser(it.uid)
ws.sendSerialized(SongResponse( if(user != null) {
it.type.value, val session = SongConfigService.getConfig(user)
it.uid, sessions[session.token]?.forEach { ws ->
it.reqUid, ws.sendSerialized(
it.name, SongResponse(
it.author, it.type.value,
it.time it.uid,
)) it.reqUid,
it.name,
it.author,
it.time
)
)
}
} }
} }
} }

View File

@ -18,7 +18,7 @@ import java.util.concurrent.ConcurrentLinkedQueue
fun Routing.wsSongRoutes() { fun Routing.wsSongRoutes() {
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>() val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
val status = ConcurrentHashMap<String, SongType>() val status = ConcurrentHashMap<String, SongType>()
val logger = LoggerFactory.getLogger(this.javaClass.name) val logger = LoggerFactory.getLogger("WSSongRoutes")
fun addSession(uid: String, session: WebSocketServerSession) { fun addSession(uid: String, session: WebSocketServerSession) {
sessions.computeIfAbsent(uid) { ConcurrentLinkedQueue() }.add(session) sessions.computeIfAbsent(uid) { ConcurrentLinkedQueue() }.add(session)

View File

@ -19,7 +19,7 @@ import java.util.concurrent.ConcurrentLinkedQueue
fun Routing.wsTimerRoutes() { fun Routing.wsTimerRoutes() {
val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>() val sessions = ConcurrentHashMap<String, ConcurrentLinkedQueue<WebSocketServerSession>>()
val status = ConcurrentHashMap<String, TimerType>() val status = ConcurrentHashMap<String, TimerType>()
val logger = LoggerFactory.getLogger(this.javaClass.name) val logger = LoggerFactory.getLogger("WSTimerRoutes")
fun addSession(uid: String, session: WebSocketServerSession) { fun addSession(uid: String, session: WebSocketServerSession) {
sessions.computeIfAbsent(uid) { ConcurrentLinkedQueue() }.add(session) sessions.computeIfAbsent(uid) { ConcurrentLinkedQueue() }.add(session)