TelegramBotApiLibraries/resender/src/commonMain/kotlin/MessagesResender.kt

83 lines
3.4 KiB
Kotlin
Raw Normal View History

2023-01-14 16:37:56 +00:00
package dev.inmo.tgbotapi.libraries.resender
2024-01-13 08:37:21 +00:00
import dev.inmo.micro_utils.common.applyDiff
2023-01-14 16:37:56 +00:00
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.ForwardMessage
import dev.inmo.tgbotapi.requests.send.CopyMessage
2024-01-13 08:37:21 +00:00
import dev.inmo.tgbotapi.requests.send.CopyMessages
2023-01-14 16:37:56 +00:00
import dev.inmo.tgbotapi.requests.send.media.SendMediaGroup
import dev.inmo.tgbotapi.types.ChatIdentifier
import dev.inmo.tgbotapi.types.IdChatIdentifier
import dev.inmo.tgbotapi.types.mediaCountInMediaGroup
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
class MessagesResender(
private val bot: TelegramBot,
private val cacheChatId: ChatIdentifier
) {
suspend fun resend(
targetChatId: IdChatIdentifier,
messagesInfo: List<MessageMetaInfo>,
onBetweenMessages: suspend (sent: List<MessageMetaInfo>, toBeSent: List<MessageMetaInfo>) -> Unit
2023-01-14 16:37:56 +00:00
): List<Pair<MessageMetaInfo, MessageMetaInfo>> {
2024-01-13 08:37:21 +00:00
val currentGroup = mutableListOf<MessageMetaInfo>()
suspend fun makeCopy(): List<Pair<MessageMetaInfo, MessageMetaInfo>> {
currentGroup.sortBy { it.messageId }
while (currentGroup.isNotEmpty()) {
return runCatching {
bot.execute(
CopyMessages(
toChatId = targetChatId,
fromChatId = currentGroup.firstOrNull() ?.chatId ?: return emptyList(),
messageIds = currentGroup.map { it.messageId }
2023-01-14 16:37:56 +00:00
)
2024-01-13 08:37:21 +00:00
).mapIndexed { i, newMessageId ->
currentGroup[i] to MessageMetaInfo(targetChatId, newMessageId)
}.also {
currentGroup.clear()
2023-01-14 16:37:56 +00:00
}
2024-01-13 08:37:21 +00:00
}.getOrElse {
currentGroup.applyDiff(
currentGroup.filter {
runCatching {
2023-01-14 16:37:56 +00:00
bot.execute(
2024-01-13 08:37:21 +00:00
ForwardMessage(
toChatId = cacheChatId,
fromChatId = it.chatId,
messageId = it.messageId
2023-01-14 16:37:56 +00:00
)
)
2024-01-13 08:37:21 +00:00
}.isSuccess
2023-01-14 16:37:56 +00:00
}
2024-01-13 08:37:21 +00:00
)
null
} ?: continue
2023-01-14 16:37:56 +00:00
}
2024-01-13 08:37:21 +00:00
return emptyList()
}
2023-01-14 16:37:56 +00:00
2024-01-13 08:37:21 +00:00
val copied = mutableListOf<Pair<MessageMetaInfo, MessageMetaInfo>>()
for (content in messagesInfo) {
when {
currentGroup.isEmpty() ||
currentGroup.first().chatId == content.chatId -> currentGroup.add(content)
else -> {
onBetweenMessages(copied.map { it.first }, currentGroup.toList())
copied.addAll(makeCopy())
}
}
2023-01-14 16:37:56 +00:00
}
2024-01-13 08:37:21 +00:00
if (currentGroup.isNotEmpty()) {
onBetweenMessages(copied.map { it.first }, currentGroup.toList())
copied.addAll(makeCopy())
}
return copied.toList()
2023-01-14 16:37:56 +00:00
}
suspend fun resend(
targetChatId: IdChatIdentifier,
messagesInfo: List<MessageMetaInfo>
): List<Pair<MessageMetaInfo, MessageMetaInfo>> = resend(targetChatId, messagesInfo) { _, _ -> }
2023-01-14 16:37:56 +00:00
}