add raw fields for polls

This commit is contained in:
InsanusMokrassar 2022-01-06 16:42:19 +06:00
parent 3150cd6a37
commit 41852dde7c
7 changed files with 68 additions and 5 deletions

View File

@ -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
}

View File

@ -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

View File

@ -43,6 +43,7 @@ kotlin {
dependencies {
implementation kotlin('stdlib')
api project(":tgbotapi.core")
api "dev.inmo:micro_utils.crypto:$micro_utils_version"
}
}

View File

@ -3365,3 +3365,21 @@ inline fun MessageContent.asTextedInput(): TextedInput? = this as? TextedInput
@PreviewFeature
inline fun MessageContent.requireTextedInput(): TextedInput = this as TextedInput
@PreviewFeature
inline fun <T> 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 <T> 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

View File

@ -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

View File

@ -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()

View File

@ -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"