Skip to content

Commit

Permalink
Merge pull request #4587 from vector-im/feature/bma/is_consent
Browse files Browse the repository at this point in the history
Iterate on user consent dialog for identity server
  • Loading branch information
bmarty authored Nov 30, 2021
2 parents 8fd5e42 + 9d6ac08 commit 3f39c5d
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 67 deletions.
1 change: 1 addition & 0 deletions changelog.d/4577.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Iterate on the consent dialog of the identity server.
41 changes: 33 additions & 8 deletions vector/src/main/java/im/vector/app/core/utils/Dialogs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
package im.vector.app.core.utils

import android.content.Context
import android.text.method.LinkMovementMethod
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.TextView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import im.vector.app.R
import im.vector.app.features.discovery.IdentityServerWithTerms
import me.gujun.android.span.link
import me.gujun.android.span.span

/**
* Open a web view above the current activity.
Expand All @@ -40,16 +45,36 @@ fun Context.displayInWebView(url: String) {
.show()
}

fun Context.showIdentityServerConsentDialog(configuredIdentityServer: String?, policyLinkCallback: () -> Unit, consentCallBack: (() -> Unit)) {
fun Context.showIdentityServerConsentDialog(identityServerWithTerms: IdentityServerWithTerms?,
consentCallBack: (() -> Unit)) {
// Build the message
val content = span {
+getString(R.string.identity_server_consent_dialog_content_3)
+"\n\n"
if (identityServerWithTerms?.policies?.isNullOrEmpty() == false) {
span {
textStyle = "bold"
text = getString(R.string.settings_privacy_policy)
}
identityServerWithTerms.policies.forEach {
+"\n"
// Use the url as the text too
link(it.url, it.url)
}
+"\n\n"
}
+getString(R.string.identity_server_consent_dialog_content_question)
}
MaterialAlertDialogBuilder(this)
.setTitle(getString(R.string.identity_server_consent_dialog_title_2, configuredIdentityServer ?: ""))
.setMessage(R.string.identity_server_consent_dialog_content_2)
.setPositiveButton(R.string.yes) { _, _ ->
.setTitle(getString(R.string.identity_server_consent_dialog_title_2, identityServerWithTerms?.serverUrl.orEmpty()))
.setMessage(content)
.setPositiveButton(R.string.reactions_agree) { _, _ ->
consentCallBack.invoke()
}
.setNeutralButton(R.string.identity_server_consent_dialog_neutral_policy) { _, _ ->
policyLinkCallback.invoke()
}
.setNegativeButton(R.string.no, null)
.setNegativeButton(R.string.action_not_now, null)
.show()
.apply {
// Make the link(s) clickable. Must be called after show()
(findViewById(android.R.id.message) as? TextView)?.movementMethod = LinkMovementMethod.getInstance()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ import im.vector.app.core.platform.VectorViewModelAction
sealed class ContactsBookAction : VectorViewModelAction {
data class FilterWith(val filter: String) : ContactsBookAction()
data class OnlyBoundContacts(val onlyBoundContacts: Boolean) : ContactsBookAction()
object UserConsentRequest : ContactsBookAction()
object UserConsentGranted : ContactsBookAction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import com.airbnb.mvrx.activityViewModel
import com.airbnb.mvrx.withState
import im.vector.app.core.extensions.cleanup
import im.vector.app.core.extensions.configureWith
import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.extensions.hideKeyboard
import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.core.utils.showIdentityServerConsentDialog
import im.vector.app.databinding.FragmentContactsBookBinding
import im.vector.app.features.navigation.SettingsActivityPayload
import im.vector.app.features.userdirectory.PendingSelection
import im.vector.app.features.userdirectory.UserListAction
import im.vector.app.features.userdirectory.UserListSharedAction
Expand Down Expand Up @@ -68,22 +68,27 @@ class ContactsBookFragment @Inject constructor(
setupConsentView()
setupOnlyBoundContactsView()
setupCloseView()
contactsBookViewModel.observeViewEvents {
when (it) {
is ContactsBookViewEvents.Failure -> showFailure(it.throwable)
is ContactsBookViewEvents.OnPoliciesRetrieved -> showConsentDialog(it)
}.exhaustive
}
}

private fun setupConsentView() {
views.phoneBookSearchForMatrixContacts.setOnClickListener {
withState(contactsBookViewModel) { state ->
requireContext().showIdentityServerConsentDialog(
state.identityServerUrl,
policyLinkCallback = {
navigator.openSettings(requireContext(), SettingsActivityPayload.DiscoverySettings(expandIdentityPolicies = true))
},
consentCallBack = { contactsBookViewModel.handle(ContactsBookAction.UserConsentGranted) }
)
}
contactsBookViewModel.handle(ContactsBookAction.UserConsentRequest)
}
}

private fun showConsentDialog(event: ContactsBookViewEvents.OnPoliciesRetrieved) {
requireContext().showIdentityServerConsentDialog(
event.identityServerWithTerms,
consentCallBack = { contactsBookViewModel.handle(ContactsBookAction.UserConsentGranted) }
)
}

private fun setupOnlyBoundContactsView() {
views.phoneBookOnlyBoundContacts.checkedChanges()
.onEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.app.features.contactsbook

import im.vector.app.core.platform.VectorViewEvents
import im.vector.app.features.discovery.IdentityServerWithTerms

sealed class ContactsBookViewEvents : VectorViewEvents {
data class Failure(val throwable: Throwable) : ContactsBookViewEvents()
data class OnPoliciesRetrieved(val identityServerWithTerms: IdentityServerWithTerms?) : ContactsBookViewEvents()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,34 @@

package im.vector.app.features.contactsbook

import androidx.lifecycle.viewModelScope
import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.Success
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import im.vector.app.R
import im.vector.app.core.contacts.ContactsDataSource
import im.vector.app.core.contacts.MappedContact
import im.vector.app.core.di.MavericksAssistedViewModelFactory
import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.platform.EmptyViewEvents
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.core.resources.StringProvider
import im.vector.app.features.discovery.fetchIdentityServerWithTerms
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.identity.IdentityServiceError
import org.matrix.android.sdk.api.session.identity.ThreePid
import timber.log.Timber

class ContactsBookViewModel @AssistedInject constructor(@Assisted
initialState: ContactsBookViewState,
private val contactsDataSource: ContactsDataSource,
private val session: Session) :
VectorViewModel<ContactsBookViewState, ContactsBookAction, EmptyViewEvents>(initialState) {
class ContactsBookViewModel @AssistedInject constructor(
@Assisted initialState: ContactsBookViewState,
private val contactsDataSource: ContactsDataSource,
private val stringProvider: StringProvider,
private val session: Session
) : VectorViewModel<ContactsBookViewState, ContactsBookAction, ContactsBookViewEvents>(initialState) {

@AssistedFactory
interface Factory : MavericksAssistedViewModelFactory<ContactsBookViewModel, ContactsBookViewState> {
Expand Down Expand Up @@ -162,9 +164,22 @@ class ContactsBookViewModel @AssistedInject constructor(@Assisted
is ContactsBookAction.FilterWith -> handleFilterWith(action)
is ContactsBookAction.OnlyBoundContacts -> handleOnlyBoundContacts(action)
ContactsBookAction.UserConsentGranted -> handleUserConsentGranted()
ContactsBookAction.UserConsentRequest -> handleUserConsentRequest()
}.exhaustive
}

private fun handleUserConsentRequest() {
viewModelScope.launch {
val event = try {
val result = session.fetchIdentityServerWithTerms(stringProvider.getString(R.string.resources_language))
ContactsBookViewEvents.OnPoliciesRetrieved(result)
} catch (throwable: Throwable) {
ContactsBookViewEvents.Failure(throwable)
}
_viewEvents.post(event)
}
}

private fun handleUserConsentGranted() {
session.identityService().setUserConsent(true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ class DiscoverySettingsFragment @Inject constructor(
if (newValue) {
withState(viewModel) { state ->
requireContext().showIdentityServerConsentDialog(
state.identityServer.invoke()?.serverUrl,
policyLinkCallback = { viewModel.handle(DiscoverySettingsAction.SetPoliciesExpandState(expanded = true)) },
state.identityServer.invoke(),
consentCallBack = { viewModel.handle(DiscoverySettingsAction.UpdateUserConsent(true)) }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,3 @@ data class DiscoverySettingsState(
val userConsent: Boolean = false,
val isIdentityPolicyUrlsExpanded: Boolean = false
) : MavericksState

data class IdentityServerWithTerms(
val serverUrl: String,
val policies: List<IdentityServerPolicy>
)

data class IdentityServerPolicy(val name: String, val url: String)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.core.resources.StringProvider
import im.vector.app.core.utils.ensureProtocol
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
Expand All @@ -39,7 +38,6 @@ import org.matrix.android.sdk.api.session.identity.IdentityServiceError
import org.matrix.android.sdk.api.session.identity.IdentityServiceListener
import org.matrix.android.sdk.api.session.identity.SharedState
import org.matrix.android.sdk.api.session.identity.ThreePid
import org.matrix.android.sdk.api.session.terms.TermsService
import org.matrix.android.sdk.flow.flow

class DiscoverySettingsViewModel @AssistedInject constructor(
Expand All @@ -56,7 +54,6 @@ class DiscoverySettingsViewModel @AssistedInject constructor(
companion object : MavericksViewModelFactory<DiscoverySettingsViewModel, DiscoverySettingsState> by hiltMavericksViewModelFactory()

private val identityService = session.identityService()
private val termsService: TermsService = session

private val identityServerManagerListener = object : IdentityServiceListener {
override fun onIdentityServerChange() = withState { state ->
Expand Down Expand Up @@ -397,29 +394,14 @@ class DiscoverySettingsViewModel @AssistedInject constructor(
}
}
viewModelScope.launch {
runCatching { fetchIdentityServerWithTerms() }.fold(
runCatching { session.fetchIdentityServerWithTerms(stringProvider.getString(R.string.resources_language)) }.fold(
onSuccess = { setState { copy(identityServer = Success(it)) } },
onFailure = { _viewEvents.post(DiscoverySettingsViewEvents.Failure(it)) }
)
}
}

private suspend fun fetchIdentityServerWithTerms(): IdentityServerWithTerms? {
val identityServerUrl = identityService.getCurrentIdentityServerUrl()
return identityServerUrl?.let {
val terms = termsService.getTerms(TermsService.ServiceType.IdentityService, identityServerUrl.ensureProtocol())
.serverResponse
.getLocalizedTerms(stringProvider.getString(R.string.resources_language))
val policyUrls = terms.mapNotNull {
val name = it.localizedName ?: it.policyName
val url = it.localizedUrl
if (name == null || url == null) {
null
} else {
IdentityServerPolicy(name = name, url = url)
}
}
IdentityServerWithTerms(identityServerUrl, policyUrls)
}
return session.fetchIdentityServerWithTerms(stringProvider.getString(R.string.resources_language))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

suspend fun Session.fetchIdentityServerWithTerms(userLanguage: String): IdentityServerWithTerms? {
val identityServerUrl = identityService().getCurrentIdentityServerUrl()
return identityServerUrl?.let {
val terms = getTerms(TermsService.ServiceType.IdentityService, identityServerUrl.ensureProtocol())
.serverResponse
.getLocalizedTerms(userLanguage)
val policyUrls = terms.mapNotNull {
val name = it.localizedName ?: it.policyName
val url = it.localizedUrl
if (name == null || url == null) {
null
} else {
IdentityServerPolicy(name = name, url = url)
}
}
IdentityServerWithTerms(identityServerUrl, policyUrls)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.app.features.discovery

data class IdentityServerWithTerms(
val serverUrl: String,
val policies: List<IdentityServerPolicy>
)

data class IdentityServerPolicy(
val name: String,
val url: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sealed class UserListAction : VectorViewModelAction {
data class AddPendingSelection(val pendingSelection: PendingSelection) : UserListAction()
data class RemovePendingSelection(val pendingSelection: PendingSelection) : UserListAction()
object ComputeMatrixToLinkForSharing : UserListAction()
object UserConsentRequest : UserListAction()
data class UpdateUserConsent(val consent: Boolean) : UserListAction()
object Resumed : UserListAction()
}
Loading

0 comments on commit 3f39c5d

Please sign in to comment.