diff --git a/src/main/java/it/auties/whatsapp/api/Whatsapp.java b/src/main/java/it/auties/whatsapp/api/Whatsapp.java index 6de2fefe..bb6611a1 100644 --- a/src/main/java/it/auties/whatsapp/api/Whatsapp.java +++ b/src/main/java/it/auties/whatsapp/api/Whatsapp.java @@ -587,7 +587,7 @@ private CompletableFuture 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); } @@ -1282,7 +1282,7 @@ public CompletableFuture 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); } @@ -1303,7 +1303,7 @@ public CompletableFuture 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); } @@ -1406,7 +1406,7 @@ private CompletableFuture 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); } @@ -1430,7 +1430,7 @@ private CompletableFuture 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); @@ -1482,7 +1482,7 @@ private CompletableFuture 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); } @@ -1557,7 +1557,7 @@ public CompletableFuture 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); } @@ -1608,7 +1608,7 @@ public CompletableFuture 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); } @@ -1633,7 +1633,7 @@ public CompletableFuture 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); } @@ -2176,12 +2176,12 @@ private List 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() { @@ -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, "keep@s.whatsapp.net"); + return PatchEntry.of(timeFormatSync, Operation.SET, "keep@s.whatsapp.net"); } 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() { @@ -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 sendRecentMessage(Jid jid) { diff --git a/src/main/java/it/auties/whatsapp/model/sync/PatchRequest.java b/src/main/java/it/auties/whatsapp/model/sync/PatchRequest.java index f65ae687..b6a4c506 100644 --- a/src/main/java/it/auties/whatsapp/model/sync/PatchRequest.java +++ b/src/main/java/it/auties/whatsapp/model/sync/PatchRequest.java @@ -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 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 createArguments(ActionValueSync sync, String... args) { diff --git a/src/main/java/it/auties/whatsapp/socket/AppStateHandler.java b/src/main/java/it/auties/whatsapp/socket/AppStateHandler.java index 3b010e6f..6bfeda55 100644 --- a/src/main/java/it/auties/whatsapp/socket/AppStateHandler.java +++ b/src/main/java/it/auties/whatsapp/socket/AppStateHandler.java @@ -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());