add publisher

This commit is contained in:
InsanusMokrassar 2022-08-19 22:45:28 +06:00
parent 9502accf65
commit b1eed5ca1c
5 changed files with 94 additions and 8 deletions

View File

@ -1,11 +1,12 @@
[versions] [versions]
kotlin = "1.7.10" kotlin = "1.7.10"
kotlin-serialization = "1.4.0-RC" kotlin-serialization = "1.4.0"
plagubot = "2.1.0" plagubot = "2.1.0"
tgbotapi = "3.1.1" tgbotapi = "3.1.1"
microutils = "0.12.1" microutils = "0.12.2"
kslog = "0.5.1"
dexcount = "3.1.0" dexcount = "3.1.0"
junit_version = "4.12" junit_version = "4.12"
@ -32,6 +33,7 @@ tgbotapi = { module = "dev.inmo:tgbotapi", version.ref = "tgbotapi" }
plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" } plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" }
microutils-repos-common = { module = "dev.inmo:micro_utils.repos.common", version.ref = "microutils" } microutils-repos-common = { module = "dev.inmo:micro_utils.repos.common", version.ref = "microutils" }
microutils-repos-exposed = { module = "dev.inmo:micro_utils.repos.exposed", version.ref = "microutils" } microutils-repos-exposed = { module = "dev.inmo:micro_utils.repos.exposed", version.ref = "microutils" }
kslog = { module = "dev.inmo:kslog", version.ref = "kslog" }
# buildscript classpaths # buildscript classpaths

View File

@ -12,6 +12,7 @@ kotlin {
dependencies { dependencies {
api libs.tgbotapi api libs.tgbotapi
api libs.microutils.repos.common api libs.microutils.repos.common
api libs.kslog
} }
} }
jvmMain { jvmMain {

View 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() }
)
}
}
}
}

View File

@ -1,12 +1,39 @@
package dev.inmo.plaguposter.posts package dev.inmo.plaguposter.posts
import dev.inmo.kslog.common.logger
import dev.inmo.kslog.common.w
import dev.inmo.plagubot.Plugin import dev.inmo.plagubot.Plugin
import kotlinx.serialization.json.JsonObject import dev.inmo.plaguposter.posts.exposed.ExposedPostsRepo
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.posts.sending.PostPublisher
import dev.inmo.tgbotapi.types.ChatId
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.koin.core.module.Module import org.koin.core.module.Module
object Plugin : Plugin { object Plugin : Plugin {
@Serializable
data class Config(
@SerialName("targetChat")
val targetChatId: ChatId,
@SerialName("cacheChat")
val cacheChatId: ChatId
)
override fun Module.setupDI(database: Database, params: JsonObject) { override fun Module.setupDI(database: Database, params: JsonObject) {
TODO("Not yet implemented") val configJson = params["posts"] ?: this@Plugin.let {
it.logger.w {
"Unable to load posts plugin due to absence of `posts` key in config"
}
return
}
single { get<Json>().decodeFromJsonElement(Config.serializer(), configJson) }
single<PostsRepo> { ExposedPostsRepo(database, get()) }
single {
val config = get<Config>()
PostPublisher(get(), get(), config.cacheChatId, config.targetChatId)
}
} }
} }

View File

@ -4,12 +4,8 @@ import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.repos.KeyValuesRepo import dev.inmo.micro_utils.repos.KeyValuesRepo
import dev.inmo.micro_utils.repos.exposed.* import dev.inmo.micro_utils.repos.exposed.*
import dev.inmo.plaguposter.posts.models.* import dev.inmo.plaguposter.posts.models.*
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.tgbotapi.types.ChatId import dev.inmo.tgbotapi.types.ChatId
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateStatement
import sun.security.pkcs.ContentInfo
internal class ExposedContentInfoRepo( internal class ExposedContentInfoRepo(
override val database: Database, override val database: Database,