@@ -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
@@ -78,7 +80,7 @@ import org.matrix.rustcomponents.sdk.RoomVisibility as RustRoomVisibility
7880class RustMatrixClient constructor(
7981 private val client : Client ,
8082 private val sessionStore : SessionStore ,
81- appCoroutineScope : CoroutineScope ,
83+ private val appCoroutineScope : CoroutineScope ,
8284 private val dispatchers : CoroutineDispatchers ,
8385 private val baseDirectory : File ,
8486 baseCacheDirectory : File ,
@@ -104,10 +106,20 @@ class RustMatrixClient constructor(
104106
105107 private val notificationService = RustNotificationService (notificationClient)
106108
109+ private val isLoggingOut = AtomicBoolean (false )
110+
107111 private val clientDelegate = object : ClientDelegate {
108112 override fun didReceiveAuthError (isSoftLogout : Boolean ) {
109- // TODO handle this
110- Timber .v(" didReceiveAuthError(isSoftLogout=$isSoftLogout )" )
113+ Timber .w(" didReceiveAuthError(isSoftLogout=$isSoftLogout )" )
114+ if (isLoggingOut.getAndSet(true ).not ()) {
115+ Timber .v(" didReceiveAuthError -> do the cleanup" )
116+ // TODO handle isSoftLogout parameter.
117+ appCoroutineScope.launch {
118+ doLogout(doRequest = false )
119+ }
120+ } else {
121+ Timber .v(" didReceiveAuthError -> already cleaning up" )
122+ }
111123 }
112124 }
113125
@@ -275,11 +287,15 @@ class RustMatrixClient constructor(
275287 baseDirectory.deleteSessionDirectory(userID = sessionId.value, deleteCryptoDb = false )
276288 }
277289
278- override suspend fun logout () = withContext(sessionDispatcher) {
279- try {
280- client.logout()
281- } catch (failure: Throwable ) {
282- Timber .e(failure, " Fail to call logout on HS. Still delete local files." )
290+ override suspend fun logout () = doLogout(doRequest = true )
291+
292+ private suspend fun doLogout (doRequest : Boolean ) = withContext(sessionDispatcher) {
293+ if (doRequest) {
294+ try {
295+ client.logout()
296+ } catch (failure: Throwable ) {
297+ Timber .e(failure, " Fail to call logout on HS. Still delete local files." )
298+ }
283299 }
284300 close()
285301 baseDirectory.deleteSessionDirectory(userID = sessionId.value, deleteCryptoDb = true )
0 commit comments