Skip to content

Commit 39a8d9d

Browse files
authored
Merge pull request #903 from vector-im/feature/bma/sessionDeleted
React to session deletion (from another session)
2 parents 9741b3d + e8b1f26 commit 39a8d9d

File tree

1 file changed

+24
-8
lines changed
  • libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl

1 file changed

+24
-8
lines changed

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import kotlinx.coroutines.flow.filter
6161
import kotlinx.coroutines.flow.first
6262
import kotlinx.coroutines.flow.launchIn
6363
import kotlinx.coroutines.flow.onEach
64+
import kotlinx.coroutines.launch
6465
import kotlinx.coroutines.withContext
6566
import kotlinx.coroutines.withTimeout
6667
import org.matrix.rustcomponents.sdk.Client
@@ -70,6 +71,7 @@ import org.matrix.rustcomponents.sdk.RoomListItem
7071
import org.matrix.rustcomponents.sdk.use
7172
import timber.log.Timber
7273
import java.io.File
74+
import java.util.concurrent.atomic.AtomicBoolean
7375
import org.matrix.rustcomponents.sdk.CreateRoomParameters as RustCreateRoomParameters
7476
import org.matrix.rustcomponents.sdk.RoomPreset as RustRoomPreset
7577
import org.matrix.rustcomponents.sdk.RoomVisibility as RustRoomVisibility
@@ -80,7 +82,7 @@ class RustMatrixClient constructor(
8082
private val client: Client,
8183
private val syncService: ClientSyncService,
8284
private val sessionStore: SessionStore,
83-
appCoroutineScope: CoroutineScope,
85+
private val appCoroutineScope: CoroutineScope,
8486
private val dispatchers: CoroutineDispatchers,
8587
private val baseDirectory: File,
8688
baseCacheDirectory: File,
@@ -103,10 +105,20 @@ class RustMatrixClient constructor(
103105

104106
private val notificationService = RustNotificationService(notificationClient)
105107

108+
private val isLoggingOut = AtomicBoolean(false)
109+
106110
private val clientDelegate = object : ClientDelegate {
107111
override fun didReceiveAuthError(isSoftLogout: Boolean) {
108-
//TODO handle this
109-
Timber.v("didReceiveAuthError(isSoftLogout=$isSoftLogout)")
112+
Timber.w("didReceiveAuthError(isSoftLogout=$isSoftLogout)")
113+
if (isLoggingOut.getAndSet(true).not()) {
114+
Timber.v("didReceiveAuthError -> do the cleanup")
115+
//TODO handle isSoftLogout parameter.
116+
appCoroutineScope.launch {
117+
doLogout(doRequest = false)
118+
}
119+
} else {
120+
Timber.v("didReceiveAuthError -> already cleaning up")
121+
}
110122
}
111123
}
112124

@@ -274,11 +286,15 @@ class RustMatrixClient constructor(
274286
baseDirectory.deleteSessionDirectory(userID = sessionId.value, deleteCryptoDb = false)
275287
}
276288

277-
override suspend fun logout() = withContext(sessionDispatcher) {
278-
try {
279-
client.logout()
280-
} catch (failure: Throwable) {
281-
Timber.e(failure, "Fail to call logout on HS. Still delete local files.")
289+
override suspend fun logout() = doLogout(doRequest = true)
290+
291+
private suspend fun doLogout(doRequest: Boolean) = withContext(sessionDispatcher) {
292+
if (doRequest) {
293+
try {
294+
client.logout()
295+
} catch (failure: Throwable) {
296+
Timber.e(failure, "Fail to call logout on HS. Still delete local files.")
297+
}
282298
}
283299
close()
284300
baseDirectory.deleteSessionDirectory(userID = sessionId.value, deleteCryptoDb = true)

0 commit comments

Comments
 (0)