complete adding of panel plugin

This commit is contained in:
2022-09-15 02:15:35 +06:00
parent 849df78238
commit 2a883c25ca
6 changed files with 142 additions and 17 deletions

View File

@@ -12,6 +12,7 @@ kotlin {
dependencies {
api project(":plaguposter.common")
api project(":plaguposter.ratings")
api project(":plaguposter.posts.panel")
}
}
jvmMain {

View File

@@ -14,17 +14,21 @@ import dev.inmo.plaguposter.inlines.models.Format
import dev.inmo.plaguposter.inlines.models.OfferTemplate
import dev.inmo.plaguposter.inlines.repos.InlineTemplatesRepo
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.panel.PanelButtonBuilder
import dev.inmo.plaguposter.posts.panel.PanelButtonsAPI
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.ratings.models.Rating
import dev.inmo.plaguposter.ratings.repo.RatingsRepo
import dev.inmo.plaguposter.ratings.source.models.*
import dev.inmo.plaguposter.ratings.source.repos.*
import dev.inmo.tgbotapi.extensions.api.answers.answer
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.api.send.send
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
import dev.inmo.tgbotapi.types.message.textsources.regular
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
@@ -41,7 +45,8 @@ object Plugin : Plugin {
@Serializable(RatingsVariantsSerializer::class)
val variants: RatingsVariants,
val autoAttach: Boolean,
val ratingOfferText: String
val ratingOfferText: String,
val panelButtonText: String = "Ratings"
)
override fun Module.setupDI(database: Database, params: JsonObject) {
@@ -66,6 +71,7 @@ object Plugin : Plugin {
val ratingsRepo = koin.get<RatingsRepo>()
val postsRepo = koin.get<PostsRepo>()
val config = koin.get<Config>()
val panelApi = koin.getOrNull<PanelButtonsAPI>()
onPollUpdates (markerFactory = { it.id }) { poll ->
val postId = pollsToPostsIdsRepo.get(poll.id) ?: return@onPollUpdates
@@ -92,6 +98,7 @@ object Plugin : Plugin {
pollsToPostsIdsRepo.set(sent.content.poll.id, postId)
pollsToMessageInfoRepo.set(sent.content.poll.id, sent.short())
}.getOrNull() ?: continue
panelApi ?.forceRefresh(postId)
return true
}
return false
@@ -105,6 +112,8 @@ object Plugin : Plugin {
delete(messageInfo.chatId, messageInfo.messageId)
}.onFailure {
this@Plugin.logger.e(it) { "Something went wrong when trying to remove ratings message ($messageInfo) for post $postId" }
}.onSuccess {
panelApi ?.forceRefresh(postId)
}.isSuccess
}.any().also {
if (it) {
@@ -209,5 +218,35 @@ object Plugin : Plugin {
)
)
}
koin.getOrNull<PanelButtonsAPI>() ?.apply {
add(
PanelButtonBuilder {
CallbackDataInlineKeyboardButton(
config.panelButtonText + if (pollsToPostsIdsRepo.keys(it.id, firstPageWithOneElementPagination).results.any()) {
SuccessfulSymbol
} else {
UnsuccessfulSymbol
},
"toggle_ratings ${it.id.string}"
)
}
)
onMessageDataCallbackQuery(
initialFilter = {
it.data.startsWith("toggle_ratings ")
}
) {
val postId = it.data.removePrefix("toggle_ratings ").let(::PostId)
if (pollsToPostsIdsRepo.keys(postId, firstPageWithOneElementPagination).results.any()) {
detachPoll(postId)
} else {
attachPoll(postId)
}
answer(it)
}
}
}
}