refactor isAdmin

This commit is contained in:
InsanusMokrassar 2021-02-23 13:21:04 +06:00
parent 71f5bfc9f9
commit 3026f8d1f6
3 changed files with 4 additions and 6 deletions

View File

@ -5,7 +5,9 @@ import dev.inmo.tgbotapi.types.ChatMember.abstracts.AdministratorChatMember
interface AdminsCacheAPI { interface AdminsCacheAPI {
suspend fun getChatAdmins(chatId: ChatId): List<AdministratorChatMember>? suspend fun getChatAdmins(chatId: ChatId): List<AdministratorChatMember>?
suspend fun isAdmin(userId: UserId, chatId: ChatId): Boolean suspend fun isAdmin(chatId: ChatId, userId: UserId): Boolean = getChatAdmins(chatId) ?.any {
it.user.id == userId
} == true
suspend fun settings(): AdminsCacheSettingsAPI suspend fun settings(): AdminsCacheSettingsAPI
} }

View File

@ -39,10 +39,6 @@ class DefaultAdminsCacheAPI(
} }
} }
override suspend fun isAdmin(userId: UserId, chatId: ChatId): Boolean = getChatAdmins(chatId) ?.any {
it.user.id == userId
} == true
override suspend fun settings(): AdminsCacheSettingsAPI = settingsAPI override suspend fun settings(): AdminsCacheSettingsAPI = settingsAPI
} }

View File

@ -7,7 +7,7 @@ import dev.inmo.tgbotapi.types.message.abstracts.*
suspend fun AdminsCacheAPI.userIsAdmin(chatId: ChatId, userId: UserId) = this.isAdmin() suspend fun AdminsCacheAPI.userIsAdmin(chatId: ChatId, userId: UserId) = this.isAdmin()
suspend fun AdminsCacheAPI.verifyMessageFromAdmin(message: ContentMessage<*>) = when (message) { suspend fun AdminsCacheAPI.verifyMessageFromAdmin(message: ContentMessage<*>) = when (message) {
is CommonGroupContentMessage<*> -> isAdmin(message.user.id, message.chat.id) is CommonGroupContentMessage<*> -> isAdmin(message.chat.id, message.user.id)
is AnonymousGroupContentMessage<*> -> true is AnonymousGroupContentMessage<*> -> true
else -> false else -> false
} }