add method to AdminsCacheAPI

This commit is contained in:
InsanusMokrassar 2021-03-30 22:36:19 +06:00
parent 06d5cb904b
commit b76a574929
2 changed files with 19 additions and 0 deletions

View File

@ -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<AdministratorChatMember>?
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
}

View File

@ -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
}