Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / mex / WhatsAppMexVariables.kt
WhatsAppMexVariables.kt239 lines · 7.7 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp.mex |
| 2 | |
| 3 | import kotlinx.serialization.json.Json |
| 4 | import kotlinx.serialization.json.JsonObject |
| 5 | import kotlinx.serialization.json.add |
| 6 | import kotlinx.serialization.json.addJsonObject |
| 7 | import kotlinx.serialization.json.buildJsonObject |
| 8 | import kotlinx.serialization.json.put |
| 9 | import kotlinx.serialization.json.putJsonArray |
| 10 | import kotlinx.serialization.json.putJsonObject |
| 11 | import java.util.Base64 as JavaBase64 |
| 12 | |
| 13 | /** |
| 14 | * JVM-pure `variables` builders for the MEX operations in [WhatsAppMexOps]. |
| 15 | * |
| 16 | * Shapes follow the SDL argument names from w2.md §8.0 (a single `variables` map keyed by |
| 17 | * argument name); JIDs and binary fields are standard Base64 (Argo BYTES over the JSON shim). |
| 18 | * Kept separate from the operations so the builders are unit-testable off-device. |
| 19 | */ |
| 20 | object WhatsAppMexVariables { |
| 21 | |
| 22 | private const val PREKEY_VERSION_BYTE = 0x05 |
| 23 | private const val SHIFT_BYTE2 = 16 |
| 24 | private const val SHIFT_BYTE1 = 8 |
| 25 | |
| 26 | private val json = Json { encodeDefaults = true } |
| 27 | |
| 28 | fun buildGroupQueryByIdVariables(groupJid: String, queryContext: String): String = |
| 29 | json.encodeToString( |
| 30 | JsonObject.serializer(), |
| 31 | buildJsonObject { |
| 32 | putJsonObject("group_input") { |
| 33 | put("group_jid", groupJid) |
| 34 | put("query_context", queryContext) |
| 35 | } |
| 36 | }, |
| 37 | ) |
| 38 | |
| 39 | fun buildParticipatingGroupsVariables(userJid: String?): String = |
| 40 | json.encodeToString( |
| 41 | JsonObject.serializer(), |
| 42 | buildJsonObject { |
| 43 | putJsonObject("participating_groups_input") { |
| 44 | if (userJid != null) put("id", userJid) |
| 45 | } |
| 46 | }, |
| 47 | ) |
| 48 | |
| 49 | fun buildGroupBatchQueryVariables(groupJids: List<String>, queryContext: String): String = |
| 50 | json.encodeToString( |
| 51 | JsonObject.serializer(), |
| 52 | buildJsonObject { |
| 53 | putJsonObject("batch_query_input") { |
| 54 | putJsonArray("groups") { |
| 55 | groupJids.forEach { jid -> |
| 56 | addJsonObject { |
| 57 | put("group_jid", jid) |
| 58 | put("query_context", queryContext) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | }, |
| 64 | ) |
| 65 | |
| 66 | fun buildContactDiscoveryVariables(rawPhoneNumbers: List<String>, discoveryContext: String): String = |
| 67 | json.encodeToString( |
| 68 | JsonObject.serializer(), |
| 69 | buildJsonObject { |
| 70 | putJsonObject("input") { |
| 71 | putJsonArray("contacts") { |
| 72 | rawPhoneNumbers.forEach { pn -> |
| 73 | addJsonObject { putJsonObject("phone") { put("raw_pn", pn) } } |
| 74 | } |
| 75 | } |
| 76 | put("context", discoveryContext) |
| 77 | } |
| 78 | }, |
| 79 | ) |
| 80 | |
| 81 | fun buildPrimaryContactsFullSyncVariables( |
| 82 | rawPhoneNumbers: List<String>, |
| 83 | sessionId: String, |
| 84 | pageIndex: Int, |
| 85 | last: Boolean, |
| 86 | syncContext: String, |
| 87 | ): String = json.encodeToString( |
| 88 | JsonObject.serializer(), |
| 89 | buildJsonObject { |
| 90 | putJsonObject("input") { |
| 91 | putJsonArray("primary_contacts") { |
| 92 | rawPhoneNumbers.forEach { pn -> |
| 93 | addJsonObject { putJsonObject("phone") { put("raw_pn", pn) } } |
| 94 | } |
| 95 | } |
| 96 | putJsonObject("cursor") { |
| 97 | put("session_id", sessionId) |
| 98 | put("page_index", pageIndex) |
| 99 | put("last", last) |
| 100 | } |
| 101 | put("context", syncContext) |
| 102 | } |
| 103 | }, |
| 104 | ) |
| 105 | |
| 106 | fun buildUsernameCheckVariables( |
| 107 | username: String, |
| 108 | includeSuggestions: Boolean, |
| 109 | source: String, |
| 110 | sessionId: String?): String = |
| 111 | json.encodeToString( |
| 112 | JsonObject.serializer(), |
| 113 | buildJsonObject { |
| 114 | put("username", username) |
| 115 | put("include_suggestions", includeSuggestions) |
| 116 | put("source", source) |
| 117 | if (sessionId != null) put("session_id", sessionId) |
| 118 | }, |
| 119 | ) |
| 120 | |
| 121 | fun buildUsernameSetVariables(username: String, pin: String?, source: String, sessionId: String?): String = |
| 122 | json.encodeToString( |
| 123 | JsonObject.serializer(), |
| 124 | buildJsonObject { |
| 125 | put("username", username) |
| 126 | put("source", source) |
| 127 | if (pin != null) put("pin", pin) |
| 128 | if (sessionId != null) put("session_id", sessionId) |
| 129 | }, |
| 130 | ) |
| 131 | |
| 132 | fun buildBlocklistGetVariables(dhash: String?): String = |
| 133 | json.encodeToString( |
| 134 | JsonObject.serializer(), |
| 135 | buildJsonObject { if (dhash != null) put("dhash", dhash) }, |
| 136 | ) |
| 137 | |
| 138 | fun buildUpdateBlocklistLidVariables( |
| 139 | userLid: String, |
| 140 | identifierKey: String, |
| 141 | identifierValue: String, |
| 142 | action: String, |
| 143 | dhash: String?, |
| 144 | ): String = json.encodeToString( |
| 145 | JsonObject.serializer(), |
| 146 | buildJsonObject { |
| 147 | putJsonObject("block_input") { |
| 148 | put("user", userLid) |
| 149 | putJsonObject("identifier") { put(identifierKey, identifierValue) } |
| 150 | put("action", action) |
| 151 | if (dhash != null) put("dhash", dhash) |
| 152 | } |
| 153 | }, |
| 154 | ) |
| 155 | |
| 156 | fun buildOnlineOrLastStatusVariables(lidJids: List<String>, lastActiveFilter: String?): String = |
| 157 | json.encodeToString( |
| 158 | JsonObject.serializer(), |
| 159 | buildJsonObject { |
| 160 | putJsonObject("get_online_or_last_status_input") { |
| 161 | putJsonArray("online_or_last_status_input") { |
| 162 | lidJids.forEach { jid -> addJsonObject { put("jid", jid) } } |
| 163 | } |
| 164 | if (lastActiveFilter != null) put("last_active_filter", lastActiveFilter) |
| 165 | } |
| 166 | }, |
| 167 | ) |
| 168 | |
| 169 | fun buildNewsletterVariables(key: String, keyType: String, viewRole: String?): String = |
| 170 | json.encodeToString( |
| 171 | JsonObject.serializer(), |
| 172 | buildJsonObject { |
| 173 | putJsonObject("input") { |
| 174 | put("key", key) |
| 175 | put("type", keyType) |
| 176 | if (viewRole != null) put("view_role", viewRole) |
| 177 | } |
| 178 | }, |
| 179 | ) |
| 180 | |
| 181 | fun buildNewsletterIdVariables(newsletterId: String): String = |
| 182 | json.encodeToString( |
| 183 | JsonObject.serializer(), |
| 184 | buildJsonObject { put("newsletter_id", newsletterId) }, |
| 185 | ) |
| 186 | |
| 187 | fun buildGetMessagingKeysVariables(userJids: List<String>): String = |
| 188 | json.encodeToString( |
| 189 | JsonObject.serializer(), |
| 190 | buildJsonObject { |
| 191 | putJsonObject("input") { |
| 192 | putJsonArray("users") { userJids.forEach { add(it) } } |
| 193 | } |
| 194 | }, |
| 195 | ) |
| 196 | |
| 197 | /** |
| 198 | * Pure builder for the `xwa2_set_messaging_keys` `variables` (§8.2). Binary fields are standard |
| 199 | * Base64; ids are 3-byte big-endian (skey/prekey id space), `type` is Base64 of `0x05`. |
| 200 | */ |
| 201 | fun buildSetMessagingKeysVariables( |
| 202 | identity: ByteArray, |
| 203 | skeyId: Int, |
| 204 | skeyValue: ByteArray, |
| 205 | skeySignature: ByteArray, |
| 206 | prekeys: List<Pair<Int, ByteArray>>, |
| 207 | ): String = json.encodeToString( |
| 208 | JsonObject.serializer(), |
| 209 | buildJsonObject { |
| 210 | putJsonObject("input") { |
| 211 | put("type", b64(byteArrayOf(PREKEY_VERSION_BYTE.toByte()))) |
| 212 | put("identity", b64(identity)) |
| 213 | putJsonObject("skey") { |
| 214 | put("id", b64(int3BE(skeyId))) |
| 215 | put("value", b64(skeyValue)) |
| 216 | put("signature", b64(skeySignature)) |
| 217 | } |
| 218 | putJsonArray("prekeys") { |
| 219 | prekeys.forEach { (id, value) -> |
| 220 | addJsonObject { |
| 221 | put("id", b64(int3BE(id))) |
| 222 | put("value", b64(value)) |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | }, |
| 228 | ) |
| 229 | |
| 230 | /** Standard Base64 (with padding), JVM-pure so the builders are unit-testable off-device. */ |
| 231 | private fun b64(bytes: ByteArray): String = JavaBase64.getEncoder().encodeToString(bytes) |
| 232 | |
| 233 | /** 3-byte big-endian encoding of a prekey/skey id (matches the classic wire encoding). */ |
| 234 | private fun int3BE(id: Int): ByteArray = byteArrayOf( |
| 235 | (id ushr SHIFT_BYTE2).toByte(), |
| 236 | (id ushr SHIFT_BYTE1).toByte(), |
| 237 | id.toByte(), |
| 238 | ) |
| 239 | } |