1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 16:47:13 +00:00

ChatPermissions now is interface

This commit is contained in:
InsanusMokrassar 2023-02-06 10:24:55 +06:00
parent 554d47e301
commit 81fdf50217
3 changed files with 136 additions and 41 deletions

View File

@ -5,36 +5,32 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
@Serializable
data class ChatPermissions(
@SerialName(canSendMessagesField)
val canSendMessages: Boolean? = null,
@SerialName(canSendAudiosField)
val canSendAudios: Boolean? = null,
@SerialName(canSendDocumentsField)
val canSendDocuments: Boolean? = null,
@SerialName(canSendPhotosField)
val canSendPhotos: Boolean? = null,
@SerialName(canSendVideosField)
val canSendVideos: Boolean? = null,
@SerialName(canSendVideoNotesField)
val canSendVideoNotes: Boolean? = null,
@SerialName(canSendVoiceNotesField)
val canSendVoiceNotes: Boolean? = null,
@SerialName(canSendPollsField)
val canSendPolls: Boolean? = null,
@SerialName(canSendOtherMessagesField)
val canSendOtherMessages: Boolean? = null,
@SerialName(canAddWebPagePreviewsField)
val canAddWebPagePreviews: Boolean? = null,
@SerialName(canChangeInfoField)
val canChangeInfo: Boolean? = null,
@SerialName(canInviteUsersField)
val canInviteUsers: Boolean? = null,
@SerialName(canPinMessagesField)
val canPinMessages: Boolean? = null
) {
/**
* Represents any type with common permissions list
*
* !!WARNING!! Default serializer of this interface is using [ChatPermissionsImpl] as surrogate and in fact serialized
* and deserialized as [ChatPermissionsImpl]. In case you wish some custom behaviour you must implement your own
* [KSerializer] or pass another serializer
*/
@Serializable(ChatPermissions.Companion::class)
interface ChatPermissions {
val canSendMessages: Boolean?
val canSendAudios: Boolean?
val canSendDocuments: Boolean?
val canSendPhotos: Boolean?
val canSendVideos: Boolean?
val canSendVideoNotes: Boolean?
val canSendVoiceNotes: Boolean?
val canSendPolls: Boolean?
val canSendOtherMessages: Boolean?
val canAddWebPagePreviews: Boolean?
val canChangeInfo: Boolean?
val canInviteUsers: Boolean?
val canPinMessages: Boolean?
@Transient
val isGranular
get() = canSendAudios != null ||
@ -44,7 +40,37 @@ data class ChatPermissions(
canSendVideos != null ||
canSendVoiceNotes != null
companion object {
companion object : KSerializer<ChatPermissions> {
operator fun invoke(
canSendMessages: Boolean? = null,
canSendAudios: Boolean? = null,
canSendDocuments: Boolean? = null,
canSendPhotos: Boolean? = null,
canSendVideos: Boolean? = null,
canSendVideoNotes: Boolean? = null,
canSendVoiceNotes: Boolean? = null,
canSendPolls: Boolean? = null,
canSendOtherMessages: Boolean? = null,
canAddWebPagePreviews: Boolean? = null,
canChangeInfo: Boolean? = null,
canInviteUsers: Boolean? = null,
canPinMessages: Boolean? = null
) = ChatPermissionsImpl(
canSendMessages = canSendMessages,
canSendAudios = canSendAudios,
canSendDocuments = canSendDocuments,
canSendPhotos = canSendPhotos,
canSendVideos = canSendVideos,
canSendVideoNotes = canSendVideoNotes,
canSendVoiceNotes = canSendVoiceNotes,
canSendPolls = canSendPolls,
canSendOtherMessages = canSendOtherMessages,
canAddWebPagePreviews = canAddWebPagePreviews,
canChangeInfo = canChangeInfo,
canInviteUsers = canInviteUsers,
canPinMessages = canPinMessages
)
fun Granular(
canSendMessages: Boolean? = null,
canSendAudios: Boolean? = null,
@ -97,6 +123,37 @@ data class ChatPermissions(
canInviteUsers = canInviteUsers,
canPinMessages = canPinMessages
)
private val realSerializer = ChatPermissionsImpl.serializer()
override val descriptor: SerialDescriptor
get() = realSerializer.descriptor
override fun deserialize(decoder: Decoder): ChatPermissions {
return realSerializer.deserialize(decoder)
}
override fun serialize(encoder: Encoder, value: ChatPermissions) {
realSerializer.serialize(
encoder,
(value as? ChatPermissionsImpl) ?: value.run {
ChatPermissionsImpl(
canSendMessages = canSendMessages,
canSendAudios = canSendAudios,
canSendDocuments = canSendDocuments,
canSendPhotos = canSendPhotos,
canSendVideos = canSendVideos,
canSendVideoNotes = canSendVideoNotes,
canSendVoiceNotes = canSendVoiceNotes,
canSendPolls = canSendPolls,
canSendOtherMessages = canSendOtherMessages,
canAddWebPagePreviews = canAddWebPagePreviews,
canChangeInfo = canChangeInfo,
canInviteUsers = canInviteUsers,
canPinMessages = canPinMessages
)
}
)
}
}
}

View File

@ -0,0 +1,37 @@
package dev.inmo.tgbotapi.types.chat
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
@Serializable
data class ChatPermissionsImpl(
@SerialName(canSendMessagesField)
override val canSendMessages: Boolean? = null,
@SerialName(canSendAudiosField)
override val canSendAudios: Boolean? = null,
@SerialName(canSendDocumentsField)
override val canSendDocuments: Boolean? = null,
@SerialName(canSendPhotosField)
override val canSendPhotos: Boolean? = null,
@SerialName(canSendVideosField)
override val canSendVideos: Boolean? = null,
@SerialName(canSendVideoNotesField)
override val canSendVideoNotes: Boolean? = null,
@SerialName(canSendVoiceNotesField)
override val canSendVoiceNotes: Boolean? = null,
@SerialName(canSendPollsField)
override val canSendPolls: Boolean? = null,
@SerialName(canSendOtherMessagesField)
override val canSendOtherMessages: Boolean? = null,
@SerialName(canAddWebPagePreviewsField)
override val canAddWebPagePreviews: Boolean? = null,
@SerialName(canChangeInfoField)
override val canChangeInfo: Boolean? = null,
@SerialName(canInviteUsersField)
override val canInviteUsers: Boolean? = null,
@SerialName(canPinMessagesField)
override val canPinMessages: Boolean? = null
) : ChatPermissions

View File

@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.types.chat.member
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.ChatPermissions
import dev.inmo.tgbotapi.types.chat.User
import kotlinx.serialization.*
@ -13,25 +14,25 @@ data class RestrictedChatMember(
@SerialName(isMemberField)
val isMember: Boolean = false,
@SerialName(canSendMessagesField)
val canSendMessages: Boolean = false,
override val canSendMessages: Boolean = false,
@SerialName(canSendAudiosField)
val canSendAudios: Boolean = false,
override val canSendAudios: Boolean = false,
@SerialName(canSendDocumentsField)
val canSendDocuments: Boolean = false,
override val canSendDocuments: Boolean = false,
@SerialName(canSendPhotosField)
val canSendPhotos: Boolean = false,
override val canSendPhotos: Boolean = false,
@SerialName(canSendVideosField)
val canSendVideos: Boolean = false,
override val canSendVideos: Boolean = false,
@SerialName(canSendVideoNotesField)
val canSendVideoNotes: Boolean = false,
override val canSendVideoNotes: Boolean = false,
@SerialName(canSendVoiceNotesField)
val canSendVoiceNotes: Boolean = false,
override val canSendVoiceNotes: Boolean = false,
@SerialName(canSendPollsField)
val canSendPolls: Boolean = false,
override val canSendPolls: Boolean = false,
@SerialName(canSendOtherMessagesField)
val canSendOtherMessages: Boolean = false,
override val canSendOtherMessages: Boolean = false,
@SerialName(canAddWebPagePreviewsField)
val canAddWebpagePreviews: Boolean = false,
override val canAddWebPagePreviews: Boolean = false,
@SerialName(canChangeInfoField)
override val canChangeInfo: Boolean = false,
@SerialName(canInviteUsersField)
@ -40,7 +41,7 @@ data class RestrictedChatMember(
override val canPinMessages: Boolean = false,
@SerialName(canManageTopicsField)
override val canManageTopics: Boolean = false
) : BannedChatMember, SpecialRightsChatMember {
) : BannedChatMember, SpecialRightsChatMember, ChatPermissions {
@SerialName(statusField)
@Required
private val type: String = "restricted"