From 7e41d731f6949191fd9a00c794b7b18f5be293a5 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 30 May 2024 13:10:36 +0200 Subject: [PATCH] Move getCryptoVersion from CryptoService to Matrix companion, it does not need a session to get the value. This simplify a lot accessing this data. --- .../java/org/matrix/android/sdk/api/Matrix.kt | 7 +++ .../sdk/api/session/crypto/CryptoService.kt | 2 - .../sdk/internal/crypto/RustCryptoService.kt | 7 --- .../java/im/vector/app/VectorApplication.kt | 1 + .../impl/AutoSuperPropertiesFlowProvider.kt | 43 ------------------- .../analytics/impl/DefaultVectorAnalytics.kt | 8 ---- .../app/features/rageshake/BugReporter.kt | 2 +- .../VectorSettingsHelpAboutFragment.kt | 2 +- .../impl/DefaultVectorAnalyticsTest.kt | 37 ---------------- .../FakeAutoSuperPropertiesFlowProvider.kt | 36 ---------------- 10 files changed, 10 insertions(+), 135 deletions(-) delete mode 100644 vector/src/main/java/im/vector/app/features/analytics/impl/AutoSuperPropertiesFlowProvider.kt delete mode 100644 vector/src/test/java/im/vector/app/test/fakes/FakeAutoSuperPropertiesFlowProvider.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt index 8893229a76..b9780b8021 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt @@ -147,5 +147,12 @@ class Matrix(context: Context, matrixConfiguration: MatrixConfiguration) { fun getSdkVersion(): String { return BuildConfig.SDK_VERSION + " (" + BuildConfig.GIT_SDK_REVISION + ")" } + + fun getCryptoVersion(longFormat: Boolean): String { + val version = org.matrix.rustcomponents.sdk.crypto.version() + val gitHash = org.matrix.rustcomponents.sdk.crypto.versionInfo().gitSha + val vodozemac = org.matrix.rustcomponents.sdk.crypto.vodozemacVersion() + return if (longFormat) "Rust SDK $version ($gitHash), Vodozemac $vodozemac" else version + } } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt index ba6b1f23b1..fa1208059a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt @@ -60,8 +60,6 @@ interface CryptoService { suspend fun deleteDevices(@Size(min = 1) deviceIds: List, userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor) - fun getCryptoVersion(longFormat: Boolean): String - fun isCryptoEnabled(): Boolean fun isRoomBlacklistUnverifiedDevices(roomId: String?): Boolean diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustCryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustCryptoService.kt index ada434ddca..a6e4efd875 100755 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustCryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustCryptoService.kt @@ -183,13 +183,6 @@ internal class RustCryptoService @Inject constructor( deleteDevices(listOf(deviceId), userInteractiveAuthInterceptor) } - override fun getCryptoVersion(longFormat: Boolean): String { - val version = org.matrix.rustcomponents.sdk.crypto.version() - val gitHash = org.matrix.rustcomponents.sdk.crypto.versionInfo().gitSha - val vodozemac = org.matrix.rustcomponents.sdk.crypto.vodozemacVersion() - return if (longFormat) "Rust SDK $version ($gitHash), Vodozemac $vodozemac" else version - } - override suspend fun getMyCryptoDevice(): CryptoDeviceInfo = withContext(coroutineDispatchers.io) { olmMachine.ownDevice() } diff --git a/vector-app/src/main/java/im/vector/app/VectorApplication.kt b/vector-app/src/main/java/im/vector/app/VectorApplication.kt index 0f83f57d01..df1c584b3a 100644 --- a/vector-app/src/main/java/im/vector/app/VectorApplication.kt +++ b/vector-app/src/main/java/im/vector/app/VectorApplication.kt @@ -135,6 +135,7 @@ class VectorApplication : SuperProperties( appPlatform = SuperProperties.AppPlatform.EA, cryptoSDK = SuperProperties.CryptoSDK.Rust, + cryptoSDKVersion = Matrix.getCryptoVersion(longFormat = false) ) ) invitesAcceptor.initialize() diff --git a/vector/src/main/java/im/vector/app/features/analytics/impl/AutoSuperPropertiesFlowProvider.kt b/vector/src/main/java/im/vector/app/features/analytics/impl/AutoSuperPropertiesFlowProvider.kt deleted file mode 100644 index 76763e3bab..0000000000 --- a/vector/src/main/java/im/vector/app/features/analytics/impl/AutoSuperPropertiesFlowProvider.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2024 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.analytics.impl - -import im.vector.app.ActiveSessionDataSource -import im.vector.app.features.analytics.plan.SuperProperties -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.distinctUntilChanged -import kotlinx.coroutines.flow.map -import javax.inject.Inject - -/** - * Gathers the super properties that are static to this platform or - * that can be automatically resolved from the current session. - */ -class AutoSuperPropertiesFlowProvider @Inject constructor( - activeSessionDataSource: ActiveSessionDataSource, -) { - - val superPropertiesFlow: Flow = activeSessionDataSource.stream() - .map { session -> - SuperProperties( - appPlatform = SuperProperties.AppPlatform.EA, - cryptoSDK = SuperProperties.CryptoSDK.Rust, - cryptoSDKVersion = session.getOrNull()?.cryptoService()?.getCryptoVersion(false) - ) - } - .distinctUntilChanged() -} diff --git a/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt b/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt index baefe18cf5..a7df8c54a8 100644 --- a/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt +++ b/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt @@ -42,7 +42,6 @@ class DefaultVectorAnalytics @Inject constructor( private val analyticsConfig: AnalyticsConfig, private val analyticsStore: AnalyticsStore, private val lateInitUserPropertiesFactory: LateInitUserPropertiesFactory, - private val autoSuperPropertiesFlowProvider: AutoSuperPropertiesFlowProvider, @NamedGlobalScope private val globalScope: CoroutineScope ) : VectorAnalytics { @@ -70,7 +69,6 @@ class DefaultVectorAnalytics @Inject constructor( override fun init() { observeUserConsent() observeAnalyticsId() - observeAutoSuperProperties() } override fun getUserConsent(): Flow { @@ -118,12 +116,6 @@ class DefaultVectorAnalytics @Inject constructor( .launchIn(globalScope) } - private fun observeAutoSuperProperties() { - autoSuperPropertiesFlowProvider.superPropertiesFlow.onEach { - updateSuperProperties(it) - }.launchIn(globalScope) - } - private suspend fun identifyPostHog() { val id = analyticsId ?: return if (!userConsent.orFalse()) return diff --git a/vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt b/vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt index f89b6749a3..611c210f39 100755 --- a/vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt +++ b/vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt @@ -265,7 +265,7 @@ class BugReporter @Inject constructor( activeSessionHolder.getSafeActiveSession()?.let { session -> userId = session.myUserId deviceId = session.sessionParams.deviceId - olmVersion = session.cryptoService().getCryptoVersion(true) + olmVersion = Matrix.getCryptoVersion(true) } if (!mIsCancelled) { diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsHelpAboutFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsHelpAboutFragment.kt index 47db90466e..359cc041f8 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsHelpAboutFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsHelpAboutFragment.kt @@ -96,7 +96,7 @@ class VectorSettingsHelpAboutFragment : // olm version findPreference(VectorPreferences.SETTINGS_CRYPTO_VERSION_PREFERENCE_KEY)!! - .summary = session.cryptoService().getCryptoVersion(true) + .summary = Matrix.getCryptoVersion(true) } companion object { diff --git a/vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt b/vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt index 3b0d13c945..72ff56bc27 100644 --- a/vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt +++ b/vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt @@ -18,7 +18,6 @@ package im.vector.app.features.analytics.impl import im.vector.app.features.analytics.plan.SuperProperties import im.vector.app.test.fakes.FakeAnalyticsStore -import im.vector.app.test.fakes.FakeAutoSuperPropertiesFlowProvider import im.vector.app.test.fakes.FakeLateInitUserPropertiesFactory import im.vector.app.test.fakes.FakePostHog import im.vector.app.test.fakes.FakePostHogFactory @@ -46,7 +45,6 @@ class DefaultVectorAnalyticsTest { private val fakeAnalyticsStore = FakeAnalyticsStore() private val fakeLateInitUserPropertiesFactory = FakeLateInitUserPropertiesFactory() private val fakeSentryAnalytics = FakeSentryAnalytics() - private val fakeAutoSuperPropertiesFlowProvider = FakeAutoSuperPropertiesFlowProvider() private val defaultVectorAnalytics = DefaultVectorAnalytics( postHogFactory = FakePostHogFactory(fakePostHog.instance).instance, @@ -55,7 +53,6 @@ class DefaultVectorAnalyticsTest { globalScope = CoroutineScope(Dispatchers.Unconfined), analyticsConfig = anAnalyticsConfig(isEnabled = true), lateInitUserPropertiesFactory = fakeLateInitUserPropertiesFactory.instance, - autoSuperPropertiesFlowProvider = fakeAutoSuperPropertiesFlowProvider.instance, ) @Before @@ -289,40 +286,6 @@ class DefaultVectorAnalyticsTest { ) } - @Test - fun `Update super properties from flow`() = runTest { - fakeAnalyticsStore.givenUserContent(consent = true) - - fakeAutoSuperPropertiesFlowProvider.postSuperProperty( - SuperProperties( - cryptoSDKVersion = "0" - ) - ) - - val fakeEvent = aVectorAnalyticsEvent("THE_NAME", null) - defaultVectorAnalytics.capture(fakeEvent) - - fakePostHog.verifyEventTracked( - "THE_NAME", - mapOf( - "cryptoSDKVersion" to "0" - ) - ) - - fakeAutoSuperPropertiesFlowProvider.postSuperProperty(SuperProperties( - cryptoSDKVersion = "1" - )) - - defaultVectorAnalytics.capture(fakeEvent) - - fakePostHog.verifyEventTracked( - "THE_NAME", - mapOf( - "cryptoSDKVersion" to "1" - ) - ) - } - private fun Map?.clearNulls(): Map? { if (this == null) return null diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeAutoSuperPropertiesFlowProvider.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeAutoSuperPropertiesFlowProvider.kt deleted file mode 100644 index eff184298e..0000000000 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeAutoSuperPropertiesFlowProvider.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2024 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 im.vector.app.features.analytics.impl.AutoSuperPropertiesFlowProvider -import im.vector.app.features.analytics.plan.SuperProperties -import io.mockk.every -import io.mockk.mockk -import kotlinx.coroutines.flow.MutableSharedFlow - -class FakeAutoSuperPropertiesFlowProvider { - - val flow = MutableSharedFlow() - - val instance = mockk().also { - every { it.superPropertiesFlow } returns flow - } - - suspend fun postSuperProperty(properties: SuperProperties) { - flow.emit(properties) - } -}