Fix duplicate numbers, remove shortcodes entirely.

This commit is contained in:
Greyson Parrelli
2025-07-03 14:09:54 -04:00
committed by Alex Hart
parent 1ba34bb685
commit 5d0f71e02c
15 changed files with 332 additions and 122 deletions

View File

@@ -53,6 +53,23 @@ object SignalE164Util {
return getFormatter().formatAsE164(input)
}
/**
* Formats the number as an E164, or null if the number cannot be reasonably interpreted as a phone number, or if
* the number is a shortcode (<= 6 digits, excluding leading '+' and zeroes).
*
* This does not check if the number is *valid* for a given region. Instead, it's very lenient and just
* does it's best to interpret the input string as a number that could be put into the E164 format.
*
* Note that shortcodes will not have leading '+' signs.
*
* In other words, if this method returns null, you likely do not have anything that could be considered
* a phone number.
*/
@JvmStatic
fun formatNonShortCodeAsE164(input: String): String? {
return getFormatter().formatAsE164(input)?.takeIf { input.trimStart('+', '0').length > 6 }
}
/**
* Returns true if the input string can be considered an E164. Specifically, it returns true if we could figure out how to format it as an E164.
*/
@@ -61,6 +78,14 @@ object SignalE164Util {
return formatAsE164(input) != null
}
/**
* Performs the same check as [isPotentialE164], with the additional validation to check if there are more than 6 digits in the number.
* When counting digits, leading zeroes and '+' will be ignored.
*/
fun isPotentialNonShortCodeE164(input: String): Boolean {
return formatNonShortCodeAsE164(input) != null
}
private fun getFormatter(): E164Util.Formatter {
val localNumber = SignalStore.account.e164 ?: return defaultFormatter
val formatter = cachedFormatters[localNumber]