mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
separated User object and additional fields for Bot representation
This commit is contained in:
parent
8687a2ba6b
commit
a8ae0a296a
@ -26,6 +26,10 @@
|
|||||||
* `UnknownKeyboardButtonPollType`
|
* `UnknownKeyboardButtonPollType`
|
||||||
* `RegularKeyboardButtonPollType`
|
* `RegularKeyboardButtonPollType`
|
||||||
* `QuizKeyboardButtonPollType`
|
* `QuizKeyboardButtonPollType`
|
||||||
|
* `User` now is sealed class
|
||||||
|
* `CommonUser` was added as representation of default `User`
|
||||||
|
* `Bot` was added as representation of bot user
|
||||||
|
* `GetMe` now return `Bot` object
|
||||||
|
|
||||||
## 0.22.0
|
## 0.22.0
|
||||||
|
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.requests
|
package com.github.insanusmokrassar.TelegramBotAPI.requests
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
|
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.Bot
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.User
|
import com.github.insanusmokrassar.TelegramBotAPI.types.User
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
class GetMe : SimpleRequest<User> {
|
class GetMe : SimpleRequest<Bot> {
|
||||||
override fun method(): String = "getMe"
|
override fun method(): String = "getMe"
|
||||||
override val resultDeserializer: DeserializationStrategy<User>
|
override val resultDeserializer: DeserializationStrategy<Bot>
|
||||||
get() = User.serializer()
|
get() = Bot.serializer()
|
||||||
override val requestSerializer: SerializationStrategy<*>
|
override val requestSerializer: SerializationStrategy<*>
|
||||||
get() = serializer()
|
get() = serializer()
|
||||||
}
|
}
|
@ -57,6 +57,9 @@ const val isBotField = "is_bot"
|
|||||||
const val firstNameField = "first_name"
|
const val firstNameField = "first_name"
|
||||||
const val lastNameField = "last_name"
|
const val lastNameField = "last_name"
|
||||||
const val languageCodeField = "language_code"
|
const val languageCodeField = "language_code"
|
||||||
|
const val canJoinGroupsField = "can_join_groups"
|
||||||
|
const val canReadAllGroupMessagesField = "can_read_all_group_messages"
|
||||||
|
const val supportInlineQueriesField = "supports_inline_queries"
|
||||||
const val textEntitiesField = "text_entities"
|
const val textEntitiesField = "text_entities"
|
||||||
const val stickerSetNameField = "set_name"
|
const val stickerSetNameField = "set_name"
|
||||||
const val stickerSetNameFullField = "sticker_set_name"
|
const val stickerSetNameFullField = "sticker_set_name"
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types
|
package com.github.insanusmokrassar.TelegramBotAPI.types
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat
|
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.PrivateChat
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.*
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.json.Json
|
||||||
|
import kotlinx.serialization.json.JsonObjectSerializer
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class User(
|
sealed class User : PrivateChat
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class CommonUser(
|
||||||
override val id: ChatId,
|
override val id: ChatId,
|
||||||
@SerialName(isBotField)
|
|
||||||
val isBot: Boolean = false,
|
|
||||||
@SerialName(firstNameField)
|
@SerialName(firstNameField)
|
||||||
override val firstName: String,
|
override val firstName: String,
|
||||||
@SerialName(lastNameField)
|
@SerialName(lastNameField)
|
||||||
@ -17,4 +19,49 @@ data class User(
|
|||||||
override val username: Username? = null,
|
override val username: Username? = null,
|
||||||
@SerialName(languageCodeField)
|
@SerialName(languageCodeField)
|
||||||
val languageCode: String? = null
|
val languageCode: String? = null
|
||||||
) : PrivateChat
|
) : User()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class Bot(
|
||||||
|
override val id: ChatId,
|
||||||
|
@SerialName(firstNameField)
|
||||||
|
override val firstName: String,
|
||||||
|
@SerialName(lastNameField)
|
||||||
|
override val lastName: String = "",
|
||||||
|
@SerialName(usernameField)
|
||||||
|
override val username: Username? = null,
|
||||||
|
@SerialName(canJoinGroupsField)
|
||||||
|
val canJoinGroups: Boolean = false,
|
||||||
|
@SerialName(canReadAllGroupMessagesField)
|
||||||
|
val canReadAllGroupMessages: Boolean = false,
|
||||||
|
@SerialName(supportInlineQueriesField)
|
||||||
|
val supportsInlineQueries: Boolean = false
|
||||||
|
) : User() {
|
||||||
|
@SerialName(isBotField)
|
||||||
|
private val isBot = true
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializer(User::class)
|
||||||
|
internal object UserSerializer : KSerializer<User> {
|
||||||
|
override fun deserialize(decoder: Decoder): User {
|
||||||
|
val asJson = JsonObjectSerializer.deserialize(decoder)
|
||||||
|
|
||||||
|
return when {
|
||||||
|
asJson.getPrimitiveOrNull(isBotField) ?.booleanOrNull != true -> Json.nonstrict.fromJson(
|
||||||
|
CommonUser.serializer(),
|
||||||
|
asJson
|
||||||
|
)
|
||||||
|
else -> Json.nonstrict.fromJson(
|
||||||
|
Bot.serializer(),
|
||||||
|
asJson
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun serialize(encoder: Encoder, obj: User) {
|
||||||
|
when (obj) {
|
||||||
|
is CommonUser -> CommonUser.serializer().serialize(encoder, obj)
|
||||||
|
is Bot -> Bot.serializer().serialize(encoder, obj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user