2022-08-19 16:45:28 +00:00
|
|
|
package dev.inmo.plaguposter.posts.sending
|
|
|
|
|
|
|
|
import dev.inmo.kslog.common.logger
|
|
|
|
import dev.inmo.kslog.common.w
|
2022-08-20 16:26:38 +00:00
|
|
|
import dev.inmo.micro_utils.repos.deleteById
|
2022-08-19 16:45:28 +00:00
|
|
|
import dev.inmo.plaguposter.posts.models.PostId
|
|
|
|
import dev.inmo.plaguposter.posts.repo.PostsRepo
|
|
|
|
import dev.inmo.tgbotapi.bot.TelegramBot
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.forwardMessage
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.copyMessage
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.send
|
|
|
|
import dev.inmo.tgbotapi.extensions.utils.*
|
|
|
|
import dev.inmo.tgbotapi.types.*
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.MediaGroupContent
|
2022-11-17 09:09:10 +00:00
|
|
|
import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
|
2022-08-19 16:45:28 +00:00
|
|
|
|
|
|
|
class PostPublisher(
|
|
|
|
private val bot: TelegramBot,
|
2022-08-20 16:26:38 +00:00
|
|
|
private val postsRepo: PostsRepo,
|
2022-12-14 05:50:02 +00:00
|
|
|
private val cachingChatId: IdChatIdentifier,
|
|
|
|
private val targetChatId: IdChatIdentifier,
|
2022-08-20 16:26:38 +00:00
|
|
|
private val deleteAfterPosting: Boolean = true
|
2022-08-19 16:45:28 +00:00
|
|
|
) {
|
|
|
|
suspend fun publish(postId: PostId) {
|
2022-08-20 16:26:38 +00:00
|
|
|
val messagesInfo = postsRepo.getById(postId) ?: let {
|
2022-08-19 16:45:28 +00:00
|
|
|
logger.w { "Unable to get post with id $postId for publishing" }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
val sortedMessagesContents = messagesInfo.content.groupBy { it.group }.flatMap { (group, list) ->
|
|
|
|
if (group == null) {
|
|
|
|
list.map {
|
|
|
|
it.order to listOf(it)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
listOf(list.first().order to list)
|
|
|
|
}
|
|
|
|
}.sortedBy { it.first }
|
|
|
|
|
|
|
|
sortedMessagesContents.forEach { (_, contents) ->
|
|
|
|
contents.singleOrNull() ?.also {
|
2022-11-17 09:09:10 +00:00
|
|
|
runCatching {
|
|
|
|
bot.copyMessage(targetChatId, it.chatId, it.messageId)
|
|
|
|
}.onFailure { _ ->
|
|
|
|
runCatching {
|
|
|
|
bot.forwardMessage(
|
|
|
|
it.chatId,
|
|
|
|
targetChatId,
|
|
|
|
it.messageId
|
|
|
|
)
|
|
|
|
}.onSuccess {
|
|
|
|
bot.copyMessage(targetChatId, it)
|
|
|
|
}
|
|
|
|
}
|
2022-08-19 16:45:28 +00:00
|
|
|
return@forEach
|
|
|
|
}
|
|
|
|
val resultContents = contents.mapNotNull {
|
2022-09-04 10:11:54 +00:00
|
|
|
it.order to (bot.forwardMessage(toChatId = cachingChatId, fromChatId = it.chatId, messageId = it.messageId).contentMessageOrNull() ?: return@mapNotNull null)
|
2022-11-17 09:09:10 +00:00
|
|
|
}.sortedBy { it.first }.mapNotNull { (_, forwardedMessage) ->
|
|
|
|
forwardedMessage.withContentOrNull<MediaGroupPartContent>() ?: null.also { _ ->
|
|
|
|
bot.copyMessage(targetChatId, forwardedMessage)
|
2022-08-19 16:45:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
resultContents.singleOrNull() ?.also {
|
|
|
|
bot.copyMessage(targetChatId, it)
|
|
|
|
return@forEach
|
|
|
|
} ?: resultContents.chunked(mediaCountInMediaGroup.last).forEach {
|
|
|
|
bot.send(
|
|
|
|
targetChatId,
|
|
|
|
it.map { it.content.toMediaGroupMemberTelegramMedia() }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-20 16:26:38 +00:00
|
|
|
if (deleteAfterPosting) {
|
|
|
|
postsRepo.deleteById(postId)
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:45:28 +00:00
|
|
|
}
|
|
|
|
}
|