Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Execute 1 request instead of 2 for 3Pid lookup (#3344)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarty committed Oct 7, 2019
1 parent 1ef06b1 commit efa3f50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Other changes:

Bugfix:
- Fix reset password broken UI (#3125)
- Execute 1 request instead of 2 for 3Pid lookup (#3344)

Translations:
-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<String>> {
override fun onSuccess(info: List<String>) {
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<List<String>> {
override fun onSuccess(info: List<String>) {
setState {
copy(
phoneNumbersList = Success(toPidInfoList(knownMsisdns, info))
emailList = Success(toPidInfoList(knownEmails, info.take(knownEmails.size))),
phoneNumbersList = Success(toPidInfoList(knownMsisdns, info.takeLast(knownMsisdns.size)))
)
}
}
Expand All @@ -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)) })
)
}
}

})
}

Expand All @@ -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)
)
}
}
Expand Down

0 comments on commit efa3f50

Please sign in to comment.