PlaguPoster/triggers/selector_with_timer/src/jvmMain/kotlin/Plugin.kt

188 lines
7.7 KiB
Kotlin
Raw Normal View History

2022-09-06 18:23:14 +00:00
package dev.inmo.plaguposter.triggers.selector_with_timer
2023-08-12 17:58:28 +00:00
import korlibs.time.DateFormat
2022-09-06 18:23:14 +00:00
import dev.inmo.krontab.KrontabTemplate
import dev.inmo.krontab.toSchedule
2023-03-18 06:35:18 +00:00
import dev.inmo.krontab.utils.asFlowWithDelays
2023-03-18 07:12:05 +00:00
import dev.inmo.krontab.utils.asFlowWithoutDelays
import dev.inmo.micro_utils.coroutines.runCatchingSafely
2022-09-06 18:23:14 +00:00
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.koin.singleWithRandomQualifier
2023-03-18 07:12:05 +00:00
import dev.inmo.micro_utils.pagination.FirstPagePagination
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.firstIndex
import dev.inmo.micro_utils.pagination.lastIndexExclusive
2022-09-06 18:23:14 +00:00
import dev.inmo.plagubot.Plugin
import dev.inmo.plagubot.plugins.inline.queries.models.Format
import dev.inmo.plagubot.plugins.inline.queries.models.OfferTemplate
2023-03-18 08:05:09 +00:00
import dev.inmo.plagubot.plugins.inline.queries.repos.InlineTemplatesRepo
2023-03-18 07:12:05 +00:00
import dev.inmo.plaguposter.common.ChatConfig
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.repo.ReadPostsRepo
2022-09-06 18:23:14 +00:00
import dev.inmo.plaguposter.posts.sending.PostPublisher
import dev.inmo.plaguposter.ratings.selector.Selector
2023-03-18 07:12:05 +00:00
import dev.inmo.tgbotapi.extensions.api.answers.answer
import dev.inmo.tgbotapi.extensions.api.edit.edit
import dev.inmo.tgbotapi.extensions.api.send.send
2022-09-06 18:23:14 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
2023-03-18 07:12:05 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessageDataCallbackQuery
import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat
import dev.inmo.tgbotapi.extensions.utils.formatting.makeLinkToMessage
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard
import dev.inmo.tgbotapi.extensions.utils.types.buttons.urlButton
import dev.inmo.tgbotapi.types.BotCommand
2023-03-18 07:12:05 +00:00
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
import dev.inmo.tgbotapi.utils.row
2022-09-06 18:23:14 +00:00
import kotlinx.coroutines.FlowPreview
2023-03-18 07:12:05 +00:00
import kotlinx.coroutines.flow.collectIndexed
import kotlinx.coroutines.flow.take
2022-09-06 18:23:14 +00:00
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
object Plugin : Plugin {
2023-03-18 07:12:05 +00:00
private const val pageCallbackDataQueryPrefix = "publishing_autoschedule page"
private const val pageCallbackDataQuerySize = 5
@Serializable
2022-09-06 18:23:14 +00:00
internal data class Config(
@SerialName("krontab")
2023-03-18 07:12:05 +00:00
val krontabTemplate: KrontabTemplate,
2023-11-06 13:18:19 +00:00
val dateTimeFormat: String = "HH:mm:ss, dd.MM.yyyy",
val retryOnPostFailureTimes: Int = 0
2022-09-06 18:23:14 +00:00
) {
@Transient
val krontab by lazy {
krontabTemplate.toSchedule()
}
2023-03-18 07:12:05 +00:00
@Transient
val format: DateFormat = DateFormat(dateTimeFormat)
2022-09-06 18:23:14 +00:00
}
override fun Module.setupDI(database: Database, params: JsonObject) {
single { get<Json>().decodeFromJsonElement(Config.serializer(), params["timer_trigger"] ?: return@single null) }
}
@OptIn(FlowPreview::class)
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
val publisher = koin.get<PostPublisher>()
val selector = koin.get<Selector>()
2022-12-14 05:50:02 +00:00
val filters = koin.getAll<AutopostFilter>().distinct()
2023-03-18 07:12:05 +00:00
val chatConfig = koin.get<ChatConfig>()
val postsRepo = koin.get<ReadPostsRepo>()
2023-03-18 08:05:09 +00:00
koin.getOrNull<InlineTemplatesRepo>() ?.apply {
addTemplate(
OfferTemplate(
"Autoschedule buttons",
listOf(
Format(
"/autoschedule_panel"
)
),
"Show autoscheduling publishing info"
)
)
}
2023-03-18 07:12:05 +00:00
val krontab = koin.get<Config>().krontab
2023-11-06 13:18:19 +00:00
val retryOnPostFailureTimes = koin.get<Config>().retryOnPostFailureTimes
2023-03-18 07:12:05 +00:00
val dateTimeFormat = koin.get<Config>().format
krontab.asFlowWithDelays().subscribeSafelyWithoutExceptions(this) { dateTime ->
2023-11-06 13:18:19 +00:00
var leftRetries = retryOnPostFailureTimes
do {
val success = runCatching {
selector.takeOneOrNull(now = dateTime) ?.let { postId ->
if (filters.all { it.check(postId, dateTime) }) {
publisher.publish(postId)
} else {
false
}
} ?: false
}.getOrElse {
false
2022-12-14 05:50:02 +00:00
}
2023-11-06 13:18:19 +00:00
if (success) {
break;
}
leftRetries--;
} while (leftRetries > 0)
2022-09-06 18:23:14 +00:00
}
2023-03-18 07:12:05 +00:00
suspend fun buildPage(pagination: Pagination = FirstPagePagination(size = pageCallbackDataQuerySize)): InlineKeyboardMarkup {
return inlineKeyboard {
row {
if (pagination.page > 1) {
dataButton("⬅️", "${pageCallbackDataQueryPrefix}0")
}
if (pagination.page > 0) {
dataButton("◀️", "${pageCallbackDataQueryPrefix}${pagination.page - 1}")
}
dataButton("\uD83D\uDD04 ${pagination.page}", "${pageCallbackDataQueryPrefix}${pagination.page}")
dataButton("▶️", "${pageCallbackDataQueryPrefix}${pagination.page + 1}")
}
val selected = mutableListOf<PostId>()
krontab.asFlowWithoutDelays().take(pagination.lastIndexExclusive).collectIndexed { i, dateTime ->
2023-11-06 13:18:19 +00:00
val postId = selector.takeOneOrNull(now = dateTime, exclude = selected) ?.also { postId ->
2023-03-18 07:12:05 +00:00
if (filters.all { it.check(postId, dateTime) }) {
selected.add(postId)
} else {
return@collectIndexed
}
}
val post = postsRepo.getFirstMessageInfo(postId ?: return@collectIndexed)
if (i < pagination.firstIndex || post == null) {
return@collectIndexed
}
row {
urlButton(
2023-03-18 07:48:26 +00:00
dateTime.local.format(dateTimeFormat),
2023-03-18 07:12:05 +00:00
makeLinkToMessage(post.chatId, post.messageId)
)
}
}
}
}
onCommand("autoschedule_panel", initialFilter = { chatConfig.allSourceChatIds.any { chatId -> it.sameChat(chatId) } }) {
2023-03-18 07:12:05 +00:00
val keyboard = buildPage()
runCatchingSafely {
edit(it, replyMarkup = keyboard) {
+"Your schedule:"
}
}.onFailure { _ ->
send(it.chat, replyMarkup = keyboard) {
+"Your schedule:"
}
}
}
onMessageDataCallbackQuery(
2023-03-18 07:48:26 +00:00
Regex("^$pageCallbackDataQueryPrefix\\d+"),
initialFilter = { chatConfig.allSourceChatIds.any { sourceChatId -> it.message.sameChat(sourceChatId) } }
2023-03-18 07:12:05 +00:00
) {
val page = it.data.removePrefix(pageCallbackDataQueryPrefix).toIntOrNull() ?: let { _ ->
answer(it)
return@onMessageDataCallbackQuery
}
runCatchingSafely {
edit(
it.message,
replyMarkup = buildPage(Pagination(page, size = pageCallbackDataQuerySize))
)
}.onFailure { _ ->
answer(it)
}
}
2022-09-06 18:23:14 +00:00
}
}