Skip to content

Commit

Permalink
Small api fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Auties00 committed Dec 28, 2023
1 parent f58a234 commit 8445b81
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/main/java/it/auties/whatsapp/model/chat/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import it.auties.whatsapp.model.message.model.MessageCategory;
import it.auties.whatsapp.model.sync.HistorySyncMessage;
import it.auties.whatsapp.util.Clock;
import it.auties.whatsapp.util.ConcurrentDoublyLinkedHashedDequeue;
import it.auties.whatsapp.util.ConcurrentLinkedHashedDequeue;

import java.time.Instant;
import java.time.ZonedDateTime;
Expand All @@ -37,7 +37,7 @@ public final class Chat implements ProtobufMessage, JidProvider {
private final Jid jid;

@ProtobufProperty(index = 2, type = ProtobufType.OBJECT, repeated = true)
private final ConcurrentDoublyLinkedHashedDequeue<HistorySyncMessage> historySyncMessages;
private final ConcurrentLinkedHashedDequeue<HistorySyncMessage> historySyncMessages;

@ProtobufProperty(index = 3, type = ProtobufType.STRING)
private final Jid newJid;
Expand Down Expand Up @@ -154,7 +154,7 @@ public final class Chat implements ProtobufMessage, JidProvider {
private final Set<GroupPastParticipant> pastParticipants;

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public Chat(Jid jid, ConcurrentDoublyLinkedHashedDequeue<HistorySyncMessage> historySyncMessages, Jid newJid, Jid oldJid, int unreadMessagesCount, boolean readOnly, boolean endOfHistoryTransfer, ChatEphemeralTimer ephemeralMessageDuration, long ephemeralMessagesToggleTimeSeconds, EndOfHistoryTransferType endOfHistoryTransferType, long timestampSeconds, String name, boolean notSpam, boolean archived, ChatDisappear disappearInitiator, boolean markedAsUnread, List<GroupParticipant> participants, byte[] token, long tokenTimestampSeconds, byte[] identityKey, int pinnedTimestampSeconds, ChatMute mute, ChatWallpaper wallpaper, MediaVisibility mediaVisibility, long tokenSenderTimestampSeconds, boolean suspended, boolean terminated, long foundationTimestampSeconds, Jid founder, String description, boolean support, boolean parentGroup, boolean defaultSubGroup, Jid parentGroupJid, String displayName, Jid phoneJid, boolean shareOwnPhoneNumber, boolean pnhDuplicateLidThread, Jid lidJid, ConcurrentHashMap<Jid, ContactStatus> presences, Set<Jid> participantsPreKeys, Set<GroupPastParticipant> pastParticipants) {
public Chat(Jid jid, ConcurrentLinkedHashedDequeue<HistorySyncMessage> historySyncMessages, Jid newJid, Jid oldJid, int unreadMessagesCount, boolean readOnly, boolean endOfHistoryTransfer, ChatEphemeralTimer ephemeralMessageDuration, long ephemeralMessagesToggleTimeSeconds, EndOfHistoryTransferType endOfHistoryTransferType, long timestampSeconds, String name, boolean notSpam, boolean archived, ChatDisappear disappearInitiator, boolean markedAsUnread, List<GroupParticipant> participants, byte[] token, long tokenTimestampSeconds, byte[] identityKey, int pinnedTimestampSeconds, ChatMute mute, ChatWallpaper wallpaper, MediaVisibility mediaVisibility, long tokenSenderTimestampSeconds, boolean suspended, boolean terminated, long foundationTimestampSeconds, Jid founder, String description, boolean support, boolean parentGroup, boolean defaultSubGroup, Jid parentGroupJid, String displayName, Jid phoneJid, boolean shareOwnPhoneNumber, boolean pnhDuplicateLidThread, Jid lidJid, ConcurrentHashMap<Jid, ContactStatus> presences, Set<Jid> participantsPreKeys, Set<GroupPastParticipant> pastParticipants) {
this.jid = jid;
this.historySyncMessages = historySyncMessages;
this.newJid = newJid;
Expand Down Expand Up @@ -199,7 +199,7 @@ public Chat(Jid jid, ConcurrentDoublyLinkedHashedDequeue<HistorySyncMessage> his
this.pastParticipants = pastParticipants;
}

public Chat(Jid jid, ConcurrentDoublyLinkedHashedDequeue<HistorySyncMessage> historySyncMessages, Jid newJid, Jid oldJid, int unreadMessagesCount, boolean readOnly, boolean endOfHistoryTransfer, ChatEphemeralTimer ephemeralMessageDuration, long ephemeralMessagesToggleTimeSeconds, EndOfHistoryTransferType endOfHistoryTransferType, long timestampSeconds, String name, boolean notSpam, boolean archived, ChatDisappear disappearInitiator, boolean markedAsUnread, List<GroupParticipant> participants, byte[] token, long tokenTimestampSeconds, byte[] identityKey, int pinnedTimestampSeconds, ChatMute mute, ChatWallpaper wallpaper, MediaVisibility mediaVisibility, long tokenSenderTimestampSeconds, boolean suspended, boolean terminated, long foundationTimestampSeconds, Jid founder, String description, boolean support, boolean parentGroup, boolean defaultSubGroup, Jid parentGroupJid, String displayName, Jid phoneJid, boolean shareOwnPhoneNumber, boolean pnhDuplicateLidThread, Jid lidJid) {
public Chat(Jid jid, ConcurrentLinkedHashedDequeue<HistorySyncMessage> historySyncMessages, Jid newJid, Jid oldJid, int unreadMessagesCount, boolean readOnly, boolean endOfHistoryTransfer, ChatEphemeralTimer ephemeralMessageDuration, long ephemeralMessagesToggleTimeSeconds, EndOfHistoryTransferType endOfHistoryTransferType, long timestampSeconds, String name, boolean notSpam, boolean archived, ChatDisappear disappearInitiator, boolean markedAsUnread, List<GroupParticipant> participants, byte[] token, long tokenTimestampSeconds, byte[] identityKey, int pinnedTimestampSeconds, ChatMute mute, ChatWallpaper wallpaper, MediaVisibility mediaVisibility, long tokenSenderTimestampSeconds, boolean suspended, boolean terminated, long foundationTimestampSeconds, Jid founder, String description, boolean support, boolean parentGroup, boolean defaultSubGroup, Jid parentGroupJid, String displayName, Jid phoneJid, boolean shareOwnPhoneNumber, boolean pnhDuplicateLidThread, Jid lidJid) {
this.jid = jid;
this.historySyncMessages = historySyncMessages;
this.newJid = newJid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import it.auties.whatsapp.model.info.NewsletterMessageInfo;
import it.auties.whatsapp.model.jid.Jid;
import it.auties.whatsapp.model.jid.JidProvider;
import it.auties.whatsapp.util.ConcurrentDoublyLinkedHashedDequeue;
import it.auties.whatsapp.util.ConcurrentLinkedHashedDequeue;

import java.util.Collection;
import java.util.Collections;
Expand All @@ -17,7 +17,7 @@ public final class Newsletter implements JidProvider {
private NewsletterState state;
private NewsletterMetadata metadata;
private final NewsletterViewerMetadata viewerMetadata;
private final ConcurrentDoublyLinkedHashedDequeue<NewsletterMessageInfo> messages;
private final ConcurrentLinkedHashedDequeue<NewsletterMessageInfo> messages;

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
Newsletter(
Expand All @@ -30,21 +30,21 @@ public final class Newsletter implements JidProvider {
@JsonProperty("viewer_metadata")
NewsletterViewerMetadata viewerMetadata,
@JsonProperty("messages")
ConcurrentDoublyLinkedHashedDequeue<NewsletterMessageInfo> messages
ConcurrentLinkedHashedDequeue<NewsletterMessageInfo> messages
) {
this.jid = jid;
this.state = state;
this.metadata = metadata;
this.viewerMetadata = viewerMetadata;
this.messages = Objects.requireNonNullElseGet(messages, ConcurrentDoublyLinkedHashedDequeue::new);
this.messages = Objects.requireNonNullElseGet(messages, ConcurrentLinkedHashedDequeue::new);
}

public Newsletter(Jid jid, NewsletterState state, NewsletterMetadata metadata, NewsletterViewerMetadata viewerMetadata) {
this.jid = jid;
this.state = state;
this.metadata = metadata;
this.viewerMetadata = viewerMetadata;
this.messages = new ConcurrentDoublyLinkedHashedDequeue<>();
this.messages = new ConcurrentLinkedHashedDequeue<>();
}

public void addMessage(NewsletterMessageInfo message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package it.auties.whatsapp.model.request;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public record UserChosenNameRequest(List<Variable> variables) {
public record Variable(@JsonProperty("user_id") String userId) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package it.auties.whatsapp.model.response;

import com.fasterxml.jackson.core.type.TypeReference;
import it.auties.whatsapp.model.node.Node;
import it.auties.whatsapp.util.Clock;
import it.auties.whatsapp.util.Json;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public record ContactAboutResponse(Optional<String> about, Optional<ZonedDateTime> timestamp) {
public static ContactAboutResponse ofNode(Node source) {
return new ContactAboutResponse(
source.contentAsString(),
Clock.parseSeconds(source.attributes().getLong("t"))
);
}

@SuppressWarnings("unchecked")
public static Optional<ContactAboutResponse> ofJson(String json) {
try {
var parsedJson = Json.readValue(json, new TypeReference<Map<String, Object>>() {});
var data = (Map<String, ?>) parsedJson.get("data");
var updates = (List<?>) data.get("xwa2_users_updates_since");
var latestUpdate = (Map<String, ?>) updates.getFirst();
var updatesData = (List<?>) latestUpdate.get("updates");
var latestUpdateData = (Map<String, ?>) updatesData.getFirst();
return Optional.of(new ContactAboutResponse(Optional.ofNullable((String) latestUpdateData.get("text")), Optional.empty()));
} catch (Throwable throwable) {
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package it.auties.whatsapp.model.response;

import com.fasterxml.jackson.core.type.TypeReference;
import it.auties.whatsapp.util.Json;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public record UserChosenNameResponse(Optional<String> name) {

@SuppressWarnings("unchecked")
public static Optional<UserChosenNameResponse> ofJson(String json) {
try {
var parsedJson = Json.readValue(json, new TypeReference<Map<String, Object>>() {});
var data = (Map<String, ?>) parsedJson.get("data");
var updates = (List<?>) data.get("xwa2_users_updates_since");
var latestUpdate = (Map<String, ?>) updates.getFirst();
var updatesData = (List<?>) latestUpdate.get("updates");
var latestUpdateData = (Map<String, ?>) updatesData.getFirst();
return Optional.of(new UserChosenNameResponse(Optional.ofNullable((String) latestUpdateData.get("text"))));
} catch (Throwable throwable) {
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class ConcurrentDoublyLinkedHashedDequeue<E> extends AbstractQueue<E> implements Deque<E> {
public class ConcurrentLinkedHashedDequeue<E> extends AbstractQueue<E> implements Deque<E> {
private final AtomicReference<Node<E>> head;
private final AtomicReference<Node<E>> tail;
private final Set<Integer> hashes;

public ConcurrentDoublyLinkedHashedDequeue() {
public ConcurrentLinkedHashedDequeue() {
this.head = new AtomicReference<>(null);
this.tail = new AtomicReference<>(null);
this.hashes = ConcurrentHashMap.newKeySet();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/it/auties/whatsapp/local/WebRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class WebRunner {
public static void main(String[] args) {
var whatsapp = Whatsapp.webBuilder()
.lastConnection()
.newConnection()
.historyLength(WebHistoryLength.zero())
.unregistered(393495089819L, PairingCodeHandler.toTerminal())
.addLoggedInListener(api -> System.out.printf("Connected: %s%n", api.store().privacySettings()))
Expand Down

0 comments on commit 8445b81

Please sign in to comment.