All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.auties.whatsapp.api.ConnectionBuilder Maven / Gradle / Ivy

package it.auties.whatsapp.api;

import it.auties.whatsapp.controller.ControllerSerializer;
import it.auties.whatsapp.controller.StoreKeysPair;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

/**
 * A builder to specify the type of connection to use
 *
 * @param  the type of the newsletters
 */
@SuppressWarnings("unused")
public final class ConnectionBuilder> {
    private final ClientType clientType;
    private ControllerSerializer serializer;

    ConnectionBuilder(ClientType clientType) {
        this.clientType = clientType;
        this.serializer = ControllerSerializer.toSmile();
    }

    /**
     * Uses a custom serializer
     *
     * @param serializer the non-null serializer to use
     * @return the same instance for chaining
     */
    public ConnectionBuilder serializer(ControllerSerializer serializer) {
        this.serializer = serializer;
        return this;
    }

    /**
     * Creates a new connection using a random uuid
     *
     * @return a non-null options selector
     */
    public T newConnection() {
        return newConnection(UUID.randomUUID());
    }

    /**
     * Creates a new connection using a unique identifier
     * If a session with the given id already whatsappOldEligible, it will be retrieved.
     * Otherwise, a new one will be created.
     *
     * @param uuid the nullable uuid to use to create the connection
     * @return a non-null options selector
     */
    public T newConnection(UUID uuid) {
        var sessionUuid = Objects.requireNonNullElseGet(uuid, UUID::randomUUID);
        var sessionStoreAndKeys = serializer.deserializeStoreKeysPair(sessionUuid, null, null, clientType)
                .orElseGet(() -> serializer.newStoreKeysPair(sessionUuid, null, null, clientType));
        return createConnection(sessionStoreAndKeys);
    }

    /**
     * Creates a new connection using a phone number
     * If a session with the given phone number already whatsappOldEligible, it will be retrieved.
     * Otherwise, a new one will be created.
     *
     * @param phoneNumber the nullable uuid to use to create the connection
     * @return a non-null options selector
     */
    public T newConnection(long phoneNumber) {
        var sessionStoreAndKeys = serializer.deserializeStoreKeysPair(null, phoneNumber, null, clientType)
                .orElseGet(() -> serializer.newStoreKeysPair(UUID.randomUUID(), phoneNumber, null, clientType));
        return createConnection(sessionStoreAndKeys);
    }

    /**
     * Creates a new connection using an alias
     * If a session with the given alias already whatsappOldEligible, it will be retrieved.
     * Otherwise, a new one will be created.
     *
     * @param alias the nullable alias to use to create the connection
     * @return a non-null options selector
     */
    public T newConnection(String alias) {
        var sessionStoreAndKeys = serializer.deserializeStoreKeysPair(null, null, alias, clientType)
                .orElseGet(() -> serializer.newStoreKeysPair(UUID.randomUUID(), null, alias != null ? List.of(alias) : null, clientType));
        return createConnection(sessionStoreAndKeys);
    }

    /**
     * Creates a new connection from the first connection that was serialized
     * If no connection is available, a new one will be created
     *
     * @return a non-null options selector
     */
    public T firstConnection() {
        return newConnection(serializer.listIds(clientType).peekFirst());
    }

    /**
     * Creates a new connection from the last connection that was serialized
     * If no connection is available, a new one will be created
     *
     * @return a non-null options selector
     */
    public T lastConnection() {
        return newConnection(serializer.listIds(clientType).peekLast());
    }

    /**
     * Creates a new connection from the last connection that was serialized
     * If no connection is available, an empty optional will be returned
     *
     * @return a non-null options selector
     */
    public Optional newOptionalConnection(UUID uuid) {
        var sessionUuid = Objects.requireNonNullElseGet(uuid, UUID::randomUUID);
        return serializer.deserializeStoreKeysPair(sessionUuid, null, null, clientType)
                .map(this::createConnection);
    }

    /**
     * Creates a new connection from the last connection that was serialized
     * If no connection is available, an empty optional will be returned
     *
     * @return a non-null options selector
     */
    public Optional newOptionalConnection(Long phoneNumber) {
        return serializer.deserializeStoreKeysPair(null, phoneNumber, null, clientType)
                .map(this::createConnection);
    }

    /**
     * Creates a new connection using an alias
     * If no connection is available, an empty optional will be returned
     *
     * @param alias the nullable alias to use to create the connection
     * @return a non-null options selector
     */
    public Optional newOptionalConnection(String alias) {
        return serializer.deserializeStoreKeysPair(null, null, alias, clientType)
                .map(this::createConnection);
    }

    /**
     * Creates a new connection from the first connection that was serialized
     *
     * @return an optional
     */
    public Optional firstOptionalConnection() {
        return newOptionalConnection(serializer.listIds(clientType).peekFirst());
    }

    /**
     * Creates a new connection from the last connection that was serialized
     *
     * @return an optional
     */
    public Optional lastOptionalConnection() {
        return newOptionalConnection(serializer.listIds(clientType).peekLast());
    }

    @SuppressWarnings("unchecked")
    private T createConnection(StoreKeysPair sessionStoreAndKeys) {
        return (T) switch (clientType) {
            case WEB -> new WebOptionsBuilder(sessionStoreAndKeys.store(), sessionStoreAndKeys.keys());
            case MOBILE -> new MobileOptionsBuilder(sessionStoreAndKeys.store(), sessionStoreAndKeys.keys());
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy