Represent phone numbers using a string

This commit is contained in:
Andrea Cavalli 2023-02-27 20:51:49 +01:00
parent ac1533a62b
commit 67282b5aac
5 changed files with 20 additions and 14 deletions

View File

@ -6,7 +6,7 @@ public interface AuthenticationData {
boolean isBot();
long getUserPhoneNumber();
String getUserPhoneNumber();
String getBotToken();
@ -14,7 +14,15 @@ public interface AuthenticationData {
return new AuthenticationDataQrCode();
}
/**
* Deprecated, use {@link #user(String)} instead
*/
@Deprecated
static AuthenticationData user(long userPhoneNumber) {
return user(String.valueOf(userPhoneNumber));
}
static AuthenticationData user(String userPhoneNumber) {
return new AuthenticationDataImpl(userPhoneNumber, null);
}

View File

@ -5,10 +5,10 @@ import java.util.Objects;
@SuppressWarnings("unused")
final class AuthenticationDataImpl implements AuthenticationData {
private final Long userPhoneNumber;
private final String userPhoneNumber;
private final String botToken;
AuthenticationDataImpl(Long userPhoneNumber, String botToken) {
AuthenticationDataImpl(String userPhoneNumber, String botToken) {
if ((userPhoneNumber == null) == (botToken == null)) {
throw new IllegalArgumentException("Please use either a bot token or a phone number");
}
@ -32,7 +32,7 @@ final class AuthenticationDataImpl implements AuthenticationData {
}
@Override
public long getUserPhoneNumber() {
public String getUserPhoneNumber() {
if (userPhoneNumber == null) {
throw new UnsupportedOperationException("This is not a user");
}
@ -50,7 +50,7 @@ final class AuthenticationDataImpl implements AuthenticationData {
@Override
public String toString() {
if (userPhoneNumber != null) {
return "+" + userPhoneNumber;
return userPhoneNumber;
} else {
return "\"" + botToken + "\"";
}

View File

@ -13,7 +13,7 @@ class AuthenticationDataQrCode implements AuthenticationData {
}
@Override
public long getUserPhoneNumber() {
public String getUserPhoneNumber() {
throw new UnsupportedOperationException("This is not a user");
}

View File

@ -47,7 +47,7 @@ final class AuthorizationStateWaitAuthenticationDataHandler implements GenericUp
} else {
PhoneNumberAuthenticationSettings phoneSettings = new PhoneNumberAuthenticationSettings(false, false, false, false, null);
String phoneNumber = String.valueOf(authenticationData.getUserPhoneNumber());
String phoneNumber = authenticationData.getUserPhoneNumber();
SetAuthenticationPhoneNumber response = new SetAuthenticationPhoneNumber(phoneNumber, phoneSettings);
client.send(response, ok -> {
if (ok.getConstructor() == TdApi.Error.CONSTRUCTOR) {

View File

@ -11,7 +11,7 @@ public final class ConsoleInteractiveAuthenticationData implements Authenticatio
private boolean isQr;
private boolean isBot;
private String botToken;
private long phoneNumber;
private String phoneNumber;
ConsoleInteractiveAuthenticationData() {
@ -38,7 +38,7 @@ public final class ConsoleInteractiveAuthenticationData implements Authenticatio
}
@Override
public long getUserPhoneNumber() {
public String getUserPhoneNumber() {
initializeIfNeeded();
if (isBot || isQr) {
throw new UnsupportedOperationException("This is not a user");
@ -100,7 +100,7 @@ public final class ConsoleInteractiveAuthenticationData implements Authenticatio
} while (token.length() < 5 || !token.contains(":"));
this.isBot = true;
this.phoneNumber = -1;
this.phoneNumber = null;
this.botToken = token;
this.isQr = false;
} else if ("PHONE".equals(mode)) {
@ -109,16 +109,14 @@ public final class ConsoleInteractiveAuthenticationData implements Authenticatio
phoneNumber = ScannerUtils.askParameter("login", "Please type your phone number");
} while (phoneNumber.length() < 3);
long phoneNumberLong = Long.parseLong(phoneNumber.replaceAll("\\D", ""));
this.isBot = false;
this.phoneNumber = phoneNumberLong;
this.phoneNumber = phoneNumber;
this.botToken = null;
this.isQr = false;
} else {
this.isBot = false;
this.phoneNumber = -1;
this.phoneNumber = null;
this.botToken = null;
this.isQr = true;
}