2022-09-14 17:32:32 +00:00
|
|
|
package dev.inmo.plaguposter.posts.panel
|
|
|
|
|
2022-09-14 19:39:51 +00:00
|
|
|
import com.benasher44.uuid.uuid4
|
|
|
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
|
|
|
import dev.inmo.micro_utils.koin.getAllDistinct
|
|
|
|
import dev.inmo.micro_utils.repos.deleteById
|
|
|
|
import dev.inmo.micro_utils.repos.set
|
2022-09-14 17:32:32 +00:00
|
|
|
import dev.inmo.plagubot.Plugin
|
2022-09-14 19:39:51 +00:00
|
|
|
import dev.inmo.plaguposter.common.ChatConfig
|
|
|
|
import dev.inmo.plaguposter.posts.models.PostId
|
|
|
|
import dev.inmo.plaguposter.posts.panel.repos.PostsMessages
|
|
|
|
import dev.inmo.plaguposter.posts.repo.PostsRepo
|
2022-09-16 10:53:57 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
2022-09-14 19:39:51 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.delete
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.edit.edit
|
2022-09-16 10:53:57 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.edit.text.editMessageText
|
2022-09-14 19:39:51 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.send
|
2022-09-14 17:32:32 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
2022-09-14 19:39:51 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessageDataCallbackQuery
|
|
|
|
import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage
|
|
|
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
|
|
|
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard
|
2022-09-14 20:15:35 +00:00
|
|
|
import dev.inmo.tgbotapi.types.ChatId
|
|
|
|
import dev.inmo.tgbotapi.types.MessageIdentifier
|
2022-09-14 19:39:51 +00:00
|
|
|
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
|
|
|
|
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
|
|
|
|
import dev.inmo.tgbotapi.types.message.ParseMode
|
|
|
|
import kotlinx.coroutines.flow.first
|
2022-09-14 17:32:32 +00:00
|
|
|
import kotlinx.serialization.Serializable
|
|
|
|
import kotlinx.serialization.json.*
|
|
|
|
import org.jetbrains.exposed.sql.Database
|
|
|
|
import org.koin.core.Koin
|
|
|
|
import org.koin.core.module.Module
|
|
|
|
|
|
|
|
object Plugin : Plugin {
|
|
|
|
@Serializable
|
|
|
|
internal data class Config (
|
2022-09-14 19:39:51 +00:00
|
|
|
val text: String = "Post settings:",
|
|
|
|
val parseMode: ParseMode? = null,
|
|
|
|
val buttonsPerRow: Int = 4,
|
|
|
|
val deleteButtonText: String? = null,
|
2022-09-16 10:53:57 +00:00
|
|
|
val rootButtonText: String = "\uD83D\uDD19",
|
2022-09-16 15:11:30 +00:00
|
|
|
val refreshButtonText: String? = "\uD83D\uDD04"
|
2022-09-14 17:32:32 +00:00
|
|
|
)
|
|
|
|
override fun Module.setupDI(database: Database, params: JsonObject) {
|
2022-09-14 19:39:51 +00:00
|
|
|
params["panel"] ?.let { element ->
|
|
|
|
single { get<Json>().decodeFromJsonElement(Config.serializer(), element) }
|
|
|
|
}
|
|
|
|
single {
|
|
|
|
val config = getOrNull<Config>() ?: Config()
|
|
|
|
val builtInButtons = listOfNotNull(
|
|
|
|
config.deleteButtonText ?.let { text ->
|
|
|
|
PanelButtonBuilder {
|
|
|
|
CallbackDataInlineKeyboardButton(
|
|
|
|
text,
|
|
|
|
"delete ${it.id.string}"
|
|
|
|
)
|
|
|
|
}
|
2022-09-16 10:53:57 +00:00
|
|
|
},
|
2022-09-16 15:11:30 +00:00
|
|
|
config.refreshButtonText ?.let { text ->
|
2022-09-16 10:53:57 +00:00
|
|
|
PanelButtonBuilder {
|
|
|
|
CallbackDataInlineKeyboardButton(
|
|
|
|
text,
|
|
|
|
"refresh ${it.id.string}"
|
|
|
|
)
|
|
|
|
}
|
2022-09-14 19:39:51 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
PanelButtonsAPI(
|
|
|
|
getAllDistinct<PanelButtonBuilder>() + builtInButtons,
|
|
|
|
config.rootButtonText
|
|
|
|
)
|
|
|
|
}
|
2022-09-14 17:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
2022-09-14 19:39:51 +00:00
|
|
|
val postsRepo = koin.get<PostsRepo>()
|
|
|
|
val chatsConfig = koin.get<ChatConfig>()
|
|
|
|
val config = koin.getOrNull<Config>() ?: Config()
|
2022-09-14 20:15:35 +00:00
|
|
|
val api = koin.get<PanelButtonsAPI>()
|
2022-09-14 19:39:51 +00:00
|
|
|
val postsMessages = PostsMessages(koin.get(), koin.get())
|
|
|
|
|
|
|
|
postsRepo.newObjectsFlow.subscribeSafelyWithoutExceptions(this) {
|
|
|
|
val firstContent = it.content.first()
|
2022-09-14 20:15:35 +00:00
|
|
|
val buttons = api.buttonsBuilders.chunked(config.buttonsPerRow).mapNotNull { row ->
|
2022-09-14 19:39:51 +00:00
|
|
|
row.mapNotNull { builder ->
|
|
|
|
builder.buildButton(it)
|
|
|
|
}.takeIf { it.isNotEmpty() }
|
|
|
|
}
|
|
|
|
send(
|
|
|
|
firstContent.chatId,
|
|
|
|
text = config.text,
|
|
|
|
parseMode = config.parseMode,
|
|
|
|
replyToMessageId = firstContent.messageId,
|
|
|
|
replyMarkup = InlineKeyboardMarkup(buttons),
|
|
|
|
disableNotification = true
|
|
|
|
).also { sentMessage ->
|
|
|
|
postsMessages.set(it.id, sentMessage.chat.id to sentMessage.messageId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
postsRepo.deletedObjectsIdsFlow.subscribeSafelyWithoutExceptions(this) {
|
|
|
|
val (chatId, messageId) = postsMessages.get(it) ?: return@subscribeSafelyWithoutExceptions
|
|
|
|
|
|
|
|
delete(chatId, messageId)
|
|
|
|
}
|
|
|
|
|
2022-09-16 10:53:57 +00:00
|
|
|
suspend fun refreshPostMessage(
|
2022-09-14 20:15:35 +00:00
|
|
|
postId: PostId,
|
|
|
|
chatId: ChatId,
|
|
|
|
messageId: MessageIdentifier
|
2022-09-14 19:39:51 +00:00
|
|
|
) {
|
2022-09-14 20:15:35 +00:00
|
|
|
val post = postsRepo.getById(postId) ?: return
|
|
|
|
val buttons = api.buttonsBuilders.chunked(config.buttonsPerRow).mapNotNull { row ->
|
2022-09-14 19:39:51 +00:00
|
|
|
row.mapNotNull { builder ->
|
|
|
|
builder.buildButton(post)
|
|
|
|
}.takeIf { it.isNotEmpty() }
|
|
|
|
}
|
2022-09-16 10:53:57 +00:00
|
|
|
editMessageText(
|
2022-09-14 20:15:35 +00:00
|
|
|
chatId,
|
|
|
|
messageId,
|
2022-09-16 10:53:57 +00:00
|
|
|
text = config.text,
|
2022-09-14 19:39:51 +00:00
|
|
|
replyMarkup = InlineKeyboardMarkup(buttons)
|
|
|
|
)
|
|
|
|
}
|
2022-09-14 20:15:35 +00:00
|
|
|
|
|
|
|
onMessageDataCallbackQuery (
|
|
|
|
initialFilter = {
|
|
|
|
it.data.startsWith(PanelButtonsAPI.openGlobalMenuDataPrefix) && it.message.chat.id == chatsConfig.sourceChatId
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
val postId = it.data.removePrefix(PanelButtonsAPI.openGlobalMenuDataPrefix).let(::PostId)
|
2022-09-16 10:53:57 +00:00
|
|
|
refreshPostMessage(postId, it.message.chat.id, it.message.messageId)
|
2022-09-16 15:14:38 +00:00
|
|
|
answer(it)
|
2022-09-14 20:15:35 +00:00
|
|
|
}
|
2022-09-14 19:39:51 +00:00
|
|
|
onMessageDataCallbackQuery(
|
|
|
|
initialFilter = {
|
|
|
|
it.data.startsWith("delete ") && it.message.chat.id == chatsConfig.sourceChatId
|
|
|
|
}
|
|
|
|
) { query ->
|
|
|
|
val postId = query.data.removePrefix("delete ").let(::PostId)
|
|
|
|
val post = postsRepo.getById(postId) ?: return@onMessageDataCallbackQuery
|
|
|
|
|
|
|
|
val approveData = uuid4().toString()
|
|
|
|
|
|
|
|
edit(
|
|
|
|
query.message,
|
|
|
|
replyMarkup = flatInlineKeyboard {
|
|
|
|
dataButton("\uD83D\uDDD1", approveData)
|
2022-09-14 20:15:35 +00:00
|
|
|
api.RootPanelButtonBuilder.buildButton(post) ?.let(::add)
|
2022-09-14 19:39:51 +00:00
|
|
|
}
|
|
|
|
)
|
2022-09-16 10:53:57 +00:00
|
|
|
answer(query)
|
2022-09-14 19:39:51 +00:00
|
|
|
|
|
|
|
val pushedButton = waitMessageDataCallbackQuery().first {
|
|
|
|
it.message.sameMessage(query.message)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pushedButton.data == approveData) {
|
|
|
|
postsRepo.deleteById(postId)
|
|
|
|
}
|
|
|
|
}
|
2022-09-16 10:53:57 +00:00
|
|
|
onMessageDataCallbackQuery(
|
|
|
|
initialFilter = {
|
|
|
|
it.data.startsWith("refresh ") && it.message.chat.id == chatsConfig.sourceChatId
|
|
|
|
}
|
|
|
|
) { query ->
|
|
|
|
val postId = query.data.removePrefix("refresh ").let(::PostId)
|
|
|
|
val (chatId, messageId) = postsMessages.get(postId) ?: return@onMessageDataCallbackQuery
|
|
|
|
|
|
|
|
refreshPostMessage(
|
|
|
|
postId,
|
|
|
|
chatId,
|
|
|
|
messageId
|
|
|
|
)
|
|
|
|
answer(query)
|
|
|
|
}
|
2022-09-14 20:15:35 +00:00
|
|
|
|
|
|
|
api.forceRefreshFlow.subscribeSafelyWithoutExceptions(this) {
|
|
|
|
val (chatId, messageId) = postsMessages.get(it) ?: return@subscribeSafelyWithoutExceptions
|
2022-09-16 10:53:57 +00:00
|
|
|
refreshPostMessage(it, chatId, messageId)
|
2022-09-14 20:15:35 +00:00
|
|
|
}
|
2022-09-14 17:32:32 +00:00
|
|
|
}
|
|
|
|
}
|