mirror of
https://github.com/InsanusMokrassar/PlaguPoster.git
synced 2024-11-25 08:58:45 +00:00
complete timer plugin
This commit is contained in:
parent
c632a2ba14
commit
b9c78982b5
@ -1,15 +1,27 @@
|
|||||||
package dev.inmo.plaguposter.common
|
package dev.inmo.plaguposter.common
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.types.ChatId
|
import dev.inmo.tgbotapi.types.ChatId
|
||||||
|
import dev.inmo.tgbotapi.types.FullChatIdentifierSerializer
|
||||||
|
import dev.inmo.tgbotapi.types.IdChatIdentifier
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class ChatConfig(
|
data class ChatConfig(
|
||||||
@SerialName("targetChat")
|
@SerialName("targetChat")
|
||||||
val targetChatId: ChatId,
|
@Serializable(FullChatIdentifierSerializer::class)
|
||||||
|
val targetChatId: IdChatIdentifier,
|
||||||
@SerialName("sourceChat")
|
@SerialName("sourceChat")
|
||||||
val sourceChatId: ChatId,
|
@Serializable(FullChatIdentifierSerializer::class)
|
||||||
|
val sourceChatId: IdChatIdentifier,
|
||||||
@SerialName("cacheChat")
|
@SerialName("cacheChat")
|
||||||
val cacheChatId: ChatId
|
@Serializable(FullChatIdentifierSerializer::class)
|
||||||
)
|
val cacheChatId: IdChatIdentifier
|
||||||
|
) {
|
||||||
|
fun check(chatId: IdChatIdentifier) = when (chatId) {
|
||||||
|
targetChatId,
|
||||||
|
sourceChatId,
|
||||||
|
cacheChatId -> true
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,10 +6,20 @@ import dev.inmo.kslog.common.logger
|
|||||||
import dev.inmo.plagubot.Plugin
|
import dev.inmo.plagubot.Plugin
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import org.jetbrains.exposed.sql.Database
|
||||||
import org.koin.core.Koin
|
import org.koin.core.Koin
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
object CommonPlugin : Plugin {
|
object CommonPlugin : Plugin {
|
||||||
private val Log = logger
|
private val Log = logger
|
||||||
|
override fun Module.setupDI(database: Database, params: JsonObject) {
|
||||||
|
single { CoroutineScope(Dispatchers.Default + SupervisorJob()) }
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
||||||
val config = koin.get<ChatConfig>()
|
val config = koin.get<ChatConfig>()
|
||||||
|
|
||||||
|
@ -17,8 +17,8 @@ import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent
|
|||||||
class PostPublisher(
|
class PostPublisher(
|
||||||
private val bot: TelegramBot,
|
private val bot: TelegramBot,
|
||||||
private val postsRepo: PostsRepo,
|
private val postsRepo: PostsRepo,
|
||||||
private val cachingChatId: ChatId,
|
private val cachingChatId: IdChatIdentifier,
|
||||||
private val targetChatId: ChatId,
|
private val targetChatId: IdChatIdentifier,
|
||||||
private val deleteAfterPosting: Boolean = true
|
private val deleteAfterPosting: Boolean = true
|
||||||
) {
|
) {
|
||||||
suspend fun publish(postId: PostId) {
|
suspend fun publish(postId: PostId) {
|
||||||
|
@ -101,6 +101,7 @@ object Plugin : Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val post = postsRepo.getById(postId) ?: return false
|
val post = postsRepo.getById(postId) ?: return false
|
||||||
|
ratingsRepo.set(postId, Rating(0.0))
|
||||||
for (content in post.content) {
|
for (content in post.content) {
|
||||||
runCatchingSafely {
|
runCatchingSafely {
|
||||||
val sent = send(
|
val sent = send(
|
||||||
@ -140,7 +141,7 @@ object Plugin : Plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
postsRepo.deletedObjectsIdsFlow.subscribeSafelyWithoutExceptions(this) { postId ->
|
ratingsRepo.onValueRemoved.subscribeSafelyWithoutExceptions(this) { postId ->
|
||||||
detachPoll(postId)
|
detachPoll(postId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package dev.inmo.plaguposter.ratings.exposed
|
package dev.inmo.plaguposter.ratings.exposed
|
||||||
|
|
||||||
import dev.inmo.micro_utils.pagination.utils.optionallyReverse
|
import dev.inmo.micro_utils.pagination.utils.optionallyReverse
|
||||||
|
import dev.inmo.micro_utils.repos.exposed.initTable
|
||||||
import dev.inmo.micro_utils.repos.exposed.keyvalue.AbstractExposedKeyValueRepo
|
import dev.inmo.micro_utils.repos.exposed.keyvalue.AbstractExposedKeyValueRepo
|
||||||
import dev.inmo.plaguposter.posts.models.PostId
|
import dev.inmo.plaguposter.posts.models.PostId
|
||||||
import dev.inmo.plaguposter.ratings.models.Rating
|
import dev.inmo.plaguposter.ratings.models.Rating
|
||||||
@ -24,6 +25,10 @@ class ExposedRatingsRepo (
|
|||||||
override val ResultRow.asObject: Rating
|
override val ResultRow.asObject: Rating
|
||||||
get() = get(ratingsColumn).let(::Rating)
|
get() = get(ratingsColumn).let(::Rating)
|
||||||
|
|
||||||
|
init {
|
||||||
|
initTable()
|
||||||
|
}
|
||||||
|
|
||||||
override fun update(k: PostId, v: Rating, it: UpdateBuilder<Int>) {
|
override fun update(k: PostId, v: Rating, it: UpdateBuilder<Int>) {
|
||||||
it[ratingsColumn] = v.double
|
it[ratingsColumn] = v.double
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,8 @@ dependencies {
|
|||||||
api project(":plaguposter.triggers.command")
|
api project(":plaguposter.triggers.command")
|
||||||
api project(":plaguposter.triggers.selector_with_timer")
|
api project(":plaguposter.triggers.selector_with_timer")
|
||||||
api project(":plaguposter.triggers.timer")
|
api project(":plaguposter.triggers.timer")
|
||||||
|
api project(":plaguposter.triggers.timer.disablers.autoposts")
|
||||||
|
api project(":plaguposter.triggers.timer.disablers.ratings")
|
||||||
api project(":plaguposter.ratings")
|
api project(":plaguposter.ratings")
|
||||||
api project(":plaguposter.ratings.source")
|
api project(":plaguposter.ratings.source")
|
||||||
api project(":plaguposter.ratings.selector")
|
api project(":plaguposter.ratings.selector")
|
||||||
|
@ -13,6 +13,8 @@ String[] includes = [
|
|||||||
":triggers:selector_with_timer",
|
":triggers:selector_with_timer",
|
||||||
":triggers:selector_with_scheduling",
|
":triggers:selector_with_scheduling",
|
||||||
":triggers:timer",
|
":triggers:timer",
|
||||||
|
":triggers:timer:disablers:ratings",
|
||||||
|
":triggers:timer:disablers:autoposts",
|
||||||
":inlines",
|
":inlines",
|
||||||
// ":settings",
|
// ":settings",
|
||||||
":runner"
|
":runner"
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
package dev.inmo.plaguposter.triggers.selector_with_timer
|
||||||
|
|
||||||
|
import com.soywiz.klock.DateTime
|
||||||
|
import dev.inmo.plaguposter.posts.models.PostId
|
||||||
|
|
||||||
|
fun interface AutopostFilter {
|
||||||
|
suspend fun check(postId: PostId, dateTime: DateTime): Boolean
|
||||||
|
}
|
@ -34,9 +34,12 @@ object Plugin : Plugin {
|
|||||||
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
||||||
val publisher = koin.get<PostPublisher>()
|
val publisher = koin.get<PostPublisher>()
|
||||||
val selector = koin.get<Selector>()
|
val selector = koin.get<Selector>()
|
||||||
koin.get<Config>().krontab.asFlow().subscribeSafelyWithoutExceptions(this) {
|
val filters = koin.getAll<AutopostFilter>().distinct()
|
||||||
selector.take(now = it).forEach { postId ->
|
koin.get<Config>().krontab.asFlow().subscribeSafelyWithoutExceptions(this) { dateTime ->
|
||||||
publisher.publish(postId)
|
selector.take(now = dateTime).forEach { postId ->
|
||||||
|
if (filters.all { it.check(postId, dateTime) }) {
|
||||||
|
publisher.publish(postId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ kotlin {
|
|||||||
dependencies {
|
dependencies {
|
||||||
api project(":plaguposter.common")
|
api project(":plaguposter.common")
|
||||||
api project(":plaguposter.posts")
|
api project(":plaguposter.posts")
|
||||||
|
api project(":plaguposter.posts.panel")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
18
triggers/timer/disablers/autoposts/build.gradle
Normal file
18
triggers/timer/disablers/autoposts/build.gradle
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
api project(":plaguposter.common")
|
||||||
|
api project(":plaguposter.triggers.timer")
|
||||||
|
api project(":plaguposter.triggers.selector_with_timer")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
package dev.inmo.plaguposter.triggers.timer.disablers.autoposts
|
@ -0,0 +1,27 @@
|
|||||||
|
package dev.inmo.plaguposter.triggers.timer.disablers.autoposts
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
|
import dev.inmo.micro_utils.koin.singleWithRandomQualifier
|
||||||
|
import dev.inmo.micro_utils.koin.singleWithRandomQualifierAndBinds
|
||||||
|
import dev.inmo.micro_utils.pagination.FirstPagePagination
|
||||||
|
import dev.inmo.micro_utils.repos.unset
|
||||||
|
import dev.inmo.plagubot.Plugin
|
||||||
|
import dev.inmo.plaguposter.ratings.repo.RatingsRepo
|
||||||
|
import dev.inmo.plaguposter.triggers.selector_with_timer.AutopostFilter
|
||||||
|
import dev.inmo.plaguposter.triggers.timer.TimersRepo
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.serialization.json.*
|
||||||
|
import org.jetbrains.exposed.sql.Database
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
|
object Plugin : Plugin {
|
||||||
|
override fun Module.setupDI(database: Database, params: JsonObject) {
|
||||||
|
singleWithRandomQualifier<AutopostFilter> {
|
||||||
|
val timersRepo = get<TimersRepo>()
|
||||||
|
AutopostFilter { _, dateTime ->
|
||||||
|
val result = timersRepo.keys(dateTime, FirstPagePagination(1))
|
||||||
|
result.results.isEmpty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.plaguposter.triggers.timer.disablers.autoposts"/>
|
18
triggers/timer/disablers/ratings/build.gradle
Normal file
18
triggers/timer/disablers/ratings/build.gradle
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
api project(":plaguposter.common")
|
||||||
|
api project(":plaguposter.triggers.timer")
|
||||||
|
api project(":plaguposter.ratings")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
package dev.inmo.plaguposter.triggers.timer.disablers.ratings
|
@ -0,0 +1,26 @@
|
|||||||
|
package dev.inmo.plaguposter.triggers.timer.disablers.ratings
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
|
import dev.inmo.micro_utils.koin.singleWithRandomQualifier
|
||||||
|
import dev.inmo.micro_utils.repos.unset
|
||||||
|
import dev.inmo.plagubot.Plugin
|
||||||
|
import dev.inmo.plaguposter.ratings.repo.RatingsRepo
|
||||||
|
import dev.inmo.plaguposter.triggers.timer.TimersRepo
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.serialization.json.*
|
||||||
|
import org.jetbrains.exposed.sql.Database
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
|
object Plugin : Plugin {
|
||||||
|
override fun Module.setupDI(database: Database, params: JsonObject) {
|
||||||
|
singleWithRandomQualifier(createdAtStart = true) {
|
||||||
|
val timersRepo = get<TimersRepo>()
|
||||||
|
val ratingsRepo = get<RatingsRepo>()
|
||||||
|
val scope = get<CoroutineScope>()
|
||||||
|
|
||||||
|
timersRepo.onNewValue.subscribeSafelyWithoutExceptions(scope) {
|
||||||
|
ratingsRepo.unset(it.first)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.inmo.plaguposter.triggers.timer.disablers.ratings"/>
|
@ -1,54 +1,87 @@
|
|||||||
package dev.inmo.plaguposter.triggers.timer
|
package dev.inmo.plaguposter.triggers.timer
|
||||||
|
|
||||||
|
import com.soywiz.klock.DateFormat
|
||||||
import com.soywiz.klock.DateTime
|
import com.soywiz.klock.DateTime
|
||||||
import com.soywiz.klock.DateTimeTz
|
import com.soywiz.klock.DateTimeTz
|
||||||
import com.soywiz.klock.Month
|
import com.soywiz.klock.Month
|
||||||
import com.soywiz.klock.Year
|
import com.soywiz.klock.Year
|
||||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||||
|
import dev.inmo.micro_utils.repos.unset
|
||||||
import dev.inmo.plaguposter.common.SuccessfulSymbol
|
import dev.inmo.plaguposter.common.SuccessfulSymbol
|
||||||
import dev.inmo.plaguposter.posts.models.Post
|
import dev.inmo.plaguposter.common.UnsuccessfulSymbol
|
||||||
import dev.inmo.plaguposter.posts.models.PostId
|
import dev.inmo.plaguposter.posts.models.PostId
|
||||||
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
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.edit.edit
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessageDataCallbackQuery
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessageDataCallbackQuery
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
|
||||||
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
|
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
|
||||||
|
import dev.inmo.tgbotapi.utils.bold
|
||||||
|
import dev.inmo.tgbotapi.utils.buildEntities
|
||||||
import dev.inmo.tgbotapi.utils.row
|
import dev.inmo.tgbotapi.utils.row
|
||||||
|
|
||||||
object ButtonsBuilder {
|
object ButtonsBuilder {
|
||||||
|
private const val changeTimeData = "timer_time_hint"
|
||||||
|
private const val changeDateData = "timer_date_hint"
|
||||||
private const val changeHoursDataPrefix = "timer_h"
|
private const val changeHoursDataPrefix = "timer_h"
|
||||||
private const val changeMinutesDataPrefix = "timer_m"
|
private const val changeMinutesDataPrefix = "timer_m"
|
||||||
private const val changeDayDataPrefix = "timer_d"
|
private const val changeDayDataPrefix = "timer_d"
|
||||||
private const val changeMonthDataPrefix = "timer_M"
|
private const val changeMonthDataPrefix = "timer_M"
|
||||||
private const val changeYearDataPrefix = "timer_y"
|
private const val changeYearDataPrefix = "timer_y"
|
||||||
private const val changeDateDataPrefix = "timer_s"
|
private const val changeDateDataPrefix = "timer_s"
|
||||||
|
private const val cancelDateData = "timer_c"
|
||||||
|
private const val deleteDateDataPrefix = "timer_r"
|
||||||
|
val datePrintFormat = DateFormat("hh:mm, dd.MM.yyyy, zzz")
|
||||||
|
|
||||||
fun buildTimerButtons(
|
fun buildTimerButtons(
|
||||||
postId: PostId,
|
postId: PostId,
|
||||||
dateTime: DateTimeTz
|
dateTime: DateTimeTz,
|
||||||
) = flatInlineKeyboard {
|
exists: Boolean
|
||||||
|
) = inlineKeyboard {
|
||||||
val unixMillis = dateTime.utc.unixMillisLong
|
val unixMillis = dateTime.utc.unixMillisLong
|
||||||
dataButton(dateTime.hours.toString(), "$changeHoursDataPrefix $postId $unixMillis")
|
row {
|
||||||
dataButton(":${dateTime.minutes}", "$changeMinutesDataPrefix $postId $unixMillis")
|
dataButton("Time:", changeTimeData)
|
||||||
|
dataButton(dateTime.hours.toString(), "$changeHoursDataPrefix $postId $unixMillis")
|
||||||
|
dataButton(dateTime.minutes.toString(), "$changeMinutesDataPrefix $postId $unixMillis")
|
||||||
|
}
|
||||||
|
row {
|
||||||
|
dataButton("Date:", changeDateData)
|
||||||
|
dataButton(dateTime.dayOfMonth.toString(), "$changeDayDataPrefix $postId $unixMillis")
|
||||||
|
dataButton(dateTime.month1.toString(), "$changeMonthDataPrefix $postId $unixMillis")
|
||||||
|
dataButton(dateTime.yearInt.toString(), "$changeYearDataPrefix $postId $unixMillis")
|
||||||
|
}
|
||||||
|
|
||||||
dataButton(dateTime.dayOfMonth.toString(), "$changeDayDataPrefix $postId $unixMillis")
|
row {
|
||||||
dataButton(".${dateTime.month1}", "$changeMonthDataPrefix $postId $unixMillis")
|
if (exists) {
|
||||||
dataButton(".${dateTime.yearInt}", "$changeYearDataPrefix $postId $unixMillis")
|
dataButton("\uD83D\uDDD1", "$deleteDateDataPrefix $postId")
|
||||||
|
}
|
||||||
|
dataButton(UnsuccessfulSymbol, cancelDateData)
|
||||||
|
dataButton(SuccessfulSymbol, "$changeDateDataPrefix $postId $unixMillis")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dataButton(SuccessfulSymbol, "$changeDateDataPrefix $postId $unixMillis")
|
fun buildTimerTextSources(
|
||||||
|
currentDateTime: DateTime,
|
||||||
|
previousTime: DateTime?
|
||||||
|
) = buildEntities {
|
||||||
|
previousTime ?.let {
|
||||||
|
+ "Previous timer time: " + bold(it.local.toString(datePrintFormat)) + "\n"
|
||||||
|
}
|
||||||
|
+"Currently editing time: " + bold(currentDateTime.local.toString(datePrintFormat))
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun BehaviourContext.includeKeyboardHandling(
|
suspend fun BehaviourContext.includeKeyboardHandling(
|
||||||
|
timersRepo: TimersRepo,
|
||||||
onSavePublishingTime: suspend (PostId, DateTime) -> Boolean
|
onSavePublishingTime: suspend (PostId, DateTime) -> Boolean
|
||||||
) {
|
) {
|
||||||
fun buildKeyboard(
|
fun buildKeyboard(
|
||||||
prefix: String,
|
prefix: String,
|
||||||
postId: PostId,
|
postId: PostId,
|
||||||
values: Iterable<Int>,
|
values: Iterable<Int>,
|
||||||
min: DateTime = DateTime.now(),
|
min: DateTime = nearestAvailableTimerTime(),
|
||||||
dateConverter: (Int) -> DateTimeTz
|
dateConverter: (Int) -> DateTimeTz
|
||||||
): InlineKeyboardMarkup {
|
): InlineKeyboardMarkup {
|
||||||
return inlineKeyboard {
|
return inlineKeyboard {
|
||||||
@ -90,13 +123,18 @@ object ButtonsBuilder {
|
|||||||
|
|
||||||
val currentMillis = rawDateTimeMillis.toLongOrNull() ?: return@onMessageDataCallbackQuery
|
val currentMillis = rawDateTimeMillis.toLongOrNull() ?: return@onMessageDataCallbackQuery
|
||||||
val currentDateTime = DateTime(currentMillis)
|
val currentDateTime = DateTime(currentMillis)
|
||||||
|
val postId = PostId(rawPostId)
|
||||||
|
val previousTime = timersRepo.get(postId)
|
||||||
edit(
|
edit(
|
||||||
it.message,
|
it.message.withContentOrNull() ?: return@onMessageDataCallbackQuery,
|
||||||
buildTimerButtons(
|
replyMarkup = buildTimerButtons(
|
||||||
PostId(rawPostId),
|
postId,
|
||||||
currentDateTime.local
|
currentDateTime.local,
|
||||||
|
timersRepo.contains(postId)
|
||||||
)
|
)
|
||||||
)
|
) {
|
||||||
|
+buildTimerTextSources(currentDateTime, previousTime)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +143,7 @@ object ButtonsBuilder {
|
|||||||
buildStandardDataCallbackQuery(
|
buildStandardDataCallbackQuery(
|
||||||
changeHoursDataPrefix,
|
changeHoursDataPrefix,
|
||||||
{
|
{
|
||||||
val now = DateTime.now().local
|
val now = nearestAvailableTimerTime().local
|
||||||
|
|
||||||
if (now.dateEq(it)) {
|
if (now.dateEq(it)) {
|
||||||
now.hours .. 23
|
now.hours .. 23
|
||||||
@ -123,7 +161,7 @@ object ButtonsBuilder {
|
|||||||
buildStandardDataCallbackQuery(
|
buildStandardDataCallbackQuery(
|
||||||
changeMinutesDataPrefix,
|
changeMinutesDataPrefix,
|
||||||
{
|
{
|
||||||
val now = DateTime.now().local
|
val now = nearestAvailableTimerTime().local
|
||||||
|
|
||||||
if (now.dateEq(it) && now.hours >= it.hours) {
|
if (now.dateEq(it) && now.hours >= it.hours) {
|
||||||
now.minutes until 60
|
now.minutes until 60
|
||||||
@ -141,7 +179,7 @@ object ButtonsBuilder {
|
|||||||
buildStandardDataCallbackQuery(
|
buildStandardDataCallbackQuery(
|
||||||
changeDayDataPrefix,
|
changeDayDataPrefix,
|
||||||
{
|
{
|
||||||
val now = DateTime.now().local
|
val now = nearestAvailableTimerTime().local
|
||||||
|
|
||||||
if (now.yearInt == it.yearInt && now.month0 == it.month0) {
|
if (now.yearInt == it.yearInt && now.month0 == it.month0) {
|
||||||
now.dayOfMonth .. it.month.days(it.year)
|
now.dayOfMonth .. it.month.days(it.year)
|
||||||
@ -159,7 +197,7 @@ object ButtonsBuilder {
|
|||||||
buildStandardDataCallbackQuery(
|
buildStandardDataCallbackQuery(
|
||||||
changeMonthDataPrefix,
|
changeMonthDataPrefix,
|
||||||
{
|
{
|
||||||
val now = DateTime.now().local
|
val now = nearestAvailableTimerTime().local
|
||||||
|
|
||||||
if (now.year == it.year) {
|
if (now.year == it.year) {
|
||||||
now.month1 .. 12
|
now.month1 .. 12
|
||||||
@ -177,7 +215,8 @@ object ButtonsBuilder {
|
|||||||
buildStandardDataCallbackQuery(
|
buildStandardDataCallbackQuery(
|
||||||
changeYearDataPrefix,
|
changeYearDataPrefix,
|
||||||
{
|
{
|
||||||
(it.year.year .. (it.year.year + 5))
|
val now = nearestAvailableTimerTime().local
|
||||||
|
(now.year.year .. (now.year.year + 5))
|
||||||
}
|
}
|
||||||
) { newValue, oldDateTime ->
|
) { newValue, oldDateTime ->
|
||||||
DateTimeTz.local(
|
DateTimeTz.local(
|
||||||
@ -186,6 +225,14 @@ object ButtonsBuilder {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onMessageDataCallbackQuery(changeTimeData) {
|
||||||
|
answer(it, "Use the buttons to the right to set post publishing time (hh:mm)", showAlert = true)
|
||||||
|
}
|
||||||
|
|
||||||
|
onMessageDataCallbackQuery(changeDateData) {
|
||||||
|
answer(it, "Use the buttons to the right to set post publishing date (dd.MM.yyyy)", showAlert = true)
|
||||||
|
}
|
||||||
|
|
||||||
onMessageDataCallbackQuery(Regex("$changeDateDataPrefix .*")) {
|
onMessageDataCallbackQuery(Regex("$changeDateDataPrefix .*")) {
|
||||||
val (_, rawPostId, rawDateTimeMillis) = it.data.split(" ")
|
val (_, rawPostId, rawDateTimeMillis) = it.data.split(" ")
|
||||||
val currentMillis = rawDateTimeMillis.toLongOrNull() ?: return@onMessageDataCallbackQuery
|
val currentMillis = rawDateTimeMillis.toLongOrNull() ?: return@onMessageDataCallbackQuery
|
||||||
@ -200,6 +247,29 @@ object ButtonsBuilder {
|
|||||||
it,
|
it,
|
||||||
if (success) "Successfully set timer" else "Unable to set timer"
|
if (success) "Successfully set timer" else "Unable to set timer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
it.message.delete(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
onMessageDataCallbackQuery(Regex("$deleteDateDataPrefix .*")) {
|
||||||
|
val (_, rawPostId) = it.data.split(" ")
|
||||||
|
val postId = PostId(rawPostId)
|
||||||
|
|
||||||
|
val success = runCatchingSafely {
|
||||||
|
timersRepo.unset(postId)
|
||||||
|
true
|
||||||
|
}.getOrElse { false }
|
||||||
|
|
||||||
|
answer(
|
||||||
|
it,
|
||||||
|
if (success) "Successfully unset timer" else "Unable to unset timer"
|
||||||
|
)
|
||||||
|
|
||||||
|
it.message.delete(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
onMessageDataCallbackQuery(cancelDateData) {
|
||||||
|
delete(it.message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
package dev.inmo.plaguposter.triggers.timer
|
||||||
|
|
||||||
|
import com.soywiz.klock.DateTime
|
||||||
|
import com.soywiz.klock.minutes
|
||||||
|
|
||||||
|
fun nearestAvailableTimerTime() = (DateTime.now() + 1.minutes).copyDayOfMonth(
|
||||||
|
milliseconds = 0,
|
||||||
|
seconds = 0
|
||||||
|
)
|
28
triggers/timer/src/commonMain/kotlin/TimerPanelButton.kt
Normal file
28
triggers/timer/src/commonMain/kotlin/TimerPanelButton.kt
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package dev.inmo.plaguposter.triggers.timer
|
||||||
|
|
||||||
|
import dev.inmo.plaguposter.common.SuccessfulSymbol
|
||||||
|
import dev.inmo.plaguposter.common.UnsuccessfulSymbol
|
||||||
|
import dev.inmo.plaguposter.posts.models.RegisteredPost
|
||||||
|
import dev.inmo.plaguposter.posts.panel.PanelButtonBuilder
|
||||||
|
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
|
||||||
|
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.InlineKeyboardButton
|
||||||
|
|
||||||
|
class TimerPanelButton(
|
||||||
|
private val timersRepo: TimersRepo
|
||||||
|
) : PanelButtonBuilder {
|
||||||
|
override val weight: Int
|
||||||
|
get() = 0
|
||||||
|
|
||||||
|
override suspend fun buildButton(post: RegisteredPost): InlineKeyboardButton? {
|
||||||
|
val publishingTime = timersRepo.get(post.id)
|
||||||
|
|
||||||
|
return CallbackDataInlineKeyboardButton(
|
||||||
|
"⏰ ${ if (publishingTime == null) UnsuccessfulSymbol else SuccessfulSymbol }",
|
||||||
|
"$timerSetPrefix ${post.id}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val timerSetPrefix = "timer_set_init"
|
||||||
|
}
|
||||||
|
}
|
@ -2,14 +2,20 @@ package dev.inmo.plaguposter.triggers.timer
|
|||||||
|
|
||||||
import com.soywiz.klock.DateTime
|
import com.soywiz.klock.DateTime
|
||||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||||
|
import dev.inmo.micro_utils.koin.singleWithRandomQualifierAndBinds
|
||||||
import dev.inmo.micro_utils.repos.set
|
import dev.inmo.micro_utils.repos.set
|
||||||
import dev.inmo.plagubot.Plugin
|
import dev.inmo.plagubot.Plugin
|
||||||
|
import dev.inmo.plaguposter.common.ChatConfig
|
||||||
|
import dev.inmo.plaguposter.posts.models.PostId
|
||||||
import dev.inmo.plaguposter.posts.repo.ReadPostsRepo
|
import dev.inmo.plaguposter.posts.repo.ReadPostsRepo
|
||||||
import dev.inmo.plaguposter.triggers.timer.repo.ExposedTimersRepo
|
import dev.inmo.plaguposter.triggers.timer.repo.ExposedTimersRepo
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
||||||
import dev.inmo.tgbotapi.extensions.api.edit.edit
|
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.api.send.send
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessageDataCallbackQuery
|
||||||
import kotlinx.serialization.json.*
|
import kotlinx.serialization.json.*
|
||||||
import org.jetbrains.exposed.sql.Database
|
import org.jetbrains.exposed.sql.Database
|
||||||
import org.koin.core.Koin
|
import org.koin.core.Koin
|
||||||
@ -20,39 +26,41 @@ object Plugin : Plugin {
|
|||||||
override fun Module.setupDI(database: Database, params: JsonObject) {
|
override fun Module.setupDI(database: Database, params: JsonObject) {
|
||||||
single { ExposedTimersRepo(get(), get(), get()) } binds arrayOf(TimersRepo::class)
|
single { ExposedTimersRepo(get(), get(), get()) } binds arrayOf(TimersRepo::class)
|
||||||
single(createdAtStart = true) { TimersHandler(get(), get(), get()) }
|
single(createdAtStart = true) { TimersHandler(get(), get(), get()) }
|
||||||
|
singleWithRandomQualifierAndBinds { TimerPanelButton(get()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
||||||
val postsRepo = koin.get<ReadPostsRepo>()
|
|
||||||
val timersRepo = koin.get<TimersRepo>()
|
val timersRepo = koin.get<TimersRepo>()
|
||||||
|
val chatsConfig = koin.get<ChatConfig>()
|
||||||
with(ButtonsBuilder) {
|
with(ButtonsBuilder) {
|
||||||
includeKeyboardHandling { postId, dateTime ->
|
includeKeyboardHandling(timersRepo) { postId, dateTime ->
|
||||||
timersRepo.set(postId, dateTime)
|
timersRepo.set(postId, dateTime)
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onCommand("test") {
|
onMessageDataCallbackQuery(
|
||||||
val reply = it.replyTo ?: return@onCommand
|
Regex("${TimerPanelButton.timerSetPrefix} [^\\s]+"),
|
||||||
val postId = postsRepo.getIdByChatAndMessage(
|
initialFilter = {
|
||||||
reply.chat.id,
|
chatsConfig.check(it.message.chat.id)
|
||||||
reply.messageId
|
}
|
||||||
) ?: return@onCommand
|
) {
|
||||||
|
val (_, postIdRaw) = it.data.split(" ")
|
||||||
|
val postId = PostId(postIdRaw)
|
||||||
|
val now = nearestAvailableTimerTime()
|
||||||
|
val exists = timersRepo.get(postId)
|
||||||
|
val textSources = ButtonsBuilder.buildTimerTextSources(now, exists)
|
||||||
val buttons = ButtonsBuilder.buildTimerButtons(
|
val buttons = ButtonsBuilder.buildTimerButtons(
|
||||||
postId,
|
postId,
|
||||||
DateTime.nowLocal()
|
now.local,
|
||||||
|
exists != null
|
||||||
)
|
)
|
||||||
runCatchingSafely {
|
reply(
|
||||||
edit(
|
it.message,
|
||||||
it,
|
textSources,
|
||||||
buttons
|
replyMarkup = buttons
|
||||||
)
|
)
|
||||||
}.onFailure { _ ->
|
|
||||||
send(
|
answer(it)
|
||||||
it.chat,
|
|
||||||
"Buttons",
|
|
||||||
replyMarkup = buttons
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user