This commit is contained in:
InsanusMokrassar 2021-04-05 23:34:41 +06:00
parent 338e97770d
commit 6fe5f96e4e

View File

@ -31,6 +31,8 @@ import dev.inmo.tgbotapi.types.message.abstracts.Message
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.toList
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlin.random.Random
@ -256,7 +258,6 @@ data class ExpressionCaptchaProvider(
) {
val userBanDateTime = eventDateTime + checkTimeSpan
newUsers.mapNotNull { user ->
safelyWithoutExceptions {
launch {
doInSubContext {
val callbackData = ExpressionBuilder.createExpression(
@ -290,8 +291,11 @@ data class ExpressionCaptchaProvider(
}
}
var passed = true
var passed: Boolean? = null
val passedMutex = Mutex()
val callback: suspend (Boolean) -> Unit = {
passedMutex.withLock {
if (passed == null) {
removeRedundantMessages()
passed = it
if (it) {
@ -302,8 +306,18 @@ data class ExpressionCaptchaProvider(
}
}
}
}
}
val banJob = launch {
delay((userBanDateTime - eventDateTime).millisecondsLong)
if (passed == null) {
callback(false)
stop()
}
}
val job = parallel {
var leftAttempts = attempts
waitDataCallbackQuery {
when {
@ -313,7 +327,7 @@ data class ExpressionCaptchaProvider(
if (leftAttempts < 1) {
this
} else {
launch { answerCallbackQuery(this@waitDataCallbackQuery, leftRetriesText + leftAttempts) }
answerCallbackQuery(this@waitDataCallbackQuery, leftRetriesText + leftAttempts)
null
}
}
@ -321,15 +335,10 @@ data class ExpressionCaptchaProvider(
}
}.first()
banJob.cancel()
callback(leftAttempts > 0)
}
delay((userBanDateTime - eventDateTime).millisecondsLong)
if (job.isActive) job.cancel()
callback(passed)
}
}
}
}.joinAll()
}