diff --git a/CHANGELOG.md b/CHANGELOG.md index ab060d7134..2c9731657c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 9.2.0 +* All the request chat keyboards has changed their parameters `ChatAdministratorRights` to `ChatAdministratorRightsImpl` + ## 9.1.2 * `Core`: diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/buttons/KeyboardButtonRequestChat.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/buttons/KeyboardButtonRequestChat.kt index fccb2c64ce..8aefd7c333 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/buttons/KeyboardButtonRequestChat.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/buttons/KeyboardButtonRequestChat.kt @@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.types.buttons import dev.inmo.tgbotapi.types.botAdministratorRightsField import dev.inmo.tgbotapi.types.botIsMemberField import dev.inmo.tgbotapi.types.chat.member.ChatAdministratorRights +import dev.inmo.tgbotapi.types.chat.member.ChatAdministratorRightsImpl import dev.inmo.tgbotapi.types.chatHasUsernameField import dev.inmo.tgbotapi.types.chatIsChannelField import dev.inmo.tgbotapi.types.chatIsCreatedField @@ -30,9 +31,9 @@ data class KeyboardButtonRequestChat( @SerialName(chatIsCreatedField) val isOwnedBy: Boolean? = null, @SerialName(userAdministratorRightsField) - val userRightsInChat: ChatAdministratorRights? = null, + val userRightsInChat: ChatAdministratorRightsImpl? = null, @SerialName(botAdministratorRightsField) - val botRightsInChat: ChatAdministratorRights? = null, + val botRightsInChat: ChatAdministratorRightsImpl? = null, @SerialName(botIsMemberField) val botIsMember: Boolean? = null ) { @@ -41,8 +42,8 @@ data class KeyboardButtonRequestChat( requestId: RequestId, isPublic: Boolean? = null, isOwnedBy: Boolean? = null, - userRightsInChat: ChatAdministratorRights? = null, - botRightsInChat: ChatAdministratorRights? = null, + userRightsInChat: ChatAdministratorRightsImpl? = null, + botRightsInChat: ChatAdministratorRightsImpl? = null, botIsMember: Boolean? = null ) = KeyboardButtonRequestChat( requestId = requestId, @@ -60,8 +61,8 @@ data class KeyboardButtonRequestChat( isForum: Boolean? = null, isPublic: Boolean? = null, isOwnedBy: Boolean? = null, - userRightsInChat: ChatAdministratorRights? = null, - botRightsInChat: ChatAdministratorRights? = null, + userRightsInChat: ChatAdministratorRightsImpl? = null, + botRightsInChat: ChatAdministratorRightsImpl? = null, botIsMember: Boolean? = null ) = KeyboardButtonRequestChat( requestId = requestId, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/buttons/reply/ReplyKeyboardButtonsShortcuts.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/buttons/reply/ReplyKeyboardButtonsShortcuts.kt index 98090b301c..9ec9c0774b 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/buttons/reply/ReplyKeyboardButtonsShortcuts.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/buttons/reply/ReplyKeyboardButtonsShortcuts.kt @@ -2,7 +2,7 @@ package dev.inmo.tgbotapi.types.buttons.reply import dev.inmo.tgbotapi.types.buttons.* import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.* -import dev.inmo.tgbotapi.types.chat.member.ChatAdministratorRights +import dev.inmo.tgbotapi.types.chat.member.ChatAdministratorRightsImpl import dev.inmo.tgbotapi.types.request.RequestId import dev.inmo.tgbotapi.types.webapps.WebAppInfo @@ -120,8 +120,8 @@ inline fun requestChatReplyButton( isForum: Boolean? = null, isPublic: Boolean? = null, isOwnedBy: Boolean? = null, - userRightsInChat: ChatAdministratorRights? = null, - botRightsInChat: ChatAdministratorRights? = null, + userRightsInChat: ChatAdministratorRightsImpl? = null, + botRightsInChat: ChatAdministratorRightsImpl? = null, botIsMember: Boolean = false ) = requestChatReplyButton( text, @@ -145,8 +145,8 @@ inline fun requestChannelReplyButton( requestId: RequestId, isPublic: Boolean? = null, isOwnedBy: Boolean? = null, - userRightsInChat: ChatAdministratorRights? = null, - botRightsInChat: ChatAdministratorRights? = null, + userRightsInChat: ChatAdministratorRightsImpl? = null, + botRightsInChat: ChatAdministratorRightsImpl? = null, botIsMember: Boolean = false ) = requestChatReplyButton( text, @@ -170,8 +170,8 @@ inline fun requestChannelReplyButton( isForum: Boolean? = null, isPublic: Boolean? = null, isOwnedBy: Boolean? = null, - userRightsInChat: ChatAdministratorRights? = null, - botRightsInChat: ChatAdministratorRights? = null, + userRightsInChat: ChatAdministratorRightsImpl? = null, + botRightsInChat: ChatAdministratorRightsImpl? = null, botIsMember: Boolean? = null ) = requestChatReplyButton( text, diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/AdministratorChatMemberImpl.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/AdministratorChatMemberImpl.kt index 587f64e815..523ac72d19 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/AdministratorChatMemberImpl.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/AdministratorChatMemberImpl.kt @@ -45,5 +45,6 @@ data class AdministratorChatMemberImpl( ) : AdministratorChatMember { @SerialName(statusField) @Required - private val type: String = "administrator" + override val status: ChatMember.Status + get() = ChatMember.Status.Administrator } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatAdministratorRights.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatAdministratorRights.kt index 1436523014..9700737298 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatAdministratorRights.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatAdministratorRights.kt @@ -1,5 +1,8 @@ package dev.inmo.tgbotapi.types.chat.member +import dev.inmo.tgbotapi.types.* +import kotlinx.serialization.SerialName + sealed interface SpecialChatAdministratorRights { val canChangeInfo: Boolean val canInviteUsers: Boolean @@ -19,4 +22,40 @@ sealed interface ChatAdministratorRights : SpecialChatAdministratorRights { val canPostStories: Boolean val canEditStories: Boolean val canDeleteStories: Boolean + + companion object { + operator fun invoke( + canChangeInfo: Boolean = false, + canPostMessages: Boolean = false, + canEditMessages: Boolean = false, + canRemoveMessages: Boolean = false, + canInviteUsers: Boolean = false, + canRestrictMembers: Boolean = false, + canPinMessages: Boolean = false, + canPromoteMembers: Boolean = false, + canManageVideoChats: Boolean = false, + canManageChat: Boolean = false, + isAnonymous: Boolean = false, + canManageTopics: Boolean = false, + canPostStories: Boolean = false, + canEditStories: Boolean = false, + canDeleteStories: Boolean = false + ) = ChatAdministratorRightsImpl( + canChangeInfo = canChangeInfo, + canPostMessages = canPostMessages, + canEditMessages = canEditMessages, + canRemoveMessages = canRemoveMessages, + canInviteUsers = canInviteUsers, + canRestrictMembers = canRestrictMembers, + canPinMessages = canPinMessages, + canPromoteMembers = canPromoteMembers, + canManageVideoChats = canManageVideoChats, + canManageChat = canManageChat, + isAnonymous = isAnonymous, + canManageTopics = canManageTopics, + canPostStories = canPostStories, + canEditStories = canEditStories, + canDeleteStories = canDeleteStories + ) + } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatMember.kt index 112e078521..e8de6e9608 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/ChatMember.kt @@ -13,7 +13,18 @@ import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonPrimitive @Serializable(ChatMemberSerializer::class) -sealed interface ChatMember : WithUser +sealed interface ChatMember : WithUser { + @Serializable + enum class Status(val status: String) { + Creator("creator"), + Administrator("administrator"), + Member("member"), + Restricted("restricted"), + Left("left"), + Kicked("kicked") + } + val status: Status +} @RiskFeature object ChatMemberSerializer : KSerializer { @@ -21,14 +32,16 @@ object ChatMemberSerializer : KSerializer { override fun deserialize(decoder: Decoder): ChatMember { val json = JsonObject.serializer().deserialize(decoder) - return when (json[statusField] ?.jsonPrimitive ?.content ?: error("Status field of chat member must be specified, but incoming json contains next: $json")) { - "creator" -> nonstrictJsonFormat.decodeFromJsonElement(OwnerChatMember.serializer(), json) - "administrator" -> nonstrictJsonFormat.decodeFromJsonElement(AdministratorChatMemberImpl.serializer(), json) - "member" -> nonstrictJsonFormat.decodeFromJsonElement(MemberChatMemberImpl.serializer(), json) - "restricted" -> nonstrictJsonFormat.decodeFromJsonElement(RestrictedChatMember.serializer(), json) - "left" -> nonstrictJsonFormat.decodeFromJsonElement(LeftChatMemberImpl.serializer(), json) - "kicked" -> nonstrictJsonFormat.decodeFromJsonElement(KickedChatMember.serializer(), json) - else -> error("Unknown type of chat member in json: $json") + val status = ChatMember.Status.valueOf( + json[statusField] ?.jsonPrimitive ?.content ?: error("Status field of chat member must be specified, but incoming json contains next: $json") + ) + return when (status) { + ChatMember.Status.Creator -> nonstrictJsonFormat.decodeFromJsonElement(OwnerChatMember.serializer(), json) + ChatMember.Status.Administrator -> nonstrictJsonFormat.decodeFromJsonElement(AdministratorChatMemberImpl.serializer(), json) + ChatMember.Status.Member -> nonstrictJsonFormat.decodeFromJsonElement(MemberChatMemberImpl.serializer(), json) + ChatMember.Status.Restricted -> nonstrictJsonFormat.decodeFromJsonElement(RestrictedChatMember.serializer(), json) + ChatMember.Status.Left -> nonstrictJsonFormat.decodeFromJsonElement(LeftChatMemberImpl.serializer(), json) + ChatMember.Status.Kicked -> nonstrictJsonFormat.decodeFromJsonElement(KickedChatMember.serializer(), json) } } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/KickedChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/KickedChatMember.kt index 96c0778236..b54d906ed3 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/KickedChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/KickedChatMember.kt @@ -13,5 +13,6 @@ data class KickedChatMember( ) : BannedChatMember { @SerialName(statusField) @Required - private val type: String = "kicked" + override val status: ChatMember.Status + get() = ChatMember.Status.Kicked } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/LeftChatMemberImpl.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/LeftChatMemberImpl.kt index 617d2f2531..5ba2171d32 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/LeftChatMemberImpl.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/LeftChatMemberImpl.kt @@ -8,5 +8,6 @@ import kotlinx.serialization.* data class LeftChatMemberImpl(@SerialName(userField) override val user: User) : LeftChatMember { @SerialName(statusField) @Required - private val type: String = "left" + override val status: ChatMember.Status + get() = ChatMember.Status.Left } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/MemberChatMemberImpl.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/MemberChatMemberImpl.kt index 489bbb9b92..8233cc2800 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/MemberChatMemberImpl.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/MemberChatMemberImpl.kt @@ -8,5 +8,6 @@ import kotlinx.serialization.* data class MemberChatMemberImpl(@SerialName(userField) override val user: User) : MemberChatMember { @SerialName(statusField) @Required - private val type: String = "member" + override val status: ChatMember.Status + get() = ChatMember.Status.Member } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/OwnerChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/OwnerChatMember.kt index 446af8be73..1a825d2db3 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/OwnerChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/OwnerChatMember.kt @@ -42,7 +42,9 @@ data class OwnerChatMember( override val canEditStories: Boolean = true @Transient override val canDeleteStories: Boolean = true + @SerialName(statusField) @Required - private val type: String = "creator" + override val status: ChatMember.Status + get() = ChatMember.Status.Creator } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/RestrictedChatMember.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/RestrictedChatMember.kt index 5fb0aa985f..2529b77eff 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/RestrictedChatMember.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/chat/member/RestrictedChatMember.kt @@ -44,5 +44,6 @@ data class RestrictedChatMember( ) : BannedChatMember, SpecialRightsChatMember, ChatPermissions { @SerialName(statusField) @Required - private val type: String = "restricted" + override val status: ChatMember.Status + get() = ChatMember.Status.Restricted } diff --git a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/types/buttons/ReplyKeyboardBuilder.kt b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/types/buttons/ReplyKeyboardBuilder.kt index 3720c54e82..e250373b2f 100644 --- a/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/types/buttons/ReplyKeyboardBuilder.kt +++ b/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/types/buttons/ReplyKeyboardBuilder.kt @@ -3,7 +3,7 @@ package dev.inmo.tgbotapi.extensions.utils.types.buttons import dev.inmo.tgbotapi.types.buttons.* import dev.inmo.tgbotapi.types.buttons.reply.requestChatReplyButton import dev.inmo.tgbotapi.types.buttons.reply.requestUserReplyButton -import dev.inmo.tgbotapi.types.chat.member.ChatAdministratorRights +import dev.inmo.tgbotapi.types.chat.member.ChatAdministratorRightsImpl import dev.inmo.tgbotapi.types.request.RequestId import dev.inmo.tgbotapi.types.webapps.WebAppInfo import dev.inmo.tgbotapi.utils.* @@ -220,8 +220,8 @@ inline fun ReplyKeyboardRowBuilder.requestChatButton( isForum: Boolean? = null, isPublic: Boolean? = null, isOwnedBy: Boolean? = null, - userRightsInChat: ChatAdministratorRights? = null, - botRightsInChat: ChatAdministratorRights? = null, + userRightsInChat: ChatAdministratorRightsImpl? = null, + botRightsInChat: ChatAdministratorRightsImpl? = null, botIsMember: Boolean? = null ) = requestChatButton( text, @@ -248,8 +248,8 @@ inline fun ReplyKeyboardRowBuilder.requestChannelButton( requestId: RequestId, isPublic: Boolean? = null, isOwnedBy: Boolean? = null, - userRightsInChat: ChatAdministratorRights? = null, - botRightsInChat: ChatAdministratorRights? = null, + userRightsInChat: ChatAdministratorRightsImpl? = null, + botRightsInChat: ChatAdministratorRightsImpl? = null, botIsMember: Boolean? = null ) = requestChatButton( text, @@ -275,8 +275,8 @@ inline fun ReplyKeyboardRowBuilder.requestGroupButton( isForum: Boolean? = null, isPublic: Boolean? = null, isOwnedBy: Boolean? = null, - userRightsInChat: ChatAdministratorRights? = null, - botRightsInChat: ChatAdministratorRights? = null, + userRightsInChat: ChatAdministratorRightsImpl? = null, + botRightsInChat: ChatAdministratorRightsImpl? = null, botIsMember: Boolean? = null ) = requestChatButton( text,