Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/DRAW-275' into sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
comforest committed Sep 17, 2024
2 parents 6ce3521 + 7448064 commit 3eb1818
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 0 deletions.
17 changes: 17 additions & 0 deletions adapter/webhook/discord/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import org.springframework.boot.gradle.tasks.bundling.BootJar

plugins {
kotlin("plugin.spring")
}

dependencies {
implementation(project(":domain"))
implementation(project(":support:logging"))

implementation("org.springframework.boot:spring-boot-starter-webflux:${Versions.SPRING_BOOT}")
}

tasks {
withType<Jar> { enabled = true }
withType<BootJar> { enabled = false }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.xorker.draw.webhook.discord

import com.xorker.draw.webhook.discord.dto.DiscordMessage
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.service.annotation.PostExchange
import reactor.core.publisher.Mono

internal interface DiscordClient {
@PostExchange("/api/webhooks/{path}")
fun sendMessage(
@PathVariable("path") path: String,
@RequestBody discordMessage: DiscordMessage,
): Mono<Void>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.xorker.draw.webhook.discord

import com.xorker.draw.notify.NotifyRepository
import com.xorker.draw.notify.NotifyType
import com.xorker.draw.support.logging.logger
import com.xorker.draw.webhook.discord.config.DiscordProperties
import com.xorker.draw.webhook.discord.dto.DiscordEmbed
import com.xorker.draw.webhook.discord.dto.DiscordMessage
import org.springframework.stereotype.Component

@Component
internal class DiscordWebHookAdapter(
private val discordClient: DiscordClient,
private val discordProperties: DiscordProperties,
) : NotifyRepository {
val logger = logger()

override fun notifyMessage(notifyType: NotifyType) {
sendMessage(notifyType)
}

private fun sendMessage(notifyType: NotifyType) {
try {
when (notifyType) {
is NotifyType.DiscordRandomMatchingNotifyType ->
discordClient.sendMessage(discordProperties.randomMatchingUrl, notifyType.toDiscordMessage()).block()

is NotifyType.DiscordStartGameNotifyType ->
discordClient.sendMessage(discordProperties.startGameUrl, notifyType.toDiscordMessage()).block()
}
} catch (ex: Exception) {
logger.warn(ex.message, ex)
}
}

fun NotifyType.toDiscordMessage(): DiscordMessage = when (this) {
is NotifyType.DiscordRandomMatchingNotifyType -> DiscordMessage(listOf(DiscordEmbed(this.message)))
is NotifyType.DiscordStartGameNotifyType -> DiscordMessage(listOf(DiscordEmbed(this.message)))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.xorker.draw.webhook.discord.config

import com.xorker.draw.webhook.discord.DiscordClient
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.support.WebClientAdapter
import org.springframework.web.service.invoker.HttpServiceProxyFactory

@EnableConfigurationProperties(DiscordProperties::class)
@Configuration
internal class DiscordClientConfig {

@Bean
fun webClient(): WebClient = WebClient.create("https://discord.com")

@Bean
fun discordClient(): DiscordClient {
val factory = HttpServiceProxyFactory.builder()
.exchangeAdapter(WebClientAdapter.create(webClient()))
.build()

return factory.createClient(DiscordClient::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.xorker.draw.webhook.discord.config

import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties(prefix = "discord")
internal data class DiscordProperties(
val randomMatchingUrl: String,
val startGameUrl: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.xorker.draw.webhook.discord.dto

data class DiscordEmbed(
val title: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.xorker.draw.webhook.discord.dto

data class DiscordMessage(
val embeds: List<DiscordEmbed>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
discord:
random-matching-url: ${RANDOM_MATCHING_URL}
start-game-url: ${START_GAME_URL}
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies {
implementation(project(":adapter:memory"))
implementation(project(":adapter:oauth"))
implementation(project(":adapter:rdb"))
implementation(project(":adapter:webhook:discord"))
implementation(project(":support:jwt"))
implementation(project(":support:time"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.xorker.draw.mafia
import com.xorker.draw.exception.InvalidRequestOtherPlayingException
import com.xorker.draw.mafia.phase.MafiaPhaseUseCase
import com.xorker.draw.notification.PushMessageUseCase
import com.xorker.draw.notify.NotifyRepository
import com.xorker.draw.notify.NotifyType
import com.xorker.draw.user.User
import com.xorker.draw.websocket.WaitingQueueUseCase
import org.springframework.stereotype.Service
Expand All @@ -15,6 +17,7 @@ internal class MafiaGameRandomMatchingService(
private val mafiaPhaseUseCase: MafiaPhaseUseCase,
private val mafiaGameRoomService: MafiaGameRoomService,
private val pushMessageUseCase: PushMessageUseCase,
private val notifyRepository: NotifyRepository,
) : WaitingQueueUseCase {

override fun enqueue(user: User, locale: String) {
Expand All @@ -23,6 +26,7 @@ internal class MafiaGameRandomMatchingService(

mafiaGameWaitingQueueRepository.enqueue(user, locale)
mafiaGameMessenger.unicastRandomMatching(user.id)
notifyRepository.notifyMessage(NotifyType.DiscordRandomMatchingNotifyType(user.name, locale))

synchronized(this) {
val size = mafiaGameWaitingQueueRepository.size(locale)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.xorker.draw.mafia.phase
import com.xorker.draw.mafia.MafiaGameInfo
import com.xorker.draw.mafia.MafiaKeywordRepository
import com.xorker.draw.mafia.MafiaPhase
import com.xorker.draw.notify.NotifyRepository
import com.xorker.draw.notify.NotifyType
import com.xorker.draw.timer.TimerRepository
import org.springframework.stereotype.Component
import kotlin.random.Random
Expand All @@ -11,6 +13,7 @@ import kotlin.random.Random
internal class MafiaPhaseStartGameProcessor(
private val mafiaKeywordRepository: MafiaKeywordRepository,
private val timerRepository: TimerRepository,
private val notifyRepository: NotifyRepository,
) {
private val random: Random = Random(System.currentTimeMillis())

Expand All @@ -34,6 +37,8 @@ internal class MafiaPhaseStartGameProcessor(
)
gameInfo.phase = phase

notifyRepository.notifyMessage(NotifyType.DiscordStartGameNotifyType(room.id, room.locale))

return phase
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.xorker.draw.notify

interface NotifyRepository {
fun notifyMessage(notifyType: NotifyType)
}
51 changes: 51 additions & 0 deletions domain/src/main/kotlin/com/xorker/draw/notify/NotifyType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.xorker.draw.notify

import com.xorker.draw.exception.UnSupportedException
import com.xorker.draw.room.RoomId

sealed class NotifyType {
class DiscordRandomMatchingNotifyType(
name: String,
language: String,
) : NotifyType() {
val message: String

private var _language: LanguageType? = null

init {
_language = getLanguageType(language)
message = _language!!.content + " ${name}님이 빠른 게임 상대를 기다리고 있어요."
}
}

class DiscordStartGameNotifyType(
roomId: RoomId,
language: String,
) : NotifyType() {
val message: String

private var _language: LanguageType? = null

init {
_language = getLanguageType(language)
message = _language!!.content + " ${roomId.value} 방에서 게임이 시작되었어요."
}
}

fun getLanguageType(language: String): LanguageType {
if (language == "ko") {
return LanguageType.KOREAN
} else if (language == "en") {
return LanguageType.ENGLISH
}
throw UnSupportedException
}
}

enum class LanguageType(
val content: String,
) {
KOREAN("\uD83C\uDDF0\uD83C\uDDF7"),
ENGLISH("\uD83C\uDDFA\uD83C\uDDF8"),
;
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include(
"adapter:memory",
"adapter:oauth",
"adapter:rdb",
"adapter:webhook:discord",
"app:api",
"app:support:auth",
"app:support:exception",
Expand Down

0 comments on commit 3eb1818

Please sign in to comment.