diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatJoinRequest.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatJoinRequest.kt index bfdfc6050f..3a2c827b51 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatJoinRequest.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatJoinRequest.kt @@ -1,5 +1,6 @@ package dev.inmo.tgbotapi.types +import com.soywiz.klock.DateTime import dev.inmo.tgbotapi.CommonAbstracts.FromUser import dev.inmo.tgbotapi.types.chat.abstracts.PublicChat import kotlinx.serialization.SerialName @@ -20,4 +21,7 @@ data class ChatJoinRequest( val inviteLink: ChatInviteLink, @SerialName(bioField) val bio: String? = null -) : FromUser +) : FromUser { + val dateTime: DateTime + get() = date.asDate +} 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 48535529b6..b91d6a98e6 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 @@ -54,7 +54,9 @@ sealed interface Poll { } @Serializable(PollSerializer::class) -sealed interface MultipleAnswersPoll : Poll +sealed interface MultipleAnswersPoll : Poll { + val allowMultipleAnswers: Boolean +} @Serializable private class RawPoll( @@ -122,7 +124,7 @@ data class RegularPoll( override val votesCount: Int, override val isClosed: Boolean = false, override val isAnonymous: Boolean = false, - val allowMultipleAnswers: Boolean = false, + override val allowMultipleAnswers: Boolean = false, override val scheduledCloseInfo: ScheduledCloseInfo? = null ) : MultipleAnswersPoll diff --git a/tgbotapi.utils/build.gradle b/tgbotapi.utils/build.gradle index f5f75bdb2c..b0e8726c5e 100644 --- a/tgbotapi.utils/build.gradle +++ b/tgbotapi.utils/build.gradle @@ -43,6 +43,7 @@ kotlin { dependencies { implementation kotlin('stdlib') api project(":tgbotapi.core") + api "dev.inmo:micro_utils.crypto:$micro_utils_version" } } diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt index a9e46e1268..3ea58f68ab 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/ClassCasts.kt @@ -3365,3 +3365,21 @@ inline fun MessageContent.asTextedInput(): TextedInput? = this as? TextedInput @PreviewFeature inline fun MessageContent.requireTextedInput(): TextedInput = this as TextedInput + +@PreviewFeature +inline fun ScheduledCloseInfo.whenExactScheduledCloseInfo(block: (ExactScheduledCloseInfo) -> T) = asExactScheduledCloseInfo() ?.let(block) + +@PreviewFeature +inline fun ScheduledCloseInfo.asExactScheduledCloseInfo(): ExactScheduledCloseInfo? = this as? ExactScheduledCloseInfo + +@PreviewFeature +inline fun ScheduledCloseInfo.requireExactScheduledCloseInfo(): ExactScheduledCloseInfo = this as ExactScheduledCloseInfo + +@PreviewFeature +inline fun ScheduledCloseInfo.whenApproximateScheduledCloseInfo(block: (ApproximateScheduledCloseInfo) -> T) = asApproximateScheduledCloseInfo() ?.let(block) + +@PreviewFeature +inline fun ScheduledCloseInfo.asApproximateScheduledCloseInfo(): ApproximateScheduledCloseInfo? = this as? ApproximateScheduledCloseInfo + +@PreviewFeature +inline fun ScheduledCloseInfo.requireApproximateScheduledCloseInfo(): ApproximateScheduledCloseInfo = this as ApproximateScheduledCloseInfo diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/Message.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/Message.kt index 652d84de23..8b20c66359 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/Message.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/Message.kt @@ -22,8 +22,6 @@ import dev.inmo.tgbotapi.types.polls.Poll import dev.inmo.tgbotapi.types.venue.Venue import dev.inmo.tgbotapi.utils.RiskFeature -private const val RawFieldsUsageWarning = "This API is raw and experimental. So, it is possible that behaviour of this API will be changed in near updates" - @RiskFeature(RawFieldsUsageWarning) inline val Message.from: User? get() = asFromUser() ?.from diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/Poll.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/Poll.kt new file mode 100644 index 0000000000..0612f012a0 --- /dev/null +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/Poll.kt @@ -0,0 +1,37 @@ +package dev.inmo.tgbotapi.extensions.utils.extensions.raw + +import dev.inmo.tgbotapi.extensions.utils.* +import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.types.MessageEntity.textsources.TextSourcesList +import dev.inmo.tgbotapi.types.polls.* +import dev.inmo.tgbotapi.utils.RiskFeature +import kotlinx.serialization.json.jsonPrimitive + +@RiskFeature(RawFieldsUsageWarning) +val Poll.total_voter_count: Int + get() = votesCount +@RiskFeature(RawFieldsUsageWarning) +val Poll.type: String + get() = when (this) { + is RegularPoll -> "regular" + is UnknownPollType -> raw["type"]!!.jsonPrimitive.content + is QuizPoll -> "quiz" + } +@RiskFeature(RawFieldsUsageWarning) +val Poll.allows_multiple_answers: Boolean + get() = asMultipleAnswersPoll() ?.allowMultipleAnswers == true +@RiskFeature(RawFieldsUsageWarning) +val Poll.correct_option_id: Int? + get() = asQuizPoll() ?.correctOptionId +@RiskFeature(RawFieldsUsageWarning) +val Poll.explanation: String? + get() = asQuizPoll() ?.text +@RiskFeature(RawFieldsUsageWarning) +val Poll.explanation_entities: TextSourcesList? + get() = asQuizPoll() ?.textSources +@RiskFeature(RawFieldsUsageWarning) +val Poll.open_period: Seconds? + get() = scheduledCloseInfo ?.asApproximateScheduledCloseInfo() ?.openDuration ?.seconds ?.toInt() +@RiskFeature(RawFieldsUsageWarning) +val Poll.close_date: TelegramDate? + get() = scheduledCloseInfo ?.asExactScheduledCloseInfo() ?.closeDateTime ?.toTelegramDate() diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/RawFieldsUsageWarning.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/RawFieldsUsageWarning.kt new file mode 100644 index 0000000000..8a09157cdd --- /dev/null +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/raw/RawFieldsUsageWarning.kt @@ -0,0 +1,3 @@ +package dev.inmo.tgbotapi.extensions.utils.extensions.raw + +internal const val RawFieldsUsageWarning = "This API is raw and experimental. So, it is possible that behaviour of this API will be changed in near updates"