Always perform CDSI lookups when starting new chats.

This commit is contained in:
Greyson Parrelli
2024-03-06 12:11:52 -05:00
committed by Alex Hart
parent 184c1b67cc
commit 2e4ac7ede1
15 changed files with 264 additions and 126 deletions

View File

@@ -27,6 +27,7 @@ import org.thoughtcrime.securesms.registration.RegistrationUtil
import org.thoughtcrime.securesms.storage.StorageSyncHelper
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.thoughtcrime.securesms.util.Util
import org.whispersystems.signalservice.api.push.ServiceId
import org.whispersystems.signalservice.api.push.SignalServiceAddress
import org.whispersystems.signalservice.api.util.UuidUtil
import java.io.IOException
@@ -112,6 +113,19 @@ object ContactDiscovery {
}
}
/**
* Looks up the PNI/ACI for an E164. Only creates a recipient if the number is in the CDS directory.
* Use sparingly! This will always use up the user's CDS quota. Always prefer other syncing methods for bulk lookups.
*
* Returns a [LookupResult] if the E164 is in the CDS directory, or null if it is not.
* Important: Just because a user is not in the directory does not mean they are not registered. They could have discoverability off.
*/
@Throws(IOException::class)
@WorkerThread
fun lookupE164(e164: String): LookupResult? {
return ContactDiscoveryRefreshV2.lookupE164(e164)
}
@JvmStatic
@WorkerThread
fun syncRecipientInfoWithSystemContacts(context: Context) {
@@ -278,7 +292,7 @@ object ContactDiscovery {
/**
* Whether or not a session exists with the provided recipient.
*/
fun hasSession(id: RecipientId): Boolean {
private fun hasSession(id: RecipientId): Boolean {
val recipient = Recipient.resolved(id)
if (!recipient.hasServiceId()) {
@@ -295,4 +309,10 @@ object ContactDiscovery {
val registeredIds: Set<RecipientId>,
val rewrites: Map<String, String>
)
data class LookupResult(
val recipientId: RecipientId,
val pni: ServiceId.PNI,
val aci: ServiceId.ACI?
)
}

View File

@@ -86,6 +86,42 @@ object ContactDiscoveryRefreshV2 {
}
}
@Throws(IOException::class)
@WorkerThread
@Synchronized
fun lookupE164(e164: String): ContactDiscovery.LookupResult? {
val response: CdsiV2Service.Response = try {
ApplicationDependencies.getSignalServiceAccountManager().getRegisteredUsersWithCdsi(
emptySet(),
setOf(e164),
SignalDatabase.recipients.getAllServiceIdProfileKeyPairs(),
Optional.empty(),
BuildConfig.CDSI_MRENCLAVE,
10_000,
if (FeatureFlags.useLibsignalNetForCdsiLookup()) BuildConfig.LIBSIGNAL_NET_ENV else null
) {
Log.i(TAG, "Ignoring token for one-off lookup.")
}
} catch (e: CdsiResourceExhaustedException) {
Log.w(TAG, "CDS resource exhausted! Can try again in ${e.retryAfterSeconds} seconds.")
SignalStore.misc().cdsBlockedUtil = System.currentTimeMillis() + e.retryAfterSeconds.seconds.inWholeMilliseconds
throw e
} catch (e: CdsiInvalidTokenException) {
Log.w(TAG, "We did not provide a token, but still got a token error! Unexpected, but ignoring.")
throw e
}
return response.results[e164]?.let { item ->
val id = SignalDatabase.recipients.processIndividualCdsLookup(e164 = e164, aci = item.aci.orElse(null), pni = item.pni)
ContactDiscovery.LookupResult(
recipientId = id,
pni = item.pni,
aci = item.aci?.orElse(null)
)
}
}
@Throws(IOException::class)
private fun refreshInternal(
recipientE164s: Set<String>,