diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/GetMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/GetMatrixClientInfoUseCase.kt index a41931ed4a..c248340786 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/GetMatrixClientInfoUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/GetMatrixClientInfoUseCase.kt @@ -18,23 +18,24 @@ package im.vector.app.features.settings.devices.v2.details.extended import MATRIX_CLIENT_INFO_KEY_PREFIX import im.vector.app.core.di.ActiveSessionHolder +import org.matrix.android.sdk.api.session.events.model.toModel import javax.inject.Inject /** - * This use case retrieves the current account data event containing extended client info. + * 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, ) { - // TODO add unit tests - fun execute(): MatrixClientInfoContent? { + fun execute(deviceId: String): MatrixClientInfoContent? { return activeSessionHolder .getSafeActiveSession() ?.let { session -> - val type = MATRIX_CLIENT_INFO_KEY_PREFIX + session.sessionParams.deviceId + val type = MATRIX_CLIENT_INFO_KEY_PREFIX + deviceId val content = session.accountDataService().getUserAccountDataEvent(type)?.content - MatrixClientInfoContent.fromJson(content) + content.toModel() } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/MatrixClientInfoContent.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/MatrixClientInfoContent.kt index 814f7ef5bb..a2b7191bb7 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/MatrixClientInfoContent.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/MatrixClientInfoContent.kt @@ -31,13 +31,4 @@ data class MatrixClientInfoContent( // app url (optional, applicable only for web) @Json(name = "url") val url: String? = null, -) { - companion object { - fun fromJson(obj: Any?): MatrixClientInfoContent? { - return Moshi.Builder() - .build() - .adapter(MatrixClientInfoContent::class.java) - .fromJsonValue(obj) - } - } -} +) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCase.kt index 1e3837bc17..43426de56c 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCase.kt @@ -32,9 +32,12 @@ class SetMatrixClientInfoUseCase @Inject constructor( suspend fun execute(clientInfo: MatrixClientInfoContent): Result = runCatching { activeSessionHolder.getSafeActiveSession() ?.let { session -> - val type = MATRIX_CLIENT_INFO_KEY_PREFIX + session.sessionParams.deviceId - session.accountDataService() - .updateUserAccountData(type, clientInfo.toContent()) + val deviceId = session.sessionParams.deviceId + if (deviceId != null) { + val type = MATRIX_CLIENT_INFO_KEY_PREFIX + deviceId + session.accountDataService() + .updateUserAccountData(type, clientInfo.toContent()) + } } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCase.kt index 4058332127..ca953ce5d2 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCase.kt @@ -16,6 +16,7 @@ package im.vector.app.features.settings.devices.v2.details.extended +import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.resources.AppNameProvider import im.vector.app.core.resources.BuildMeta import javax.inject.Inject @@ -24,19 +25,22 @@ 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, ) { + // TODO call the use case after signin + on app startup // TODO add unit tests suspend fun execute(): Result = runCatching { val clientInfo = MatrixClientInfoContent( name = appNameProvider.getAppName(), version = buildMeta.versionName ) - val storedClientInfo = getMatrixClientInfoUseCase.execute() + val sessionId = activeSessionHolder.getActiveSession().sessionParams.deviceId + val storedClientInfo = sessionId?.let { getMatrixClientInfoUseCase.execute(it) } if (clientInfo != storedClientInfo) { setMatrixClientInfoUseCase.execute(clientInfo) } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/extended/GetMatrixClientInfoUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/extended/GetMatrixClientInfoUseCaseTest.kt new file mode 100644 index 0000000000..4a90f90c13 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/details/extended/GetMatrixClientInfoUseCaseTest.kt @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.settings.devices.v2.details.extended + +import MATRIX_CLIENT_INFO_KEY_PREFIX +import im.vector.app.test.fakes.FakeActiveSessionHolder +import org.amshove.kluent.shouldBe +import org.amshove.kluent.shouldBeEqualTo +import org.junit.Test + +private const val A_DEVICE_ID = "device-id" +private const val A_CLIENT_NAME = "client-name" +private const val A_CLIENT_VERSION = "client-version" +private const val A_CLIENT_URL = "client-url" + +class GetMatrixClientInfoUseCaseTest { + + private val fakeActiveSessionHolder = FakeActiveSessionHolder() + + private val getMatrixClientInfoUseCase = GetMatrixClientInfoUseCase( + activeSessionHolder = fakeActiveSessionHolder.instance + ) + + @Test + fun `given a device id and existing content when getting the info then result should contain that info`() { + // Given + givenClientInfoContent(A_DEVICE_ID) + val expectedClientInfo = MatrixClientInfoContent( + name = A_CLIENT_NAME, + version = A_CLIENT_VERSION, + url = A_CLIENT_URL, + ) + + // When + val result = getMatrixClientInfoUseCase.execute(A_DEVICE_ID) + + // Then + result shouldBeEqualTo expectedClientInfo + } + + @Test + fun `given no active session when getting the info then result should be null`() { + // Given + fakeActiveSessionHolder.givenGetSafeActiveSessionReturns(null) + + // When + val result = getMatrixClientInfoUseCase.execute(A_DEVICE_ID) + + // Then + result shouldBe null + } + + private fun givenClientInfoContent(deviceId: String) { + val type = MATRIX_CLIENT_INFO_KEY_PREFIX + deviceId + val content = mapOf( + Pair("name", A_CLIENT_NAME), + Pair("version", A_CLIENT_VERSION), + Pair("url", A_CLIENT_URL), + ) + fakeActiveSessionHolder.fakeSession + .fakeSessionAccountDataService + .givenGetUserAccountDataEventReturns(type, content) + } +} diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt index bf437c123b..29b634eb0c 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt @@ -43,6 +43,7 @@ class FakeSession( val fakeRoomService: FakeRoomService = FakeRoomService(), val fakePushersService: FakePushersService = FakePushersService(), private val fakeEventService: FakeEventService = FakeEventService(), + val fakeSessionAccountDataService: FakeSessionAccountDataService = FakeSessionAccountDataService(), ) : Session by mockk(relaxed = true) { init { @@ -60,6 +61,7 @@ class FakeSession( override fun roomService() = fakeRoomService override fun eventService() = fakeEventService override fun pushersService() = fakePushersService + override fun accountDataService() = fakeSessionAccountDataService fun givenVectorStore(vectorSessionStore: VectorSessionStore) { coEvery { diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeSessionAccountDataService.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeSessionAccountDataService.kt new file mode 100644 index 0000000000..5b9c918b64 --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeSessionAccountDataService.kt @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.test.fakes + +import io.mockk.every +import io.mockk.mockk +import org.matrix.android.sdk.api.session.accountdata.SessionAccountDataService +import org.matrix.android.sdk.api.session.accountdata.UserAccountDataEvent +import org.matrix.android.sdk.api.session.events.model.Content + +class FakeSessionAccountDataService : SessionAccountDataService by mockk() { + + fun givenGetUserAccountDataEventReturns(type: String, content: Content) { + every { getUserAccountDataEvent(type) } returns UserAccountDataEvent(type, content) + } +}