2022-08-18 12:59:05 +00:00
|
|
|
package dev.inmo.plaguposter.posts
|
|
|
|
|
2022-08-19 16:45:28 +00:00
|
|
|
import dev.inmo.kslog.common.logger
|
|
|
|
import dev.inmo.kslog.common.w
|
2022-09-04 09:46:45 +00:00
|
|
|
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
|
|
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
|
|
|
import dev.inmo.micro_utils.repos.deleteById
|
2022-08-18 12:59:05 +00:00
|
|
|
import dev.inmo.plagubot.Plugin
|
2022-09-04 09:46:45 +00:00
|
|
|
import dev.inmo.plaguposter.common.SuccessfulSymbol
|
|
|
|
import dev.inmo.plaguposter.common.UnsuccessfulSymbol
|
2022-08-19 16:45:28 +00:00
|
|
|
import dev.inmo.plaguposter.posts.exposed.ExposedPostsRepo
|
2022-09-09 13:40:46 +00:00
|
|
|
import dev.inmo.plaguposter.common.ChatConfig
|
2022-10-11 05:38:50 +00:00
|
|
|
import dev.inmo.plagubot.plugins.inline.queries.models.Format
|
|
|
|
import dev.inmo.plagubot.plugins.inline.queries.models.OfferTemplate
|
|
|
|
import dev.inmo.plagubot.plugins.inline.queries.repos.InlineTemplatesRepo
|
2022-09-04 07:27:35 +00:00
|
|
|
import dev.inmo.plaguposter.posts.repo.*
|
2022-08-19 16:45:28 +00:00
|
|
|
import dev.inmo.plaguposter.posts.sending.PostPublisher
|
2022-09-04 09:46:45 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.delete
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.edit.edit
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
|
|
|
import dev.inmo.tgbotapi.types.message.textsources.regular
|
2022-08-19 16:45:28 +00:00
|
|
|
import kotlinx.serialization.Serializable
|
|
|
|
import kotlinx.serialization.json.*
|
2022-08-18 12:59:05 +00:00
|
|
|
import org.jetbrains.exposed.sql.Database
|
2022-09-04 09:46:45 +00:00
|
|
|
import org.koin.core.Koin
|
2022-08-18 12:59:05 +00:00
|
|
|
import org.koin.core.module.Module
|
2022-09-04 07:27:35 +00:00
|
|
|
import org.koin.dsl.binds
|
2022-08-18 12:59:05 +00:00
|
|
|
|
|
|
|
object Plugin : Plugin {
|
2022-09-04 09:46:45 +00:00
|
|
|
@Serializable
|
|
|
|
data class Config(
|
|
|
|
val chats: ChatConfig,
|
2022-09-04 09:48:45 +00:00
|
|
|
val autoRemoveMessages: Boolean = true,
|
|
|
|
val deleteAfterPublishing: Boolean = true
|
2022-09-04 09:46:45 +00:00
|
|
|
)
|
2022-08-18 12:59:05 +00:00
|
|
|
override fun Module.setupDI(database: Database, params: JsonObject) {
|
2022-08-19 16:45:28 +00:00
|
|
|
val configJson = params["posts"] ?: this@Plugin.let {
|
|
|
|
it.logger.w {
|
|
|
|
"Unable to load posts plugin due to absence of `posts` key in config"
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-09-04 09:46:45 +00:00
|
|
|
single { get<Json>().decodeFromJsonElement(Config.serializer(), configJson) }
|
|
|
|
single { get<Config>().chats }
|
2022-09-04 07:27:35 +00:00
|
|
|
single { ExposedPostsRepo(database) } binds arrayOf(
|
|
|
|
PostsRepo::class,
|
|
|
|
ReadPostsRepo::class,
|
|
|
|
WritePostsRepo::class,
|
|
|
|
)
|
2022-08-19 16:45:28 +00:00
|
|
|
single {
|
2022-09-04 09:48:45 +00:00
|
|
|
val config = get<Config>()
|
|
|
|
PostPublisher(get(), get(), config.chats.cacheChatId, config.chats.targetChatId, config.deleteAfterPublishing)
|
2022-08-19 16:45:28 +00:00
|
|
|
}
|
2022-08-18 12:59:05 +00:00
|
|
|
}
|
2022-09-04 09:46:45 +00:00
|
|
|
|
|
|
|
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
|
|
|
val postsRepo = koin.get<PostsRepo>()
|
|
|
|
val config = koin.get<Config>()
|
|
|
|
|
|
|
|
if (config.autoRemoveMessages) {
|
|
|
|
postsRepo.removedPostsFlow.subscribeSafelyWithoutExceptions(this) {
|
|
|
|
it.content.forEach {
|
|
|
|
runCatchingSafely {
|
|
|
|
delete(it.chatId, it.messageId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onCommand("delete_post", requireOnlyCommandInMessage = true) {
|
|
|
|
val messageInReply = it.replyTo ?: run {
|
|
|
|
reply(it, "Reply some message of post to delete it")
|
|
|
|
return@onCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
val postId = postsRepo.getIdByChatAndMessage(messageInReply.chat.id, messageInReply.messageId) ?: run {
|
|
|
|
reply(it, "Unable to find post id by message")
|
|
|
|
return@onCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
postsRepo.deleteById(postId)
|
|
|
|
|
|
|
|
if (postsRepo.contains(postId)) {
|
|
|
|
edit(it, it.content.textSources + regular(UnsuccessfulSymbol))
|
|
|
|
} else {
|
|
|
|
edit(it, it.content.textSources + regular(SuccessfulSymbol))
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 13:40:46 +00:00
|
|
|
|
|
|
|
koin.getOrNull<InlineTemplatesRepo>() ?.addTemplate(
|
|
|
|
OfferTemplate(
|
|
|
|
"Delete post",
|
|
|
|
listOf(
|
|
|
|
Format("/delete_post")
|
2022-09-09 13:46:32 +00:00
|
|
|
),
|
|
|
|
"Should be used with a reply on any post message"
|
2022-09-09 13:40:46 +00:00
|
|
|
)
|
|
|
|
)
|
2022-09-04 09:46:45 +00:00
|
|
|
}
|
2022-08-18 12:59:05 +00:00
|
|
|
}
|