Legals: improve API to get homeserver terms

This commit is contained in:
Benoit Marty 2021-12-09 12:15:19 +01:00 committed by Benoit Marty
parent 6ba5c7af27
commit bf7907a119
4 changed files with 34 additions and 49 deletions

View file

@ -16,15 +16,12 @@
package org.matrix.android.sdk.api.session.terms package org.matrix.android.sdk.api.session.terms
import org.matrix.android.sdk.internal.session.terms.TermsResponse
interface TermsService { interface TermsService {
enum class ServiceType { enum class ServiceType {
IntegrationManager, IntegrationManager,
IdentityService, IdentityService
/**
* It's only possible to use this value for [getTerms]
*/
Homeserver
} }
suspend fun getTerms(serviceType: ServiceType, baseUrl: String): GetTermsResponse suspend fun getTerms(serviceType: ServiceType, baseUrl: String): GetTermsResponse
@ -33,4 +30,10 @@ interface TermsService {
baseUrl: String, baseUrl: String,
agreedUrls: List<String>, agreedUrls: List<String>,
token: String?) 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
} }

View file

@ -51,51 +51,34 @@ internal class DefaultTermsService @Inject constructor(
override suspend fun getTerms(serviceType: TermsService.ServiceType, override suspend fun getTerms(serviceType: TermsService.ServiceType,
baseUrl: String): GetTermsResponse { baseUrl: String): GetTermsResponse {
return if (serviceType == TermsService.ServiceType.Homeserver) { val url = buildUrl(baseUrl, serviceType)
getHomeserverTerms(baseUrl) val termsResponse = executeRequest(null) {
} else { termsAPI.getTerms("${url}terms")
val url = buildUrl(baseUrl, serviceType)
val termsResponse = executeRequest(null) {
termsAPI.getTerms("${url}terms")
}
GetTermsResponse(termsResponse, getAlreadyAcceptedTermUrlsFromAccountData())
} }
return GetTermsResponse(termsResponse, getAlreadyAcceptedTermUrlsFromAccountData())
} }
/** /**
* We use a trick here to get the homeserver T&C, we use the register API * 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 { return try {
executeRequest(null) { executeRequest(null) {
termsAPI.register(baseUrl + NetworkConstants.URI_API_PREFIX_PATH_R0 + "register") termsAPI.register(baseUrl + NetworkConstants.URI_API_PREFIX_PATH_R0 + "register")
} }
// Return empty result if it succeed, but it should never happen // Return empty result if it succeed, but it should never happen
buildEmptyGetTermsResponse() TermsResponse()
} catch (throwable: Throwable) { } catch (throwable: Throwable) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
((throwable.toRegistrationFlowResponse() TermsResponse(
?.params?.get(LoginFlowTypes.TERMS) as? JsonDict) policies = (throwable.toRegistrationFlowResponse()
?.get("policies") as? JsonDict) ?.params
?.let { dict -> ?.get(LoginFlowTypes.TERMS) as? JsonDict)
GetTermsResponse( ?.get("policies") as? JsonDict
serverResponse = TermsResponse( )
policies = dict
),
alreadyAcceptedTermUrls = emptySet()
)
}
?: buildEmptyGetTermsResponse()
} }
} }
private fun buildEmptyGetTermsResponse(): GetTermsResponse {
return GetTermsResponse(
serverResponse = TermsResponse(),
alreadyAcceptedTermUrls = emptySet()
)
}
override suspend fun agreeToTerms(serviceType: TermsService.ServiceType, override suspend fun agreeToTerms(serviceType: TermsService.ServiceType,
baseUrl: String, baseUrl: String,
agreedUrls: List<String>, agreedUrls: List<String>,
@ -133,8 +116,6 @@ internal class DefaultTermsService @Inject constructor(
val servicePath = when (serviceType) { val servicePath = when (serviceType) {
TermsService.ServiceType.IntegrationManager -> NetworkConstants.URI_INTEGRATION_MANAGER_PATH TermsService.ServiceType.IntegrationManager -> NetworkConstants.URI_INTEGRATION_MANAGER_PATH
TermsService.ServiceType.IdentityService -> NetworkConstants.URI_IDENTITY_PATH_V2 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" return "${baseUrl.ensureTrailingSlash()}$servicePath"
} }

View file

@ -19,25 +19,27 @@ package im.vector.app.features.discovery
import im.vector.app.core.utils.ensureProtocol import im.vector.app.core.utils.ensureProtocol
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.terms.TermsService 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? { suspend fun Session.fetchIdentityServerWithTerms(userLanguage: String): ServerAndPolicies? {
val identityServerUrl = identityService().getCurrentIdentityServerUrl() return identityService().getCurrentIdentityServerUrl()
return identityServerUrl?.let { ?.let { identityServerUrl ->
fetchTerms(it, TermsService.ServiceType.IdentityService, userLanguage) val termsResponse = getTerms(TermsService.ServiceType.IdentityService, identityServerUrl.ensureProtocol())
} .serverResponse
buildServerAndPolicies(identityServerUrl, termsResponse, userLanguage)
}
} }
suspend fun Session.fetchHomeserverWithTerms(userLanguage: String): ServerAndPolicies { suspend fun Session.fetchHomeserverWithTerms(userLanguage: String): ServerAndPolicies {
val homeserverUrl = sessionParams.homeServerUrl 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, private fun buildServerAndPolicies(serviceUrl: String,
serviceType: TermsService.ServiceType, termsResponse: TermsResponse,
userLanguage: String): ServerAndPolicies { userLanguage: String): ServerAndPolicies {
val terms = getTerms(serviceType, serviceUrl.ensureProtocol()) val terms = termsResponse.getLocalizedTerms(userLanguage)
.serverResponse
.getLocalizedTerms(userLanguage)
val policyUrls = terms.mapNotNull { val policyUrls = terms.mapNotNull {
val name = it.localizedName ?: it.policyName val name = it.localizedName ?: it.policyName
val url = it.localizedUrl val url = it.localizedUrl

View file

@ -53,7 +53,6 @@ class ReviewTermsFragment @Inject constructor(
termsController.description = when (reviewTermsViewModel.termsArgs.type) { termsController.description = when (reviewTermsViewModel.termsArgs.type) {
TermsService.ServiceType.IdentityService -> getString(R.string.terms_description_for_identity_server) TermsService.ServiceType.IdentityService -> getString(R.string.terms_description_for_identity_server)
TermsService.ServiceType.IntegrationManager -> getString(R.string.terms_description_for_integration_manager) TermsService.ServiceType.IntegrationManager -> getString(R.string.terms_description_for_integration_manager)
TermsService.ServiceType.Homeserver -> error("Homeserver is not supported here")
} }
termsController.listener = this termsController.listener = this