Skip to content

Commit

Permalink
Allow setting a username with explicit descriminator
Browse files Browse the repository at this point in the history
Fixes #1469
  • Loading branch information
AsamK committed Feb 22, 2024
1 parent 0bb2a64 commit 83d4718
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions graalvm-config-dir/jni-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@
{
"name":"org.signal.libsignal.protocol.state.SignedPreKeyStore"
},
{
"name":"org.signal.libsignal.usernames.BadDiscriminatorCharacterException",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String"] }]
},
{
"name":"org.signal.libsignal.usernames.BadNicknameCharacterException",
"methods":[{"name":"<init>","parameterTypes":["java.lang.String"] }]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,21 +321,42 @@ public void handlePniChangeNumberMessage(
public static final int USERNAME_MIN_LENGTH = 3;
public static final int USERNAME_MAX_LENGTH = 32;

public void reserveUsername(String nickname) throws IOException, BaseUsernameException {
public void reserveUsernameFromNickname(String nickname) throws IOException, BaseUsernameException {
final var currentUsername = account.getUsername();
if (currentUsername != null) {
final var currentNickname = currentUsername.substring(0, currentUsername.indexOf('.'));
if (currentNickname.equals(nickname)) {
try {
refreshCurrentUsername();
return;
} catch (IOException | BaseUsernameException e) {
logger.warn("[reserveUsername] Failed to refresh current username, trying to claim new username");
}
return;
}
}

final var candidates = Username.candidatesFrom(nickname, USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH);
reserveUsername(candidates);
}

public void reserveExactUsername(String username) throws IOException, BaseUsernameException {
final var currentUsername = account.getUsername();
if (currentUsername != null) {
if (currentUsername.equals(username)) {
try {
refreshCurrentUsername();
return;
} catch (IOException | BaseUsernameException e) {
logger.warn("[reserveUsername] Failed to refresh current username, trying to claim new username");
}
}
}

final var candidates = List.of(new Username(username));
reserveUsername(candidates);
}

private void reserveUsername(final List<Username> candidates) throws IOException {
final var candidateHashes = new ArrayList<String>();
for (final var candidate : candidates) {
candidateHashes.add(Base64.encodeUrlSafeWithoutPadding(candidate.getHash()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,11 @@ public UsernameLinkUrl getUsernameLink() {
@Override
public void setUsername(final String username) throws IOException, InvalidUsernameException {
try {
context.getAccountHelper().reserveUsername(username);
if (username.contains(".")) {
context.getAccountHelper().reserveExactUsername(username);
} else {
context.getAccountHelper().reserveUsernameFromNickname(username);
}
} catch (BaseUsernameException e) {
throw new InvalidUsernameException(e.getMessage() + " (" + e.getClass().getSimpleName() + ")", e);
}
Expand Down

0 comments on commit 83d4718

Please sign in to comment.