Multi accounts - experimental first implementation (#5285)

* Multi account - Do not reset analytics store on sign out.

Else when 1 of many accounts is removed, the analytics opt in screen is displayed again.

* Multi accounts - first implementation.

* Multi accounts - Prevent user from logging twice with the same account

* Multi accounts - ignore automatic GoBack in case of error.

* Multi accounts - update first view when adding an account.

* Rename method storeData to addSession.

* Multi accounts - handle account switch when coming from a notification

* Multi accounts - handle login link when there is already an account.

* Multi accounts - handle click on push history for not current account.

* Multi accounts - improve layout and add preview.

* Add accountselect modules

* Multi accounts - incoming share with account selection

* Multi accounts - check the feature flag before allowing login using login link.

* Multi accounts - swipe on account icon

* Cleanup

* Multi accounts - fix other implementation of SessionStore

* Multi accounts - fix PreferencesRootPresenterTest

* Multi accounts - Add test on AccountSelectPresenter

* Multi accounts - Fix test on HomePresenter - WIP

* Update database to be able to sort accounts by creation date.

* Add unit test on takeCurrentUserWithNeighbors

* Fix test and improve code.

* Add exception

* Multi accounts - handle permalink

* Code quality

* Multi accounts - localization

* Fix issue after rebase on develop

* Fix issue after rebase on develop

* Fix tests

* Fix tests

* Fix tests

* Fix tests

* Update Multi accounts flag details.

* Add missing test on DatabaseSessionStore

* Add missing preview on LoginModeView

* Remove dead code.

* Add missing preview on PushHistoryView

* Document API.

* Rename API and update test.

* Remove MatrixAuthenticationService.loggedInStateFlow()

* Update screenshots

* Remove unused import

* Add exception

* Fix compilation issue after rebase on develop.

* Update screenshots

* Fix test

* Avoid calling getLatestSession() twice

* Rename `matrixUserAndNeighbors` to `currentUserAndNeighbors`

* Extract code to its own class.

* Add comment to clarify the code.

* Init current user profile with what we now have in the database.

It allows having the cached data (user display name and avatar) when starting the application when no network is available.

* Let the RustMatrixClient update the profile in the session database

* Fix test.

* When logging out from Pin code screen, logout from all the sessions.

tom

* Make PushData.clientSecret mandatory.
Also do not restore the last session as a fallback, it can lead to error in a multi account context, or even when a ghost pusher send a Push.

* Change test in RustMatrixAuthenticationServiceTest

* Do not use MatrixAuthenticationService in RootFlowNode, only use SessionStore

* Remove MatrixAuthenticationService.getLatestSessionId()

* Fix compilation issue after merging develop

* Add test on DefaultAccountSelectEntryPoint

* Fix compilation issue after merging develop

* Introduce LoggedInAccountSwitcherNode, to improve animation when switching between accounts.

* Rename Node to follow naming convention.

* Fix navigation issue after login.

* Remove unused import

* Revert "Fix navigation issue after login."

This reverts commit e409630856d7a7e741548016d7afe174ff1b40f7.

* Revert "Rename Node to follow naming convention."

This reverts commit 883b1f37c7207512d9f6605749977ad9045846a1.

* Revert "Introduce LoggedInAccountSwitcherNode, to improve animation when switching between accounts."

This reverts commit 9c698ff8152aceb5fd2b8b5ab5f609d28de64d24.

* Metro now have `@AssistedInject`.

* Update screenshots

* Introduce DelegateTransitionHandler and use it in RootFlowNode

---------

Co-authored-by: ElementBot <android@element.io>
Co-authored-by: ganfra <francoisg@element.io>
This commit is contained in:
Benoit Marty
2025-09-26 15:45:06 +02:00
committed by GitHub
parent f1cd80ede8
commit 73a6ba2849
117 changed files with 2161 additions and 281 deletions

View File

@@ -39,4 +39,12 @@ data class SessionData(
val sessionPath: String,
/** The path to the cache data stored for the session in the filesystem. */
val cachePath: String,
/** The position, to be able to order account. */
val position: Long,
/** The index of the last date of session usage. */
val lastUsageIndex: Long,
/** The optional display name of the user. */
val userDisplayName: String?,
/** The optional avatar URL of the user. */
val userAvatarUrl: String?,
)

View File

@@ -11,8 +11,22 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
interface SessionStore {
/**
* A flow emitting the current logged in state.
* If there is at least one session, the state is [LoggedInState.LoggedIn] with the latest used session.
* If there is no session, the state is [LoggedInState.NotLoggedIn].
*/
fun loggedInStateFlow(): Flow<LoggedInState>
/**
* Return a flow of all sessions ordered by last usage descending.
*/
fun sessionsFlow(): Flow<List<SessionData>>
/**
* Add a new session. If other sessions exist, the new one will be set as the latest used one, and
* the added session position will be set to a value higher than the other session positions.
*/
suspend fun addSession(sessionData: SessionData)
/**
@@ -20,9 +34,35 @@ interface SessionStore {
* No op if userId is not found in DB.
*/
suspend fun updateData(sessionData: SessionData)
/**
* Update the user profile info of the session matching the userId.
*/
suspend fun updateUserProfile(sessionId: String, displayName: String?, avatarUrl: String?)
/**
* Get the session data matching the userId, or null if not found.
*/
suspend fun getSession(sessionId: String): SessionData?
/**
* Get all sessions ordered by last usage descending.
*/
suspend fun getAllSessions(): List<SessionData>
/**
* Get the latest session, or null if no session exists.
*/
suspend fun getLatestSession(): SessionData?
/**
* Set the session with [sessionId] as the latest used one.
*/
suspend fun setLatestSession(sessionId: String)
/**
* Remove the session matching the sessionId.
*/
suspend fun removeSession(sessionId: String)
}

View File

@@ -36,7 +36,7 @@ dependencies {
sqldelight {
databases {
create("SessionDatabase") {
// https://cashapp.github.io/sqldelight/2.0.0/android_sqlite/migrations/
// https://sqldelight.github.io/sqldelight/2.1.0/android_sqlite/migrations/
// To generate a .db file from your latest schema, run this task
// ./gradlew generateDebugSessionDatabaseSchema
// Test migration by running

View File

@@ -34,7 +34,7 @@ class DatabaseSessionStore(
private val sessionDataMutex = Mutex()
override fun loggedInStateFlow(): Flow<LoggedInState> {
return database.sessionDataQueries.selectFirst()
return database.sessionDataQueries.selectLatest()
.asFlow()
.mapToOneOrNull(dispatchers.io)
.map {
@@ -51,7 +51,17 @@ class DatabaseSessionStore(
override suspend fun addSession(sessionData: SessionData) {
sessionDataMutex.withLock {
database.sessionDataQueries.insertSessionData(sessionData.toDbModel())
val lastUsageIndex = getLastUsageIndex()
database.sessionDataQueries.insertSessionData(
sessionData
.copy(
// position value does not really matter, so just use lastUsageIndex + 1 to ensure that
// the value is always greater than value of any existing account
position = lastUsageIndex + 1,
lastUsageIndex = lastUsageIndex + 1,
)
.toDbModel()
)
}
}
@@ -65,18 +75,71 @@ class DatabaseSessionStore(
Timber.e("User ${sessionData.userId} not found in session database")
return
}
// Copy new data from SDK, but keep login timestamp
// Copy new data from SDK, but keep application data
database.sessionDataQueries.updateSession(
sessionData.copy(
loginTimestamp = result.loginTimestamp,
position = result.position,
lastUsageIndex = result.lastUsageIndex,
userDisplayName = result.userDisplayName,
userAvatarUrl = result.userAvatarUrl,
).toDbModel()
)
}
}
override suspend fun updateUserProfile(sessionId: String, displayName: String?, avatarUrl: String?) {
sessionDataMutex.withLock {
val result = database.sessionDataQueries.selectByUserId(sessionId)
.executeAsOneOrNull()
?.toApiModel()
if (result == null) {
Timber.e("User $sessionId not found in session database")
return
}
database.sessionDataQueries.updateSession(
result.copy(
userDisplayName = displayName,
userAvatarUrl = avatarUrl,
).toDbModel()
)
}
}
override suspend fun setLatestSession(sessionId: String) {
val latestSession = getLatestSession()
if (latestSession?.userId == sessionId) {
// Already the latest session
return
}
val lastUsageIndex = latestSession?.lastUsageIndex ?: 0
val result = database.sessionDataQueries.selectByUserId(sessionId)
.executeAsOneOrNull()
?.toApiModel()
if (result == null) {
Timber.e("User $sessionId not found in session database")
return
}
sessionDataMutex.withLock {
// Update lastUsageIndex of the session
database.sessionDataQueries.updateSession(
result.copy(
lastUsageIndex = lastUsageIndex + 1,
).toDbModel()
)
}
}
private fun getLastUsageIndex(): Long {
return database.sessionDataQueries.selectLatest()
.executeAsOneOrNull()
?.lastUsageIndex
?: -1L
}
override suspend fun getLatestSession(): SessionData? {
return sessionDataMutex.withLock {
database.sessionDataQueries.selectFirst()
database.sessionDataQueries.selectLatest()
.executeAsOneOrNull()
?.toApiModel()
}

View File

@@ -27,6 +27,10 @@ internal fun SessionData.toDbModel(): DbSessionData {
passphrase = passphrase,
sessionPath = sessionPath,
cachePath = cachePath,
position = position,
lastUsageIndex = lastUsageIndex,
userDisplayName = userDisplayName,
userAvatarUrl = userAvatarUrl,
)
}
@@ -45,5 +49,9 @@ internal fun DbSessionData.toApiModel(): SessionData {
passphrase = passphrase,
sessionPath = sessionPath,
cachePath = cachePath,
position = position,
lastUsageIndex = lastUsageIndex,
userDisplayName = userDisplayName,
userAvatarUrl = userAvatarUrl,
)
}

View File

@@ -27,15 +27,25 @@ CREATE TABLE SessionData (
-- added in version 6
sessionPath TEXT NOT NULL DEFAULT "",
-- added in version 9
cachePath TEXT NOT NULL DEFAULT ""
cachePath TEXT NOT NULL DEFAULT "",
-- added in version 10
-- position, to be able to sort account by session creation date
position INTEGER NOT NULL DEFAULT 0,
-- index of the last usage session. Each time the current session change, the index of the current
-- session is incremented to the max value + 1 so it becomes the current session
lastUsageIndex INTEGER NOT NULL DEFAULT 0,
-- user display name
userDisplayName TEXT,
-- user avatar url
userAvatarUrl TEXT
);
selectFirst:
SELECT * FROM SessionData LIMIT 1;
selectLatest:
SELECT * FROM SessionData ORDER BY lastUsageIndex DESC LIMIT 1;
selectAll:
SELECT * FROM SessionData;
SELECT * FROM SessionData ORDER BY lastUsageIndex DESC;
selectByUserId:
SELECT * FROM SessionData WHERE userId = ?;

View File

@@ -0,0 +1,9 @@
-- Migrate DB from version 9
-- Add position to be able to sort account by session creation date
-- Add lastUsageIndex so we can restore the last session and switch to another one
-- Add display name and avatar url of the user so that we can display a list of accounts.
ALTER TABLE SessionData ADD COLUMN position INTEGER NOT NULL DEFAULT 0;
ALTER TABLE SessionData ADD COLUMN lastUsageIndex INTEGER NOT NULL DEFAULT 0;
ALTER TABLE SessionData ADD COLUMN userDisplayName TEXT;
ALTER TABLE SessionData ADD COLUMN userAvatarUrl TEXT;

View File

@@ -13,6 +13,7 @@ import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.matrix.session.SessionData
import io.element.android.libraries.sessionstorage.api.LoggedInState
import io.element.android.libraries.sessionstorage.api.LoginType
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
@@ -45,11 +46,11 @@ class DatabaseSessionStoreTest {
@Test
fun `addSession persists the SessionData into the DB`() = runTest {
assertThat(database.sessionDataQueries.selectFirst().executeAsOneOrNull()).isNull()
assertThat(database.sessionDataQueries.selectLatest().executeAsOneOrNull()).isNull()
databaseSessionStore.addSession(aSessionData.toApiModel())
assertThat(database.sessionDataQueries.selectFirst().executeAsOneOrNull()).isEqualTo(aSessionData)
assertThat(database.sessionDataQueries.selectLatest().executeAsOneOrNull()).isEqualTo(aSessionData)
assertThat(database.sessionDataQueries.selectAll().executeAsList().size).isEqualTo(1)
}
@@ -59,7 +60,12 @@ class DatabaseSessionStoreTest {
assertThat(awaitItem()).isEqualTo(LoggedInState.NotLoggedIn)
databaseSessionStore.addSession(aSessionData.toApiModel())
assertThat(awaitItem()).isEqualTo(LoggedInState.LoggedIn(sessionId = aSessionData.userId, isTokenValid = true))
// TODO add more sessions in multi-account PR.
// Add a second session
databaseSessionStore.addSession(aSessionData.copy(userId = "otherUserId").toApiModel())
assertThat(awaitItem()).isEqualTo(LoggedInState.LoggedIn(sessionId = "otherUserId", isTokenValid = true))
// Remove the second session
databaseSessionStore.removeSession("otherUserId")
assertThat(awaitItem()).isEqualTo(LoggedInState.LoggedIn(sessionId = aSessionData.userId, isTokenValid = true))
// Remove the first session
databaseSessionStore.removeSession(aSessionData.userId)
assertThat(awaitItem()).isEqualTo(LoggedInState.NotLoggedIn)
@@ -124,7 +130,83 @@ class DatabaseSessionStoreTest {
}
@Test
fun `update session update all fields except loginTimestamp`() = runTest {
fun `updateUserProfile does nothing if the session is not found`() = runTest {
databaseSessionStore.updateUserProfile(aSessionData.userId, "userDisplayName", "userAvatarUrl")
assertThat(database.sessionDataQueries.selectByUserId(aSessionData.userId).executeAsOneOrNull()).isNull()
}
@Test
fun `updateUserProfile update the data`() = runTest {
database.sessionDataQueries.insertSessionData(aSessionData)
databaseSessionStore.updateUserProfile(aSessionData.userId, "userDisplayName", "userAvatarUrl")
val updatedSession = database.sessionDataQueries.selectByUserId(aSessionData.userId).executeAsOne()
assertThat(updatedSession.userDisplayName).isEqualTo("userDisplayName")
assertThat(updatedSession.userAvatarUrl).isEqualTo("userAvatarUrl")
}
@Test
fun `setLatestSession is no op when the session is already the latest session`() = runTest {
database.sessionDataQueries.insertSessionData(aSessionData)
val session = database.sessionDataQueries.selectByUserId(aSessionData.userId).executeAsOne()
assertThat(session.lastUsageIndex).isEqualTo(0)
assertThat(session.position).isEqualTo(0)
databaseSessionStore.setLatestSession(aSessionData.userId)
assertThat(database.sessionDataQueries.selectByUserId(aSessionData.userId).executeAsOne().lastUsageIndex).isEqualTo(0)
}
@Test
fun `setLatestSession is no op when the session is not found`() = runTest {
databaseSessionStore.setLatestSession(aSessionData.userId)
}
@Test
fun `multi session test`() = runTest {
databaseSessionStore.addSession(aSessionData.toApiModel())
val session = databaseSessionStore.getSession(aSessionData.userId)!!
assertThat(session.lastUsageIndex).isEqualTo(0)
assertThat(session.position).isEqualTo(0)
val secondSessionData = aSessionData.copy(
userId = "otherUserId",
position = 1,
lastUsageIndex = 1,
)
databaseSessionStore.addSession(secondSessionData.toApiModel())
val secondSession = database.sessionDataQueries.selectByUserId(secondSessionData.userId).executeAsOne()
assertThat(secondSession.lastUsageIndex).isEqualTo(1)
assertThat(secondSession.position).isEqualTo(1)
// Set the first session as the latest
databaseSessionStore.setLatestSession(aSessionData.userId)
val firstSession = database.sessionDataQueries.selectByUserId(aSessionData.userId).executeAsOne()
assertThat(firstSession.lastUsageIndex).isEqualTo(2)
assertThat(firstSession.position).isEqualTo(0)
// Check that the second session has not been altered
val secondSession2 = database.sessionDataQueries.selectByUserId(secondSessionData.userId).executeAsOne()
assertThat(secondSession2.lastUsageIndex).isEqualTo(1)
assertThat(secondSession2.position).isEqualTo(1)
}
@Test
fun `test sessionsFlow()`() = runTest {
databaseSessionStore.sessionsFlow().test {
assertThat(awaitItem()).isEmpty()
databaseSessionStore.addSession(aSessionData.toApiModel())
assertThat(awaitItem().size).isEqualTo(1)
val secondSessionData = aSessionData.copy(
userId = "otherUserId",
position = 1,
lastUsageIndex = 1,
)
databaseSessionStore.addSession(secondSessionData.toApiModel())
assertThat(awaitItem().size).isEqualTo(2)
databaseSessionStore.removeSession(aSessionData.userId)
assertThat(awaitItem().size).isEqualTo(1)
databaseSessionStore.removeSession(secondSessionData.userId)
assertThat(awaitItem()).isEmpty()
}
}
@Test
fun `update session update all fields except info used by the application`() = runTest {
val firstSessionData = SessionData(
userId = "userId",
deviceId = "deviceId",
@@ -139,6 +221,10 @@ class DatabaseSessionStoreTest {
passphrase = "aPassphrase",
sessionPath = "sessionPath",
cachePath = "cachePath",
position = 0,
lastUsageIndex = 0,
userDisplayName = "userDisplayName",
userAvatarUrl = "userAvatarUrl",
)
val secondSessionData = SessionData(
userId = "userId",
@@ -152,8 +238,12 @@ class DatabaseSessionStoreTest {
isTokenValid = 1,
loginType = null,
passphrase = "aPassphraseAltered",
sessionPath = "sessionPath",
cachePath = "cachePath",
sessionPath = "sessionPathAltered",
cachePath = "cachePathAltered",
position = 1,
lastUsageIndex = 1,
userDisplayName = "userDisplayNameAltered",
userAvatarUrl = "userAvatarUrlAltered",
)
assertThat(firstSessionData.userId).isEqualTo(secondSessionData.userId)
assertThat(firstSessionData.loginTimestamp).isNotEqualTo(secondSessionData.loginTimestamp)
@@ -174,6 +264,11 @@ class DatabaseSessionStoreTest {
assertThat(alteredSession.loginTimestamp).isEqualTo(firstSessionData.loginTimestamp)
assertThat(alteredSession.oidcData).isEqualTo(secondSessionData.oidcData)
assertThat(alteredSession.passphrase).isEqualTo(secondSessionData.passphrase)
// Check that application data have not been altered
assertThat(alteredSession.position).isEqualTo(firstSessionData.position)
assertThat(alteredSession.lastUsageIndex).isEqualTo(firstSessionData.lastUsageIndex)
assertThat(alteredSession.userDisplayName).isEqualTo(firstSessionData.userDisplayName)
assertThat(alteredSession.userAvatarUrl).isEqualTo(firstSessionData.userAvatarUrl)
}
@Test
@@ -188,10 +283,14 @@ class DatabaseSessionStoreTest {
loginTimestamp = 1,
oidcData = "aOidcData",
isTokenValid = 1,
loginType = null,
loginType = LoginType.PASSWORD.name,
passphrase = "aPassphrase",
sessionPath = "sessionPath",
cachePath = "cachePath",
position = 0,
lastUsageIndex = 0,
userDisplayName = "userDisplayName",
userAvatarUrl = "userAvatarUrl",
)
val secondSessionData = SessionData(
userId = "userIdUnknown",
@@ -203,10 +302,14 @@ class DatabaseSessionStoreTest {
loginTimestamp = 2,
oidcData = "aOidcDataAltered",
isTokenValid = 1,
loginType = null,
loginType = LoginType.PASSWORD.name,
passphrase = "aPassphraseAltered",
sessionPath = "sessionPath",
cachePath = "cachePath",
sessionPath = "sessionPathAltered",
cachePath = "cachePathAltered",
position = 1,
lastUsageIndex = 1,
userDisplayName = "userDisplayNameAltered",
userAvatarUrl = "userAvatarUrlAltered",
)
assertThat(firstSessionData.userId).isNotEqualTo(secondSessionData.userId)
@@ -216,14 +319,6 @@ class DatabaseSessionStoreTest {
// Get the session and check that it has not been altered
val notAlteredSession = databaseSessionStore.getSession(firstSessionData.userId)!!.toDbModel()
assertThat(notAlteredSession.userId).isEqualTo(firstSessionData.userId)
assertThat(notAlteredSession.deviceId).isEqualTo(firstSessionData.deviceId)
assertThat(notAlteredSession.accessToken).isEqualTo(firstSessionData.accessToken)
assertThat(notAlteredSession.refreshToken).isEqualTo(firstSessionData.refreshToken)
assertThat(notAlteredSession.homeserverUrl).isEqualTo(firstSessionData.homeserverUrl)
assertThat(notAlteredSession.slidingSyncProxy).isEqualTo(firstSessionData.slidingSyncProxy)
assertThat(notAlteredSession.loginTimestamp).isEqualTo(firstSessionData.loginTimestamp)
assertThat(notAlteredSession.oidcData).isEqualTo(firstSessionData.oidcData)
assertThat(notAlteredSession.passphrase).isEqualTo(firstSessionData.passphrase)
assertThat(notAlteredSession).isEqualTo(firstSessionData)
}
}

View File

@@ -24,4 +24,8 @@ internal fun aSessionData() = SessionData(
passphrase = null,
sessionPath = "sessionPath",
cachePath = "cachePath",
position = 0,
lastUsageIndex = 0,
userDisplayName = null,
userAvatarUrl = null,
)

View File

@@ -17,6 +17,8 @@ import kotlinx.coroutines.flow.map
class InMemorySessionStore(
initialList: List<SessionData> = emptyList(),
private val updateUserProfileResult: (String, String?, String?) -> Unit = { _, _, _ -> error("Not implemented") },
private val setLatestSessionResult: (String) -> Unit = { error("Not implemented") },
) : SessionStore {
private val sessionDataListFlow = MutableStateFlow(initialList)
@@ -53,6 +55,10 @@ class InMemorySessionStore(
}
}
override suspend fun updateUserProfile(sessionId: String, displayName: String?, avatarUrl: String?) {
updateUserProfileResult(sessionId, displayName, avatarUrl)
}
override suspend fun getSession(sessionId: String): SessionData? {
return sessionDataListFlow.value.firstOrNull { it.userId == sessionId }
}
@@ -65,6 +71,10 @@ class InMemorySessionStore(
return sessionDataListFlow.value.firstOrNull()
}
override suspend fun setLatestSession(sessionId: String) {
setLatestSessionResult(sessionId)
}
override suspend fun removeSession(sessionId: String) {
val currentList = sessionDataListFlow.value.toMutableList()
currentList.removeAll { it.userId == sessionId }

View File

@@ -18,7 +18,11 @@ fun aSessionData(
cachePath: String = "/a/path/to/a/cache",
accessToken: String = "anAccessToken",
refreshToken: String? = "aRefreshToken",
): SessionData {
position: Long = 0,
lastUsageIndex: Long = 0,
userDisplayName: String? = null,
userAvatarUrl: String? = null,
): SessionData {
return SessionData(
userId = sessionId,
deviceId = deviceId,
@@ -33,5 +37,9 @@ fun aSessionData(
passphrase = null,
sessionPath = sessionPath,
cachePath = cachePath,
position = position,
lastUsageIndex = lastUsageIndex,
userDisplayName = userDisplayName,
userAvatarUrl = userAvatarUrl,
)
}