diff --git a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt index 3e47eb81a5..504ed830df 100644 --- a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt @@ -19,6 +19,7 @@ package im.vector.app.core.session import android.content.Context import dagger.hilt.android.qualifiers.ApplicationContext import im.vector.app.core.extensions.startSyncing +import im.vector.app.core.session.clientinfo.UpdateMatrixClientInfoUseCase import im.vector.app.features.call.webrtc.WebRtcCallManager import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.sync.FilterService @@ -28,6 +29,7 @@ import javax.inject.Inject class ConfigureAndStartSessionUseCase @Inject constructor( @ApplicationContext private val context: Context, private val webRtcCallManager: WebRtcCallManager, + private val updateMatrixClientInfoUseCase: UpdateMatrixClientInfoUseCase, ) { // TODO add unit tests @@ -40,5 +42,6 @@ class ConfigureAndStartSessionUseCase @Inject constructor( } session.pushersService().refreshPushers() webRtcCallManager.checkForProtocolsSupportIfNeeded() + updateMatrixClientInfoUseCase.execute(session) } } diff --git a/vector/src/main/java/im/vector/app/core/session/clientinfo/GetMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/core/session/clientinfo/GetMatrixClientInfoUseCase.kt index 270457504c..18b731ea09 100644 --- a/vector/src/main/java/im/vector/app/core/session/clientinfo/GetMatrixClientInfoUseCase.kt +++ b/vector/src/main/java/im/vector/app/core/session/clientinfo/GetMatrixClientInfoUseCase.kt @@ -16,7 +16,7 @@ package im.vector.app.core.session.clientinfo -import im.vector.app.core.di.ActiveSessionHolder +import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.events.model.toModel import javax.inject.Inject @@ -24,17 +24,11 @@ import javax.inject.Inject * This use case retrieves the current account data event containing extended client info * for a given deviceId. */ -class GetMatrixClientInfoUseCase @Inject constructor( - private val activeSessionHolder: ActiveSessionHolder, -) { +class GetMatrixClientInfoUseCase @Inject constructor() { - fun execute(deviceId: String): MatrixClientInfoContent? { - return activeSessionHolder - .getSafeActiveSession() - ?.let { session -> - val type = MATRIX_CLIENT_INFO_KEY_PREFIX + deviceId - val content = session.accountDataService().getUserAccountDataEvent(type)?.content - content.toModel() - } + fun execute(session: Session, deviceId: String): MatrixClientInfoContent? { + val type = MATRIX_CLIENT_INFO_KEY_PREFIX + deviceId + val content = session.accountDataService().getUserAccountDataEvent(type)?.content + return content.toModel() } } diff --git a/vector/src/main/java/im/vector/app/core/session/clientinfo/SetMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/core/session/clientinfo/SetMatrixClientInfoUseCase.kt index 677d589dec..5ec428eed0 100644 --- a/vector/src/main/java/im/vector/app/core/session/clientinfo/SetMatrixClientInfoUseCase.kt +++ b/vector/src/main/java/im/vector/app/core/session/clientinfo/SetMatrixClientInfoUseCase.kt @@ -16,28 +16,23 @@ package im.vector.app.core.session.clientinfo -import im.vector.app.core.di.ActiveSessionHolder +import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.events.model.toContent import javax.inject.Inject /** * This use case sets the account data event containing extended client info. */ -class SetMatrixClientInfoUseCase @Inject constructor( - private val activeSessionHolder: ActiveSessionHolder, -) { +class SetMatrixClientInfoUseCase @Inject constructor() { - suspend fun execute(clientInfo: MatrixClientInfoContent): Result = runCatching { - activeSessionHolder.getSafeActiveSession() - ?.let { session -> - val deviceId = session.sessionParams.deviceId.orEmpty() - if (deviceId.isNotEmpty()) { - val type = MATRIX_CLIENT_INFO_KEY_PREFIX + deviceId - session.accountDataService() - .updateUserAccountData(type, clientInfo.toContent()) - } else { - throw NoDeviceIdError() - } - } + suspend fun execute(session: Session, clientInfo: MatrixClientInfoContent): Result = runCatching { + val deviceId = session.sessionParams.deviceId.orEmpty() + if (deviceId.isNotEmpty()) { + val type = MATRIX_CLIENT_INFO_KEY_PREFIX + deviceId + session.accountDataService() + .updateUserAccountData(type, clientInfo.toContent()) + } else { + throw NoDeviceIdError() + } } } diff --git a/vector/src/main/java/im/vector/app/core/session/clientinfo/UpdateMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/core/session/clientinfo/UpdateMatrixClientInfoUseCase.kt index d4d35db4bb..a5cd2bb948 100644 --- a/vector/src/main/java/im/vector/app/core/session/clientinfo/UpdateMatrixClientInfoUseCase.kt +++ b/vector/src/main/java/im/vector/app/core/session/clientinfo/UpdateMatrixClientInfoUseCase.kt @@ -16,35 +16,43 @@ package im.vector.app.core.session.clientinfo -import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.resources.AppNameProvider import im.vector.app.core.resources.BuildMeta +import im.vector.app.features.session.coroutineScope +import kotlinx.coroutines.launch +import org.matrix.android.sdk.api.session.Session +import timber.log.Timber import javax.inject.Inject /** * This use case updates if needed the account data event containing extended client info. */ class UpdateMatrixClientInfoUseCase @Inject constructor( - private val activeSessionHolder: ActiveSessionHolder, private val appNameProvider: AppNameProvider, private val buildMeta: BuildMeta, private val getMatrixClientInfoUseCase: GetMatrixClientInfoUseCase, private val setMatrixClientInfoUseCase: SetMatrixClientInfoUseCase, ) { - suspend fun execute(): Result = runCatching { - val clientInfo = MatrixClientInfoContent( - name = appNameProvider.getAppName(), - version = buildMeta.versionName - ) - val deviceId = activeSessionHolder.getActiveSession().sessionParams.deviceId.orEmpty() - if (deviceId.isNotEmpty()) { - val storedClientInfo = getMatrixClientInfoUseCase.execute(deviceId) - if (clientInfo != storedClientInfo) { - setMatrixClientInfoUseCase.execute(clientInfo) + fun execute(session: Session) { + session.coroutineScope.launch { + runCatching { + val clientInfo = MatrixClientInfoContent( + name = appNameProvider.getAppName(), + version = buildMeta.versionName + ) + val deviceId = session.sessionParams.deviceId.orEmpty() + if (deviceId.isNotEmpty()) { + val storedClientInfo = getMatrixClientInfoUseCase.execute(session, deviceId) + Timber.d("storedClientInfo=$storedClientInfo, current client info=$clientInfo") + if (clientInfo != storedClientInfo) { + Timber.d("client info need to be updated") + setMatrixClientInfoUseCase.execute(session, clientInfo) + } + } else { + throw NoDeviceIdError() + } } - } else { - throw NoDeviceIdError() } } }