From c0d40a3c5d354debf2a0225e7fec7fa092124f2e Mon Sep 17 00:00:00 2001 From: sunwoong Date: Thu, 5 Sep 2024 16:26:47 +0900 Subject: [PATCH] =?UTF-8?q?DRAW-275=20feat:=20discord=20webhook=20?= =?UTF-8?q?=EC=9E=91=EC=97=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../draw/webhook/discord/DiscordClient.kt | 15 +++++++ .../webhook/discord/DiscordWebHookAdapter.kt | 40 +++++++++++++++++++ .../discord/config/DiscordClientConfig.kt | 26 ++++++++++++ .../discord/config/DiscordProperties.kt | 9 +++++ .../draw/webhook/discord/dto/DiscordEmbed.kt | 5 +++ .../webhook/discord/dto/DiscordMessage.kt | 5 +++ .../main/resources/application-discord.yml | 3 ++ 7 files changed, 103 insertions(+) create mode 100644 adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/DiscordClient.kt create mode 100644 adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/DiscordWebHookAdapter.kt create mode 100644 adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/config/DiscordClientConfig.kt create mode 100644 adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/config/DiscordProperties.kt create mode 100644 adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/dto/DiscordEmbed.kt create mode 100644 adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/dto/DiscordMessage.kt create mode 100644 adapter/webhook/discord/src/main/resources/application-discord.yml diff --git a/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/DiscordClient.kt b/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/DiscordClient.kt new file mode 100644 index 00000000..e6cf44a3 --- /dev/null +++ b/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/DiscordClient.kt @@ -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 +} diff --git a/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/DiscordWebHookAdapter.kt b/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/DiscordWebHookAdapter.kt new file mode 100644 index 00000000..056a252f --- /dev/null +++ b/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/DiscordWebHookAdapter.kt @@ -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))) + } +} diff --git a/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/config/DiscordClientConfig.kt b/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/config/DiscordClientConfig.kt new file mode 100644 index 00000000..cd8bf3d5 --- /dev/null +++ b/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/config/DiscordClientConfig.kt @@ -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) + } +} diff --git a/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/config/DiscordProperties.kt b/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/config/DiscordProperties.kt new file mode 100644 index 00000000..7ade83a6 --- /dev/null +++ b/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/config/DiscordProperties.kt @@ -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, +) diff --git a/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/dto/DiscordEmbed.kt b/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/dto/DiscordEmbed.kt new file mode 100644 index 00000000..c335033b --- /dev/null +++ b/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/dto/DiscordEmbed.kt @@ -0,0 +1,5 @@ +package com.xorker.draw.webhook.discord.dto + +data class DiscordEmbed( + val title: String, +) diff --git a/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/dto/DiscordMessage.kt b/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/dto/DiscordMessage.kt new file mode 100644 index 00000000..ce92cf01 --- /dev/null +++ b/adapter/webhook/discord/src/main/kotlin/com/xorker/draw/webhook/discord/dto/DiscordMessage.kt @@ -0,0 +1,5 @@ +package com.xorker.draw.webhook.discord.dto + +data class DiscordMessage( + val embeds: List, +) diff --git a/adapter/webhook/discord/src/main/resources/application-discord.yml b/adapter/webhook/discord/src/main/resources/application-discord.yml new file mode 100644 index 00000000..e781edcd --- /dev/null +++ b/adapter/webhook/discord/src/main/resources/application-discord.yml @@ -0,0 +1,3 @@ +discord: + random-matching-url: ${RANDOM_MATCHING_URL} + start-game-url: ${START_GAME_URL}