From 29e5a04135de1073fa850e108db9e193b2eef72b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 12 Aug 2023 23:49:25 +0600 Subject: [PATCH] update dependencies && add opportunity to use several targetChatIds instead of one --- common/src/commonMain/kotlin/ChatConfig.kt | 17 ++++++-- common/src/jvmMain/kotlin/CommonPlugin.kt | 3 +- gradle/libs.versions.toml | 18 ++++---- gradle/wrapper/gradle-wrapper.properties | 2 +- .../kotlin/sending/PostPublisher.kt | 42 +++++++++++-------- posts/src/jvmMain/kotlin/Plugin.kt | 3 +- 6 files changed, 51 insertions(+), 34 deletions(-) diff --git a/common/src/commonMain/kotlin/ChatConfig.kt b/common/src/commonMain/kotlin/ChatConfig.kt index 181deb1..dc09bb0 100644 --- a/common/src/commonMain/kotlin/ChatConfig.kt +++ b/common/src/commonMain/kotlin/ChatConfig.kt @@ -1,6 +1,5 @@ package dev.inmo.plaguposter.common -import dev.inmo.tgbotapi.types.ChatId import dev.inmo.tgbotapi.types.FullChatIdentifierSerializer import dev.inmo.tgbotapi.types.IdChatIdentifier import kotlinx.serialization.SerialName @@ -10,14 +9,26 @@ import kotlinx.serialization.Serializable data class ChatConfig( @SerialName("targetChat") @Serializable(FullChatIdentifierSerializer::class) - val targetChatId: IdChatIdentifier, + val targetChatId: IdChatIdentifier? = null, @SerialName("sourceChat") @Serializable(FullChatIdentifierSerializer::class) val sourceChatId: IdChatIdentifier, @SerialName("cacheChat") @Serializable(FullChatIdentifierSerializer::class) - val cacheChatId: IdChatIdentifier + val cacheChatId: IdChatIdentifier, + @SerialName("targetChats") + val targetChatIds: List<@Serializable(FullChatIdentifierSerializer::class) IdChatIdentifier> = emptyList(), ) { + val allTargetChatIds by lazy { + listOfNotNull(targetChatId) + targetChatIds + } + + init { + require(targetChatId != null || targetChatIds.isNotEmpty()) { + "One of fields, 'targetChat' or 'targetChats' should be presented" + } + } + fun check(chatId: IdChatIdentifier) = when (chatId) { targetChatId, sourceChatId, diff --git a/common/src/jvmMain/kotlin/CommonPlugin.kt b/common/src/jvmMain/kotlin/CommonPlugin.kt index 9f55aa6..5324019 100644 --- a/common/src/jvmMain/kotlin/CommonPlugin.kt +++ b/common/src/jvmMain/kotlin/CommonPlugin.kt @@ -1,6 +1,5 @@ package dev.inmo.plaguposter.common -import dev.inmo.kslog.common.i import dev.inmo.kslog.common.iS import dev.inmo.kslog.common.logger import dev.inmo.plagubot.Plugin @@ -27,7 +26,7 @@ object CommonPlugin : Plugin { override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { val config = koin.get() - Log.iS { "Target chat info: ${getChat(config.targetChatId)}" } + Log.iS { "Target chats info: ${config.allTargetChatIds.map { getChat(it) }.joinToString()}" } Log.iS { "Source chat info: ${getChat(config.sourceChatId)}" } Log.iS { "Cache chat info: ${getChat(config.cacheChatId)}" } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ee7cbd8..bf3de3e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,16 +1,16 @@ [versions] -kotlin = "1.8.21" -kotlin-serialization = "1.5.0" +kotlin = "1.8.22" +kotlin-serialization = "1.5.1" -plagubot = "5.1.2" -tgbotapi = "7.1.2" -microutils = "0.18.1" -kslog = "1.1.1" -krontab = "1.0.0" -plagubot-plugins = "0.11.2" +plagubot = "7.0.0" +tgbotapi = "9.0.0" +microutils = "0.19.9" +kslog = "1.1.2" +krontab = "2.1.2" +plagubot-plugins = "0.13.0" -dokka = "1.8.10" +dokka = "1.8.20" psql = "42.6.0" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 070cb70..98debb8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/posts/src/commonMain/kotlin/sending/PostPublisher.kt b/posts/src/commonMain/kotlin/sending/PostPublisher.kt index 3073c03..3d3d736 100644 --- a/posts/src/commonMain/kotlin/sending/PostPublisher.kt +++ b/posts/src/commonMain/kotlin/sending/PostPublisher.kt @@ -18,7 +18,7 @@ class PostPublisher( private val bot: TelegramBot, private val postsRepo: PostsRepo, private val cachingChatId: IdChatIdentifier, - private val targetChatId: IdChatIdentifier, + private val targetChatIds: List, private val deleteAfterPosting: Boolean = true ) { suspend fun publish(postId: PostId) { @@ -38,17 +38,19 @@ class PostPublisher( sortedMessagesContents.forEach { (_, contents) -> contents.singleOrNull() ?.also { - runCatching { - bot.copyMessage(targetChatId, it.chatId, it.messageId) - }.onFailure { _ -> + targetChatIds.forEach { targetChatId -> runCatching { - bot.forwardMessage( - it.chatId, - targetChatId, - it.messageId - ) - }.onSuccess { - bot.copyMessage(targetChatId, it) + bot.copyMessage(targetChatId, it.chatId, it.messageId) + }.onFailure { _ -> + runCatching { + bot.forwardMessage( + it.chatId, + targetChatId, + it.messageId + ) + }.onSuccess { + bot.copyMessage(targetChatId, it) + } } } return@forEach @@ -57,17 +59,23 @@ class PostPublisher( it.order to (bot.forwardMessage(toChatId = cachingChatId, fromChatId = it.chatId, messageId = it.messageId).contentMessageOrNull() ?: return@mapNotNull null) }.sortedBy { it.first }.mapNotNull { (_, forwardedMessage) -> forwardedMessage.withContentOrNull() ?: null.also { _ -> - bot.copyMessage(targetChatId, forwardedMessage) + targetChatIds.forEach { targetChatId -> + bot.copyMessage(targetChatId, forwardedMessage) + } } } resultContents.singleOrNull() ?.also { - bot.copyMessage(targetChatId, it) + targetChatIds.forEach { targetChatId -> + bot.copyMessage(targetChatId, it) + } return@forEach } ?: resultContents.chunked(mediaCountInMediaGroup.last).forEach { - bot.send( - targetChatId, - it.map { it.content.toMediaGroupMemberTelegramMedia() } - ) + targetChatIds.forEach { targetChatId -> + bot.send( + targetChatId, + it.map { it.content.toMediaGroupMemberTelegramMedia() } + ) + } } } diff --git a/posts/src/jvmMain/kotlin/Plugin.kt b/posts/src/jvmMain/kotlin/Plugin.kt index 047094a..61eb501 100644 --- a/posts/src/jvmMain/kotlin/Plugin.kt +++ b/posts/src/jvmMain/kotlin/Plugin.kt @@ -29,7 +29,6 @@ import kotlinx.serialization.json.* import org.jetbrains.exposed.sql.Database import org.koin.core.Koin import org.koin.core.module.Module -import org.koin.dsl.binds object Plugin : Plugin { @Serializable @@ -59,7 +58,7 @@ object Plugin : Plugin { } single { val config = get() - PostPublisher(get(), get(), config.chats.cacheChatId, config.chats.targetChatId, config.deleteAfterPublishing) + PostPublisher(get(), get(), config.chats.cacheChatId, config.chats.allTargetChatIds, config.deleteAfterPublishing) } }