Skip to content

Commit e8b1f26

Browse files
committed
React to session deletion (from another session)
1 parent d7cb8e0 commit e8b1f26

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
@@ -78,7 +80,7 @@ import org.matrix.rustcomponents.sdk.RoomVisibility as RustRoomVisibility
7880
class 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

Comments
 (0)