diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/terms/TermsService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/terms/TermsService.kt index 9898923003..e64cf1872e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/terms/TermsService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/terms/TermsService.kt @@ -16,15 +16,12 @@ package org.matrix.android.sdk.api.session.terms +import org.matrix.android.sdk.internal.session.terms.TermsResponse + interface TermsService { enum class ServiceType { IntegrationManager, - IdentityService, - - /** - * It's only possible to use this value for [getTerms] - */ - Homeserver + IdentityService } suspend fun getTerms(serviceType: ServiceType, baseUrl: String): GetTermsResponse @@ -33,4 +30,10 @@ interface TermsService { baseUrl: String, agreedUrls: List, token: String?) + + /** + * Get the homeserver terms, from the register API. + * Will be updated once https://github.com/matrix-org/matrix-doc/pull/3012 will be implemented. + */ + suspend fun getHomeserverTerms(baseUrl: String): TermsResponse } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/terms/DefaultTermsService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/terms/DefaultTermsService.kt index 5cdef3db0a..c52c6a404e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/terms/DefaultTermsService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/terms/DefaultTermsService.kt @@ -51,51 +51,34 @@ internal class DefaultTermsService @Inject constructor( override suspend fun getTerms(serviceType: TermsService.ServiceType, baseUrl: String): GetTermsResponse { - return if (serviceType == TermsService.ServiceType.Homeserver) { - getHomeserverTerms(baseUrl) - } else { - val url = buildUrl(baseUrl, serviceType) - val termsResponse = executeRequest(null) { - termsAPI.getTerms("${url}terms") - } - GetTermsResponse(termsResponse, getAlreadyAcceptedTermUrlsFromAccountData()) + val url = buildUrl(baseUrl, serviceType) + val termsResponse = executeRequest(null) { + termsAPI.getTerms("${url}terms") } + return GetTermsResponse(termsResponse, getAlreadyAcceptedTermUrlsFromAccountData()) } /** * We use a trick here to get the homeserver T&C, we use the register API */ - private suspend fun getHomeserverTerms(baseUrl: String): GetTermsResponse { + override suspend fun getHomeserverTerms(baseUrl: String): TermsResponse { return try { executeRequest(null) { termsAPI.register(baseUrl + NetworkConstants.URI_API_PREFIX_PATH_R0 + "register") } // Return empty result if it succeed, but it should never happen - buildEmptyGetTermsResponse() + TermsResponse() } catch (throwable: Throwable) { @Suppress("UNCHECKED_CAST") - ((throwable.toRegistrationFlowResponse() - ?.params?.get(LoginFlowTypes.TERMS) as? JsonDict) - ?.get("policies") as? JsonDict) - ?.let { dict -> - GetTermsResponse( - serverResponse = TermsResponse( - policies = dict - ), - alreadyAcceptedTermUrls = emptySet() - ) - } - ?: buildEmptyGetTermsResponse() + TermsResponse( + policies = (throwable.toRegistrationFlowResponse() + ?.params + ?.get(LoginFlowTypes.TERMS) as? JsonDict) + ?.get("policies") as? JsonDict + ) } } - private fun buildEmptyGetTermsResponse(): GetTermsResponse { - return GetTermsResponse( - serverResponse = TermsResponse(), - alreadyAcceptedTermUrls = emptySet() - ) - } - override suspend fun agreeToTerms(serviceType: TermsService.ServiceType, baseUrl: String, agreedUrls: List, @@ -133,8 +116,6 @@ internal class DefaultTermsService @Inject constructor( val servicePath = when (serviceType) { TermsService.ServiceType.IntegrationManager -> NetworkConstants.URI_INTEGRATION_MANAGER_PATH TermsService.ServiceType.IdentityService -> NetworkConstants.URI_IDENTITY_PATH_V2 - TermsService.ServiceType.Homeserver -> - error("You cannot use this API with parameter TermsService.ServiceType.Homeserver") } return "${baseUrl.ensureTrailingSlash()}$servicePath" } diff --git a/vector/src/main/java/im/vector/app/features/discovery/Extensions.kt b/vector/src/main/java/im/vector/app/features/discovery/Extensions.kt index 1360c9e95d..24d675695b 100644 --- a/vector/src/main/java/im/vector/app/features/discovery/Extensions.kt +++ b/vector/src/main/java/im/vector/app/features/discovery/Extensions.kt @@ -19,25 +19,27 @@ package im.vector.app.features.discovery import im.vector.app.core.utils.ensureProtocol import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.terms.TermsService +import org.matrix.android.sdk.internal.session.terms.TermsResponse suspend fun Session.fetchIdentityServerWithTerms(userLanguage: String): ServerAndPolicies? { - val identityServerUrl = identityService().getCurrentIdentityServerUrl() - return identityServerUrl?.let { - fetchTerms(it, TermsService.ServiceType.IdentityService, userLanguage) - } + return identityService().getCurrentIdentityServerUrl() + ?.let { identityServerUrl -> + val termsResponse = getTerms(TermsService.ServiceType.IdentityService, identityServerUrl.ensureProtocol()) + .serverResponse + buildServerAndPolicies(identityServerUrl, termsResponse, userLanguage) + } } suspend fun Session.fetchHomeserverWithTerms(userLanguage: String): ServerAndPolicies { val homeserverUrl = sessionParams.homeServerUrl - return fetchTerms(homeserverUrl, TermsService.ServiceType.Homeserver, userLanguage) + val terms = getHomeserverTerms(homeserverUrl.ensureProtocol()) + return buildServerAndPolicies(homeserverUrl, terms, userLanguage) } -private suspend fun Session.fetchTerms(serviceUrl: String, - serviceType: TermsService.ServiceType, - userLanguage: String): ServerAndPolicies { - val terms = getTerms(serviceType, serviceUrl.ensureProtocol()) - .serverResponse - .getLocalizedTerms(userLanguage) +private fun buildServerAndPolicies(serviceUrl: String, + termsResponse: TermsResponse, + userLanguage: String): ServerAndPolicies { + val terms = termsResponse.getLocalizedTerms(userLanguage) val policyUrls = terms.mapNotNull { val name = it.localizedName ?: it.policyName val url = it.localizedUrl diff --git a/vector/src/main/java/im/vector/app/features/terms/ReviewTermsFragment.kt b/vector/src/main/java/im/vector/app/features/terms/ReviewTermsFragment.kt index 6381854433..cb76e5b31f 100644 --- a/vector/src/main/java/im/vector/app/features/terms/ReviewTermsFragment.kt +++ b/vector/src/main/java/im/vector/app/features/terms/ReviewTermsFragment.kt @@ -53,7 +53,6 @@ class ReviewTermsFragment @Inject constructor( termsController.description = when (reviewTermsViewModel.termsArgs.type) { TermsService.ServiceType.IdentityService -> getString(R.string.terms_description_for_identity_server) TermsService.ServiceType.IntegrationManager -> getString(R.string.terms_description_for_integration_manager) - TermsService.ServiceType.Homeserver -> error("Homeserver is not supported here") } termsController.listener = this