Skip to content

Commit

Permalink
More work for API stability
Browse files Browse the repository at this point in the history
  • Loading branch information
Auties00 committed Oct 21, 2023
1 parent aed2f21 commit 03cc219
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
28 changes: 14 additions & 14 deletions src/main/java/it/auties/whatsapp/api/Whatsapp.java
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ private CompletableFuture<Void> mark(JidProvider chat, boolean read) {
var range = createRange(chat, false);
var markAction = new MarkChatAsReadAction(read, Optional.of(range));
var syncAction = ActionValueSync.of(markAction);
var entry = PatchEntry.of(syncAction, Operation.SET, 3, chat.toJid().toString());
var entry = PatchEntry.of(syncAction, Operation.SET, chat.toJid().toString());
var request = new PatchRequest(PatchType.REGULAR_HIGH, List.of(entry));
return socketHandler.pushPatch(request);
}
Expand Down Expand Up @@ -1282,7 +1282,7 @@ public CompletableFuture<Void> muteChat(JidProvider chat, ChatMute mute) {
var endTimeStamp = mute.type() == ChatMute.Type.MUTED_FOR_TIMEFRAME ? mute.endTimeStamp() * 1000L : mute.endTimeStamp();
var muteAction = new MuteAction(true, OptionalLong.of(endTimeStamp), false);
var syncAction = ActionValueSync.of(muteAction);
var entry = PatchEntry.of(syncAction, Operation.SET, 2, chat.toJid().toString());
var entry = PatchEntry.of(syncAction, Operation.SET, chat.toJid().toString());
var request = new PatchRequest(PatchType.REGULAR_HIGH, List.of(entry));
return socketHandler.pushPatch(request);
}
Expand All @@ -1303,7 +1303,7 @@ public CompletableFuture<Void> unmuteChat(JidProvider chat) {

var muteAction = new MuteAction(false, null, false);
var syncAction = ActionValueSync.of(muteAction);
var entry = PatchEntry.of(syncAction, Operation.SET, 2, chat.toJid().toString());
var entry = PatchEntry.of(syncAction, Operation.SET, chat.toJid().toString());
var request = new PatchRequest(PatchType.REGULAR_HIGH, List.of(entry));
return socketHandler.pushPatch(request);
}
Expand Down Expand Up @@ -1406,7 +1406,7 @@ private CompletableFuture<Void> pinChat(JidProvider chat, boolean pin) {

var pinAction = new PinAction(pin);
var syncAction = ActionValueSync.of(pinAction);
var entry = PatchEntry.of(syncAction, Operation.SET, 5, chat.toJid().toString());
var entry = PatchEntry.of(syncAction, Operation.SET, chat.toJid().toString());
var request = new PatchRequest(PatchType.REGULAR_LOW, List.of(entry));
return socketHandler.pushPatch(request);
}
Expand All @@ -1430,7 +1430,7 @@ private CompletableFuture<ChatMessageInfo> starMessage(ChatMessageInfo info, boo

var starAction = new StarAction(star);
var syncAction = ActionValueSync.of(starAction);
var entry = PatchEntry.of(syncAction, Operation.SET, 3, info.chatJid()
var entry = PatchEntry.of(syncAction, Operation.SET, info.chatJid()
.toString(), info.id(), fromMeToFlag(info), participantToFlag(info));
var request = new PatchRequest(PatchType.REGULAR_HIGH, List.of(entry));
return socketHandler.pushPatch(request).thenApplyAsync(ignored -> info);
Expand Down Expand Up @@ -1482,7 +1482,7 @@ private CompletableFuture<Void> archiveChat(JidProvider chat, boolean archive) {
var range = createRange(chat, false);
var archiveAction = new ArchiveChatAction(archive, Optional.of(range));
var syncAction = ActionValueSync.of(archiveAction);
var entry = PatchEntry.of(syncAction, Operation.SET, 3, chat.toJid().toString());
var entry = PatchEntry.of(syncAction, Operation.SET, chat.toJid().toString());
var request = new PatchRequest(PatchType.REGULAR_LOW, List.of(entry));
return socketHandler.pushPatch(request);
}
Expand Down Expand Up @@ -1557,7 +1557,7 @@ public CompletableFuture<Void> deleteMessage(ChatMessageInfo messageInfo, boolea
var range = createRange(messageInfo.chatJid(), false);
var deleteMessageAction = new DeleteMessageForMeAction(false, messageInfo.timestampSeconds().orElse(0L));
var syncAction = ActionValueSync.of(deleteMessageAction);
var entry = PatchEntry.of(syncAction, Operation.SET, 3, messageInfo.chatJid().toString(), messageInfo.id(), fromMeToFlag(messageInfo), participantToFlag(messageInfo));
var entry = PatchEntry.of(syncAction, Operation.SET, messageInfo.chatJid().toString(), messageInfo.id(), fromMeToFlag(messageInfo), participantToFlag(messageInfo));
var request = new PatchRequest(PatchType.REGULAR_HIGH, List.of(entry));
yield socketHandler.pushPatch(request);
}
Expand Down Expand Up @@ -1608,7 +1608,7 @@ public CompletableFuture<Void> deleteChat(JidProvider chat) {
var range = createRange(chat.toJid(), false);
var deleteChatAction = new DeleteChatAction(Optional.of(range));
var syncAction = ActionValueSync.of(deleteChatAction);
var entry = PatchEntry.of(syncAction, Operation.SET, 6, chat.toJid().toString(), "1");
var entry = PatchEntry.of(syncAction, Operation.SET, chat.toJid().toString(), "1");
var request = new PatchRequest(PatchType.REGULAR_HIGH, List.of(entry));
return socketHandler.pushPatch(request);
}
Expand All @@ -1633,7 +1633,7 @@ public CompletableFuture<Void> clearChat(JidProvider chat, boolean keepStarredMe
var range = createRange(chat.toJid(), true);
var clearChatAction = new ClearChatAction(Optional.of(range));
var syncAction = ActionValueSync.of(clearChatAction);
var entry = PatchEntry.of(syncAction, Operation.SET, 6, chat.toJid().toString(), booleanToInt(keepStarredMessages), "0");
var entry = PatchEntry.of(syncAction, Operation.SET, chat.toJid().toString(), booleanToInt(keepStarredMessages), "0");
var request = new PatchRequest(PatchType.REGULAR_HIGH, List.of(entry));
return socketHandler.pushPatch(request);
}
Expand Down Expand Up @@ -2176,12 +2176,12 @@ private List<PatchEntry> createContactEntries() {

private PatchEntry createPushNameEntry() {
var pushNameSetting = new PushNameSettings(store().name());
return PatchEntry.of(ActionValueSync.of(pushNameSetting), Operation.SET, 1);
return PatchEntry.of(ActionValueSync.of(pushNameSetting), Operation.SET);
}

private PatchEntry createLocaleEntry() {
var localeSetting = new LocaleSettings(store().locale().orElse("en-US"));
return PatchEntry.of(ActionValueSync.of(localeSetting), Operation.SET, 3);
return PatchEntry.of(ActionValueSync.of(localeSetting), Operation.SET);
}

private PatchEntry createAndroidEntry() {
Expand All @@ -2202,12 +2202,12 @@ private PatchEntry createAndroidEntry() {
private PatchEntry createNuxRequest() {
var nuxAction = new NuxAction(true);
var timeFormatSync = ActionValueSync.of(nuxAction);
return PatchEntry.of(timeFormatSync, Operation.SET, 7, "[email protected]");
return PatchEntry.of(timeFormatSync, Operation.SET, "[email protected]");
}

private PatchEntry createPrimaryVersionEntry(PrimaryVersionAction primaryVersion, String to) {
var timeFormatSync = ActionValueSync.of(primaryVersion);
return PatchEntry.of(timeFormatSync, Operation.SET, 7, to);
return PatchEntry.of(timeFormatSync, Operation.SET, to);
}

private PatchEntry createTimeFormatEntry() {
Expand All @@ -2219,7 +2219,7 @@ private PatchEntry createTimeFormatEntry() {
private PatchEntry createContactRequestEntry(Contact contact) {
var action = new ContactAction(null, contact.shortName(), contact.fullName());
var sync = ActionValueSync.of(action);
return PatchEntry.of(sync, Operation.SET, 2, contact.jid().toString());
return PatchEntry.of(sync, Operation.SET, contact.jid().toString());
}

private CompletableFuture<Void> sendRecentMessage(Jid jid) {
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/it/auties/whatsapp/model/sync/PatchRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@

import it.auties.whatsapp.model.sync.RecordSync.Operation;
import it.auties.whatsapp.util.Json;
import it.auties.whatsapp.util.Specification;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public record PatchRequest(PatchType type, List<PatchEntry> entries) {
public record PatchEntry(ActionValueSync sync, String index, int version, Operation operation) {
public static PatchEntry of(ActionValueSync sync, Operation operation) {
return of(sync, operation, Specification.Signal.CURRENT_VERSION);
}

public static PatchEntry of(ActionValueSync sync, Operation operation, int version, String... args) {
public record PatchEntry(ActionValueSync sync, String index, Operation operation) {
public static PatchEntry of(ActionValueSync sync, Operation operation, String... args) {
var index = Json.writeValueAsString(createArguments(sync, args));
return new PatchEntry(sync, index, version, operation);
return new PatchEntry(sync, index, operation);
}

private static List<String> createArguments(ActionValueSync sync, String... args) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/it/auties/whatsapp/socket/AppStateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,15 @@ private PushRequest createPushRequest(Jid jid, PatchRequest request) {

private MutationResult createMutationSync(PatchEntry patch, MutationKeys mutationKeys, AppStateSyncKey key, KeyId syncId) {
var index = patch.index().getBytes(StandardCharsets.UTF_8);
var actionVersion = patch.sync()
.action()
.orElseThrow(() -> new NoSuchElementException("Missing action"))
.actionVersion();
var actionData = new ActionDataSyncBuilder()
.index(index)
.value(patch.sync())
.padding(new byte[0])
.version(patch.version())
.version(actionVersion)
.build();
var encoded = ActionDataSyncSpec.encode(actionData);
var encrypted = AesCbc.encryptAndPrefix(encoded, mutationKeys.encKey());
Expand Down

0 comments on commit 03cc219

Please sign in to comment.