1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-25 03:28:44 +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, chatId: ChatIdentifier,
userId: UserId, userId: UserId,
untilDate: TelegramDate? = null, untilDate: TelegramDate? = null,
permissions: ChatPermissions = ChatPermissions() permissions: ChatPermissions = ChatPermissions(),
) = execute(RestrictChatMember(chatId, userId, untilDate, permissions)) useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
) = execute(RestrictChatMember(chatId, userId, untilDate, permissions, useIndependentChatPermissions))
suspend fun TelegramBot.restrictChatMember( suspend fun TelegramBot.restrictChatMember(
chat: PublicChat, chat: PublicChat,
userId: UserId, userId: UserId,
untilDate: TelegramDate? = null, untilDate: TelegramDate? = null,
permissions: ChatPermissions = ChatPermissions() permissions: ChatPermissions = ChatPermissions(),
) = restrictChatMember(chat.id, userId, untilDate, permissions) useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
) = restrictChatMember(chat.id, userId, untilDate, permissions, useIndependentChatPermissions)
suspend fun TelegramBot.restrictChatMember( suspend fun TelegramBot.restrictChatMember(
chatId: IdChatIdentifier, chatId: IdChatIdentifier,
user: User, user: User,
untilDate: TelegramDate? = null, untilDate: TelegramDate? = null,
permissions: ChatPermissions = ChatPermissions() permissions: ChatPermissions = ChatPermissions(),
) = restrictChatMember(chatId, user.id, untilDate, permissions) useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
) = restrictChatMember(chatId, user.id, untilDate, permissions, useIndependentChatPermissions)
suspend fun TelegramBot.restrictChatMember( suspend fun TelegramBot.restrictChatMember(
chat: PublicChat, chat: PublicChat,
user: User, user: User,
untilDate: TelegramDate? = null, untilDate: TelegramDate? = null,
permissions: ChatPermissions = ChatPermissions() permissions: ChatPermissions = ChatPermissions(),
) = restrictChatMember(chat.id, user.id, untilDate, permissions) 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( suspend fun TelegramBot.setDefaultChatMembersPermissions(
chatId: ChatIdentifier, chatId: ChatIdentifier,
permissions: ChatPermissions permissions: ChatPermissions,
) = execute(SetChatPermissions(chatId, permissions)) useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
) = execute(SetChatPermissions(chatId, permissions, useIndependentChatPermissions))
suspend fun TelegramBot.setDefaultChatMembersPermissions( suspend fun TelegramBot.setDefaultChatMembersPermissions(
chat: PublicChat, chat: PublicChat,
permissions: ChatPermissions permissions: ChatPermissions,
) = setDefaultChatMembersPermissions(chat.id, permissions) useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
) = setDefaultChatMembersPermissions(chat.id, permissions, useIndependentChatPermissions)

View File

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

View File

@ -12,7 +12,9 @@ data class SetChatPermissions (
@SerialName(chatIdField) @SerialName(chatIdField)
override val chatId: ChatIdentifier, override val chatId: ChatIdentifier,
@SerialName(permissionsField) @SerialName(permissionsField)
val permissions: ChatPermissions val permissions: ChatPermissions,
@SerialName(useIndependentChatPermissionsField)
val useIndependentChatPermissions: Boolean? = permissions.isGranular.takeIf { it }
): ChatRequest, SimpleRequest<Boolean> { ): ChatRequest, SimpleRequest<Boolean> {
override fun method(): String = "setChatPermissions" override fun method(): String = "setChatPermissions"
override val resultDeserializer: DeserializationStrategy<Boolean> 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 canPromoteMembersField = "can_promote_members"
const val canManageVoiceChatsField = "can_manage_voice_chats" const val canManageVoiceChatsField = "can_manage_voice_chats"
const val canManageVideoChatsField = "can_manage_video_chats" const val canManageVideoChatsField = "can_manage_video_chats"
const val useIndependentChatPermissionsField = "use_independent_chat_permissions"
const val rightsField = "rights" const val rightsField = "rights"
const val forChannelsField = "for_channels" const val forChannelsField = "for_channels"
const val canManageChatField = "can_manage_chat" const val canManageChatField = "can_manage_chat"

View File

@ -1,38 +1,104 @@
package dev.inmo.tgbotapi.types.chat package dev.inmo.tgbotapi.types.chat
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
@Serializable @Serializable
data class ChatPermissions( data class ChatPermissions(
@SerialName(canSendMessagesField) @SerialName(canSendMessagesField)
val canSendMessages: Boolean = false, val canSendMessages: Boolean? = null,
@SerialName(canSendAudiosField) @SerialName(canSendAudiosField)
val canSendAudios: Boolean = false, val canSendAudios: Boolean? = null,
@SerialName(canSendDocumentsField) @SerialName(canSendDocumentsField)
val canSendDocuments: Boolean = false, val canSendDocuments: Boolean? = null,
@SerialName(canSendPhotosField) @SerialName(canSendPhotosField)
val canSendPhotos: Boolean = false, val canSendPhotos: Boolean? = null,
@SerialName(canSendVideosField) @SerialName(canSendVideosField)
val canSendVideos: Boolean = false, val canSendVideos: Boolean? = null,
@SerialName(canSendVideoNotesField) @SerialName(canSendVideoNotesField)
val canSendVideoNotes: Boolean = false, val canSendVideoNotes: Boolean? = null,
@SerialName(canSendVoiceNotesField) @SerialName(canSendVoiceNotesField)
val canSendVoiceNotes: Boolean = false, val canSendVoiceNotes: Boolean? = null,
@SerialName(canSendPollsField) @SerialName(canSendPollsField)
val canSendPolls: Boolean = false, val canSendPolls: Boolean? = null,
@SerialName(canSendOtherMessagesField) @SerialName(canSendOtherMessagesField)
val canSendOtherMessages: Boolean = false, val canSendOtherMessages: Boolean? = null,
@SerialName(canAddWebPagePreviewsField) @SerialName(canAddWebPagePreviewsField)
val canAddWebPagePreviews: Boolean = false, val canAddWebPagePreviews: Boolean? = null,
@SerialName(canChangeInfoField) @SerialName(canChangeInfoField)
val canChangeInfo: Boolean = false, val canChangeInfo: Boolean? = null,
@SerialName(canInviteUsersField) @SerialName(canInviteUsersField)
val canInviteUsers: Boolean = false, val canInviteUsers: Boolean? = null,
@SerialName(canPinMessagesField) @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( val LeftRestrictionsChatPermissions = ChatPermissions(
canSendMessages = true, canSendMessages = true,