1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-26 11:38:09 +00:00

UnknownUpdate -> UnknownUpdateType, unknown types for messages and chats

This commit is contained in:
InsanusMokrassar 2020-01-23 03:47:43 +06:00
parent a8ca45a4bd
commit 83e5d40443
7 changed files with 112 additions and 82 deletions

View File

@ -80,6 +80,10 @@ while they can work incorrectly
### 0.22.2 CashTag and independent updates handling
* `cashtag` entity type was added
* Several `Unknown*` realizations was added:
* `UnknownUpdateType`
* `UnknownMessageType`
* `UnknownChatType`
* New type of updates was added: `UnknownUpdate`. It will be used in cases when type of update is unknown in system
## 0.21.0 TelegramBotAPI 4.5

View File

@ -1,10 +1,12 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.chat
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.UnknownChatType
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.extended.ExtendedChat
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.extended.*
import com.github.insanusmokrassar.TelegramBotAPI.types.typeField
import kotlinx.serialization.*
import kotlinx.serialization.internal.LongSerializer
import kotlinx.serialization.internal.StringDescriptor
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObjectSerializer
@ -24,7 +26,10 @@ internal object PreviewChatSerializer : KSerializer<Chat> {
"group" -> formatter.fromJson(GroupChatImpl.serializer(), decodedJson)
"supergroup" -> formatter.fromJson(SupergroupChatImpl.serializer(), decodedJson)
"channel" -> formatter.fromJson(ChannelChatImpl.serializer(), decodedJson)
else -> throw IllegalArgumentException("Unknown type of chat")
else -> UnknownChatType(
formatter.fromJson(LongSerializer, decodedJson.getPrimitive(chatIdField)).toChatId(),
decodedJson.toString()
)
}
}

View File

@ -8,3 +8,8 @@ import kotlinx.serialization.Serializable
interface Chat {
val id: ChatId
}
data class UnknownChatType(
override val id: ChatId,
val raw: String
) : Chat

View File

@ -10,6 +10,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.games.RawGame
import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.*
import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.UnknownMessageType
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.*
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.*
@ -184,8 +185,8 @@ internal data class RawMessage(
val asMessage: Message by lazy {
chatEvent ?.let {
chatEvent ->
try {
chatEvent?.let { chatEvent ->
when (chat) {
is SupergroupChat -> SupergroupEventMessage(
messageId,
@ -207,8 +208,7 @@ internal data class RawMessage(
)
else -> throw IllegalStateException("Expected one of the public chats, but was $chat (in extracting of chat event message)")
}
} ?: content ?.let {
content ->
} ?: content?.let { content ->
media_group_id?.let {
when (from) {
null -> ChannelMediaGroupMessage(
@ -257,7 +257,8 @@ internal data class RawMessage(
)
else -> CommonMessageImpl(
messageId,
from ?: throw IllegalStateException("Was detected common message, but owner (sender) of the message was not found"),
from
?: throw IllegalStateException("Was detected common message, but owner (sender) of the message was not found"),
chat,
content,
date.asDate,
@ -269,6 +270,14 @@ internal data class RawMessage(
)
}
} ?: throw IllegalStateException("Was not found supported type of data")
} catch (e: Exception) {
UnknownMessageType(
messageId,
chat,
date.asDate,
e
)
}
}
private fun throwWrongChatEvent(expected: KClass<*>, but: ChatEvent): CommonEvent {

View File

@ -13,6 +13,13 @@ interface Message {
val date: DateTime
}
data class UnknownMessageType(
override val messageId: MessageIdentifier,
override val chat: Chat,
override val date: DateTime,
val insideException: Exception
) : Message
internal class TelegramBotAPIMessageDeserializationStrategyClass<T> : DeserializationStrategy<T> {
override val descriptor: SerialDescriptor = StringDescriptor.withName("TelegramBotAPIMessageSerializer")

View File

@ -9,7 +9,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Telegr
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.PreCheckoutQuery
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.ShippingQuery
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.Poll
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdateType
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.types.updateIdField
import kotlinx.serialization.SerialName
@ -51,7 +51,7 @@ internal data class RawUpdate constructor(
shipping_query != null -> ShippingQueryUpdate(updateId, shipping_query)
pre_checkout_query != null -> PreCheckoutQueryUpdate(updateId, pre_checkout_query)
poll != null -> PollUpdate(updateId, poll)
else -> UnknownUpdate(
else -> UnknownUpdateType(
updateId,
raw
)

View File

@ -12,7 +12,7 @@ interface Update {
val data: Any
}
data class UnknownUpdate(
data class UnknownUpdateType(
override val updateId: UpdateIdentifier,
override val data: String
) : Update