Merge pull request #27 from InsanusMokrassar/0.5.4

0.5.4
This commit is contained in:
InsanusMokrassar 2022-09-19 23:47:09 +06:00 committed by GitHub
commit f802aa6a99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 27 deletions

View File

@ -1,31 +1,31 @@
package dev.inmo.tgbotapi.libraries.cache.admins.micro_utils package dev.inmo.tgbotapi.libraries.cache.admins.micro_utils
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import dev.inmo.micro_utils.coroutines.actor import dev.inmo.micro_utils.coroutines.*
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.*
import dev.inmo.tgbotapi.libraries.cache.admins.DefaultAdminsCacheAPIRepo import dev.inmo.tgbotapi.libraries.cache.admins.DefaultAdminsCacheAPIRepo
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.member.AdministratorChatMember import dev.inmo.tgbotapi.types.chat.member.AdministratorChatMember
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.*
import kotlin.coroutines.* import kotlin.coroutines.*
private sealed class RepoActions<T> { private sealed class RepoActions<T> {
abstract val toReturn: Continuation<T> abstract val deferred: CompletableDeferred<T>
} }
private class GetUpdateDateTimeRepoAction( private class GetUpdateDateTimeRepoAction(
val chatId: ChatId, val chatId: ChatId,
override val toReturn: Continuation<DateTime?> override val deferred: CompletableDeferred<DateTime?>
) : RepoActions<DateTime?>() ) : RepoActions<DateTime?>()
private class GetChatAdminsRepoAction( private class GetChatAdminsRepoAction(
val chatId: ChatId, val chatId: ChatId,
override val toReturn: Continuation<List<AdministratorChatMember>?> override val deferred: CompletableDeferred<List<AdministratorChatMember>?>
) : RepoActions<List<AdministratorChatMember>?>() ) : RepoActions<List<AdministratorChatMember>?>()
private class SetChatAdminsRepoAction( private class SetChatAdminsRepoAction(
val chatId: ChatId, val chatId: ChatId,
val newValue: List<AdministratorChatMember>, val newValue: List<AdministratorChatMember>,
override val toReturn: Continuation<Unit> override val deferred: CompletableDeferred<Unit>
) : RepoActions<Unit>() ) : RepoActions<Unit>()
class DefaultAdminsCacheAPIRepoImpl( class DefaultAdminsCacheAPIRepoImpl(
@ -33,32 +33,54 @@ class DefaultAdminsCacheAPIRepoImpl(
private val updatesRepo: KeyValueRepo<ChatId, MilliSeconds>, private val updatesRepo: KeyValueRepo<ChatId, MilliSeconds>,
private val scope: CoroutineScope private val scope: CoroutineScope
) : DefaultAdminsCacheAPIRepo { ) : DefaultAdminsCacheAPIRepo {
private val actor = scope.actor<RepoActions<*>>(Channel.UNLIMITED) { private val actor = scope.actorAsync<RepoActions<*>>(Channel.UNLIMITED) {
safelyWithoutExceptions { safelyWithoutExceptions(
{ e ->
it.deferred.completeExceptionally(e)
}
) {
when (it) { when (it) {
is GetUpdateDateTimeRepoAction -> it.toReturn.resume( is GetUpdateDateTimeRepoAction -> it.deferred.complete(
updatesRepo.get(it.chatId) ?.let { DateTime(it.toDouble()) } updatesRepo.get(it.chatId) ?.let { DateTime(it.toDouble()) }
) )
is GetChatAdminsRepoAction -> it.toReturn.resume(adminsRepo.getAll(it.chatId)) is GetChatAdminsRepoAction -> it.deferred.complete(adminsRepo.getAll(it.chatId))
is SetChatAdminsRepoAction -> { is SetChatAdminsRepoAction -> {
adminsRepo.clear(it.chatId) adminsRepo.clear(it.chatId)
adminsRepo.set(it.chatId, it.newValue) adminsRepo.set(it.chatId, it.newValue)
updatesRepo.set(it.chatId, DateTime.now().unixMillisLong) updatesRepo.set(it.chatId, DateTime.now().unixMillisLong)
it.toReturn.resume(Unit) it.deferred.complete(Unit)
} }
} }
} }
} }
override suspend fun getChatAdmins(chatId: ChatId): List<AdministratorChatMember>? = suspendCoroutine { override suspend fun getChatAdmins(chatId: ChatId): List<AdministratorChatMember>? {
actor.trySend(GetChatAdminsRepoAction(chatId, it)) val deferred = CompletableDeferred<List<AdministratorChatMember>?>()
actor.trySend(
GetChatAdminsRepoAction(chatId, deferred)
).onFailure {
deferred.completeExceptionally(it ?: IllegalStateException("Something went wrong when tried to add getChatAdmins action"))
}
return deferred.await()
} }
override suspend fun setChatAdmins(chatId: ChatId, chatMembers: List<AdministratorChatMember>) = suspendCoroutine<Unit> { override suspend fun setChatAdmins(chatId: ChatId, chatMembers: List<AdministratorChatMember>) {
actor.trySend(SetChatAdminsRepoAction(chatId, chatMembers, it)) val deferred = CompletableDeferred<Unit>()
actor.trySend(
SetChatAdminsRepoAction(chatId, chatMembers, deferred)
).onFailure {
deferred.completeExceptionally(it ?: IllegalStateException("Something went wrong when tried to add setChatAdmins action"))
}
return deferred.await()
} }
override suspend fun lastUpdate(chatId: ChatId): DateTime? = suspendCoroutine { override suspend fun lastUpdate(chatId: ChatId): DateTime? {
actor.trySend(GetUpdateDateTimeRepoAction(chatId, it)) val deferred = CompletableDeferred<DateTime?>()
actor.trySend(
GetUpdateDateTimeRepoAction(chatId, deferred)
).onFailure {
deferred.completeExceptionally(it ?: IllegalStateException("Something went wrong when tried to add lastUpdate action"))
}
return deferred.await()
} }
} }

View File

@ -40,9 +40,9 @@ fun TelegramBot.createAdminsCacheAPI(
"AdminsTable" "AdminsTable"
).withMapper<ChatId, AdministratorChatMember, Identifier, String>( ).withMapper<ChatId, AdministratorChatMember, Identifier, String>(
keyFromToTo = { chatId }, keyFromToTo = { chatId },
valueFromToTo = { telegramAdminsSerializationFormat.encodeToString(this) }, valueFromToTo = { telegramAdminsSerializationFormat.encodeToString(AdministratorChatMember.serializer(), this) },
keyToToFrom = { toChatId() }, keyToToFrom = { toChatId() },
valueToToFrom = { telegramAdminsSerializationFormat.decodeFromString(this) } valueToToFrom = { telegramAdminsSerializationFormat.decodeFromString(AdministratorChatMember.serializer(), this) }
), ),
ExposedKeyValueRepo( ExposedKeyValueRepo(
database, database,
@ -65,9 +65,9 @@ fun TelegramBot.createAdminsCacheAPI(
"DynamicAdminsCacheSettingsAPI" "DynamicAdminsCacheSettingsAPI"
).withMapper<ChatId, AdminsCacheSettings, Identifier, String>( ).withMapper<ChatId, AdminsCacheSettings, Identifier, String>(
keyFromToTo = { chatId }, keyFromToTo = { chatId },
valueFromToTo = { telegramAdminsSerializationFormat.encodeToString(this) }, valueFromToTo = { telegramAdminsSerializationFormat.encodeToString(AdminsCacheSettings.serializer() , this) },
keyToToFrom = { toChatId() }, keyToToFrom = { toChatId() },
valueToToFrom = { telegramAdminsSerializationFormat.decodeFromString(this) } valueToToFrom = { telegramAdminsSerializationFormat.decodeFromString(AdminsCacheSettings.serializer() , this) }
), ),
scope scope
) )

View File

@ -11,10 +11,10 @@ kotlin_serialisation_core_version=1.4.0
github_release_plugin_version=2.4.1 github_release_plugin_version=2.4.1
tgbotapi_version=3.2.1 tgbotapi_version=3.2.6
micro_utils_version=0.12.11 micro_utils_version=0.12.13
exposed_version=0.39.2 exposed_version=0.39.2
plagubot_version=2.3.1 plagubot_version=2.3.3
# ANDROID # ANDROID
@ -33,5 +33,5 @@ dokka_version=1.7.10
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.5.3 version=0.5.4
android_code_version=30 android_code_version=31