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 / WhatsAppMex.kt
WhatsAppMex.kt37 lines · 1.5 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp.mex |
| 2 | |
| 3 | import android.content.Context |
| 4 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppClient |
| 5 | |
| 6 | /** |
| 7 | * MEX transport router (w2.md §5.1). Prefers the `w:mex` XMPP path over the live Noise socket and |
| 8 | * falls back to the WWW GraphQL HTTP client when disconnected — the same MEX-preferred / WWW-fallback |
| 9 | * split the official client uses (minus the Pando C++ path, which is out of scope). |
| 10 | * |
| 11 | * All calls resolve the same persisted-query [operationName] → doc_id; an uncaptured operation |
| 12 | * returns a typed `no_persisted_id:<op>` [MexResult] on either path (never crashes). |
| 13 | * |
| 14 | * Dev-only: callers go through `CommunicateRepository`, which gates every entry point on |
| 15 | * `WhatsAppFeature.enabled`. |
| 16 | */ |
| 17 | object WhatsAppMex { |
| 18 | |
| 19 | /** |
| 20 | * Execute [operationName] with the given `variables` JSON. Uses XMPP when |
| 21 | * [WhatsAppClient.isConnected]; otherwise resolves the doc_id and posts to WWW GraphQL. |
| 22 | */ |
| 23 | suspend fun call( |
| 24 | context: Context, |
| 25 | operationName: String, |
| 26 | variablesJson: String, |
| 27 | type: String = "get", |
| 28 | ): MexResult { |
| 29 | return if (WhatsAppClient.isConnected()) { |
| 30 | WhatsAppClient.mexCall(operationName, variablesJson, type) |
| 31 | } else { |
| 32 | val docId = MexPersistedQueryProvider.docIdFor(context, operationName) |
| 33 | ?: return MexResult.transport("no_persisted_id:$operationName") |
| 34 | WhatsAppWwwGraphQlClient.post(docId, variablesJson) |
| 35 | } |
| 36 | } |
| 37 | } |