-
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.
- Loading branch information
Showing
7 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
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} |