Extract session folder name computation from RustMatrixClient
This commit is contained in:
parent
f1f14e9fed
commit
125da04192
2 changed files with 35 additions and 12 deletions
|
|
@ -60,6 +60,7 @@ import io.element.android.libraries.matrix.impl.roomlist.roomOrNull
|
||||||
import io.element.android.libraries.matrix.impl.sync.RustSyncService
|
import io.element.android.libraries.matrix.impl.sync.RustSyncService
|
||||||
import io.element.android.libraries.matrix.impl.usersearch.UserProfileMapper
|
import io.element.android.libraries.matrix.impl.usersearch.UserProfileMapper
|
||||||
import io.element.android.libraries.matrix.impl.usersearch.UserSearchResultMapper
|
import io.element.android.libraries.matrix.impl.usersearch.UserSearchResultMapper
|
||||||
|
import io.element.android.libraries.matrix.impl.util.SessionDirectoryNameProvider
|
||||||
import io.element.android.libraries.matrix.impl.util.cancelAndDestroy
|
import io.element.android.libraries.matrix.impl.util.cancelAndDestroy
|
||||||
import io.element.android.libraries.matrix.impl.verification.RustSessionVerificationService
|
import io.element.android.libraries.matrix.impl.verification.RustSessionVerificationService
|
||||||
import io.element.android.libraries.sessionstorage.api.SessionStore
|
import io.element.android.libraries.sessionstorage.api.SessionStore
|
||||||
|
|
@ -134,6 +135,7 @@ class RustMatrixClient(
|
||||||
sessionCoroutineScope = sessionCoroutineScope,
|
sessionCoroutineScope = sessionCoroutineScope,
|
||||||
dispatchers = dispatchers,
|
dispatchers = dispatchers,
|
||||||
).apply { start() }
|
).apply { start() }
|
||||||
|
private val sessionDirectoryNameProvider = SessionDirectoryNameProvider()
|
||||||
|
|
||||||
private val isLoggingOut = AtomicBoolean(false)
|
private val isLoggingOut = AtomicBoolean(false)
|
||||||
|
|
||||||
|
|
@ -397,13 +399,12 @@ class RustMatrixClient(
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getCacheSize(): Long {
|
override suspend fun getCacheSize(): Long {
|
||||||
// Do not use client.userId since it can throw if client has been closed (during clear cache)
|
return baseDirectory.getCacheSize()
|
||||||
return baseDirectory.getCacheSize(userID = sessionId.value)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun clearCache() {
|
override suspend fun clearCache() {
|
||||||
close()
|
close()
|
||||||
baseDirectory.deleteSessionDirectory(userID = sessionId.value, deleteCryptoDb = false)
|
baseDirectory.deleteSessionDirectory(deleteCryptoDb = false)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun logout(ignoreSdkError: Boolean): String? = doLogout(
|
override suspend fun logout(ignoreSdkError: Boolean): String? = doLogout(
|
||||||
|
|
@ -432,7 +433,7 @@ class RustMatrixClient(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close()
|
close()
|
||||||
baseDirectory.deleteSessionDirectory(userID = sessionId.value, deleteCryptoDb = true)
|
baseDirectory.deleteSessionDirectory(deleteCryptoDb = true)
|
||||||
if (removeSession) {
|
if (removeSession) {
|
||||||
sessionStore.removeSession(sessionId.value)
|
sessionStore.removeSession(sessionId.value)
|
||||||
}
|
}
|
||||||
|
|
@ -478,12 +479,10 @@ class RustMatrixClient(
|
||||||
override fun roomMembershipObserver(): RoomMembershipObserver = roomMembershipObserver
|
override fun roomMembershipObserver(): RoomMembershipObserver = roomMembershipObserver
|
||||||
|
|
||||||
private suspend fun File.getCacheSize(
|
private suspend fun File.getCacheSize(
|
||||||
userID: String,
|
|
||||||
includeCryptoDb: Boolean = false,
|
includeCryptoDb: Boolean = false,
|
||||||
): Long = withContext(sessionDispatcher) {
|
): Long = withContext(sessionDispatcher) {
|
||||||
// Rust sanitises the user ID replacing invalid characters with an _
|
val sessionDirectoryName = sessionDirectoryNameProvider.provides(sessionId)
|
||||||
val sanitisedUserID = userID.replace(":", "_")
|
val sessionDirectory = File(this@getCacheSize, sessionDirectoryName)
|
||||||
val sessionDirectory = File(this@getCacheSize, sanitisedUserID)
|
|
||||||
if (includeCryptoDb) {
|
if (includeCryptoDb) {
|
||||||
sessionDirectory.getSizeOfFiles()
|
sessionDirectory.getSizeOfFiles()
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -500,12 +499,10 @@ class RustMatrixClient(
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun File.deleteSessionDirectory(
|
private suspend fun File.deleteSessionDirectory(
|
||||||
userID: String,
|
|
||||||
deleteCryptoDb: Boolean = false,
|
deleteCryptoDb: Boolean = false,
|
||||||
): Boolean = withContext(sessionDispatcher) {
|
): Boolean = withContext(sessionDispatcher) {
|
||||||
// Rust sanitises the user ID replacing invalid characters with an _
|
val sessionDirectoryName = sessionDirectoryNameProvider.provides(sessionId)
|
||||||
val sanitisedUserID = userID.replace(":", "_")
|
val sessionDirectory = File(this@deleteSessionDirectory, sessionDirectoryName)
|
||||||
val sessionDirectory = File(this@deleteSessionDirectory, sanitisedUserID)
|
|
||||||
if (deleteCryptoDb) {
|
if (deleteCryptoDb) {
|
||||||
// Delete the folder and all its content
|
// Delete the folder and all its content
|
||||||
sessionDirectory.deleteRecursively()
|
sessionDirectory.deleteRecursively()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2024 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.libraries.matrix.impl.util
|
||||||
|
|
||||||
|
import io.element.android.libraries.matrix.api.core.SessionId
|
||||||
|
|
||||||
|
class SessionDirectoryNameProvider {
|
||||||
|
// Rust sanitises the user ID replacing invalid characters with an _
|
||||||
|
fun provides(sessionId: SessionId): String {
|
||||||
|
return sessionId.value.replace(":", "_")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue