Merge branch 'develop' into feature/fga/room_list_api

This commit is contained in:
ganfra 2023-06-27 10:47:50 +02:00
commit 2a24d0196e
113 changed files with 2259 additions and 132 deletions

View file

@ -49,6 +49,12 @@ interface MatrixClient : Closeable {
fun sessionVerificationService(): SessionVerificationService
fun pushersService(): PushersService
fun notificationService(): NotificationService
suspend fun getCacheSize(): Long
/**
* Will close the client and delete the cache data.
*/
suspend fun clearCache()
suspend fun logout()
suspend fun loadUserDisplayName(): Result<String>
suspend fun loadUserAvatarURLString(): Result<String?>

View file

@ -21,5 +21,5 @@ import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.core.SessionId
interface NotificationService {
fun getNotification(userId: SessionId, roomId: RoomId, eventId: EventId): Result<NotificationData?>
fun getNotification(userId: SessionId, roomId: RoomId, eventId: EventId, filterByPushRules: Boolean): Result<NotificationData?>
}

View file

@ -116,4 +116,13 @@ interface MatrixRoom : Closeable {
suspend fun setTopic(topic: String): Result<Unit>
suspend fun reportContent(eventId: EventId, reason: String, blockUserId: UserId?): Result<Unit>
/**
* Share a location message in the room.
*
* @param body A human readable textual representation of the location.
* @param geoUri A geo URI (RFC 5870) representing the location e.g. `geo:51.5008,0.1247;u=35`.
* Respectively: latitude, longitude, and (optional) uncertainty.
*/
suspend fun sendLocation(body: String, geoUri: String): Result<Unit>
}

View file

@ -32,6 +32,7 @@ dependencies {
// api(projects.libraries.rustsdk)
implementation(libs.matrix.sdk)
implementation(projects.libraries.di)
implementation(projects.libraries.androidutils)
implementation(projects.services.toolbox.api)
api(projects.libraries.matrix.api)
implementation(libs.dagger)

View file

@ -16,6 +16,8 @@
package io.element.android.libraries.matrix.impl
import io.element.android.libraries.androidutils.file.getSizeOfFiles
import io.element.android.libraries.androidutils.file.safeDelete
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.core.coroutine.childScopeOf
import io.element.android.libraries.matrix.api.MatrixClient
@ -229,15 +231,25 @@ class RustMatrixClient constructor(
client.destroy()
}
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(userID = sessionId.value)
}
override suspend fun clearCache() {
close()
baseDirectory.deleteSessionDirectory(userID = sessionId.value, deleteCryptoDb = false)
}
override suspend fun logout() = withContext(dispatchers.io) {
try {
client.logout()
} catch (failure: Throwable) {
Timber.e(failure, "Fail to call logout on HS. Still delete local files.")
}
baseDirectory.deleteSessionDirectory(userID = client.userId())
sessionStore.removeSession(client.userId())
close()
baseDirectory.deleteSessionDirectory(userID = sessionId.value, deleteCryptoDb = true)
sessionStore.removeSession(sessionId.value)
}
override suspend fun loadUserDisplayName(): Result<String> = withContext(dispatchers.io) {
@ -271,11 +283,48 @@ class RustMatrixClient constructor(
override fun roomMembershipObserver(): RoomMembershipObserver = roomMembershipObserver
private fun File.deleteSessionDirectory(userID: String): Boolean {
private suspend fun File.getCacheSize(
userID: String,
includeCryptoDb: Boolean = false,
): Long = withContext(dispatchers.io) {
// Rust sanitises the user ID replacing invalid characters with an _
val sanitisedUserID = userID.replace(":", "_")
val sessionDirectory = File(this, sanitisedUserID)
return sessionDirectory.deleteRecursively()
val sessionDirectory = File(this@getCacheSize, sanitisedUserID)
if (includeCryptoDb) {
sessionDirectory.getSizeOfFiles()
} else {
listOf(
"matrix-sdk-state.sqlite3",
"matrix-sdk-state.sqlite3-shm",
"matrix-sdk-state.sqlite3-wal",
).map { fileName ->
File(sessionDirectory, fileName)
}.sumOf { file ->
file.length()
}
}
}
private suspend fun File.deleteSessionDirectory(
userID: String,
deleteCryptoDb: Boolean = false,
): Boolean = withContext(dispatchers.io) {
// Rust sanitises the user ID replacing invalid characters with an _
val sanitisedUserID = userID.replace(":", "_")
val sessionDirectory = File(this@deleteSessionDirectory, sanitisedUserID)
if (deleteCryptoDb) {
// Delete the folder and all its content
sessionDirectory.deleteRecursively()
} else {
// Delete only the state.db file
sessionDirectory.listFiles().orEmpty()
.filter { it.name.contains("matrix-sdk-state") }
.forEach { file ->
Timber.w("Deleting file ${file.name}...")
file.safeDelete()
}
true
}
}
}

View file

@ -32,10 +32,11 @@ class RustNotificationService(
override fun getNotification(
userId: SessionId,
roomId: RoomId,
eventId: EventId
eventId: EventId,
filterByPushRules: Boolean,
): Result<NotificationData?> {
return runCatching {
client.getNotificationItem(roomId.value, eventId.value)?.use(notificationMapper::map)
client.getNotificationItem(roomId.value, eventId.value, filterByPushRules)?.use(notificationMapper::map)
}
}
}

View file

@ -374,4 +374,13 @@ class RustMatrixRoom(
}
}
}
override suspend fun sendLocation(
body: String,
geoUri: String
): Result<Unit> = withContext(coroutineDispatchers.io) {
runCatching {
innerRoom.sendLocation(body, geoUri, genTransactionId())
}
}
}

