mirror of
https://github.com/InsanusMokrassar/PlaguPoster.git
synced 2025-12-06 04:55:41 +00:00
add publisher
This commit is contained in:
60
posts/src/commonMain/kotlin/sending/PostPublisher.kt
Normal file
60
posts/src/commonMain/kotlin/sending/PostPublisher.kt
Normal file
@@ -0,0 +1,60 @@
|
||||
package dev.inmo.plaguposter.posts.sending
|
||||
|
||||
import dev.inmo.kslog.common.logger
|
||||
import dev.inmo.kslog.common.w
|
||||
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
|
||||
|
||||
class PostPublisher(
|
||||
private val bot: TelegramBot,
|
||||
private val repo: PostsRepo,
|
||||
private val cachingChatId: ChatId,
|
||||
private val targetChatId: ChatId
|
||||
) {
|
||||
suspend fun publish(postId: PostId) {
|
||||
val messagesInfo = repo.getById(postId) ?: let {
|
||||
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 {
|
||||
bot.copyMessage(targetChatId, it.chatId, it.messageId)
|
||||
return@forEach
|
||||
}
|
||||
val resultContents = contents.mapNotNull {
|
||||
it.order to (bot.forwardMessage(cachingChatId, it.chatId, it.messageId).contentMessageOrNull() ?: return@mapNotNull null)
|
||||
}.sortedBy { it.first }.mapNotNull { (_, it) ->
|
||||
it.withContentOrNull<MediaGroupContent>() ?: null.also { _ ->
|
||||
bot.copyMessage(targetChatId, it)
|
||||
}
|
||||
}
|
||||
resultContents.singleOrNull() ?.also {
|
||||
bot.copyMessage(targetChatId, it)
|
||||
return@forEach
|
||||
} ?: resultContents.chunked(mediaCountInMediaGroup.last).forEach {
|
||||
bot.send(
|
||||
targetChatId,
|
||||
it.map { it.content.toMediaGroupMemberTelegramMedia() }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user