@@ -61,6 +61,7 @@ import kotlinx.coroutines.flow.filter
6161import kotlinx.coroutines.flow.first
6262import kotlinx.coroutines.flow.launchIn
6363import kotlinx.coroutines.flow.onEach
64+ import kotlinx.coroutines.launch
6465import kotlinx.coroutines.withContext
6566import kotlinx.coroutines.withTimeout
6667import org.matrix.rustcomponents.sdk.Client
@@ -70,6 +71,7 @@ import org.matrix.rustcomponents.sdk.RoomListItem
7071import org.matrix.rustcomponents.sdk.use
7172import timber.log.Timber
7273import java.io.File
74+ import java.util.concurrent.atomic.AtomicBoolean
7375import org.matrix.rustcomponents.sdk.CreateRoomParameters as RustCreateRoomParameters
7476import org.matrix.rustcomponents.sdk.RoomPreset as RustRoomPreset
7577import 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