Repository navigation
feat(voip): migrate Android accept/reject from DDP to REST #7127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6fe4e3d
feat(voip): add MediaCallsAnswerRequest for REST accept/reject
diegolmello 7142957
feat(voip): migrate handleDeclineAction from DDP to REST
diegolmello e129249
feat(voip): migrate handleAcceptAction from DDP to REST
diegolmello 1f27a7b
feat(voip): migrate rejectBusyCall from DDP to REST
diegolmello 2188f77
feat(voip): remove dead DDP accept/reject methods from VoipNotification
diegolmello b85201a
fix(voip): reuse OkHttpClient and close response body in MediaCallsAn…
diegolmello dbdda14
fix(voip): post onResult to main thread in MediaCallsAnswerRequest
diegolmello a55784d
fix(voip): remove invalid "type" field from media-calls.answer body
diegolmello 0541978
Merge branch 'feat.voip-lib-new' into refactor.ddp-android-2
diegolmello b61b4a9
Merge branch 'feat.voip-lib-new' into refactor.ddp-android-2
diegolmello 20c441e
fix(voip): tear down DDP client on Android decline after REST reject
diegolmello d501fe2
fix(voip): main-thread answer callbacks, REST KDoc, minimal failure logs
diegolmello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
android/app/src/main/java/chat/rocket/reactnative/voip/MediaCallsAnswerRequest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package chat.rocket.reactnative.voip | ||
|
|
||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import android.util.Log | ||
| import chat.rocket.reactnative.notification.Ejson | ||
| import okhttp3.Call | ||
| import okhttp3.Callback | ||
| import okhttp3.MediaType.Companion.toMediaType | ||
| import okhttp3.OkHttpClient | ||
| import okhttp3.Request | ||
| import okhttp3.RequestBody.Companion.toRequestBody | ||
| import org.json.JSONArray | ||
| import org.json.JSONObject | ||
| import java.io.IOException | ||
|
|
||
| /** | ||
| * REST client for `POST /api/v1/media-calls.answer` used by accept/reject flows. | ||
| * | ||
| * Mirrors the iOS [MediaCallsAnswerRequest.swift][ios/Shared/RocketChat/API/MediaCallsAnswerRequest.swift] | ||
| * and replaces the DDP `sendAcceptSignal` / `sendRejectSignal` / `queueAcceptSignal` / `queueRejectSignal` | ||
| * methods in [VoipNotification]. | ||
| * | ||
| * Auth headers (`x-user-id` / `x-auth-token`) are resolved from [Ejson] at call time, | ||
| * matching the pattern used by [chat.rocket.reactnative.notification.ReplyBroadcast]. | ||
| * | ||
| * [fetch]'s `onResult` is always invoked on the main thread. | ||
| * | ||
| * @param callId The call identifier from the VoIP payload. | ||
| * @param contractId The device-unique contract identifier (`Settings.Secure.ANDROID_ID`). | ||
| * @param answer Either `"accept"` or `"reject"`. | ||
| * @param supportedFeatures Optional list of supported features (e.g. `["audio"]`); sent only for accept. | ||
| */ | ||
| class MediaCallsAnswerRequest( | ||
| private val callId: String, | ||
| private val contractId: String, | ||
| private val answer: String, | ||
| private val supportedFeatures: List<String>? = null | ||
| ) { | ||
| companion object { | ||
| private const val TAG = "RocketChat.MediaCallsAnswerRequest" | ||
| private val JSON_MEDIA_TYPE = "application/json; charset=utf-8".toMediaType() | ||
| private val httpClient = OkHttpClient() | ||
| private val mainHandler = Handler(Looper.getMainLooper()) | ||
|
|
||
| @JvmStatic | ||
| fun fetch( | ||
| context: android.content.Context, | ||
| host: String, | ||
| callId: String, | ||
| contractId: String, | ||
| answer: String, | ||
| supportedFeatures: List<String>? = null, | ||
| onResult: (Boolean) -> Unit | ||
| ) { | ||
| val request = MediaCallsAnswerRequest(callId, contractId, answer, supportedFeatures) | ||
| request.execute(context, host, onResult) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Builds the JSON body for the request: | ||
| * ```json | ||
| * { | ||
| * "callId": "<callId>", | ||
| * "contractId": "<contractId>", | ||
| * "answer": "<accept|reject>", | ||
| * "supportedFeatures": ["audio"] // only when non-null for accept | ||
| * } | ||
| * ``` | ||
| */ | ||
| private fun buildBody(): JSONObject { | ||
| val json = JSONObject().apply { | ||
| put("callId", callId) | ||
| put("contractId", contractId) | ||
| put("answer", answer) | ||
| } | ||
| supportedFeatures?.let { features -> | ||
| val arr = JSONArray() | ||
| features.forEach { arr.put(it) } | ||
| json.put("supportedFeatures", arr) | ||
| } | ||
| return json | ||
| } | ||
|
|
||
| private fun execute( | ||
| context: android.content.Context, | ||
| host: String, | ||
| onResult: (Boolean) -> Unit | ||
| ) { | ||
| val ejson = Ejson().apply { this.host = host } | ||
| val userId = ejson.userId() | ||
| val token = ejson.token() | ||
|
|
||
| if (userId.isNullOrEmpty() || token.isNullOrEmpty()) { | ||
| Log.w(TAG, "Missing credentials for $host — cannot send media-call answer") | ||
| mainHandler.post { onResult(false) } | ||
| return | ||
| } | ||
|
|
||
| val serverUrl = host.removeSuffix("/") | ||
| val url = "$serverUrl/api/v1/media-calls.answer" | ||
|
|
||
| val body = buildBody().toString() | ||
| val requestBody = body.toRequestBody(JSON_MEDIA_TYPE) | ||
|
|
||
| val request = Request.Builder() | ||
| .header("x-user-id", userId) | ||
| .header("x-auth-token", token) | ||
| .url(url) | ||
| .post(requestBody) | ||
| .build() | ||
|
|
||
| httpClient.newCall(request).enqueue(object : Callback { | ||
| override fun onFailure(call: Call, e: IOException) { | ||
| Log.e(TAG, "MediaCallsAnswerRequest failed for callId=$callId: ${e.message}") | ||
| mainHandler.post { onResult(false) } | ||
| } | ||
|
|
||
| override fun onResponse(call: Call, response: okhttp3.Response) { | ||
| response.use { | ||
| val code = it.code | ||
| val success = code in 200..299 | ||
| if (success) { | ||
| Log.d(TAG, "MediaCallsAnswerRequest response for callId=$callId: code=$code success=true") | ||
| } else { | ||
| Log.w(TAG, "MediaCallsAnswerRequest failed callId=$callId code=$code answer=$answer") | ||
| } | ||
| mainHandler.post { onResult(success) } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.