From 8445b8102313ae9ea62223e43a5b1d11b1f4f93b Mon Sep 17 00:00:00 2001 From: Alessandro Autiero Date: Thu, 28 Dec 2023 21:26:27 +0100 Subject: [PATCH] Small api fixes --- .../it/auties/whatsapp/model/chat/Chat.java | 8 ++--- .../whatsapp/model/newsletter/Newsletter.java | 10 +++--- .../model/request/UserChosenNameRequest.java | 11 ++++++ .../model/response/ContactAboutResponse.java | 35 +++++++++++++++++++ .../response/UserChosenNameResponse.java | 26 ++++++++++++++ ...ava => ConcurrentLinkedHashedDequeue.java} | 4 +-- .../it/auties/whatsapp/local/WebRunner.java | 2 +- 7 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 src/main/java/it/auties/whatsapp/model/request/UserChosenNameRequest.java create mode 100644 src/main/java/it/auties/whatsapp/model/response/ContactAboutResponse.java create mode 100644 src/main/java/it/auties/whatsapp/model/response/UserChosenNameResponse.java rename src/main/java/it/auties/whatsapp/util/{ConcurrentDoublyLinkedHashedDequeue.java => ConcurrentLinkedHashedDequeue.java} (98%) diff --git a/src/main/java/it/auties/whatsapp/model/chat/Chat.java b/src/main/java/it/auties/whatsapp/model/chat/Chat.java index f733c5ec..e4b3eb12 100644 --- a/src/main/java/it/auties/whatsapp/model/chat/Chat.java +++ b/src/main/java/it/auties/whatsapp/model/chat/Chat.java @@ -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; @@ -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 historySyncMessages; + private final ConcurrentLinkedHashedDequeue historySyncMessages; @ProtobufProperty(index = 3, type = ProtobufType.STRING) private final Jid newJid; @@ -154,7 +154,7 @@ public final class Chat implements ProtobufMessage, JidProvider { private final Set pastParticipants; @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - public Chat(Jid jid, ConcurrentDoublyLinkedHashedDequeue 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 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 presences, Set participantsPreKeys, Set pastParticipants) { + public Chat(Jid jid, ConcurrentLinkedHashedDequeue 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 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 presences, Set participantsPreKeys, Set pastParticipants) { this.jid = jid; this.historySyncMessages = historySyncMessages; this.newJid = newJid; @@ -199,7 +199,7 @@ public Chat(Jid jid, ConcurrentDoublyLinkedHashedDequeue his this.pastParticipants = pastParticipants; } - public Chat(Jid jid, ConcurrentDoublyLinkedHashedDequeue 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 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 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 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; diff --git a/src/main/java/it/auties/whatsapp/model/newsletter/Newsletter.java b/src/main/java/it/auties/whatsapp/model/newsletter/Newsletter.java index 0c1ae10a..2748a9dc 100644 --- a/src/main/java/it/auties/whatsapp/model/newsletter/Newsletter.java +++ b/src/main/java/it/auties/whatsapp/model/newsletter/Newsletter.java @@ -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; @@ -17,7 +17,7 @@ public final class Newsletter implements JidProvider { private NewsletterState state; private NewsletterMetadata metadata; private final NewsletterViewerMetadata viewerMetadata; - private final ConcurrentDoublyLinkedHashedDequeue messages; + private final ConcurrentLinkedHashedDequeue messages; @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) Newsletter( @@ -30,13 +30,13 @@ public final class Newsletter implements JidProvider { @JsonProperty("viewer_metadata") NewsletterViewerMetadata viewerMetadata, @JsonProperty("messages") - ConcurrentDoublyLinkedHashedDequeue messages + ConcurrentLinkedHashedDequeue 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) { @@ -44,7 +44,7 @@ public Newsletter(Jid jid, NewsletterState state, NewsletterMetadata metadata, N this.state = state; this.metadata = metadata; this.viewerMetadata = viewerMetadata; - this.messages = new ConcurrentDoublyLinkedHashedDequeue<>(); + this.messages = new ConcurrentLinkedHashedDequeue<>(); } public void addMessage(NewsletterMessageInfo message) { diff --git a/src/main/java/it/auties/whatsapp/model/request/UserChosenNameRequest.java b/src/main/java/it/auties/whatsapp/model/request/UserChosenNameRequest.java new file mode 100644 index 00000000..dffe8919 --- /dev/null +++ b/src/main/java/it/auties/whatsapp/model/request/UserChosenNameRequest.java @@ -0,0 +1,11 @@ +package it.auties.whatsapp.model.request; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +public record UserChosenNameRequest(List variables) { + public record Variable(@JsonProperty("user_id") String userId) { + + } +} diff --git a/src/main/java/it/auties/whatsapp/model/response/ContactAboutResponse.java b/src/main/java/it/auties/whatsapp/model/response/ContactAboutResponse.java new file mode 100644 index 00000000..963fb6ab --- /dev/null +++ b/src/main/java/it/auties/whatsapp/model/response/ContactAboutResponse.java @@ -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 about, Optional timestamp) { + public static ContactAboutResponse ofNode(Node source) { + return new ContactAboutResponse( + source.contentAsString(), + Clock.parseSeconds(source.attributes().getLong("t")) + ); + } + + @SuppressWarnings("unchecked") + public static Optional ofJson(String json) { + try { + var parsedJson = Json.readValue(json, new TypeReference>() {}); + var data = (Map) parsedJson.get("data"); + var updates = (List) data.get("xwa2_users_updates_since"); + var latestUpdate = (Map) updates.getFirst(); + var updatesData = (List) latestUpdate.get("updates"); + var latestUpdateData = (Map) updatesData.getFirst(); + return Optional.of(new ContactAboutResponse(Optional.ofNullable((String) latestUpdateData.get("text")), Optional.empty())); + } catch (Throwable throwable) { + return Optional.empty(); + } + } +} diff --git a/src/main/java/it/auties/whatsapp/model/response/UserChosenNameResponse.java b/src/main/java/it/auties/whatsapp/model/response/UserChosenNameResponse.java new file mode 100644 index 00000000..ff56036b --- /dev/null +++ b/src/main/java/it/auties/whatsapp/model/response/UserChosenNameResponse.java @@ -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 name) { + + @SuppressWarnings("unchecked") + public static Optional ofJson(String json) { + try { + var parsedJson = Json.readValue(json, new TypeReference>() {}); + var data = (Map) parsedJson.get("data"); + var updates = (List) data.get("xwa2_users_updates_since"); + var latestUpdate = (Map) updates.getFirst(); + var updatesData = (List) latestUpdate.get("updates"); + var latestUpdateData = (Map) updatesData.getFirst(); + return Optional.of(new UserChosenNameResponse(Optional.ofNullable((String) latestUpdateData.get("text")))); + } catch (Throwable throwable) { + return Optional.empty(); + } + } +} diff --git a/src/main/java/it/auties/whatsapp/util/ConcurrentDoublyLinkedHashedDequeue.java b/src/main/java/it/auties/whatsapp/util/ConcurrentLinkedHashedDequeue.java similarity index 98% rename from src/main/java/it/auties/whatsapp/util/ConcurrentDoublyLinkedHashedDequeue.java rename to src/main/java/it/auties/whatsapp/util/ConcurrentLinkedHashedDequeue.java index a28613e6..7a3c506f 100644 --- a/src/main/java/it/auties/whatsapp/util/ConcurrentDoublyLinkedHashedDequeue.java +++ b/src/main/java/it/auties/whatsapp/util/ConcurrentLinkedHashedDequeue.java @@ -7,12 +7,12 @@ import java.util.function.Predicate; import java.util.stream.Collectors; -public class ConcurrentDoublyLinkedHashedDequeue extends AbstractQueue implements Deque { +public class ConcurrentLinkedHashedDequeue extends AbstractQueue implements Deque { private final AtomicReference> head; private final AtomicReference> tail; private final Set hashes; - public ConcurrentDoublyLinkedHashedDequeue() { + public ConcurrentLinkedHashedDequeue() { this.head = new AtomicReference<>(null); this.tail = new AtomicReference<>(null); this.hashes = ConcurrentHashMap.newKeySet(); diff --git a/src/test/java/it/auties/whatsapp/local/WebRunner.java b/src/test/java/it/auties/whatsapp/local/WebRunner.java index 928b7df8..4abe00e7 100644 --- a/src/test/java/it/auties/whatsapp/local/WebRunner.java +++ b/src/test/java/it/auties/whatsapp/local/WebRunner.java @@ -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()))