From 3146ce91aa227ea5955370b5b5296a15a3544a1a Mon Sep 17 00:00:00 2001
From: Taras <tarassmakula@gmail.com>
Date: Tue, 15 Mar 2022 16:33:27 +0200
Subject: [PATCH] Change to new token validation implementation

---
 .../java/com/futo/circles/core/Constants.kt   |  3 -
 .../com/futo/circles/di/DataSourceModule.kt   |  2 +-
 .../main/java/com/futo/circles/di/Modules.kt  |  2 +-
 .../extensions/MatrixTermsExtension.kt        | 80 +------------------
 .../sign_up/data_source/SignUpDataSource.kt   |  2 +-
 .../validate_token/ValidateTokenViewModel.kt  |  4 +-
 .../data_source/ValidateTokenDataSource.kt    | 32 ++------
 7 files changed, 13 insertions(+), 112 deletions(-)

diff --git a/app/src/main/java/com/futo/circles/core/Constants.kt b/app/src/main/java/com/futo/circles/core/Constants.kt
index 72c29da76..3da407607 100644
--- a/app/src/main/java/com/futo/circles/core/Constants.kt
+++ b/app/src/main/java/com/futo/circles/core/Constants.kt
@@ -7,8 +7,5 @@ const val DEFAULT_USER_PREFIX = "@notices:"
 const val DEFAULT_ERROR_MESSAGE = "Something went wrong"
 const val AUTH_EXCEPTION_REASON_KEY = "reason"
 
-const val PENDING_SESSION_PROPERTY_NAME = "pendingSessionData"
-const val CURRENT_SESSION_PROPERTY_NAME = "currentSession"
-
 const val DEFAULT_TERMS_NAME = "Terms and Conditions"
 const val TERMS_URL_EXTENSION = "_matrix/consent"