View file

@ -101,6 +101,13 @@ class FakeMatrixClient(
override fun syncService() = syncService
override suspend fun getCacheSize(): Long {
return 0
}
override suspend fun clearCache() {
}
override suspend fun logout() {
delay(100)
logoutFailure?.let { throw it }

View file

@ -31,7 +31,7 @@ import kotlinx.coroutines.flow.flowOf
val A_OIDC_DATA = OidcDetails(url = "a-url")
class FakeAuthenticationService : MatrixAuthenticationService {
private var homeserver = MutableStateFlow<MatrixHomeServerDetails?>(null)
private val homeserver = MutableStateFlow<MatrixHomeServerDetails?>(null)
private var oidcError: Throwable? = null
private var oidcCancelError: Throwable? = null
private var loginError: Throwable? = null

View file

@ -23,7 +23,7 @@ import io.element.android.libraries.matrix.api.notification.NotificationData
import io.element.android.libraries.matrix.api.notification.NotificationService
class FakeNotificationService : NotificationService {
override fun getNotification(userId: SessionId, roomId: RoomId, eventId: EventId): Result<NotificationData?> {
override fun getNotification(userId: SessionId, roomId: RoomId, eventId: EventId, filterByPushRules: Boolean): Result<NotificationData?> {
return Result.success(null)
}
}

View file

@ -77,6 +77,7 @@ class FakeMatrixRoom(
private var cancelSendResult = Result.success(Unit)
private var forwardEventResult = Result.success(Unit)
private var reportContentResult = Result.success(Unit)
private var sendLocationResult = Result.success(Unit)
var sendMediaCount = 0
private set
@ -93,6 +94,9 @@ class FakeMatrixRoom(
var reportedContentCount: Int = 0
private set
var sendLocationCount: Int = 0
private set
var isInviteAccepted: Boolean = false
private set
@ -262,6 +266,14 @@ class FakeMatrixRoom(
return reportContentResult
}
override suspend fun sendLocation(
body: String,
geoUri: String
): Result<Unit> = simulateLongTask {
sendLocationCount++
return sendLocationResult
}
override fun close() = Unit
fun givenLeaveRoomError(throwable: Throwable?) {
@ -355,4 +367,8 @@ class FakeMatrixRoom(
fun givenReportContentResult(result: Result<Unit>) {
reportContentResult = result
}
fun givenSendLocationResult(result: Result<Unit>) {
sendLocationResult = result
}
}