From 27dc302f5d6f5d881436cacfe5a5e2c23a6a6015 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 6 Feb 2021 11:53:30 +0600 Subject: [PATCH] update for compiling --- gradle.properties | 2 +- .../bot/settings/limiters/CommonLimiter.kt | 2 + .../requests/chat/modify/SetChatPhoto.kt | 2 + .../tgbotapi/requests/send/polls/SendPoll.kt | 107 ++++++++++++++---- .../passport/decrypted/AddressSecureValue.kt | 4 +- .../decrypted/PersonalDetailsSecureValue.kt | 4 +- .../dev/inmo/tgbotapi/types/polls/Poll.kt | 3 + 7 files changed, 98 insertions(+), 26 deletions(-) diff --git a/gradle.properties b/gradle.properties index 7c3511a5ab..20ac5b4bd5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,7 +7,7 @@ kotlin.incremental.js=true kotlin_version=1.4.30 kotlin_coroutines_version=1.4.2 -kotlin_serialisation_runtime_version=1.0.1 +kotlin_serialisation_runtime_version=1.1.0-RC klock_version=2.0.6 uuid_version=0.2.3 ktor_version=1.5.1 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/settings/limiters/CommonLimiter.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/settings/limiters/CommonLimiter.kt index a38d5a60fd..822f2170a6 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/settings/limiters/CommonLimiter.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/settings/limiters/CommonLimiter.kt @@ -17,7 +17,9 @@ class CommonLimiter( @Transient private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ) : RequestLimiter { + @Transient private val quotaSemaphore = Semaphore(lockCount) + @Transient private val counterRegeneratorJob = scope.launch { val regenDelay: MilliSeconds = (regenTime.toDouble() / lockCount).roundToLong() while (isActive) { diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/modify/SetChatPhoto.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/modify/SetChatPhoto.kt index f439f7e80a..4513de5fba 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/modify/SetChatPhoto.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/modify/SetChatPhoto.kt @@ -18,6 +18,8 @@ data class SetChatPhoto ( override fun method(): String = "setChatPhoto" override val resultDeserializer: DeserializationStrategy get() = Boolean.serializer() + @Transient override val mediaMap: Map = mapOf(photoField to photo) + @Transient override val paramsJson: JsonObject = toJson(serializer()) } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/polls/SendPoll.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/polls/SendPoll.kt index 0115d80198..d4bfd445cf 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/polls/SendPoll.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/send/polls/SendPoll.kt @@ -1,6 +1,7 @@ package dev.inmo.tgbotapi.requests.send.polls import com.soywiz.klock.DateTime +import com.soywiz.klock.TimeSpan import dev.inmo.tgbotapi.CommonAbstracts.* import dev.inmo.tgbotapi.requests.send.abstracts.ReplyingMarkupSendMessageRequest import dev.inmo.tgbotapi.requests.send.abstracts.SendMessageRequest @@ -16,6 +17,11 @@ import kotlinx.serialization.* private val commonResultDeserializer: DeserializationStrategy> = TelegramBotAPIMessageDeserializationStrategyClass() +private inline val ApproximateScheduledCloseInfo.openPeriod + get() = openDuration.millisecondsLong.div(1000) +private inline val ExactScheduledCloseInfo.closeDate + get() = closeDateTime.unixMillisLong.div(1000) + private fun checkPollInfo( question: String, options: List @@ -138,12 +144,23 @@ sealed class SendPoll : SendMessageRequest>, abstract val options: List abstract val isAnonymous: Boolean abstract val isClosed: Boolean - abstract val closeInfo: ScheduledCloseInfo? abstract val type: String internal abstract val openPeriod: LongSeconds? internal abstract val closeDate: LongSeconds? + protected val creationDate = DateTime.now() + open val closeInfo: ScheduledCloseInfo? + get() { + val openPeriod = openPeriod + val closeDate = closeDate + return when { + openPeriod != null -> openPeriod.asApproximateScheduledCloseInfo(creationDate) + closeDate != null -> closeDate.asExactScheduledCloseInfo + else -> null + } + } + override fun method(): String = "sendPoll" override val resultDeserializer: DeserializationStrategy> get() = commonResultDeserializer @@ -163,8 +180,10 @@ data class SendRegularPoll( override val isClosed: Boolean = false, @SerialName(allowsMultipleAnswersField) val allowMultipleAnswers: Boolean = false, - @Transient - override val closeInfo: ScheduledCloseInfo? = null, + @SerialName(openPeriodField) + override val openPeriod: LongSeconds?= null, + @SerialName(closeDateField) + override val closeDate: LongSeconds?, @SerialName(disableNotificationField) override val disableNotification: Boolean = false, @SerialName(replyToMessageIdField) @@ -178,20 +197,39 @@ data class SendRegularPoll( override val requestSerializer: SerializationStrategy<*> get() = serializer() - @SerialName(openPeriodField) - override val openPeriod: LongSeconds? - = (closeInfo as? ApproximateScheduledCloseInfo) ?.openDuration ?.millisecondsLong ?.div(1000) - - @SerialName(closeDateField) - override val closeDate: LongSeconds? - = (closeInfo as? ExactScheduledCloseInfo) ?.closeDateTime ?.unixMillisLong ?.div(1000) - init { checkPollInfo(question, options) closeInfo ?.checkSendData() } } +fun SendRegularPoll( + chatId: ChatIdentifier, + question: String, + options: List, + isAnonymous: Boolean = true, + isClosed: Boolean = false, + allowMultipleAnswers: Boolean = false, + closeInfo: ScheduledCloseInfo? = null, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + allowSendingWithoutReply: Boolean? = null, + replyMarkup: KeyboardMarkup? = null +) = SendRegularPoll( + chatId, + question, + options, + isAnonymous, + isClosed, + allowMultipleAnswers, + (closeInfo as? ApproximateScheduledCloseInfo) ?.openPeriod, + (closeInfo as? ExactScheduledCloseInfo) ?.closeDate, + disableNotification, + replyToMessageId, + allowSendingWithoutReply, + replyMarkup +) + fun SendQuizPoll( chatId: ChatIdentifier, question: String, @@ -253,6 +291,39 @@ fun SendQuizPoll( replyMarkup ) +internal fun SendQuizPoll( + chatId: ChatIdentifier, + question: String, + options: List, + correctOptionId: Int, + isAnonymous: Boolean = true, + isClosed: Boolean = false, + explanation: String? = null, + parseMode: ParseMode? = null, + rawEntities: List? = null, + closeInfo: ScheduledCloseInfo? = null, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + allowSendingWithoutReply: Boolean? = null, + replyMarkup: KeyboardMarkup? = null +) = SendQuizPoll( + chatId, + question, + options, + correctOptionId, + isAnonymous, + isClosed, + explanation, + parseMode, + rawEntities, + (closeInfo as? ApproximateScheduledCloseInfo) ?.openPeriod, + (closeInfo as? ExactScheduledCloseInfo) ?.closeDate, + disableNotification, + replyToMessageId, + allowSendingWithoutReply, + replyMarkup +) + @Serializable data class SendQuizPoll internal constructor( @SerialName(chatIdField) @@ -273,8 +344,10 @@ data class SendQuizPoll internal constructor( override val parseMode: ParseMode? = null, @SerialName(explanationEntitiesField) private val rawEntities: List? = null, - @Transient - override val closeInfo: ScheduledCloseInfo? = null, + @SerialName(openPeriodField) + override val openPeriod: LongSeconds? = null, + @SerialName(closeDateField) + override val closeDate: LongSeconds? = null, @SerialName(disableNotificationField) override val disableNotification: Boolean = false, @SerialName(replyToMessageIdField) @@ -291,14 +364,6 @@ data class SendQuizPoll internal constructor( rawEntities ?.asTextParts(explanation ?: return@lazy null) ?.justTextSources() } - @SerialName(openPeriodField) - override val openPeriod: LongSeconds? - = (closeInfo as? ApproximateScheduledCloseInfo) ?.openDuration ?.millisecondsLong ?.div(1000) - - @SerialName(closeDateField) - override val closeDate: LongSeconds? - = (closeInfo as? ExactScheduledCloseInfo) ?.closeDateTime ?.unixMillisLong ?.div(1000) - init { checkPollInfo(question, options) closeInfo ?.checkSendData() diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/decrypted/AddressSecureValue.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/decrypted/AddressSecureValue.kt index d403e11db3..e46f85bea3 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/decrypted/AddressSecureValue.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/decrypted/AddressSecureValue.kt @@ -4,13 +4,13 @@ import dev.inmo.tgbotapi.types.dataField import dev.inmo.tgbotapi.types.passport.credentials.DataCredentials import dev.inmo.tgbotapi.types.passport.credentials.EndDataCredentials import dev.inmo.tgbotapi.types.passport.decrypted.abstracts.SecureValueWithData -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable +import kotlinx.serialization.* @Serializable data class AddressSecureValue( @SerialName(dataField) override val data: DataCredentials ) : SecureValueWithData { + @Transient override val credentials: List = listOf(data) } \ No newline at end of file diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/decrypted/PersonalDetailsSecureValue.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/decrypted/PersonalDetailsSecureValue.kt index 120b2bf77a..d269f76402 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/decrypted/PersonalDetailsSecureValue.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/decrypted/PersonalDetailsSecureValue.kt @@ -4,13 +4,13 @@ import dev.inmo.tgbotapi.types.dataField import dev.inmo.tgbotapi.types.passport.credentials.DataCredentials import dev.inmo.tgbotapi.types.passport.credentials.EndDataCredentials import dev.inmo.tgbotapi.types.passport.decrypted.abstracts.SecureValueWithData -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable +import kotlinx.serialization.* @Serializable data class PersonalDetailsSecureValue( @SerialName(dataField) override val data: DataCredentials ) : SecureValueWithData { + @Transient override val credentials: List = listOf(data) } \ No newline at end of file diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/polls/Poll.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/polls/Poll.kt index 709fc5c9fe..8ca845fcd0 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/polls/Poll.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/polls/Poll.kt @@ -33,6 +33,9 @@ val LongSeconds.asApproximateScheduledCloseInfo get() = ApproximateScheduledCloseInfo( TimeSpan(this * 1000.0) ) +fun LongSeconds.asApproximateScheduledCloseInfo(startPoint: DateTime) = ApproximateScheduledCloseInfo( + TimeSpan(this * 1000.0), startPoint +) val LongSeconds.asExactScheduledCloseInfo get() = ExactScheduledCloseInfo( DateTime(unixMillis = this * 1000.0)