update dependencies && add opportunity to use several targetChatIds instead of one

This commit is contained in:
InsanusMokrassar 2023-08-12 23:49:25 +06:00
parent 6eb43055a7
commit 29e5a04135
6 changed files with 51 additions and 34 deletions

View File

@ -1,6 +1,5 @@
package dev.inmo.plaguposter.common package dev.inmo.plaguposter.common
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.FullChatIdentifierSerializer import dev.inmo.tgbotapi.types.FullChatIdentifierSerializer
import dev.inmo.tgbotapi.types.IdChatIdentifier import dev.inmo.tgbotapi.types.IdChatIdentifier
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
@ -10,14 +9,26 @@ import kotlinx.serialization.Serializable
data class ChatConfig( data class ChatConfig(
@SerialName("targetChat") @SerialName("targetChat")
@Serializable(FullChatIdentifierSerializer::class) @Serializable(FullChatIdentifierSerializer::class)
val targetChatId: IdChatIdentifier, val targetChatId: IdChatIdentifier? = null,
@SerialName("sourceChat") @SerialName("sourceChat")
@Serializable(FullChatIdentifierSerializer::class) @Serializable(FullChatIdentifierSerializer::class)
val sourceChatId: IdChatIdentifier, val sourceChatId: IdChatIdentifier,
@SerialName("cacheChat") @SerialName("cacheChat")
@Serializable(FullChatIdentifierSerializer::class) @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) { fun check(chatId: IdChatIdentifier) = when (chatId) {
targetChatId, targetChatId,
sourceChatId, sourceChatId,

View File

@ -1,6 +1,5 @@
package dev.inmo.plaguposter.common package dev.inmo.plaguposter.common
import dev.inmo.kslog.common.i
import dev.inmo.kslog.common.iS import dev.inmo.kslog.common.iS
import dev.inmo.kslog.common.logger import dev.inmo.kslog.common.logger
import dev.inmo.plagubot.Plugin import dev.inmo.plagubot.Plugin
@ -27,7 +26,7 @@ object CommonPlugin : Plugin {
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
val config = koin.get<ChatConfig>() val config = koin.get<ChatConfig>()
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 { "Source chat info: ${getChat(config.sourceChatId)}" }
Log.iS { "Cache chat info: ${getChat(config.cacheChatId)}" } Log.iS { "Cache chat info: ${getChat(config.cacheChatId)}" }
} }

View File

@ -1,16 +1,16 @@
[versions] [versions]
kotlin = "1.8.21" kotlin = "1.8.22"
kotlin-serialization = "1.5.0" kotlin-serialization = "1.5.1"
plagubot = "5.1.2" plagubot = "7.0.0"
tgbotapi = "7.1.2" tgbotapi = "9.0.0"
microutils = "0.18.1" microutils = "0.19.9"
kslog = "1.1.1" kslog = "1.1.2"
krontab = "1.0.0" krontab = "2.1.2"
plagubot-plugins = "0.11.2" plagubot-plugins = "0.13.0"
dokka = "1.8.10" dokka = "1.8.20"
psql = "42.6.0" psql = "42.6.0"

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists 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 zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

View File

@ -18,7 +18,7 @@ class PostPublisher(
private val bot: TelegramBot, private val bot: TelegramBot,
private val postsRepo: PostsRepo, private val postsRepo: PostsRepo,
private val cachingChatId: IdChatIdentifier, private val cachingChatId: IdChatIdentifier,
private val targetChatId: IdChatIdentifier, private val targetChatIds: List<IdChatIdentifier>,
private val deleteAfterPosting: Boolean = true private val deleteAfterPosting: Boolean = true
) { ) {
suspend fun publish(postId: PostId) { suspend fun publish(postId: PostId) {
@ -38,17 +38,19 @@ class PostPublisher(
sortedMessagesContents.forEach { (_, contents) -> sortedMessagesContents.forEach { (_, contents) ->
contents.singleOrNull() ?.also { contents.singleOrNull() ?.also {
runCatching { targetChatIds.forEach { targetChatId ->
bot.copyMessage(targetChatId, it.chatId, it.messageId)
}.onFailure { _ ->
runCatching { runCatching {
bot.forwardMessage( bot.copyMessage(targetChatId, it.chatId, it.messageId)
it.chatId, }.onFailure { _ ->
targetChatId, runCatching {
it.messageId bot.forwardMessage(
) it.chatId,
}.onSuccess { targetChatId,
bot.copyMessage(targetChatId, it) it.messageId
)
}.onSuccess {
bot.copyMessage(targetChatId, it)
}
} }
} }
return@forEach 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) it.order to (bot.forwardMessage(toChatId = cachingChatId, fromChatId = it.chatId, messageId = it.messageId).contentMessageOrNull() ?: return@mapNotNull null)
}.sortedBy { it.first }.mapNotNull { (_, forwardedMessage) -> }.sortedBy { it.first }.mapNotNull { (_, forwardedMessage) ->
forwardedMessage.withContentOrNull<MediaGroupPartContent>() ?: null.also { _ -> forwardedMessage.withContentOrNull<MediaGroupPartContent>() ?: null.also { _ ->
bot.copyMessage(targetChatId, forwardedMessage) targetChatIds.forEach { targetChatId ->
bot.copyMessage(targetChatId, forwardedMessage)
}
} }
} }
resultContents.singleOrNull() ?.also { resultContents.singleOrNull() ?.also {
bot.copyMessage(targetChatId, it) targetChatIds.forEach { targetChatId ->
bot.copyMessage(targetChatId, it)
}
return@forEach return@forEach
} ?: resultContents.chunked(mediaCountInMediaGroup.last).forEach { } ?: resultContents.chunked(mediaCountInMediaGroup.last).forEach {
bot.send( targetChatIds.forEach { targetChatId ->
targetChatId, bot.send(
it.map { it.content.toMediaGroupMemberTelegramMedia() } targetChatId,
) it.map { it.content.toMediaGroupMemberTelegramMedia() }
)
}
} }
} }

View File

@ -29,7 +29,6 @@ import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin import org.koin.core.Koin
import org.koin.core.module.Module import org.koin.core.module.Module
import org.koin.dsl.binds
object Plugin : Plugin { object Plugin : Plugin {
@Serializable @Serializable
@ -59,7 +58,7 @@ object Plugin : Plugin {
} }
single { single {
val config = get<Config>() val config = get<Config>()
PostPublisher(get(), get(), config.chats.cacheChatId, config.chats.targetChatId, config.deleteAfterPublishing) PostPublisher(get(), get(), config.chats.cacheChatId, config.chats.allTargetChatIds, config.deleteAfterPublishing)
} }
} }