mirror of
https://github.com/InsanusMokrassar/TelegramBotApiLibraries.git
synced 2025-09-06 08:40:04 +00:00
first version of adminbot
This commit is contained in:
17
cache/admins/common/build.gradle
vendored
Normal file
17
cache/admins/common/build.gradle
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
id "com.android.library"
|
||||
}
|
||||
|
||||
apply from: "$mppProjectWithSerializationPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api "dev.inmo:tgbotapi:$tgbotapi_version"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package dev.inmo.tgbotapi.libraries.cache.admins
|
||||
|
||||
import dev.inmo.tgbotapi.types.*
|
||||
import dev.inmo.tgbotapi.types.ChatMember.abstracts.AdministratorChatMember
|
||||
|
||||
interface AdminsCacheAPI {
|
||||
suspend fun getChatAdmins(chatId: ChatId): List<AdministratorChatMember>?
|
||||
suspend fun isAdmin(userId: UserId, chatId: ChatId): Boolean
|
||||
|
||||
suspend fun settings(): AdminsCacheSettingsAPI
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
package dev.inmo.tgbotapi.libraries.cache.admins
|
||||
|
||||
import com.soywiz.klock.minutes
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.Seconds
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AdminsCacheSettings(
|
||||
val refreshSeconds: Seconds = 10.minutes.seconds.toInt(),
|
||||
/**
|
||||
* In case this setting set up to true, will request admins list just from time to time instead of refreshing on
|
||||
* requests
|
||||
*/
|
||||
val disableRequestsRefreshMode: Boolean = false
|
||||
) {
|
||||
val refreshOnRequests: Boolean
|
||||
get() = !disableRequestsRefreshMode
|
||||
}
|
||||
|
||||
interface AdminsCacheSettingsAPI {
|
||||
suspend fun getChatSettings(chatId: ChatId): AdminsCacheSettings?
|
||||
}
|
||||
|
||||
interface MutableAdminsCacheSettingsAPI : AdminsCacheSettingsAPI {
|
||||
val chatSettingsUpdatedFlow: SharedFlow<Pair<ChatId, AdminsCacheSettings>>
|
||||
|
||||
suspend fun setChatSettings(chatId: ChatId, settings: AdminsCacheSettings)
|
||||
}
|
||||
|
||||
fun AdminsCacheSettingsAPI.asMutable(): MutableAdminsCacheSettingsAPI? = this as? MutableAdminsCacheSettingsAPI
|
||||
|
||||
@Serializable
|
||||
class StaticAdminsCacheSettingsAPI(
|
||||
private val settings: Map<ChatId, AdminsCacheSettings>
|
||||
) : AdminsCacheSettingsAPI {
|
||||
override suspend fun getChatSettings(chatId: ChatId): AdminsCacheSettings? = settings[chatId]
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,48 @@
|
||||
package dev.inmo.tgbotapi.libraries.cache.admins
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChatAdministrators
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.ChatMember.abstracts.AdministratorChatMember
|
||||
import dev.inmo.tgbotapi.types.UserId
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
interface DefaultAdminsCacheAPIRepo {
|
||||
suspend fun getChatAdmins(chatId: ChatId): List<AdministratorChatMember>?
|
||||
suspend fun setChatAdmins(chatId: ChatId, chatMembers: List<AdministratorChatMember>)
|
||||
suspend fun lastUpdate(chatId: ChatId): DateTime?
|
||||
}
|
||||
|
||||
@Serializable
|
||||
class DefaultAdminsCacheAPI(
|
||||
private val bot: TelegramBot,
|
||||
private val repo: DefaultAdminsCacheAPIRepo,
|
||||
private val settingsAPI: AdminsCacheSettingsAPI
|
||||
) : AdminsCacheAPI {
|
||||
private suspend fun triggerUpdate(chatId: ChatId): List<AdministratorChatMember> {
|
||||
val admins = bot.getChatAdministrators(chatId)
|
||||
repo.setChatAdmins(chatId, admins)
|
||||
return admins
|
||||
}
|
||||
|
||||
override suspend fun getChatAdmins(chatId: ChatId): List<AdministratorChatMember>? {
|
||||
val settings = settingsAPI.getChatSettings(chatId)
|
||||
val lastUpdate = repo.lastUpdate(chatId)
|
||||
return when {
|
||||
settings == null -> null
|
||||
settings.refreshOnRequests &&
|
||||
(lastUpdate == null || (DateTime.now() - lastUpdate).seconds > settings.refreshSeconds) -> {
|
||||
triggerUpdate(chatId)
|
||||
}
|
||||
else -> repo.getChatAdmins(chatId) ?: triggerUpdate(chatId)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun isAdmin(userId: UserId, chatId: ChatId): Boolean = getChatAdmins(chatId) ?.any {
|
||||
it.user.id == userId
|
||||
} == true
|
||||
|
||||
override suspend fun settings(): AdminsCacheSettingsAPI = settingsAPI
|
||||
|
||||
}
|
1
cache/admins/common/src/main/AndroidManifest.xml
vendored
Normal file
1
cache/admins/common/src/main/AndroidManifest.xml
vendored
Normal file
@@ -0,0 +1 @@
|
||||
<manifest package="dev.inmo.tgbotapi.libraries.cache.admins"/>
|
Reference in New Issue
Block a user