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 / MexEnvelope.kt
MexEnvelope.kt66 lines · 2.6 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp.mex |
| 2 | |
| 3 | import kotlinx.serialization.json.Json |
| 4 | import kotlinx.serialization.json.JsonElement |
| 5 | import kotlinx.serialization.json.JsonObject |
| 6 | import kotlinx.serialization.json.buildJsonObject |
| 7 | import kotlinx.serialization.json.put |
| 8 | |
| 9 | /** |
| 10 | * Pure (JVM-only, kotlinx.serialization) builders for the two MEX/GraphQL wire envelopes. Kept free |
| 11 | * of any Android dependency so the exact wire shapes can be asserted in JVM unit tests. |
| 12 | * |
| 13 | * - [buildQueryJson] — the `<query>` text node payload for MEX-over-XMPP (w2.md §5.2): |
| 14 | * `{"queryId":"<docId>","variables":{…}}` (queryId is a STRING). |
| 15 | * - [buildWwwBody] — the WWW GraphQL HTTP body (w2.md §4.1 / §5.3): |
| 16 | * `{"access_token":…,"doc_id":<int64>,"lang":…,"Content-Type":"application/json","variables":{…}}` |
| 17 | * (doc_id is a NUMBER; `Content-Type` is intentionally replicated as a JSON key, not only a |
| 18 | * header). |
| 19 | * |
| 20 | * [variablesJson] is a raw JSON object string (e.g. `{"input":{…}}`). Blank/empty is normalized to |
| 21 | * `{}` so an argument-less operation still sends `variables:{}`. |
| 22 | */ |
| 23 | object MexEnvelope { |
| 24 | |
| 25 | private val json = Json { encodeDefaults = true } |
| 26 | |
| 27 | private fun parseVariables(variablesJson: String): JsonElement { |
| 28 | val trimmed = variablesJson.trim() |
| 29 | if (trimmed.isEmpty()) return JsonObject(emptyMap()) |
| 30 | return try { |
| 31 | json.parseToJsonElement(trimmed) |
| 32 | } catch (ignored: Throwable) { |
| 33 | JsonObject(emptyMap()) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /** MEX-over-XMPP `<query>` payload: `{"queryId":"<docId>","variables":{…}}`. */ |
| 38 | fun buildQueryJson(docId: String, variablesJson: String): String { |
| 39 | val obj = buildJsonObject { |
| 40 | put("queryId", docId) |
| 41 | put("variables", parseVariables(variablesJson)) |
| 42 | } |
| 43 | return json.encodeToString(JsonObject.serializer(), obj) |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * WWW GraphQL HTTP body. [docId] is emitted as a JSON number (int64) when it parses as a Long, |
| 48 | * else as a string (defensive — the server expects a number). |
| 49 | */ |
| 50 | fun buildWwwBody( |
| 51 | accessToken: String, |
| 52 | docId: String, |
| 53 | variablesJson: String, |
| 54 | lang: String = "en_US", |
| 55 | ): String { |
| 56 | val obj = buildJsonObject { |
| 57 | put("access_token", accessToken) |
| 58 | val asLong = docId.toLongOrNull() |
| 59 | if (asLong != null) put("doc_id", asLong) else put("doc_id", docId) |
| 60 | put("lang", lang) |
| 61 | put("Content-Type", "application/json") |
| 62 | put("variables", parseVariables(variablesJson)) |
| 63 | } |
| 64 | return json.encodeToString(JsonObject.serializer(), obj) |
| 65 | } |
| 66 | } |