diff --git a/.mise.toml b/.mise.toml index 323b5f3..c06f1e1 100644 --- a/.mise.toml +++ b/.mise.toml @@ -3,4 +3,8 @@ java = "temurin-17" [tasks.release] description = 'Build and push docker image' -run = './gradlew build && ./gradlew jib --image=cr.yandex/crph26nr2d2ds65t2m7b/memes-tv:0.0.1-SNAPSHOT' \ No newline at end of file +run = './gradlew build && ./gradlew jib --image=cr.yandex/crph26nr2d2ds65t2m7b/memes-tv:0.0.1-SNAPSHOT' + +[tasks.fmt] +description = 'Fourmat source code' +run = './gradlew :spotlessApply' \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 61c3eb6..64c3411 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,6 +4,7 @@ plugins { id("org.springframework.boot") version "3.4.4" id("io.spring.dependency-management") version "1.1.7" id("com.google.cloud.tools.jib") version "3.4.5" + id("com.diffplug.spotless") version "7.0.3" } group = "com.pischule" @@ -48,3 +49,9 @@ kotlin { tasks.withType { useJUnitPlatform() } + +spotless { + kotlin { + ktfmt("0.54").kotlinlangStyle() + } +} diff --git a/src/main/kotlin/com/pischule/memestv/BotProps.kt b/src/main/kotlin/com/pischule/memestv/BotProps.kt index 4c2b959..61243de 100644 --- a/src/main/kotlin/com/pischule/memestv/BotProps.kt +++ b/src/main/kotlin/com/pischule/memestv/BotProps.kt @@ -2,8 +2,4 @@ package com.pischule.memestv import org.springframework.boot.context.properties.ConfigurationProperties -@ConfigurationProperties("bot") -data class BotProps( - val token: String, - val destinationChatId: Long, -) +@ConfigurationProperties("bot") data class BotProps(val token: String, val destinationChatId: Long) diff --git a/src/main/kotlin/com/pischule/memestv/BotService.kt b/src/main/kotlin/com/pischule/memestv/BotService.kt index f10c122..49a2859 100644 --- a/src/main/kotlin/com/pischule/memestv/BotService.kt +++ b/src/main/kotlin/com/pischule/memestv/BotService.kt @@ -33,12 +33,12 @@ class BotService( message { try { val chatId = message.chat.id - val replyToPhotos = message.replyToMessage - ?.photo - ?.takeIf { it.isNotEmpty() } - if (chatId == botProps.destinationChatId - && message.text?.lowercase() == "this" - && replyToPhotos != null + val replyToPhotos = + message.replyToMessage?.photo?.takeIf { it.isNotEmpty() } + if ( + chatId == botProps.destinationChatId && + message.text?.lowercase() == "this" && + replyToPhotos != null ) { val maxResPhoto = replyToPhotos.last().fileId val fileBytes = bot.downloadFileBytes(maxResPhoto) @@ -47,12 +47,13 @@ class BotService( fileUploaderService.uploadFile(it) log.info { "Uploaded a file $maxResPhoto to s3" } bot.setMessageReaction( - chatId = ChatId.fromId(message.chat.id), - messageId = message.messageId, - reaction = listOf(ReactionType.Emoji("👍")) - ).onError { error -> - log.warn { "Failed to react to message: $error" } - } + chatId = ChatId.fromId(message.chat.id), + messageId = message.messageId, + reaction = listOf(ReactionType.Emoji("👍")), + ) + .onError { error -> + log.warn { "Failed to react to message: $error" } + } } } } catch (e: Error) { @@ -64,34 +65,27 @@ class BotService( if (message.chat.id != botProps.destinationChatId) { bot.forwardMessage( - chatId = ChatId.fromId(botProps.destinationChatId), - fromChatId = ChatId.fromId(message.chat.id), - messageId = message.messageId, - ).fold( - { - log.info { "Forwarded pictures message: $it" } - }, - { - log.error { "Failed to forward message: $it" } - } - ) + chatId = ChatId.fromId(botProps.destinationChatId), + fromChatId = ChatId.fromId(message.chat.id), + messageId = message.messageId, + ) + .fold( + { log.info { "Forwarded pictures message: $it" } }, + { log.error { "Failed to forward message: $it" } }, + ) } bot.setMessageReaction( - chatId = ChatId.fromId(message.chat.id), - messageId = message.messageId, - reaction = listOf(ReactionType.Emoji("👀")) - ).onError { error -> - log.warn { "Failed to react to message: $error" } - } - + chatId = ChatId.fromId(message.chat.id), + messageId = message.messageId, + reaction = listOf(ReactionType.Emoji("👀")), + ) + .onError { error -> log.warn { "Failed to react to message: $error" } } } } } - Thread { - bot.startPolling() - }.start() + Thread { bot.startPolling() }.start() log.info { "Initialized bot" } } @@ -101,4 +95,4 @@ class BotService( bot.stopPolling() log.info { "Stopped bot" } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/pischule/memestv/FileUploaderService.kt b/src/main/kotlin/com/pischule/memestv/FileUploaderService.kt index 0b7106e..622fb3d 100644 --- a/src/main/kotlin/com/pischule/memestv/FileUploaderService.kt +++ b/src/main/kotlin/com/pischule/memestv/FileUploaderService.kt @@ -6,16 +6,15 @@ import aws.smithy.kotlin.runtime.content.ByteStream import org.springframework.stereotype.Service @Service -class FileUploaderService( - private val s3Client: S3Client, - private val s3Props: S3Props, -) { +class FileUploaderService(private val s3Client: S3Client, private val s3Props: S3Props) { suspend fun uploadFile(fileBytes: ByteArray) { - s3Client.putObject(PutObjectRequest{ - body = ByteStream.fromBytes(fileBytes) - bucket = s3Props.bucket - key = "_.jpeg" - }) + s3Client.putObject( + PutObjectRequest { + body = ByteStream.fromBytes(fileBytes) + bucket = s3Props.bucket + key = "_.jpeg" + } + ) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/pischule/memestv/MemesTvApplication.kt b/src/main/kotlin/com/pischule/memestv/MemesTvApplication.kt index 9664723..0a9687b 100644 --- a/src/main/kotlin/com/pischule/memestv/MemesTvApplication.kt +++ b/src/main/kotlin/com/pischule/memestv/MemesTvApplication.kt @@ -1,11 +1,9 @@ package com.pischule.memestv import org.springframework.boot.autoconfigure.SpringBootApplication -import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.boot.runApplication -@SpringBootApplication -class MemesTvApplication +@SpringBootApplication class MemesTvApplication fun main(args: Array) { runApplication(*args) diff --git a/src/main/kotlin/com/pischule/memestv/S3Config.kt b/src/main/kotlin/com/pischule/memestv/S3Config.kt index d04d9f0..7f7c08e 100644 --- a/src/main/kotlin/com/pischule/memestv/S3Config.kt +++ b/src/main/kotlin/com/pischule/memestv/S3Config.kt @@ -21,4 +21,4 @@ class S3Config { } } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/pischule/memestv/S3Props.kt b/src/main/kotlin/com/pischule/memestv/S3Props.kt index 777d160..13344d8 100644 --- a/src/main/kotlin/com/pischule/memestv/S3Props.kt +++ b/src/main/kotlin/com/pischule/memestv/S3Props.kt @@ -3,8 +3,4 @@ package com.pischule.memestv import org.springframework.boot.context.properties.ConfigurationProperties @ConfigurationProperties("s3") -data class S3Props( - val accessKeyId: String, - val secretAccessKey: String, - val bucket: String, -) +data class S3Props(val accessKeyId: String, val secretAccessKey: String, val bucket: String) diff --git a/src/test/kotlin/com/pischule/memestv/MemesTvApplicationTests.kt b/src/test/kotlin/com/pischule/memestv/MemesTvApplicationTests.kt index 635082a..19815f8 100644 --- a/src/test/kotlin/com/pischule/memestv/MemesTvApplicationTests.kt +++ b/src/test/kotlin/com/pischule/memestv/MemesTvApplicationTests.kt @@ -10,7 +10,5 @@ import org.springframework.test.context.ActiveProfiles @SpringBootTest class MemesTvApplicationTests { - @Test - fun contextLoads() { - } + @Test fun contextLoads() {} }