From c807e52ad9e404f41341b7a9c025db95540a9cbc Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Thu, 6 Jul 2023 16:13:04 -0400 Subject: [PATCH] Support CDSI ignore the useCompat flag. --- .../contacts/sync/ContactDiscoveryRefreshV2.kt | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/contacts/sync/ContactDiscoveryRefreshV2.kt b/app/src/main/java/org/thoughtcrime/securesms/contacts/sync/ContactDiscoveryRefreshV2.kt index 716621cfc3..78e280c7ca 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/contacts/sync/ContactDiscoveryRefreshV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/contacts/sync/ContactDiscoveryRefreshV2.kt @@ -170,7 +170,7 @@ object ContactDiscoveryRefreshV2 { val registeredIds: MutableSet = mutableSetOf() val rewrites: MutableMap = mutableMapOf() - if (useCompat) { + if (useCompat && response.isCompatResponse()) { val transformed: Map = response.results.mapValues { entry -> entry.value.aci.orElse(null) } val fuzzyOutput: OutputResult = FuzzyPhoneNumberHelper.generateOutput(transformed, fuzzyInput) @@ -196,6 +196,10 @@ object ContactDiscoveryRefreshV2 { SignalDatabase.recipients.bulkUpdatedRegisteredStatus(aciMap, inactiveIds) stopwatch.split("update-registered") } else { + if (useCompat) { + Log.w(TAG, "Was told to useCompat, but the server responded with a non-compat response! Assuming the server has shut off compat mode.") + } + val transformed: Map = response.results.mapValues { entry -> CdsV2Result(entry.value.pni, entry.value.aci.orElse(null)) } val fuzzyOutput: OutputResult = FuzzyPhoneNumberHelper.generateOutput(transformed, fuzzyInput) @@ -284,4 +288,13 @@ object ContactDiscoveryRefreshV2 { val nearestThousand = (this.toDouble() / 1000).roundToInt() return "~${nearestThousand}k" } + + /** + * Responses that respect useCompat will have an ACI for every user. If it doesn't, it means is a PNP response where some accounts may only have PNI's. + * There may come a day when we request compat mode but the server refuses to allow it, so we need to be able to detect when that happens to fallback + * to the PNP behavior. + */ + private fun CdsiV2Service.Response.isCompatResponse(): Boolean { + return this.results.values.all { it.hasAci() } + } }