-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
273 additions
and
35 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
tdlight-java/src/main/java/it/tdlight/client/AuthorizationStateWaitEmailAddressHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package it.tdlight.client; | ||
|
||
import it.tdlight.ExceptionHandler; | ||
import it.tdlight.TelegramClient; | ||
import it.tdlight.jni.TdApi; | ||
import it.tdlight.jni.TdApi.AuthorizationStateWaitEmailAddress; | ||
import it.tdlight.jni.TdApi.CheckAuthenticationCode; | ||
import it.tdlight.jni.TdApi.CheckAuthenticationEmailCode; | ||
import it.tdlight.jni.TdApi.SetAuthenticationEmailAddress; | ||
import it.tdlight.jni.TdApi.UpdateAuthorizationState; | ||
|
||
final class AuthorizationStateWaitEmailAddressHandler implements GenericUpdateHandler<UpdateAuthorizationState> { | ||
|
||
private final TelegramClient client; | ||
private final ClientInteraction clientInteraction; | ||
private final ExceptionHandler exceptionHandler; | ||
|
||
public AuthorizationStateWaitEmailAddressHandler(TelegramClient client, | ||
ClientInteraction clientInteraction, | ||
ExceptionHandler exceptionHandler) { | ||
this.client = client; | ||
this.clientInteraction = clientInteraction; | ||
this.exceptionHandler = exceptionHandler; | ||
} | ||
|
||
@Override | ||
public void onUpdate(UpdateAuthorizationState update) { | ||
if (update.authorizationState.getConstructor() == AuthorizationStateWaitEmailAddress.CONSTRUCTOR) { | ||
AuthorizationStateWaitEmailAddress authorizationState = (AuthorizationStateWaitEmailAddress) update.authorizationState; | ||
ParameterInfo parameterInfo = new ParameterInfoEmailAddress(authorizationState.allowAppleId, authorizationState.allowGoogleId); | ||
clientInteraction.onParameterRequest(InputParameter.ASK_EMAIL_ADDRESS, parameterInfo).whenComplete((emailAddress, ex) -> { | ||
if (ex != null) { | ||
exceptionHandler.onException(ex); | ||
return; | ||
} | ||
sendEmailAddress(emailAddress); | ||
}); | ||
} | ||
} | ||
|
||
private void sendEmailAddress(String emailAddress) { | ||
SetAuthenticationEmailAddress response = new SetAuthenticationEmailAddress(emailAddress); | ||
client.send(response, ok -> { | ||
if (ok.getConstructor() == TdApi.Error.CONSTRUCTOR) { | ||
throw new TelegramError((TdApi.Error) ok); | ||
} | ||
}, exceptionHandler); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
tdlight-java/src/main/java/it/tdlight/client/AuthorizationStateWaitEmailCodeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package it.tdlight.client; | ||
|
||
import it.tdlight.ExceptionHandler; | ||
import it.tdlight.TelegramClient; | ||
import it.tdlight.jni.TdApi; | ||
import java.util.Locale; | ||
|
||
final class AuthorizationStateWaitEmailCodeHandler implements GenericUpdateHandler<TdApi.UpdateAuthorizationState> { | ||
|
||
private final TelegramClient client; | ||
private final ClientInteraction clientInteraction; | ||
private final ExceptionHandler exceptionHandler; | ||
|
||
public AuthorizationStateWaitEmailCodeHandler(TelegramClient client, | ||
ClientInteraction clientInteraction, | ||
ExceptionHandler exceptionHandler) { | ||
this.client = client; | ||
this.clientInteraction = clientInteraction; | ||
this.exceptionHandler = exceptionHandler; | ||
} | ||
|
||
@Override | ||
public void onUpdate(TdApi.UpdateAuthorizationState update) { | ||
if (update.authorizationState.getConstructor() == TdApi.AuthorizationStateWaitEmailCode.CONSTRUCTOR) { | ||
TdApi.AuthorizationStateWaitEmailCode authorizationState = (TdApi.AuthorizationStateWaitEmailCode) update.authorizationState; | ||
ParameterInfo parameterInfo = new ParameterInfoEmailCode(authorizationState.allowAppleId, | ||
authorizationState.allowGoogleId, | ||
authorizationState.codeInfo.emailAddressPattern, | ||
authorizationState.codeInfo.length, | ||
EmailAddressResetState.valueOf(authorizationState.emailAddressResetState.getClass().getSimpleName().substring("EmailAddressResetState".length()).toUpperCase(Locale.ROOT)) | ||
); | ||
clientInteraction.onParameterRequest(InputParameter.ASK_EMAIL_CODE, parameterInfo).whenComplete((emailAddress, ex) -> { | ||
if (ex != null) { | ||
exceptionHandler.onException(ex); | ||
return; | ||
} | ||
sendEmailCode(emailAddress); | ||
}); | ||
} | ||
} | ||
|
||
private void sendEmailCode(String code) { | ||
TdApi.CheckAuthenticationEmailCode response = new TdApi.CheckAuthenticationEmailCode(new TdApi.EmailAddressAuthenticationCode(code)); | ||
client.send(response, ok -> { | ||
if (ok.getConstructor() == TdApi.Error.CONSTRUCTOR) { | ||
throw new TelegramError((TdApi.Error) ok); | ||
} | ||
}, exceptionHandler); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tdlight-java/src/main/java/it/tdlight/client/EmailAddressResetState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package it.tdlight.client; | ||
|
||
public enum EmailAddressResetState { | ||
AVAILABLE, | ||
PENDING | ||
} |
9 changes: 8 additions & 1 deletion
9
tdlight-java/src/main/java/it/tdlight/client/InputParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
package it.tdlight.client; | ||
|
||
public enum InputParameter { | ||
ASK_FIRST_NAME, ASK_LAST_NAME, ASK_CODE, ASK_PASSWORD, NOTIFY_LINK, TERMS_OF_SERVICE | ||
ASK_FIRST_NAME, | ||
ASK_LAST_NAME, | ||
ASK_CODE, | ||
ASK_PASSWORD, | ||
NOTIFY_LINK, | ||
TERMS_OF_SERVICE, | ||
ASK_EMAIL_ADDRESS, | ||
ASK_EMAIL_CODE | ||
} |
50 changes: 50 additions & 0 deletions
50
tdlight-java/src/main/java/it/tdlight/client/ParameterInfoEmailAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package it.tdlight.client; | ||
|
||
import it.tdlight.jni.TdApi.AuthenticationCodeType; | ||
import java.util.Objects; | ||
import java.util.StringJoiner; | ||
|
||
public final class ParameterInfoEmailAddress implements ParameterInfo { | ||
|
||
private final boolean allowGoogleId; | ||
private final boolean allowAppleId; | ||
|
||
public ParameterInfoEmailAddress(boolean allowGoogleId, | ||
boolean allowAppleId) { | ||
this.allowGoogleId = allowGoogleId; | ||
this.allowAppleId = allowAppleId; | ||
} | ||
|
||
public boolean isAllowGoogleId() { | ||
return allowGoogleId; | ||
} | ||
|
||
public boolean isAllowAppleId() { | ||
return allowAppleId; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
ParameterInfoEmailAddress that = (ParameterInfoEmailAddress) o; | ||
return allowGoogleId == that.allowGoogleId && allowAppleId == that.allowAppleId; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(allowGoogleId, allowAppleId); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringJoiner(", ", ParameterInfoEmailAddress.class.getSimpleName() + "[", "]") | ||
.add("allowGoogleId='" + allowGoogleId + "'") | ||
.add("allowAppleId=" + allowAppleId) | ||
.toString(); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
tdlight-java/src/main/java/it/tdlight/client/ParameterInfoEmailCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package it.tdlight.client; | ||
|
||
import it.tdlight.jni.TdApi.EmailAddressAuthenticationCodeInfo; | ||
import it.tdlight.jni.TdApi.EmailAddressResetStateAvailable; | ||
import it.tdlight.jni.TdApi.EmailAddressResetStatePending; | ||
import java.util.Objects; | ||
import java.util.StringJoiner; | ||
|
||
public final class ParameterInfoEmailCode implements ParameterInfo { | ||
|
||
private final boolean allowGoogleId; | ||
private final boolean allowAppleId; | ||
private final String emailAddressPattern; | ||
private final int emailLength; | ||
private final EmailAddressResetState emailAddressResetState; | ||
|
||
public ParameterInfoEmailCode(boolean allowGoogleId, | ||
boolean allowAppleId, | ||
String emailAddressPattern, | ||
int emailLength, | ||
EmailAddressResetState emailAddressResetState) { | ||
this.allowGoogleId = allowGoogleId; | ||
this.allowAppleId = allowAppleId; | ||
this.emailAddressPattern = emailAddressPattern; | ||
this.emailLength = emailLength; | ||
this.emailAddressResetState = emailAddressResetState; | ||
} | ||
|
||
public boolean isAllowGoogleId() { | ||
return allowGoogleId; | ||
} | ||
|
||
public boolean isAllowAppleId() { | ||
return allowAppleId; | ||
} | ||
|
||
public String getEmailAddressPattern() { | ||
return emailAddressPattern; | ||
} | ||
|
||
public int getEmailLength() { | ||
return emailLength; | ||
} | ||
|
||
public EmailAddressResetState getEmailAddressResetState() { | ||
return emailAddressResetState; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
ParameterInfoEmailCode that = (ParameterInfoEmailCode) o; | ||
return allowGoogleId == that.allowGoogleId && allowAppleId == that.allowAppleId && emailLength == that.emailLength | ||
&& Objects.equals(emailAddressPattern, that.emailAddressPattern) | ||
&& emailAddressResetState == that.emailAddressResetState; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(allowGoogleId, allowAppleId, emailAddressPattern, emailLength, emailAddressResetState); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringJoiner(", ", ParameterInfoEmailCode.class.getSimpleName() + "[", "]") | ||
.add("allowGoogleId=" + allowGoogleId) | ||
.add("allowAppleId=" + allowAppleId) | ||
.add("emailAddressPattern='" + emailAddressPattern + "'") | ||
.add("emailLength=" + emailLength) | ||
.add("emailAddressResetState=" + emailAddressResetState) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters