Format code

This commit is contained in:
2025-04-08 23:44:37 +03:00
parent e2a1c867fd
commit 8ab982b360
9 changed files with 54 additions and 62 deletions

View File

@@ -3,4 +3,8 @@ java = "temurin-17"
[tasks.release] [tasks.release]
description = 'Build and push docker image' description = 'Build and push docker image'
run = './gradlew build && ./gradlew jib --image=cr.yandex/crph26nr2d2ds65t2m7b/memes-tv:0.0.1-SNAPSHOT' run = './gradlew build && ./gradlew jib --image=cr.yandex/crph26nr2d2ds65t2m7b/memes-tv:0.0.1-SNAPSHOT'
[tasks.fmt]
description = 'Fourmat source code'
run = './gradlew :spotlessApply'

View File

@@ -4,6 +4,7 @@ plugins {
id("org.springframework.boot") version "3.4.4" id("org.springframework.boot") version "3.4.4"
id("io.spring.dependency-management") version "1.1.7" id("io.spring.dependency-management") version "1.1.7"
id("com.google.cloud.tools.jib") version "3.4.5" id("com.google.cloud.tools.jib") version "3.4.5"
id("com.diffplug.spotless") version "7.0.3"
} }
group = "com.pischule" group = "com.pischule"
@@ -48,3 +49,9 @@ kotlin {
tasks.withType<Test> { tasks.withType<Test> {
useJUnitPlatform() useJUnitPlatform()
} }
spotless {
kotlin {
ktfmt("0.54").kotlinlangStyle()
}
}

View File

@@ -2,8 +2,4 @@ package com.pischule.memestv
import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.ConfigurationProperties
@ConfigurationProperties("bot") @ConfigurationProperties("bot") data class BotProps(val token: String, val destinationChatId: Long)
data class BotProps(
val token: String,
val destinationChatId: Long,
)

View File

@@ -33,12 +33,12 @@ class BotService(
message { message {
try { try {
val chatId = message.chat.id val chatId = message.chat.id
val replyToPhotos = message.replyToMessage val replyToPhotos =
?.photo message.replyToMessage?.photo?.takeIf { it.isNotEmpty() }
?.takeIf { it.isNotEmpty() } if (
if (chatId == botProps.destinationChatId chatId == botProps.destinationChatId &&
&& message.text?.lowercase() == "this" message.text?.lowercase() == "this" &&
&& replyToPhotos != null replyToPhotos != null
) { ) {
val maxResPhoto = replyToPhotos.last().fileId val maxResPhoto = replyToPhotos.last().fileId
val fileBytes = bot.downloadFileBytes(maxResPhoto) val fileBytes = bot.downloadFileBytes(maxResPhoto)
@@ -47,12 +47,13 @@ class BotService(
fileUploaderService.uploadFile(it) fileUploaderService.uploadFile(it)
log.info { "Uploaded a file $maxResPhoto to s3" } log.info { "Uploaded a file $maxResPhoto to s3" }
bot.setMessageReaction( bot.setMessageReaction(
chatId = ChatId.fromId(message.chat.id), chatId = ChatId.fromId(message.chat.id),
messageId = message.messageId, messageId = message.messageId,
reaction = listOf(ReactionType.Emoji("👍")) reaction = listOf(ReactionType.Emoji("👍")),
).onError { error -> )
log.warn { "Failed to react to message: $error" } .onError { error ->
} log.warn { "Failed to react to message: $error" }
}
} }
} }
} catch (e: Error) { } catch (e: Error) {
@@ -64,34 +65,27 @@ class BotService(
if (message.chat.id != botProps.destinationChatId) { if (message.chat.id != botProps.destinationChatId) {
bot.forwardMessage( bot.forwardMessage(
chatId = ChatId.fromId(botProps.destinationChatId), chatId = ChatId.fromId(botProps.destinationChatId),
fromChatId = ChatId.fromId(message.chat.id), fromChatId = ChatId.fromId(message.chat.id),
messageId = message.messageId, messageId = message.messageId,
).fold( )
{ .fold(
log.info { "Forwarded pictures message: $it" } { log.info { "Forwarded pictures message: $it" } },
}, { log.error { "Failed to forward message: $it" } },
{ )
log.error { "Failed to forward message: $it" }
}
)
} }
bot.setMessageReaction( bot.setMessageReaction(
chatId = ChatId.fromId(message.chat.id), chatId = ChatId.fromId(message.chat.id),
messageId = message.messageId, messageId = message.messageId,
reaction = listOf(ReactionType.Emoji("👀")) reaction = listOf(ReactionType.Emoji("👀")),
).onError { error -> )
log.warn { "Failed to react to message: $error" } .onError { error -> log.warn { "Failed to react to message: $error" } }
}
} }
} }
} }
Thread { Thread { bot.startPolling() }.start()
bot.startPolling()
}.start()
log.info { "Initialized bot" } log.info { "Initialized bot" }
} }
@@ -101,4 +95,4 @@ class BotService(
bot.stopPolling() bot.stopPolling()
log.info { "Stopped bot" } log.info { "Stopped bot" }
} }
} }

View File

@@ -6,16 +6,15 @@ import aws.smithy.kotlin.runtime.content.ByteStream
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@Service @Service
class FileUploaderService( class FileUploaderService(private val s3Client: S3Client, private val s3Props: S3Props) {
private val s3Client: S3Client,
private val s3Props: S3Props,
) {
suspend fun uploadFile(fileBytes: ByteArray) { suspend fun uploadFile(fileBytes: ByteArray) {
s3Client.putObject(PutObjectRequest{ s3Client.putObject(
body = ByteStream.fromBytes(fileBytes) PutObjectRequest {
bucket = s3Props.bucket body = ByteStream.fromBytes(fileBytes)
key = "_.jpeg" bucket = s3Props.bucket
}) key = "_.jpeg"
}
)
} }
} }

View File

@@ -1,11 +1,9 @@
package com.pischule.memestv package com.pischule.memestv
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.runApplication import org.springframework.boot.runApplication
@SpringBootApplication @SpringBootApplication class MemesTvApplication
class MemesTvApplication
fun main(args: Array<String>) { fun main(args: Array<String>) {
runApplication<MemesTvApplication>(*args) runApplication<MemesTvApplication>(*args)

View File

@@ -21,4 +21,4 @@ class S3Config {
} }
} }
} }
} }

View File

@@ -3,8 +3,4 @@ package com.pischule.memestv
import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.ConfigurationProperties
@ConfigurationProperties("s3") @ConfigurationProperties("s3")
data class S3Props( data class S3Props(val accessKeyId: String, val secretAccessKey: String, val bucket: String)
val accessKeyId: String,
val secretAccessKey: String,
val bucket: String,
)

View File

@@ -10,7 +10,5 @@ import org.springframework.test.context.ActiveProfiles
@SpringBootTest @SpringBootTest
class MemesTvApplicationTests { class MemesTvApplicationTests {
@Test @Test fun contextLoads() {}
fun contextLoads() {
}
} }