Skip to content

Commit

Permalink
Version 0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Auties00 committed Feb 25, 2024
1 parent fd60c84 commit 8cfaadb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ In short, if you use this library without a malicious intent, you will never get
<dependency>
<groupId>com.github.auties00</groupId>
<artifactId>cobalt</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
</dependency>
```

#### Gradle

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/it/auties/whatsapp/api/MobileOptionsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<MobileOptionsBuilder> {
Expand Down Expand Up @@ -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<Boolean> exists(long phoneNumber) {
var service = new WhatsappRegistration(store, keys, null, null);
return service.exists();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public WhatsappRegistration(Store store, Keys keys, AsyncVerificationCodeSupplie
this.httpClient = createClient();
}

public CompletableFuture<Void> registerPhoneNumber() {
public CompletableFuture<RegistrationResponse> registerPhoneNumber() {
return requestVerificationCode(false)
.thenCompose(ignored -> sendVerificationCode())
.whenComplete((result, exception) -> {
Expand All @@ -59,11 +59,11 @@ public CompletableFuture<Void> registerPhoneNumber() {
});
}

public CompletableFuture<Void> requestVerificationCode() {
public CompletableFuture<RegistrationResponse> requestVerificationCode() {
return requestVerificationCode(true);
}

private CompletableFuture<Void> requestVerificationCode(boolean closeResources) {
private CompletableFuture<RegistrationResponse> requestVerificationCode(boolean closeResources) {
if(method == VerificationCodeMethod.NONE) {
return CompletableFuture.completedFuture(null);
}
Expand Down Expand Up @@ -170,16 +170,19 @@ private <T> CompletableFuture<T> clientLog(T data, Entry<String, Object>... attr
});
}

private CompletableFuture<Void> requestVerificationCode(RegistrationResponse existsResponse, VerificationCodeError lastError) {
private CompletableFuture<RegistrationResponse> 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<String, Object>[] getRequestVerificationCodeParameters(RegistrationResponse existsResponse) {
Expand Down Expand Up @@ -235,14 +238,14 @@ private Entry<String, Object>[] getRequestVerificationCodeParameters(Registratio
};
}

private CompletionStage<Void> onCodeRequestSent(RegistrationResponse existsResponse, VerificationCodeError lastError, HttpResponse<String> result) {
private CompletionStage<RegistrationResponse> onCodeRequestSent(RegistrationResponse existsResponse, VerificationCodeError lastError, HttpResponse<String> 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()) {
Expand All @@ -256,7 +259,7 @@ private CompletionStage<Void> onCodeRequestSent(RegistrationResponse existsRespo
};
}

public CompletableFuture<Void> sendVerificationCode() {
public CompletableFuture<RegistrationResponse> sendVerificationCode() {
return codeHandler.get()
.thenComposeAsync(code -> getRegistrationOptions(store, keys, true, Map.entry("code", normalizeCodeResult(code))))
.thenComposeAsync(attrs -> sendRequest("/register", attrs))
Expand All @@ -268,7 +271,7 @@ public CompletableFuture<Void> 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());
Expand Down

0 comments on commit 8cfaadb

Please sign in to comment.