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 / MexPersistedQueryProvider.kt
MexPersistedQueryProvider.kt71 lines · 2.9 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp.mex |
| 2 | |
| 3 | import android.content.Context |
| 4 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppDiag |
| 5 | import kotlinx.serialization.json.Json |
| 6 | import kotlinx.serialization.json.jsonObject |
| 7 | import kotlinx.serialization.json.jsonPrimitive |
| 8 | import java.util.concurrent.ConcurrentHashMap |
| 9 | |
| 10 | /** |
| 11 | * Resolves a MEX/GraphQL **persisted-query `doc_id`** from a GraphQL **operation name**. |
| 12 | * |
| 13 | * MEX never sends raw GraphQL text — the wire `query_id`/`doc_id` is a server-assigned 17-digit id |
| 14 | * keyed by GraphQL operation name (see w2.md §5.5). The official client resolves |
| 15 | * `operationName → doc_id` from a bundled assets JSON of the shape |
| 16 | * `{"version":1,"data":{OperationName: docId, …}}`. This class replicates that, backed by |
| 17 | * `assets/whatsapp/mex_persist_ids.json`, seeded with the 7 real committed ids from §5.5. |
| 18 | * |
| 19 | * Operations whose id is not present return `null` (callers surface a typed |
| 20 | * `no_persisted_id:<op>` failure). This deliberately does NOT crash like the official debug |
| 21 | * client's `MexPersistedIdNotFoundFailure` — a dev can drop a captured `operationName:doc_id` pair |
| 22 | * into the JSON to enable an operation without touching code. |
| 23 | */ |
| 24 | object MexPersistedQueryProvider { |
| 25 | |
| 26 | private const val TAG = "WAMex" |
| 27 | private const val ASSET_PATH = "whatsapp/mex_persist_ids.json" |
| 28 | |
| 29 | private val json = Json { ignoreUnknownKeys = true; isLenient = true } |
| 30 | |
| 31 | /** Cached `operationName → doc_id` map; loaded lazily from the asset on first use. */ |
| 32 | @Volatile |
| 33 | private var cache: Map<String, String>? = null |
| 34 | |
| 35 | /** |
| 36 | * The doc_id for [operationName], or null when it isn't in the bundled JSON (uncaptured op). |
| 37 | */ |
| 38 | fun docIdFor(context: Context, operationName: String): String? { |
| 39 | val map = cache ?: loadFromAsset(context).also { cache = it } |
| 40 | return map[operationName] |
| 41 | } |
| 42 | |
| 43 | /** Test/refresh hook: drop the cached map so the next [docIdFor] re-reads the asset. */ |
| 44 | fun invalidate() { |
| 45 | cache = null |
| 46 | } |
| 47 | |
| 48 | private fun loadFromAsset(context: Context): Map<String, String> { |
| 49 | return try { |
| 50 | val text = context.assets.open(ASSET_PATH).use { it.readBytes().toString(Charsets.UTF_8) } |
| 51 | parsePersistIds(text) |
| 52 | } catch (expected: Throwable) { |
| 53 | WhatsAppDiag.log(TAG, "persist-ids: failed to load $ASSET_PATH: ${expected.message}") |
| 54 | emptyMap() |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Pure parser for the `{"version":1,"data":{name:docId}}` shape. Kept JVM-pure (kotlinx only) |
| 60 | * so it is unit-testable without Android. Returns an empty map on malformed input. |
| 61 | */ |
| 62 | fun parsePersistIds(jsonText: String): Map<String, String> { |
| 63 | return try { |
| 64 | val root = json.parseToJsonElement(jsonText).jsonObject |
| 65 | val data = root["data"]?.jsonObject ?: return emptyMap() |
| 66 | data.mapValues { (_, v) -> v.jsonPrimitive.content } |
| 67 | } catch (ignored: Throwable) { |
| 68 | emptyMap() |
| 69 | } |
| 70 | } |
| 71 | } |