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
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,

View File

@ -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<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 { "Cache chat info: ${getChat(config.cacheChatId)}" }
}

View File

@ -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"

View File

@ -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

View File

@ -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<IdChatIdentifier>,
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<MediaGroupPartContent>() ?: 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() }
)
}
}
}

View File

@ -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<Config>()
PostPublisher(get(), get(), config.chats.cacheChatId, config.chats.targetChatId, config.deleteAfterPublishing)
PostPublisher(get(), get(), config.chats.cacheChatId, config.chats.allTargetChatIds, config.deleteAfterPublishing)
}
}