diff --git a/app/src/main/java/com/futo/circles/di/DataSourceModule.kt b/app/src/main/java/com/futo/circles/di/DataSourceModule.kt
index 0a0161d10..a438c0112 100644
--- a/app/src/main/java/com/futo/circles/di/DataSourceModule.kt
+++ b/app/src/main/java/com/futo/circles/di/DataSourceModule.kt
@@ -30,7 +30,7 @@ val dataSourceModule = module {
 
     single { SignUpDataSource(get()) }
 
-    factory { ValidateTokenDataSource(get(), get()) }
+    factory { ValidateTokenDataSource(get()) }
 
     factory { SelectSignUpTypeDataSource(get()) }
 
diff --git a/app/src/main/java/com/futo/circles/di/Modules.kt b/app/src/main/java/com/futo/circles/di/Modules.kt
index fb7b7781f..d7324146b 100644
--- a/app/src/main/java/com/futo/circles/di/Modules.kt
+++ b/app/src/main/java/com/futo/circles/di/Modules.kt
@@ -1,3 +1,3 @@
 package com.futo.circles.di
 
-val applicationModules = listOf(uiModule, dataSourceModule, retrofitModule)
\ No newline at end of file
+val applicationModules = listOf(uiModule, dataSourceModule)
\ No newline at end of file
diff --git a/app/src/main/java/com/futo/circles/extensions/MatrixTermsExtension.kt b/app/src/main/java/com/futo/circles/extensions/MatrixTermsExtension.kt
index ac6ff117b..c86bc569e 100644
--- a/app/src/main/java/com/futo/circles/extensions/MatrixTermsExtension.kt
+++ b/app/src/main/java/com/futo/circles/extensions/MatrixTermsExtension.kt
@@ -4,86 +4,10 @@ package com.futo.circles.extensions
 import com.futo.circles.core.DEFAULT_TERMS_NAME
 import com.futo.circles.model.TermsListItem
 import org.matrix.android.sdk.api.auth.registration.TermPolicies
-import org.matrix.android.sdk.internal.auth.registration.LocalizedFlowDataLoginTerms
-
-
-private fun TermPolicies.toLocalizedLoginTerms(
-    language: String = "en"
-): List<LocalizedFlowDataLoginTerms> {
-    val result = ArrayList<LocalizedFlowDataLoginTerms>()
-
-    val policies = get("policies")
-    if (policies is Map<*, *>) {
-        policies.keys.forEach { policyName ->
-            val localizedFlowDataLoginTerms = LocalizedFlowDataLoginTerms()
-            localizedFlowDataLoginTerms.policyName = policyName as String
-
-            val policy = policies[policyName]
-
-            // Enter this policy
-            if (policy is Map<*, *>) {
-                // Version
-                localizedFlowDataLoginTerms.version = policy["version"] as String?
-
-                var defaultLanguageUrlAndName: UrlAndName? = null
-                var firstUrlAndName: UrlAndName? = null
-
-                // Search for language
-                policy.keys.forEach { policyKey ->
-                    when (policyKey) {
-                        "version" -> Unit // Ignore
-                        language -> {
-                            // We found default language
-                            defaultLanguageUrlAndName = extractUrlAndName(policy[policyKey])
-                        }
-                        else -> {
-                            if (firstUrlAndName == null) {
-                                // Get at least some data
-                                firstUrlAndName = extractUrlAndName(policy[policyKey])
-                            }
-                        }
-                    }
-                }
-
-                // Copy found language data by priority
-                when {
-                    defaultLanguageUrlAndName != null -> {
-                        localizedFlowDataLoginTerms.localizedUrl = defaultLanguageUrlAndName!!.url
-                        localizedFlowDataLoginTerms.localizedName = defaultLanguageUrlAndName!!.name
-                    }
-                    firstUrlAndName != null -> {
-                        localizedFlowDataLoginTerms.localizedUrl = firstUrlAndName!!.url
-                        localizedFlowDataLoginTerms.localizedName = firstUrlAndName!!.name
-                    }
-                }
-            }
-
-            result.add(localizedFlowDataLoginTerms)
-        }
-    }
-
-    return result
-}
-
-private fun extractUrlAndName(policyData: Any?): UrlAndName? {
-    if (policyData is Map<*, *>) {
-        val url = policyData["url"] as String?
-        val name = policyData["name"] as String?
-
-        if (url != null && name != null) {
-            return UrlAndName(url, name)
-        }
-    }
-    return null
-}
-
-data class UrlAndName(
-    val url: String,
-    val name: String
-)
+import org.matrix.android.sdk.api.auth.toLocalizedLoginTerms
 
 fun TermPolicies.toTermsListItems() =
-    toLocalizedLoginTerms().mapIndexed { i, item ->
+    toLocalizedLoginTerms("en").mapIndexed { i, item ->
         TermsListItem(
             i,
             item.localizedName ?: item.policyName ?: DEFAULT_TERMS_NAME,
diff --git a/app/src/main/java/com/futo/circles/feature/sign_up/data_source/SignUpDataSource.kt b/app/src/main/java/com/futo/circles/feature/sign_up/data_source/SignUpDataSource.kt
index 04a26cf58..67cbbb4f1 100644
--- a/app/src/main/java/com/futo/circles/feature/sign_up/data_source/SignUpDataSource.kt
+++ b/app/src/main/java/com/futo/circles/feature/sign_up/data_source/SignUpDataSource.kt
@@ -68,7 +68,7 @@ class SignUpDataSource(
         val event = when (stage) {
             is Stage.Email -> NavigationEvents.VerifyEmail
             is Stage.Terms -> NavigationEvents.AcceptTerm
-            is Stage.Other -> NavigationEvents.TokenValidation
+            is Stage.Token -> NavigationEvents.TokenValidation
             else -> throw IllegalArgumentException("Not supported stage $stage")
         }
 
diff --git a/app/src/main/java/com/futo/circles/feature/validate_token/ValidateTokenViewModel.kt b/app/src/main/java/com/futo/circles/feature/validate_token/ValidateTokenViewModel.kt
index 437459369..2f8c1d156 100644
--- a/app/src/main/java/com/futo/circles/feature/validate_token/ValidateTokenViewModel.kt
+++ b/app/src/main/java/com/futo/circles/feature/validate_token/ValidateTokenViewModel.kt
@@ -5,13 +5,13 @@ import com.futo.circles.core.SingleEventLiveData
 import com.futo.circles.extensions.Response
 import com.futo.circles.extensions.launchBg
 import com.futo.circles.feature.validate_token.data_source.ValidateTokenDataSource
-import okhttp3.ResponseBody
+import org.matrix.android.sdk.api.auth.registration.RegistrationResult
 
 class ValidateTokenViewModel(
     private val dataSource: ValidateTokenDataSource
 ) : ViewModel() {
 
-    val validateLiveData = SingleEventLiveData<Response<ResponseBody>>()
+    val validateLiveData = SingleEventLiveData<Response<RegistrationResult>>()
 
     fun validateToken(token: String) {
         launchBg {
diff --git a/app/src/main/java/com/futo/circles/feature/validate_token/data_source/ValidateTokenDataSource.kt b/app/src/main/java/com/futo/circles/feature/validate_token/data_source/ValidateTokenDataSource.kt
index 7dc5cb89a..418ca12a7 100644
--- a/app/src/main/java/com/futo/circles/feature/validate_token/data_source/ValidateTokenDataSource.kt
+++ b/app/src/main/java/com/futo/circles/feature/validate_token/data_source/ValidateTokenDataSource.kt
@@ -2,42 +2,22 @@ package com.futo.circles.feature.validate_token.data_source
 
 import com.futo.circles.extensions.Response
 import com.futo.circles.extensions.createResult
-import com.futo.circles.extensions.getPendingSignUpSessionId
 import com.futo.circles.feature.sign_up.data_source.SignUpDataSource
-import com.futo.circles.io.request.ValidateSignUpTokenRequestBody
-import com.futo.circles.io.request.ValidateTokenRequestParams
-import com.futo.circles.io.service.CirclesSignUpService
 import com.futo.circles.provider.MatrixInstanceProvider
-import okhttp3.ResponseBody
-import org.matrix.android.sdk.api.auth.registration.Stage
+import org.matrix.android.sdk.api.auth.registration.RegistrationResult
 
 class ValidateTokenDataSource(
-    private val service: CirclesSignUpService,
     private val signUpDataSource: SignUpDataSource
 ) {
 
-    private val pendingSessionId by lazy {
-        MatrixInstanceProvider.matrix.authenticationService()
-            .getRegistrationWizard().getPendingSignUpSessionId()
+    private val wizard by lazy {
+        MatrixInstanceProvider.matrix.authenticationService().getRegistrationWizard()
     }
 
-    suspend fun validateToken(token: String): Response<ResponseBody> {
-        val result = createResult {
-            val type = (signUpDataSource.currentStage as? Stage.Other)?.type
-                ?: throw IllegalArgumentException()
+    suspend fun validateToken(token: String): Response<RegistrationResult> {
+        val result = createResult { wizard.registrationToken(token) }
 
-            service.validateSignUpToken(
-                ValidateSignUpTokenRequestBody(
-                    ValidateTokenRequestParams(
-                        type = type,
-                        token = token,
-                        session = pendingSessionId
-                    )
-                )
-            )
-        }
-
-        (result as? Response.Success)?.let { signUpDataSource.stageCompleted(null) }
+        (result as? Response.Success)?.let { signUpDataSource.stageCompleted(result.data) }
 
         return result
     }
-- 
GitLab