commonMain.net.folivo.trixnity.client.store.KeyStore.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trixnity-client-jvm Show documentation
Show all versions of trixnity-client-jvm Show documentation
Multiplatform Kotlin SDK for matrix-protocol
package net.folivo.trixnity.client.store
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.flow.*
import net.folivo.trixnity.client.MatrixClientConfiguration
import net.folivo.trixnity.client.flattenValues
import net.folivo.trixnity.client.store.cache.FullRepositoryObservableCache
import net.folivo.trixnity.client.store.cache.MinimalRepositoryObservableCache
import net.folivo.trixnity.client.store.repository.*
import net.folivo.trixnity.core.model.UserId
import net.folivo.trixnity.core.model.keys.Key
import net.folivo.trixnity.crypto.SecretType
import kotlin.coroutines.CoroutineContext
import kotlin.time.Duration
private val log = KotlinLogging.logger { }
class KeyStore(
outdatedKeysRepository: OutdatedKeysRepository,
deviceKeysRepository: DeviceKeysRepository,
crossSigningKeysRepository: CrossSigningKeysRepository,
keyVerificationStateRepository: KeyVerificationStateRepository,
private val keyChainLinkRepository: KeyChainLinkRepository,
secretsRepository: SecretsRepository,
secretKeyRequestRepository: SecretKeyRequestRepository,
roomKeyRequestRepository: RoomKeyRequestRepository,
private val tm: RepositoryTransactionManager,
config: MatrixClientConfiguration,
storeScope: CoroutineScope
) : Store {
private val outdatedKeysCache =
MinimalRepositoryObservableCache(
repository = outdatedKeysRepository,
tm = tm,
cacheScope = storeScope,
expireDuration = Duration.INFINITE
)
private val secretsCache =
MinimalRepositoryObservableCache(
repository = secretsRepository,
tm = tm,
cacheScope = storeScope,
expireDuration = Duration.INFINITE
)
private val deviceKeysCache =
MinimalRepositoryObservableCache(
repository = deviceKeysRepository,
tm = tm,
cacheScope = storeScope,
expireDuration = config.cacheExpireDurations.deviceKeys
)
private val crossSigningKeysCache = MinimalRepositoryObservableCache(
repository = crossSigningKeysRepository,
tm = tm,
cacheScope = storeScope,
expireDuration = config.cacheExpireDurations.crossSigningKeys
)
private val keyVerificationStateCache = MinimalRepositoryObservableCache(
repository = keyVerificationStateRepository,
tm = tm,
cacheScope = storeScope,
expireDuration = config.cacheExpireDurations.keyVerificationState
)
private val secretKeyRequestCache = FullRepositoryObservableCache(
repository = secretKeyRequestRepository,
tm = tm,
cacheScope = storeScope,
expireDuration = config.cacheExpireDurations.secretKeyRequest
) { it.content.requestId }
private val roomKeyRequestCache = FullRepositoryObservableCache(
repository = roomKeyRequestRepository,
tm = tm,
cacheScope = storeScope,
expireDuration = config.cacheExpireDurations.roomKeyRequest
) { it.content.requestId }
override suspend fun clearCache() {
tm.writeTransaction {
keyChainLinkRepository.deleteAll()
}
outdatedKeysCache.deleteAll()
deviceKeysCache.deleteAll()
crossSigningKeysCache.deleteAll()
secretKeyRequestCache.deleteAll()
roomKeyRequestCache.deleteAll()
}
override suspend fun deleteAll() {
clearCache()
secretsCache.deleteAll()
keyVerificationStateCache.deleteAll()
}
suspend fun getOutdatedKeys(): Set = outdatedKeysCache.read(1).first().orEmpty()
fun getOutdatedKeysFlow(): Flow> = outdatedKeysCache.read(1).map { it.orEmpty() }
suspend fun updateOutdatedKeys(updater: suspend (Set) -> Set) =
outdatedKeysCache.write(1) {
updater(it.orEmpty())
}
suspend fun getSecrets(): Map = secretsCache.read(1).first().orEmpty()
fun getSecretsFlow(): Flow