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
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<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,49 +51,32 @@ 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())
}
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()
TermsResponse(
policies = (throwable.toRegistrationFlowResponse()
?.params
?.get(LoginFlowTypes.TERMS) as? JsonDict)
?.get("policies") as? JsonDict
)
}
?: buildEmptyGetTermsResponse()
}
}
private fun buildEmptyGetTermsResponse(): GetTermsResponse {
return GetTermsResponse(
serverResponse = TermsResponse(),
alreadyAcceptedTermUrls = emptySet()
)
}
override suspend fun agreeToTerms(serviceType: TermsService.ServiceType,
@ -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"
}

View file

@ -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,
private fun buildServerAndPolicies(serviceUrl: String,
termsResponse: TermsResponse,
userLanguage: String): ServerAndPolicies {
val terms = getTerms(serviceType, serviceUrl.ensureProtocol())
.serverResponse
.getLocalizedTerms(userLanguage)
val terms = termsResponse.getLocalizedTerms(userLanguage)
val policyUrls = terms.mapNotNull {
val name = it.localizedName ?: it.policyName
val url = it.localizedUrl

View file

@ -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