1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 00:03:48 +00:00

add support for independent chat permissions

This commit is contained in:
InsanusMokrassar 2023-02-06 00:21:46 +06:00
parent cd596cc66d
commit 1e4a78c812
6 changed files with 105 additions and 28 deletions

View File

@ -14,27 +14,31 @@ suspend fun TelegramBot.restrictChatMember(
chatId: ChatIdentifier,
userId: UserId,
untilDate: TelegramDate? = null,
permissions: ChatPermissions = ChatPermissions()
) = execute(RestrictChatMember(chatId, userId, untilDate, permissions))
permissions: ChatPermissions = ChatPermissions(),
useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
) = execute(RestrictChatMember(chatId, userId, untilDate, permissions, useIndependentChatPermissions))
suspend fun TelegramBot.restrictChatMember(
chat: PublicChat,
userId: UserId,
untilDate: TelegramDate? = null,
permissions: ChatPermissions = ChatPermissions()
) = restrictChatMember(chat.id, userId, untilDate, permissions)
permissions: ChatPermissions = ChatPermissions(),
useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
) = restrictChatMember(chat.id, userId, untilDate, permissions, useIndependentChatPermissions)
suspend fun TelegramBot.restrictChatMember(
chatId: IdChatIdentifier,
user: User,
untilDate: TelegramDate? = null,
permissions: ChatPermissions = ChatPermissions()
) = restrictChatMember(chatId, user.id, untilDate, permissions)
permissions: ChatPermissions = ChatPermissions(),
useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
) = restrictChatMember(chatId, user.id, untilDate, permissions, useIndependentChatPermissions)
suspend fun TelegramBot.restrictChatMember(
chat: PublicChat,
user: User,
untilDate: TelegramDate? = null,
permissions: ChatPermissions = ChatPermissions()
) = restrictChatMember(chat.id, user.id, untilDate, permissions)
permissions: ChatPermissions = ChatPermissions(),
useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
) = restrictChatMember(chat.id, user.id, untilDate, permissions, useIndependentChatPermissions)

View File

@ -8,10 +8,12 @@ import dev.inmo.tgbotapi.types.chat.PublicChat
suspend fun TelegramBot.setDefaultChatMembersPermissions(
chatId: ChatIdentifier,
permissions: ChatPermissions
) = execute(SetChatPermissions(chatId, permissions))
permissions: ChatPermissions,
useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
) = execute(SetChatPermissions(chatId, permissions, useIndependentChatPermissions))
suspend fun TelegramBot.setDefaultChatMembersPermissions(
chat: PublicChat,
permissions: ChatPermissions
) = setDefaultChatMembersPermissions(chat.id, permissions)
permissions: ChatPermissions,
useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
) = setDefaultChatMembersPermissions(chat.id, permissions, useIndependentChatPermissions)

View File

@ -16,7 +16,9 @@ data class RestrictChatMember(
@SerialName(untilDateField)
override val untilDate: TelegramDate? = null,
@SerialName(permissionsField)
val permissions: ChatPermissions = ChatPermissions()
val permissions: ChatPermissions = ChatPermissions(),
@SerialName(useIndependentChatPermissionsField)
val useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
) : ChatMemberRequest<Boolean>, UntilDate {
override fun method(): String = "restrictChatMember"
override val resultDeserializer: DeserializationStrategy<Boolean>

View File

@ -12,7 +12,9 @@ data class SetChatPermissions (
@SerialName(chatIdField)
override val chatId: ChatIdentifier,
@SerialName(permissionsField)
val permissions: ChatPermissions
val permissions: ChatPermissions,
@SerialName(useIndependentChatPermissionsField)
val useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
): ChatRequest, SimpleRequest<Boolean> {
override fun method(): String = "setChatPermissions"
override val resultDeserializer: DeserializationStrategy<Boolean>

View File

@ -350,6 +350,7 @@ 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 useIndependentChatPermissionsField = "use_independent_chat_permissions"
const val rightsField = "rights"
const val forChannelsField = "for_channels"
const val canManageChatField = "can_manage_chat"

View File

@ -1,38 +1,104 @@
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 ChatPermissions(
@SerialName(canSendMessagesField)
val canSendMessages: Boolean = false,
val canSendMessages: Boolean? = null,
@SerialName(canSendAudiosField)
val canSendAudios: Boolean = false,
val canSendAudios: Boolean? = null,
@SerialName(canSendDocumentsField)
val canSendDocuments: Boolean = false,
val canSendDocuments: Boolean? = null,
@SerialName(canSendPhotosField)
val canSendPhotos: Boolean = false,
val canSendPhotos: Boolean? = null,
@SerialName(canSendVideosField)
val canSendVideos: Boolean = false,
val canSendVideos: Boolean? = null,
@SerialName(canSendVideoNotesField)
val canSendVideoNotes: Boolean = false,
val canSendVideoNotes: Boolean? = null,
@SerialName(canSendVoiceNotesField)
val canSendVoiceNotes: Boolean = false,
val canSendVoiceNotes: Boolean? = null,
@SerialName(canSendPollsField)
val canSendPolls: Boolean = false,
val canSendPolls: Boolean? = null,
@SerialName(canSendOtherMessagesField)
val canSendOtherMessages: Boolean = false,
val canSendOtherMessages: Boolean? = null,
@SerialName(canAddWebPagePreviewsField)
val canAddWebPagePreviews: Boolean = false,
val canAddWebPagePreviews: Boolean? = null,
@SerialName(canChangeInfoField)
val canChangeInfo: Boolean = false,
val canChangeInfo: Boolean? = null,
@SerialName(canInviteUsersField)
val canInviteUsers: Boolean = false,
val canInviteUsers: Boolean? = null,
@SerialName(canPinMessagesField)
val canPinMessages: Boolean = false
)
val canPinMessages: Boolean? = null
) {
@Transient
val isGranular
get() = canSendAudios != null ||
canSendDocuments != null ||
canSendVideoNotes != null ||
canSendPhotos != null ||
canSendVideos != null ||
canSendVoiceNotes != null
companion object {
fun Granular(
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
) = ChatPermissions(
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 Commonized(
canSendPolls: Boolean? = null,
canSendOtherMessages: Boolean? = null,
canAddWebPagePreviews: Boolean? = null,
canChangeInfo: Boolean? = null,
canInviteUsers: Boolean? = null,
canPinMessages: Boolean? = null
) = ChatPermissions(
canSendMessages = canSendPolls,
canSendAudios = null,
canSendDocuments = null,
canSendPhotos = null,
canSendVideos = null,
canSendVideoNotes = null,
canSendVoiceNotes = null,
canSendPolls = canSendPolls,
canSendOtherMessages = canSendOtherMessages,
canAddWebPagePreviews = canAddWebPagePreviews,
canChangeInfo = canChangeInfo,
canInviteUsers = canInviteUsers,
canPinMessages = canPinMessages
)
}
}
val LeftRestrictionsChatPermissions = ChatPermissions(
canSendMessages = true,