TelegramBotApiLibraries/cache/admins/common/src/commonMain/kotlin/dev/inmo/tgbotapi/libraries/cache/admins/AdminsCacheSettingsAPI.kt

45 lines
1.5 KiB
Kotlin
Raw Normal View History

2021-02-22 12:11:12 +00:00
package dev.inmo.tgbotapi.libraries.cache.admins
import com.soywiz.klock.minutes
2022-11-10 15:25:31 +00:00
import dev.inmo.tgbotapi.types.IdChatIdentifier
2021-02-22 12:11:12 +00:00
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
) {
2022-09-10 12:54:25 +00:00
val refreshOnCacheCalls: Boolean
2021-02-22 12:11:12 +00:00
get() = !disableRequestsRefreshMode
2022-09-10 12:54:25 +00:00
@Deprecated("Renamed", ReplaceWith("refreshOnCacheCalls"))
val refreshOnRequests: Boolean
get() = refreshOnCacheCalls
2021-02-22 12:11:12 +00:00
}
interface AdminsCacheSettingsAPI {
2022-11-10 15:25:31 +00:00
suspend fun getChatSettings(chatId: IdChatIdentifier): AdminsCacheSettings?
2021-02-22 12:11:12 +00:00
}
interface MutableAdminsCacheSettingsAPI : AdminsCacheSettingsAPI {
2022-11-10 15:25:31 +00:00
val chatSettingsUpdatedFlow: SharedFlow<Pair<IdChatIdentifier, AdminsCacheSettings>>
2021-02-22 12:11:12 +00:00
2022-11-10 15:25:31 +00:00
suspend fun setChatSettings(chatId: IdChatIdentifier, settings: AdminsCacheSettings)
2021-02-22 12:11:12 +00:00
}
fun AdminsCacheSettingsAPI.asMutable(): MutableAdminsCacheSettingsAPI? = this as? MutableAdminsCacheSettingsAPI
@Serializable
class StaticAdminsCacheSettingsAPI(
2022-11-10 15:25:31 +00:00
private val settings: Map<IdChatIdentifier, AdminsCacheSettings>
2021-02-22 12:11:12 +00:00
) : AdminsCacheSettingsAPI {
2022-11-10 15:25:31 +00:00
override suspend fun getChatSettings(chatId: IdChatIdentifier): AdminsCacheSettings? = settings[chatId]
2021-02-22 12:11:12 +00:00
}