88package io.element.android.libraries.matrix.impl
99
1010import io.element.android.libraries.core.coroutine.CoroutineDispatchers
11+ import io.element.android.libraries.core.log.logger.LoggerTag
1112import io.element.android.libraries.matrix.impl.mapper.toSessionData
1213import io.element.android.libraries.matrix.impl.paths.getSessionPaths
1314import io.element.android.libraries.matrix.impl.util.anonymizedTokens
1415import io.element.android.libraries.sessionstorage.api.SessionStore
1516import kotlinx.coroutines.CoroutineScope
16- import kotlinx.coroutines.ExperimentalCoroutinesApi
1717import kotlinx.coroutines.launch
1818import org.matrix.rustcomponents.sdk.ClientDelegate
1919import org.matrix.rustcomponents.sdk.ClientSessionDelegate
@@ -22,21 +22,20 @@ import timber.log.Timber
2222import java.lang.ref.WeakReference
2323import java.util.concurrent.atomic.AtomicBoolean
2424
25+ private val loggerTag = LoggerTag (" RustClientSessionDelegate" )
26+
2527/* *
2628 * This class is responsible for handling the session data for the Rust SDK.
2729 *
2830 * It implements both [ClientSessionDelegate] and [ClientDelegate] to react to session data updates and auth errors.
2931 *
3032 * IMPORTANT: you must set the [client] property as soon as possible so [didReceiveAuthError] can work properly.
3133 */
32- @OptIn(ExperimentalCoroutinesApi ::class )
3334class RustClientSessionDelegate (
3435 private val sessionStore : SessionStore ,
3536 private val appCoroutineScope : CoroutineScope ,
3637 coroutineDispatchers : CoroutineDispatchers ,
3738) : ClientSessionDelegate, ClientDelegate {
38- private val clientLog = Timber .tag(" $this " )
39-
4039 // Used to ensure several calls to `didReceiveAuthError` don't trigger multiple logouts
4140 private val isLoggingOut = AtomicBoolean (false )
4241
@@ -64,7 +63,7 @@ class RustClientSessionDelegate(
6463 appCoroutineScope.launch(updateTokensDispatcher) {
6564 val existingData = sessionStore.getSession(session.userId) ? : return @launch
6665 val (anonymizedAccessToken, anonymizedRefreshToken) = session.anonymizedTokens()
67- clientLog .d(
66+ Timber .tag(loggerTag.value) .d(
6867 " Saving new session data with token: access token '$anonymizedAccessToken ' and refresh token '$anonymizedRefreshToken '. " +
6968 " Was token valid: ${existingData.isTokenValid} "
7069 )
@@ -75,48 +74,48 @@ class RustClientSessionDelegate(
7574 sessionPaths = existingData.getSessionPaths(),
7675 )
7776 sessionStore.updateData(newData)
78- clientLog .d(" Saved new session data with access token: '$anonymizedAccessToken '." )
77+ Timber .tag(loggerTag.value) .d(" Saved new session data with access token: '$anonymizedAccessToken '." )
7978 }.invokeOnCompletion {
8079 if (it != null ) {
81- clientLog .e(it, " Failed to save new session data." )
80+ Timber .tag(loggerTag.value) .e(it, " Failed to save new session data." )
8281 }
8382 }
8483 }
8584
8685 override fun didReceiveAuthError (isSoftLogout : Boolean ) {
87- clientLog .w(" didReceiveAuthError(isSoftLogout=$isSoftLogout )" )
86+ Timber .tag(loggerTag.value) .w(" didReceiveAuthError(isSoftLogout=$isSoftLogout )" )
8887 if (isLoggingOut.getAndSet(true ).not ()) {
89- clientLog .v(" didReceiveAuthError -> do the cleanup" )
88+ Timber .tag(loggerTag.value) .v(" didReceiveAuthError -> do the cleanup" )
9089 // TODO handle isSoftLogout parameter.
9190 appCoroutineScope.launch(updateTokensDispatcher) {
9291 val currentClient = client.get()
9392 if (currentClient == null ) {
94- clientLog .w(" didReceiveAuthError -> no client, exiting" )
93+ Timber .tag(loggerTag.value) .w(" didReceiveAuthError -> no client, exiting" )
9594 isLoggingOut.set(false )
9695 return @launch
9796 }
9897 val existingData = sessionStore.getSession(currentClient.sessionId.value)
9998 val (anonymizedAccessToken, anonymizedRefreshToken) = existingData.anonymizedTokens()
100- clientLog .d(
99+ Timber .tag(loggerTag.value) .d(
101100 " Removing session data with access token '$anonymizedAccessToken ' " +
102101 " and refresh token '$anonymizedRefreshToken '."
103102 )
104103 if (existingData != null ) {
105104 // Set isTokenValid to false
106105 val newData = existingData.copy(isTokenValid = false )
107106 sessionStore.updateData(newData)
108- clientLog .d(" Invalidated session data with access token: '$anonymizedAccessToken '." )
107+ Timber .tag(loggerTag.value) .d(" Invalidated session data with access token: '$anonymizedAccessToken '." )
109108 } else {
110- clientLog .d(" No session data found." )
109+ Timber .tag(loggerTag.value) .d(" No session data found." )
111110 }
112111 currentClient.logout(userInitiated = false , ignoreSdkError = true )
113112 }.invokeOnCompletion {
114113 if (it != null ) {
115- clientLog .e(it, " Failed to remove session data." )
114+ Timber .tag(loggerTag.value) .e(it, " Failed to remove session data." )
116115 }
117116 }
118117 } else {
119- clientLog .v(" didReceiveAuthError -> already cleaning up" )
118+ Timber .tag(loggerTag.value) .v(" didReceiveAuthError -> already cleaning up" )
120119 }
121120 }
122121
0 commit comments