-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/feature/DRAW-275' into sandbox
- Loading branch information
Showing
14 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
15 changes: 15 additions & 0 deletions
15
adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/DiscordClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
40 changes: 40 additions & 0 deletions
40
.../webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/DiscordWebHookAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/config/DiscordClientConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...bhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/config/DiscordProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
5 changes: 5 additions & 0 deletions
5
adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/dto/DiscordEmbed.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
5 changes: 5 additions & 0 deletions
5
...ter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/dto/DiscordMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
) |
3 changes: 3 additions & 0 deletions
3
adapter/webhook/discord/src/main/resources/application-discord.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
domain/src/main/kotlin/com/xorker/draw/notify/NotifyRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
domain/src/main/kotlin/com/xorker/draw/notify/NotifyType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters