From 8cfaadb92430f850186e68ec38c397b0886701b3 Mon Sep 17 00:00:00 2001 From: Alessandro Autiero Date: Sun, 25 Feb 2024 13:46:18 +0100 Subject: [PATCH] Version 0.0.3 --- README.md | 11 +++++--- .../whatsapp/api/MobileOptionsBuilder.java | 13 ---------- .../registration/WhatsappRegistration.java | 25 +++++++++++-------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index d173b8f9..ab54c210 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ In short, if you use this library without a malicious intent, you will never get com.github.auties00 cobalt - 0.0.2 + 0.0.3 ``` @@ -60,12 +60,12 @@ In short, if you use this library without a malicious intent, you will never get 1. Groovy DSL ```groovy - implementation 'com.github.auties00:cobalt:0.0.2' + implementation 'com.github.auties00:cobalt:0.0.3' ``` 2. Kotlin DSL ```kotlin - implementation("com.github.auties00:cobalt:0.0.2") + implementation("com.github.auties00:cobalt:0.0.3") ``` ### Javadocs & Documentation @@ -252,6 +252,10 @@ Finally select the registration status of your session: ```java .register(yourPhoneNumberWithCountryCode) ``` + And get the resulting Whatsapp instance: + ```java + .whatsapp() + ``` Now you can connect to your session: ```java @@ -313,6 +317,7 @@ Remember to handle the result using, for example, `join` to await the connection }) .register(phoneNumber) // Register the phone number asynchronously, if necessary .join() // Await the result + .whatsapp() // Get the resulting whatsapp instance .addLoggedInListener(api -> System.out.printf("Connected: %s%n", api.store().privacySettings())) // Print a message when connected .addDisconnectedListener(reason -> System.out.printf("Disconnected: %s%n", reason)) // Print a message when disconnected .addNewChatMessageListener(message -> System.out.printf("New message: %s%n", message.toJson())) // Print a message when a new chat message arrives diff --git a/src/main/java/it/auties/whatsapp/api/MobileOptionsBuilder.java b/src/main/java/it/auties/whatsapp/api/MobileOptionsBuilder.java index b3fc0e76..e9dc5f58 100644 --- a/src/main/java/it/auties/whatsapp/api/MobileOptionsBuilder.java +++ b/src/main/java/it/auties/whatsapp/api/MobileOptionsBuilder.java @@ -6,10 +6,8 @@ import it.auties.whatsapp.controller.Store; import it.auties.whatsapp.model.business.BusinessCategory; import it.auties.whatsapp.model.companion.CompanionDevice; -import it.auties.whatsapp.registration.WhatsappRegistration; import java.util.Optional; -import java.util.concurrent.CompletableFuture; @SuppressWarnings("unused") public final class MobileOptionsBuilder extends OptionsBuilder { @@ -136,15 +134,4 @@ public Unverified unverified() { public Unregistered unregistered() { return new Unregistered(store, keys, errorHandler, socketExecutor); } - - /** - * Checks if a number is already registered on Whatsapp - * - * @param phoneNumber a phone number(include the prefix) - * @return a future - */ - public CompletableFuture exists(long phoneNumber) { - var service = new WhatsappRegistration(store, keys, null, null); - return service.exists(); - } } diff --git a/src/main/java/it/auties/whatsapp/registration/WhatsappRegistration.java b/src/main/java/it/auties/whatsapp/registration/WhatsappRegistration.java index 790022fb..10e15904 100644 --- a/src/main/java/it/auties/whatsapp/registration/WhatsappRegistration.java +++ b/src/main/java/it/auties/whatsapp/registration/WhatsappRegistration.java @@ -48,7 +48,7 @@ public WhatsappRegistration(Store store, Keys keys, AsyncVerificationCodeSupplie this.httpClient = createClient(); } - public CompletableFuture registerPhoneNumber() { + public CompletableFuture registerPhoneNumber() { return requestVerificationCode(false) .thenCompose(ignored -> sendVerificationCode()) .whenComplete((result, exception) -> { @@ -59,11 +59,11 @@ public CompletableFuture registerPhoneNumber() { }); } - public CompletableFuture requestVerificationCode() { + public CompletableFuture requestVerificationCode() { return requestVerificationCode(true); } - private CompletableFuture requestVerificationCode(boolean closeResources) { + private CompletableFuture requestVerificationCode(boolean closeResources) { if(method == VerificationCodeMethod.NONE) { return CompletableFuture.completedFuture(null); } @@ -170,16 +170,19 @@ private CompletableFuture clientLog(T data, Entry... attr }); } - private CompletableFuture requestVerificationCode(RegistrationResponse existsResponse, VerificationCodeError lastError) { + private CompletableFuture requestVerificationCode(RegistrationResponse existsResponse, VerificationCodeError lastError) { var options = getRegistrationOptions( store, keys, true, getRequestVerificationCodeParameters(existsResponse) ); - return options.thenCompose(attrs -> sendRequest("/code", attrs)) - .thenCompose(result -> onCodeRequestSent(existsResponse, lastError, result)) - .thenRun(() -> saveRegistrationStatus(store, keys, false)); + return options.thenComposeAsync(attrs -> sendRequest("/code", attrs)) + .thenComposeAsync(result -> onCodeRequestSent(existsResponse, lastError, result)) + .thenApplyAsync(response -> { + saveRegistrationStatus(store, keys, false); + return response; + }); } private Entry[] getRequestVerificationCodeParameters(RegistrationResponse existsResponse) { @@ -235,14 +238,14 @@ private Entry[] getRequestVerificationCodeParameters(Registratio }; } - private CompletionStage onCodeRequestSent(RegistrationResponse existsResponse, VerificationCodeError lastError, HttpResponse result) { + private CompletionStage onCodeRequestSent(RegistrationResponse existsResponse, VerificationCodeError lastError, HttpResponse result) { if (result.statusCode() != HttpURLConnection.HTTP_OK) { throw new RegistrationException(null, result.body()); } var response = Json.readValue(result.body(), RegistrationResponse.class); if (response.status() == VerificationCodeStatus.SUCCESS) { - return CompletableFuture.completedFuture(null); + return CompletableFuture.completedFuture(response); } return switch (response.errorReason()) { @@ -256,7 +259,7 @@ private CompletionStage onCodeRequestSent(RegistrationResponse existsRespo }; } - public CompletableFuture sendVerificationCode() { + public CompletableFuture sendVerificationCode() { return codeHandler.get() .thenComposeAsync(code -> getRegistrationOptions(store, keys, true, Map.entry("code", normalizeCodeResult(code)))) .thenComposeAsync(attrs -> sendRequest("/register", attrs)) @@ -268,7 +271,7 @@ public CompletableFuture sendVerificationCode() { var response = Json.readValue(result.body(), RegistrationResponse.class); if (response.status() == VerificationCodeStatus.SUCCESS) { saveRegistrationStatus(store, keys, true); - return CompletableFuture.completedFuture(null); + return CompletableFuture.completedFuture(response); } throw new RegistrationException(response, result.body());