From c027244fbd413a2bbc5d4ada0a43f244610b97f1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 11 Mar 2021 20:17:08 +0600 Subject: [PATCH] include ChatMemberUpdated functionality --- .../dev/inmo/tgbotapi/types/ChatInviteLink.kt | 16 +++++--------- .../inmo/tgbotapi/types/ChatMemberUpdated.kt | 22 +++++++++++++++++++ .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 2 ++ .../dev/inmo/tgbotapi/types/UpdateTypes.kt | 6 ++++- .../update/CommonChatMemberUpdatedUpdate.kt | 10 +++++++++ .../types/update/MyChatMemberUpdatedUpdate.kt | 10 +++++++++ .../inmo/tgbotapi/types/update/RawUpdate.kt | 9 +++++--- .../abstracts/ChatMemberUpdatedUpdate.kt | 7 ++++++ 8 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMemberUpdated.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/CommonChatMemberUpdatedUpdate.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/MyChatMemberUpdatedUpdate.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/abstracts/ChatMemberUpdatedUpdate.kt diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt index 46a46759d8..fe6a0d094c 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt @@ -17,7 +17,7 @@ private data class RawChatInviteLink( @SerialName(isRevokedField) val isRevoked: Boolean, @SerialName(expireDateField) - val expirationDateTime: UnixTimeStamp? = null, + val expirationDateTime: TelegramDate? = null, @SerialName(memberLimitField) val membersLimit: MembersLimit ?= null ) @@ -27,7 +27,7 @@ private fun ChatInviteLink.toRawChatInviteLink() = RawChatInviteLink( creator, isPrimary, isRevoked, - expirationDateTime ?.unixMillis ?.milliseconds ?.seconds ?.toLong(), + expirationDateTime ?.toTelegramDate(), membersLimit ) @@ -50,16 +50,14 @@ data class PrimaryInviteLink( @SerialName(isRevokedField) override val isRevoked: Boolean = false, @SerialName(expireDateField) - private val expireDate: UnixTimeStamp? = null, + private val expireDate: TelegramDate? = null, @SerialName(memberLimitField) override val membersLimit: MembersLimit? = null ) : ChatInviteLink() { override val isPrimary: Boolean get() = true override val expirationDateTime: DateTime? - get() { - return DateTime(expireDate ?.seconds ?.milliseconds ?: return null) - } + get() = expireDate ?.asDate } @Serializable @@ -71,16 +69,14 @@ data class CommonInviteLink( @SerialName(isRevokedField) override val isRevoked: Boolean = false, @SerialName(expireDateField) - private val expireDate: UnixTimeStamp? = null, + private val expireDate: TelegramDate? = null, @SerialName(memberLimitField) override val membersLimit: MembersLimit? = null ) : ChatInviteLink() { override val isPrimary: Boolean get() = false override val expirationDateTime: DateTime? - get() { - return DateTime(expireDate ?.seconds ?.milliseconds ?: return null) - } + get() = expireDate ?.asDate } @Serializer(ChatInviteLink::class) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMemberUpdated.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMemberUpdated.kt new file mode 100644 index 0000000000..856e113ac0 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMemberUpdated.kt @@ -0,0 +1,22 @@ +package dev.inmo.tgbotapi.types + +import dev.inmo.tgbotapi.types.ChatMember.abstracts.ChatMember +import dev.inmo.tgbotapi.types.chat.abstracts.Chat +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class ChatMemberUpdated( + @SerialName(chatField) + val chat: Chat, + @SerialName(fromField) + val user: User, + @SerialName(dateField) + val date: TelegramDate, + @SerialName(oldChatMemberField) + val oldChatMemberState: ChatMember, + @SerialName(newChatMemberField) + val newChatMemberState: ChatMember, + @SerialName(inviteLinkField) + val inviteLink: ChatInviteLink? = null +) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index 74d2f356b0..8fc47f2a24 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -253,6 +253,8 @@ const val canManageVoiceChatsField = "can_manage_voice_chats" const val canManageChatField = "can_manage_chat" const val pngStickerField = "png_sticker" const val tgsStickerField = "tgs_sticker" +const val oldChatMemberField = "old_chat_member" +const val newChatMemberField = "new_chat_member" const val okField = "ok" const val captionField = "caption" diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/UpdateTypes.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/UpdateTypes.kt index 0c2be08534..1606ea6ed4 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/UpdateTypes.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/UpdateTypes.kt @@ -11,6 +11,8 @@ const val UPDATE_SHIPPING_QUERY = "shipping_query" const val UPDATE_PRE_CHECKOUT_QUERY = "pre_checkout_query" const val UPDATE_POLL = "poll" const val UPDATE_POLL_ANSWER = "poll_answer" +const val MY_CHAT_MEMBER = "my_chat_member" +const val CHAT_MEMBER = "chat_member" val ALL_UPDATES_LIST = listOf( UPDATE_MESSAGE, @@ -23,5 +25,7 @@ val ALL_UPDATES_LIST = listOf( UPDATE_SHIPPING_QUERY, UPDATE_PRE_CHECKOUT_QUERY, UPDATE_POLL, - UPDATE_POLL_ANSWER + UPDATE_POLL_ANSWER, + MY_CHAT_MEMBER, + CHAT_MEMBER ) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/CommonChatMemberUpdatedUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/CommonChatMemberUpdatedUpdate.kt new file mode 100644 index 0000000000..4811ebaa74 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/CommonChatMemberUpdatedUpdate.kt @@ -0,0 +1,10 @@ +package dev.inmo.tgbotapi.types.update + +import dev.inmo.tgbotapi.types.ChatMemberUpdated +import dev.inmo.tgbotapi.types.UpdateIdentifier +import dev.inmo.tgbotapi.types.update.abstracts.ChatMemberUpdatedUpdate + +data class CommonChatMemberUpdatedUpdate( + override val updateId: UpdateIdentifier, + override val data: ChatMemberUpdated +) : ChatMemberUpdatedUpdate diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/MyChatMemberUpdatedUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/MyChatMemberUpdatedUpdate.kt new file mode 100644 index 0000000000..3dabbbb017 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/MyChatMemberUpdatedUpdate.kt @@ -0,0 +1,10 @@ +package dev.inmo.tgbotapi.types.update + +import dev.inmo.tgbotapi.types.ChatMemberUpdated +import dev.inmo.tgbotapi.types.UpdateIdentifier +import dev.inmo.tgbotapi.types.update.abstracts.ChatMemberUpdatedUpdate + +data class MyChatMemberUpdatedUpdate( + override val updateId: UpdateIdentifier, + override val data: ChatMemberUpdated +) : ChatMemberUpdatedUpdate diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/RawUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/RawUpdate.kt index 60080dac74..87e46b45d3 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/RawUpdate.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/RawUpdate.kt @@ -1,9 +1,9 @@ package dev.inmo.tgbotapi.types.update +import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.CallbackQuery.RawCallbackQuery import dev.inmo.tgbotapi.types.InlineQueries.ChosenInlineResult.RawChosenInlineResult import dev.inmo.tgbotapi.types.InlineQueries.query.RawInlineQuery -import dev.inmo.tgbotapi.types.UpdateIdentifier import dev.inmo.tgbotapi.types.message.abstracts.* import dev.inmo.tgbotapi.types.payments.PreCheckoutQuery import dev.inmo.tgbotapi.types.payments.ShippingQuery @@ -11,7 +11,6 @@ import dev.inmo.tgbotapi.types.polls.Poll import dev.inmo.tgbotapi.types.polls.PollAnswer import dev.inmo.tgbotapi.types.update.abstracts.UnknownUpdate import dev.inmo.tgbotapi.types.update.abstracts.Update -import dev.inmo.tgbotapi.types.updateIdField import kotlinx.serialization.* import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.jsonObject @@ -34,7 +33,9 @@ internal data class RawUpdate constructor( private val shipping_query: ShippingQuery? = null, private val pre_checkout_query: PreCheckoutQuery? = null, private val poll: Poll? = null, - private val poll_answer: PollAnswer? = null + private val poll_answer: PollAnswer? = null, + private val my_chat_member: ChatMemberUpdated? = null, + private val chat_member: ChatMemberUpdated? = null ) { private var initedUpdate: Update? = null /** @@ -58,6 +59,8 @@ internal data class RawUpdate constructor( pre_checkout_query != null -> PreCheckoutQueryUpdate(updateId, pre_checkout_query) poll != null -> PollUpdate(updateId, poll) poll_answer != null -> PollAnswerUpdate(updateId, poll_answer) + my_chat_member != null -> MyChatMemberUpdatedUpdate(updateId, my_chat_member) + chat_member != null -> CommonChatMemberUpdatedUpdate(updateId, chat_member) else -> UnknownUpdate( updateId, raw.toString(), diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/abstracts/ChatMemberUpdatedUpdate.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/abstracts/ChatMemberUpdatedUpdate.kt new file mode 100644 index 0000000000..44c11da4d3 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/update/abstracts/ChatMemberUpdatedUpdate.kt @@ -0,0 +1,7 @@ +package dev.inmo.tgbotapi.types.update.abstracts + +import dev.inmo.tgbotapi.types.ChatMemberUpdated + +interface ChatMemberUpdatedUpdate : Update { + override val data: ChatMemberUpdated +}