Skip to content

Commit

Permalink
Legals: improve API to get homeserver terms
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarty committed Dec 9, 2021
1 parent c69e155 commit 49721ed
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand Down Expand Up @@ -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"
}
Expand Down
24 changes: 13 additions & 11 deletions vector/src/main/java/im/vector/app/features/discovery/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 49721ed

Please sign in to comment.