From 2dcbc0df3220ae6aa1f4fe2a0e36802ee218b778 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 17 Apr 2022 19:56:41 +0600 Subject: [PATCH] add support of ChatAdministratorRights --- .../bot/ClearMyDefaultAdministratorRights.kt | 13 ++++++++ .../bot/GetMyDefaultAdministratorRights.kt | 12 +++++++ .../bot/SetMyDefaultAdministratorRights.kt | 18 +++++++++++ .../api/chat/members/PromoteChatMember.kt | 16 +++++----- .../bot/ClearMyDefaultAdministratorRights.kt | 18 +++++++++++ .../bot/GetMyDefaultAdministratorRights.kt | 23 ++++++++++++++ .../bot/SetMyDefaultAdministratorRights.kt | 20 ++++++++++++ .../chat/members/PromoteChatMember.kt | 4 +-- .../types/ChatAdministratorRightsImpl.kt | 31 +++++++++++++++++++ .../ChatMember/AdministratorChatMemberImpl.kt | 4 +-- .../types/ChatMember/CreatorChatMember.kt | 2 +- .../abstracts/AdministratorChatMember.kt | 13 +++----- .../abstracts/ChatAdministratorRights.kt | 18 +++++++++++ .../abstracts/SpecialRightsChatMember.kt | 6 +--- .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 3 ++ 15 files changed, 174 insertions(+), 27 deletions(-) create mode 100644 tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/ClearMyDefaultAdministratorRights.kt create mode 100644 tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/GetMyDefaultAdministratorRights.kt create mode 100644 tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/SetMyDefaultAdministratorRights.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/ClearMyDefaultAdministratorRights.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/GetMyDefaultAdministratorRights.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/SetMyDefaultAdministratorRights.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatAdministratorRightsImpl.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/ChatAdministratorRights.kt diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/ClearMyDefaultAdministratorRights.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/ClearMyDefaultAdministratorRights.kt new file mode 100644 index 0000000000..098d576213 --- /dev/null +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/ClearMyDefaultAdministratorRights.kt @@ -0,0 +1,13 @@ +package dev.inmo.tgbotapi.extensions.api.bot + +import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.requests.bot.ClearMyDefaultAdministratorRights +import dev.inmo.tgbotapi.types.ChatAdministratorRightsImpl + +suspend fun TelegramBot.clearMyDefaultAdministratorRights( + forChannels: Boolean? = null +) = execute(ClearMyDefaultAdministratorRights(forChannels)) + +suspend fun TelegramBot.clearMyDefaultAdministratorRightsForChannels() = clearMyDefaultAdministratorRights(forChannels = true) + +suspend fun TelegramBot.clearMyDefaultAdministratorRightsForGroupsAndSupergroups() = clearMyDefaultAdministratorRights(forChannels = false) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/GetMyDefaultAdministratorRights.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/GetMyDefaultAdministratorRights.kt new file mode 100644 index 0000000000..74eae1d136 --- /dev/null +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/GetMyDefaultAdministratorRights.kt @@ -0,0 +1,12 @@ +package dev.inmo.tgbotapi.extensions.api.bot + +import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.requests.bot.GetMyDefaultAdministratorRights + +suspend fun TelegramBot.getMyDefaultAdministratorRights( + forChannels: Boolean? = null +) = execute(GetMyDefaultAdministratorRights(forChannels)) + +suspend fun TelegramBot.getMyDefaultAdministratorRightsForChannels() = getMyDefaultAdministratorRights(forChannels = true) + +suspend fun TelegramBot.getMyDefaultAdministratorRightsForGroupsAndSupergroups() = getMyDefaultAdministratorRights(forChannels = false) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/SetMyDefaultAdministratorRights.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/SetMyDefaultAdministratorRights.kt new file mode 100644 index 0000000000..6f14be2a1e --- /dev/null +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/SetMyDefaultAdministratorRights.kt @@ -0,0 +1,18 @@ +package dev.inmo.tgbotapi.extensions.api.bot + +import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.requests.bot.SetMyDefaultAdministratorRights +import dev.inmo.tgbotapi.types.ChatAdministratorRightsImpl + +suspend fun TelegramBot.setMyDefaultAdministratorRights( + rights: ChatAdministratorRightsImpl, + forChannels: Boolean? = null +) = execute(SetMyDefaultAdministratorRights(rights, forChannels)) + +suspend fun TelegramBot.setMyDefaultAdministratorRightsForChannels( + rights: ChatAdministratorRightsImpl +) = setMyDefaultAdministratorRights(rights, forChannels = true) + +suspend fun TelegramBot.setMyDefaultAdministratorRightsForGroupsAndSupergroups( + rights: ChatAdministratorRightsImpl +) = setMyDefaultAdministratorRights(rights, forChannels = false) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/members/PromoteChatMember.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/members/PromoteChatMember.kt index 865fc0ee80..cae8d96c1a 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/members/PromoteChatMember.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/chat/members/PromoteChatMember.kt @@ -18,7 +18,7 @@ suspend fun TelegramBot.promoteChatMember( canRestrictMembers: Boolean? = null, canPinMessages: Boolean? = null, canPromoteMembers: Boolean? = null, - canManageVoiceChats: Boolean? = null, + canManageVideoChats: Boolean? = null, canManageChat: Boolean? ) = execute( PromoteChatMember( @@ -34,7 +34,7 @@ suspend fun TelegramBot.promoteChatMember( canRestrictMembers, canPinMessages, canPromoteMembers, - canManageVoiceChats, + canManageVideoChats, canManageChat ) ) @@ -52,7 +52,7 @@ suspend fun TelegramBot.promoteChatMember( canRestrictMembers: Boolean? = null, canPinMessages: Boolean? = null, canPromoteMembers: Boolean? = null, - canManageVoiceChats: Boolean? = null, + canManageVideoChats: Boolean? = null, canManageChat: Boolean? = null ) = promoteChatMember( chat.id, @@ -67,7 +67,7 @@ suspend fun TelegramBot.promoteChatMember( canRestrictMembers, canPinMessages, canPromoteMembers, - canManageVoiceChats, + canManageVideoChats, canManageChat ) @@ -84,7 +84,7 @@ suspend fun TelegramBot.promoteChatMember( canRestrictMembers: Boolean? = null, canPinMessages: Boolean? = null, canPromoteMembers: Boolean? = null, - canManageVoiceChats: Boolean? = null, + canManageVideoChats: Boolean? = null, canManageChat: Boolean? = null ) = promoteChatMember( chatId, @@ -99,7 +99,7 @@ suspend fun TelegramBot.promoteChatMember( canRestrictMembers, canPinMessages, canPromoteMembers, - canManageVoiceChats, + canManageVideoChats, canManageChat ) @@ -116,7 +116,7 @@ suspend fun TelegramBot.promoteChatMember( canRestrictMembers: Boolean? = null, canPinMessages: Boolean? = null, canPromoteMembers: Boolean? = null, - canManageVoiceChats: Boolean? = null, + canManageVideoChats: Boolean? = null, canManageChat: Boolean? = null ) = promoteChatMember( chat.id, @@ -131,6 +131,6 @@ suspend fun TelegramBot.promoteChatMember( canRestrictMembers, canPinMessages, canPromoteMembers, - canManageVoiceChats, + canManageVideoChats, canManageChat ) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/ClearMyDefaultAdministratorRights.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/ClearMyDefaultAdministratorRights.kt new file mode 100644 index 0000000000..7e60e578ab --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/ClearMyDefaultAdministratorRights.kt @@ -0,0 +1,18 @@ +package dev.inmo.tgbotapi.requests.bot + +import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest +import dev.inmo.tgbotapi.types.* +import kotlinx.serialization.* +import kotlinx.serialization.builtins.serializer + +@Serializable +class ClearMyDefaultAdministratorRights( + @SerialName(forChannelsField) + val forChannels: Boolean? = null +) : SimpleRequest { + override fun method(): String = "setMyDefaultAdministratorRights" + override val resultDeserializer: DeserializationStrategy + get() = Boolean.serializer() + override val requestSerializer: SerializationStrategy<*> + get() = serializer() +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/GetMyDefaultAdministratorRights.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/GetMyDefaultAdministratorRights.kt new file mode 100644 index 0000000000..e59dcc5be1 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/GetMyDefaultAdministratorRights.kt @@ -0,0 +1,23 @@ +package dev.inmo.tgbotapi.requests.bot + +import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest +import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.types.ChatMember.AdministratorChatMemberImpl +import kotlinx.serialization.* + +@Serializable +data class GetMyDefaultAdministratorRights( + @SerialName(forChannelsField) + val forChannels: Boolean? = null +) : SimpleRequest { + override fun method(): String = "getMyDefaultAdministratorRights" + override val resultDeserializer: DeserializationStrategy + get() = AdministratorChatMemberImpl.serializer() + override val requestSerializer: SerializationStrategy<*> + get() = serializer() + + companion object { + val ForChannels = GetMyDefaultAdministratorRights(true) + val ForGroups = GetMyDefaultAdministratorRights(false) + } +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/SetMyDefaultAdministratorRights.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/SetMyDefaultAdministratorRights.kt new file mode 100644 index 0000000000..cc30b7070a --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/SetMyDefaultAdministratorRights.kt @@ -0,0 +1,20 @@ +package dev.inmo.tgbotapi.requests.bot + +import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest +import dev.inmo.tgbotapi.types.* +import kotlinx.serialization.* +import kotlinx.serialization.builtins.serializer + +@Serializable +class SetMyDefaultAdministratorRights( + @SerialName(rightsField) + val rights: ChatAdministratorRightsImpl, + @SerialName(forChannelsField) + val forChannels: Boolean? = null +) : SimpleRequest { + override fun method(): String = "setMyDefaultAdministratorRights" + override val resultDeserializer: DeserializationStrategy + get() = Boolean.serializer() + override val requestSerializer: SerializationStrategy<*> + get() = serializer() +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/members/PromoteChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/members/PromoteChatMember.kt index 91e18f7381..4079eec591 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/members/PromoteChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/members/PromoteChatMember.kt @@ -32,8 +32,8 @@ data class PromoteChatMember( private val canPinMessages: Boolean? = null, @SerialName(canPromoteMembersField) private val canPromoteMembers: Boolean? = null, - @SerialName(canManageVoiceChatsField) - private val canManageVoiceChats: Boolean? = null, + @SerialName(canManageVideoChatsField) + private val canManageVideoChats: Boolean? = null, @SerialName(canManageChatField) private val canManageChat: Boolean? = null ) : ChatMemberRequest, UntilDate { diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatAdministratorRightsImpl.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatAdministratorRightsImpl.kt new file mode 100644 index 0000000000..b7398fa49d --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatAdministratorRightsImpl.kt @@ -0,0 +1,31 @@ +package dev.inmo.tgbotapi.types + +import dev.inmo.tgbotapi.types.ChatMember.abstracts.ChatAdministratorRights +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class ChatAdministratorRightsImpl( + @SerialName(canChangeInfoField) + override val canChangeInfo: Boolean = false, + @SerialName(canPostMessagesField) + override val canPostMessages: Boolean = false, + @SerialName(canEditMessagesField) + override val canEditMessages: Boolean = false, + @SerialName(canDeleteMessagesField) + override val canRemoveMessages: Boolean = false, + @SerialName(canInviteUsersField) + override val canInviteUsers: Boolean = false, + @SerialName(canRestrictMembersField) + override val canRestrictMembers: Boolean = false, + @SerialName(canPinMessagesField) + override val canPinMessages: Boolean = false, + @SerialName(canPromoteMembersField) + override val canPromoteMembers: Boolean = false, + @SerialName(canManageVideoChatsField) + override val canManageVideoChats: Boolean = false, + @SerialName(canManageChatField) + override val canManageChat: Boolean = false, + @SerialName(isAnonymousField) + override val isAnonymous: Boolean = false +) : ChatAdministratorRights diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/AdministratorChatMemberImpl.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/AdministratorChatMemberImpl.kt index b3bfd4cd18..b79d44145d 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/AdministratorChatMemberImpl.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/AdministratorChatMemberImpl.kt @@ -26,8 +26,8 @@ data class AdministratorChatMemberImpl( override val canPinMessages: Boolean = false, @SerialName(canPromoteMembersField) override val canPromoteMembers: Boolean = false, - @SerialName(canManageVoiceChatsField) - override val canManageVoiceChats: Boolean = false, + @SerialName(canManageVideoChatsField) + override val canManageVideoChats: Boolean = false, @SerialName(canManageChatField) override val canManageChat: Boolean = false, @SerialName(isAnonymousField) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/CreatorChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/CreatorChatMember.kt index 8344c7bb22..36da996d9a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/CreatorChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/CreatorChatMember.kt @@ -31,7 +31,7 @@ data class CreatorChatMember( @Transient override val canPromoteMembers: Boolean = true @Transient - override val canManageVoiceChats: Boolean = true + override val canManageVideoChats: Boolean = true @Transient override val canManageChat: Boolean = true @SerialName(statusField) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/AdministratorChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/AdministratorChatMember.kt index 3664311d96..9d48b9f9ad 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/AdministratorChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/AdministratorChatMember.kt @@ -8,17 +8,12 @@ import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder @Serializable(AdministratorChatMemberSerializer::class) -interface AdministratorChatMember : SpecialRightsChatMember { +interface AdministratorChatMember : SpecialRightsChatMember, ChatAdministratorRights { val canBeEdited: Boolean - val canPostMessages: Boolean - val canEditMessages: Boolean - val canRemoveMessages: Boolean - val canRestrictMembers: Boolean - val canPromoteMembers: Boolean - val canManageVoiceChats: Boolean - val canManageChat: Boolean - val isAnonymous: Boolean val customTitle: String? + + val canManageVoiceChats: Boolean + get() = canManageVideoChats } @RiskFeature diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/ChatAdministratorRights.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/ChatAdministratorRights.kt new file mode 100644 index 0000000000..61a3889778 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/ChatAdministratorRights.kt @@ -0,0 +1,18 @@ +package dev.inmo.tgbotapi.types.ChatMember.abstracts + +sealed interface SpecialChatAdministratorRights { + val canChangeInfo: Boolean + val canInviteUsers: Boolean + val canPinMessages: Boolean +} + +interface ChatAdministratorRights : SpecialChatAdministratorRights { + val isAnonymous: Boolean + val canManageChat: Boolean + val canRemoveMessages: Boolean + val canManageVideoChats: Boolean + val canRestrictMembers: Boolean + val canPromoteMembers: Boolean + val canPostMessages: Boolean + val canEditMessages: Boolean +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/SpecialRightsChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/SpecialRightsChatMember.kt index 796c6fa69b..79cea14abb 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/SpecialRightsChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatMember/abstracts/SpecialRightsChatMember.kt @@ -3,8 +3,4 @@ package dev.inmo.tgbotapi.types.ChatMember.abstracts import kotlinx.serialization.Serializable @Serializable(ChatMemberSerializer::class) -interface SpecialRightsChatMember : ChatMember { - val canChangeInfo: Boolean - val canInviteUsers: Boolean - val canPinMessages: Boolean -} +interface SpecialRightsChatMember : ChatMember, SpecialChatAdministratorRights 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 57ff302e1a..b097251344 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 @@ -270,6 +270,9 @@ const val canRestrictMembersField = "can_restrict_members" const val canPinMessagesField = "can_pin_messages" const val canPromoteMembersField = "can_promote_members" const val canManageVoiceChatsField = "can_manage_voice_chats" +const val canManageVideoChatsField = "can_manage_video_chats" +const val rightsField = "rights" +const val forChannelsField = "for_channels" const val canManageChatField = "can_manage_chat" const val pngStickerField = "png_sticker" const val tgsStickerField = "tgs_sticker"