Update API endpoints and integration for usernames.

This commit is contained in:
Alex Hart
2023-02-09 13:59:36 -04:00
committed by Greyson Parrelli
parent 803154c544
commit 2c48d40375
22 changed files with 172 additions and 142 deletions

View File

@@ -291,7 +291,7 @@ public class CommunicationActions {
}
}
} else {
Optional<ServiceId> serviceId = UsernameUtil.fetchAciForUsername(username);
Optional<ServiceId> serviceId = UsernameUtil.fetchAciForUsernameHash(username);
if (serviceId.isPresent()) {
recipient = Recipient.externalUsername(serviceId.get(), username);
}

View File

@@ -7,13 +7,15 @@ import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import org.signal.core.util.logging.Log;
import org.signal.libsignal.usernames.BaseUsernameException;
import org.signal.libsignal.usernames.Username;
import org.thoughtcrime.securesms.database.SignalDatabase;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientId;
import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
import org.whispersystems.signalservice.api.push.ACI;
import org.whispersystems.signalservice.api.push.ServiceId;
import org.whispersystems.util.Base64UrlSafe;
import java.io.IOException;
import java.util.Locale;
@@ -24,10 +26,10 @@ public class UsernameUtil {
private static final String TAG = Log.tag(UsernameUtil.class);
public static final int MIN_LENGTH = 4;
public static final int MAX_LENGTH = 26;
public static final int MIN_LENGTH = 3;
public static final int MAX_LENGTH = 32;
private static final Pattern FULL_PATTERN = Pattern.compile("^[a-z_][a-z0-9_]{3,25}$", Pattern.CASE_INSENSITIVE);
private static final Pattern FULL_PATTERN = Pattern.compile(String.format(Locale.US, "^[a-zA-Z_][a-zA-Z0-9_]{%d,%d}$", MIN_LENGTH - 1, MAX_LENGTH - 1), Pattern.CASE_INSENSITIVE);
private static final Pattern DIGIT_START_PATTERN = Pattern.compile("^[0-9].*$");
public static boolean isValidUsernameForSearch(@Nullable String value) {
@@ -66,9 +68,19 @@ public class UsernameUtil {
}
}
Log.d(TAG, "No local user with this username. Searching remotely.");
try {
Log.d(TAG, "No local user with this username. Searching remotely.");
ACI aci = ApplicationDependencies.getSignalServiceAccountManager().getAciByUsername(username);
return fetchAciForUsernameHash(Base64UrlSafe.encodeBytesWithoutPadding(Username.hash(username)));
} catch (BaseUsernameException e) {
return Optional.empty();
}
}
@WorkerThread
public static @NonNull Optional<ServiceId> fetchAciForUsernameHash(@NonNull String base64UrlSafeEncodedUsernameHash) {
try {
ACI aci = ApplicationDependencies.getSignalServiceAccountManager()
.getAciByUsernameHash(base64UrlSafeEncodedUsernameHash);
return Optional.ofNullable(aci);
} catch (IOException e) {
return Optional.empty();