From efa3f50aef7589a360d9b542299a125258ca7a58 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 7 Oct 2019 11:01:40 +0200 Subject: [PATCH] Execute 1 request instead of 2 for 3Pid lookup (#3344) --- CHANGES.rst | 1 + .../discovery/DiscoverySettingsViewModel.kt | 72 ++++--------------- 2 files changed, 15 insertions(+), 58 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index faeee9d9c4..3c855f0f19 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -25,6 +25,7 @@ Other changes: Bugfix: - Fix reset password broken UI (#3125) + - Execute 1 request instead of 2 for 3Pid lookup (#3344) Translations: - diff --git a/vector/src/main/java/im/vector/fragments/discovery/DiscoverySettingsViewModel.kt b/vector/src/main/java/im/vector/fragments/discovery/DiscoverySettingsViewModel.kt index d21e69b47d..3b997c3a08 100644 --- a/vector/src/main/java/im/vector/fragments/discovery/DiscoverySettingsViewModel.kt +++ b/vector/src/main/java/im/vector/fragments/discovery/DiscoverySettingsViewModel.kt @@ -368,82 +368,37 @@ class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private v copy(termsNotSigned = false) } - retrieveEmailsBinding() - retrievePhonesBinding() + retrieveBinding() } }) } - private fun retrieveEmailsBinding() { + private fun retrieveBinding() { val linkedMailsInfo = mxSession.myUser.getlinkedEmails() - val knownEmailList = linkedMailsInfo.map { it.address } + val knownEmails = linkedMailsInfo.map { it.address } // Note: it will be a list of "email" - val knownMedium = linkedMailsInfo.map { it.medium } + val knownEmailMedium = linkedMailsInfo.map { it.medium } - setState { - copy( - emailList = Success(knownEmailList.map { PidInfo(it, Loading()) }) - ) - } - - mxSession.identityServerManager.lookup3Pids(knownEmailList, - knownMedium, - object : ApiCallback> { - override fun onSuccess(info: List) { - setState { - copy( - emailList = Success(toPidInfoList(knownEmailList, info)) - ) - } - } - - override fun onUnexpectedError(e: Exception) { - if (e is TermsNotSignedException) { - setState { - copy(termsNotSigned = true) - } - } - onError(e) - } - - override fun onNetworkError(e: Exception) { - onError(e) - } - - override fun onMatrixError(e: MatrixError) { - onError(Throwable(e.message)) - } - - private fun onError(e: Throwable) { - setState { - copy( - emailList = Success(knownEmailList.map { PidInfo(it, Fail(e)) }) - ) - } - } - - }) - } - - private fun retrievePhonesBinding() { val linkedMsisdnsInfo = mxSession.myUser.getlinkedPhoneNumbers() val knownMsisdns = linkedMsisdnsInfo.map { it.address } // Note: it will be a list of "msisdn" - val knownMedium = linkedMsisdnsInfo.map { it.medium } + val knownMsisdnMedium = linkedMsisdnsInfo.map { it.medium } setState { copy( + emailList = Success(knownEmails.map { PidInfo(it, Loading()) }), phoneNumbersList = Success(knownMsisdns.map { PidInfo(it, Loading()) }) ) } - mxSession.identityServerManager.lookup3Pids(knownMsisdns, - knownMedium, + mxSession.identityServerManager.lookup3Pids(knownEmails + knownMsisdns, + knownEmailMedium + knownMsisdnMedium, object : ApiCallback> { override fun onSuccess(info: List) { setState { copy( - phoneNumbersList = Success(toPidInfoList(knownMsisdns, info)) + emailList = Success(toPidInfoList(knownEmails, info.take(knownEmails.size))), + phoneNumbersList = Success(toPidInfoList(knownMsisdns, info.takeLast(knownMsisdns.size))) ) } } @@ -465,13 +420,15 @@ class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private v onError(Throwable(e.message)) } - fun onError(e: Throwable) { + private fun onError(e: Throwable) { setState { copy( + emailList = Success(knownEmails.map { PidInfo(it, Fail(e)) }), phoneNumbersList = Success(knownMsisdns.map { PidInfo(it, Fail(e)) }) ) } } + }) } @@ -480,8 +437,7 @@ class DiscoverySettingsViewModel(initialState: DiscoverySettingsState, private v val hasMatrixId = matrixIds[addressList.indexOf(it)].isNotBlank() PidInfo( value = it, - isShared = Success(PidInfo.SharedState.SHARED.takeIf { hasMatrixId } - ?: PidInfo.SharedState.NOT_SHARED) + isShared = Success(PidInfo.SharedState.SHARED.takeIf { hasMatrixId } ?: PidInfo.SharedState.NOT_SHARED) ) } }