From b76a5749295eff2f3bc88b4f5ca2c7023081cdd7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 30 Mar 2021 22:36:19 +0600 Subject: [PATCH] add method to AdminsCacheAPI --- .../libraries/cache/admins/AdminsCacheAPI.kt | 7 +++++++ .../libraries/cache/admins/DefaultAdminsCacheAPI.kt | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/cache/admins/common/src/commonMain/kotlin/dev/inmo/tgbotapi/libraries/cache/admins/AdminsCacheAPI.kt b/cache/admins/common/src/commonMain/kotlin/dev/inmo/tgbotapi/libraries/cache/admins/AdminsCacheAPI.kt index a8fc8c5..242525f 100644 --- a/cache/admins/common/src/commonMain/kotlin/dev/inmo/tgbotapi/libraries/cache/admins/AdminsCacheAPI.kt +++ b/cache/admins/common/src/commonMain/kotlin/dev/inmo/tgbotapi/libraries/cache/admins/AdminsCacheAPI.kt @@ -1,13 +1,20 @@ package dev.inmo.tgbotapi.libraries.cache.admins +import dev.inmo.tgbotapi.extensions.utils.asGroupContentMessage import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.ChatMember.abstracts.AdministratorChatMember +import dev.inmo.tgbotapi.types.message.abstracts.GroupContentMessage +import dev.inmo.tgbotapi.types.message.abstracts.Message interface AdminsCacheAPI { suspend fun getChatAdmins(chatId: ChatId): List? suspend fun isAdmin(chatId: ChatId, userId: UserId): Boolean = getChatAdmins(chatId) ?.any { it.user.id == userId } == true + suspend fun sentByAdmin(groupContentMessage: GroupContentMessage<*>): Boolean + suspend fun sentByAdmin(message: Message): Boolean? { + return sentByAdmin(message.asGroupContentMessage() ?: return null) + } suspend fun settings(): AdminsCacheSettingsAPI } diff --git a/cache/admins/common/src/commonMain/kotlin/dev/inmo/tgbotapi/libraries/cache/admins/DefaultAdminsCacheAPI.kt b/cache/admins/common/src/commonMain/kotlin/dev/inmo/tgbotapi/libraries/cache/admins/DefaultAdminsCacheAPI.kt index d499276..af96c96 100644 --- a/cache/admins/common/src/commonMain/kotlin/dev/inmo/tgbotapi/libraries/cache/admins/DefaultAdminsCacheAPI.kt +++ b/cache/admins/common/src/commonMain/kotlin/dev/inmo/tgbotapi/libraries/cache/admins/DefaultAdminsCacheAPI.kt @@ -6,6 +6,7 @@ import dev.inmo.tgbotapi.extensions.api.bot.getMe import dev.inmo.tgbotapi.extensions.api.chat.get.getChatAdministrators import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.ChatMember.abstracts.AdministratorChatMember +import dev.inmo.tgbotapi.types.message.abstracts.* import kotlinx.serialization.Serializable interface DefaultAdminsCacheAPIRepo { @@ -49,6 +50,17 @@ class DefaultAdminsCacheAPI( } } + override suspend fun sentByAdmin(groupContentMessage: GroupContentMessage<*>): Boolean { + return when (groupContentMessage) { + is AnonymousGroupContentMessage -> true + is CommonGroupContentMessage -> isAdmin( + groupContentMessage.chat.id, + groupContentMessage.user.id + ) + else -> false + } + } + override suspend fun settings(): AdminsCacheSettingsAPI = settingsAPI }