mirror of
https://github.com/InsanusMokrassar/PlaguPoster.git
synced 2026-03-20 16:02:36 +00:00
add templating of panelButtonText for ratings
This commit is contained in:
@@ -3,6 +3,7 @@ package dev.inmo.plaguposter.ratings.source
|
||||
import com.benasher44.uuid.uuid4
|
||||
import dev.inmo.kslog.common.e
|
||||
import dev.inmo.kslog.common.logger
|
||||
import dev.inmo.micro_utils.common.fixed
|
||||
import dev.inmo.micro_utils.coroutines.runCatchingLogging
|
||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
@@ -44,6 +45,7 @@ import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard
|
||||
import dev.inmo.tgbotapi.types.ReplyParameters
|
||||
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
|
||||
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonStyle
|
||||
import dev.inmo.tgbotapi.types.keyboardButtonRequestUserLimit
|
||||
import dev.inmo.tgbotapi.types.message.textsources.boldTextSource
|
||||
import dev.inmo.tgbotapi.types.message.textsources.regularTextSource
|
||||
import dev.inmo.tgbotapi.types.polls.InputPollOption
|
||||
@@ -58,9 +60,12 @@ import kotlinx.serialization.json.*
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
import org.koin.core.qualifier.named
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
object Plugin : Plugin {
|
||||
private val ratingVariantsQualifier = named("ratingsVariants")
|
||||
private const val attachedSymbolControlSymbol = "\${attached_symbol}"
|
||||
private const val currentRatingControlSymbol = "\${rating}"
|
||||
|
||||
@Serializable
|
||||
internal data class Config(
|
||||
@@ -68,9 +73,9 @@ object Plugin : Plugin {
|
||||
val variants: RatingsVariants,
|
||||
val autoAttach: Boolean,
|
||||
val ratingOfferText: String,
|
||||
val panelButtonText: String = "Ratings",
|
||||
val panelButtonText: String = "Ratings $attachedSymbolControlSymbol",
|
||||
val ratingEnabledStyle: KeyboardButtonStyle? = null,
|
||||
val ratingDisabledStyle: KeyboardButtonStyle? = null
|
||||
val ratingDisabledStyle: KeyboardButtonStyle? = null,
|
||||
)
|
||||
|
||||
override fun Module.setupDI(params: JsonObject) {
|
||||
@@ -299,18 +304,61 @@ object Plugin : Plugin {
|
||||
panelApi ?.apply {
|
||||
add(
|
||||
PanelButtonBuilder {
|
||||
val enabled = pollsToPostsIdsRepo.keys(it.id, firstPageWithOneElementPagination).results.any()
|
||||
suspend fun isEnabled() = pollsToPostsIdsRepo.keys(it.id, firstPageWithOneElementPagination).results.any()
|
||||
val enabled = let { _ ->
|
||||
var enabled: Boolean? = null
|
||||
suspend {
|
||||
enabled ?: (isEnabled().also { enabled = it })
|
||||
}
|
||||
}
|
||||
val rating = let { _ ->
|
||||
var rating: Rating? = null
|
||||
suspend {
|
||||
if (isEnabled()) {
|
||||
rating ?: ratingsRepo.get(it.id) ?.also { rating = it }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
val resultText = config.panelButtonText
|
||||
.let {
|
||||
if (it.contains(attachedSymbolControlSymbol)) {
|
||||
it.replace(
|
||||
attachedSymbolControlSymbol,
|
||||
if (enabled()) {
|
||||
SuccessfulSymbol
|
||||
} else {
|
||||
UnsuccessfulSymbol
|
||||
}
|
||||
)
|
||||
} else {
|
||||
it
|
||||
}
|
||||
}
|
||||
.let {
|
||||
val rating = rating()
|
||||
if (rating != null) {
|
||||
it.replace(
|
||||
currentRatingControlSymbol,
|
||||
rating.double.roundToInt().toString()
|
||||
)
|
||||
} else {
|
||||
it
|
||||
}
|
||||
}
|
||||
.take(12) // maximal text appropriate for this button
|
||||
CallbackDataInlineKeyboardButton(
|
||||
config.panelButtonText + if (enabled) {
|
||||
SuccessfulSymbol
|
||||
} else {
|
||||
UnsuccessfulSymbol
|
||||
},
|
||||
resultText,
|
||||
"toggle_ratings ${it.id.string}",
|
||||
style = if (enabled) {
|
||||
config.ratingEnabledStyle
|
||||
style = if (config.ratingEnabledStyle != null || config.ratingDisabledStyle != null) {
|
||||
if (enabled()) {
|
||||
config.ratingEnabledStyle
|
||||
} else {
|
||||
config.ratingDisabledStyle
|
||||
}
|
||||
} else {
|
||||
config.ratingDisabledStyle
|
||||
null
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -42,8 +42,10 @@
|
||||
},
|
||||
"autoAttach": true,
|
||||
"ratingOfferText": "What do you think about it?",
|
||||
"panelButtonText": "Ratings ${attached_symbol}",
|
||||
"ratingEnabledStyle": "success",
|
||||
"ratingDisabledStyle": "danger"
|
||||
"ratingDisabledStyle": "danger",
|
||||
"_note": "For panelButtonText you may use two control symbols: ${attached_symbol} to attach icon about enabled state and ${rating} to enable rating showing in button"
|
||||
},
|
||||
"selector": {
|
||||
"items": [
|
||||
|
||||
Reference in New Issue
Block a user