Skip to content

Commit

Permalink
fix(Android): parse join responses correctly (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
boringcactus authored and KaylaBrady committed Dec 30, 2024
1 parent de61dc0 commit f8218eb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package com.mbta.tid.mbta_app.android.util
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import org.phoenixframework.Message
import org.phoenixframework.Payload

fun decodeMessage(rawMessage: String): Message {
val message = Json.parseToJsonElement(rawMessage).jsonArray
Expand All @@ -14,14 +16,23 @@ fun decodeMessage(rawMessage: String): Message {
val topic = message[2].jsonPrimitive.contentOrNull
val event = message[3].jsonPrimitive.contentOrNull

val payloadJson = message[4].toString()
val rawPayload: Payload
val payloadJson: String
if (message[4].jsonObject.keys == setOf("status", "response")) {
rawPayload =
mapOf("status" to message[4].jsonObject["status"]?.jsonPrimitive?.contentOrNull)
payloadJson = message[4].jsonObject["response"]?.toString() ?: "{}"
} else {
rawPayload = emptyMap()
payloadJson = message[4].toString()
}

return Message(
joinRef = joinRef,
ref = ref ?: "",
topic = topic ?: "",
event = event ?: "",
rawPayload = emptyMap(),
rawPayload = rawPayload,
payloadJson = payloadJson
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,7 @@ class SocketMessageTest {

// data observed in practice
assertEquals(
Message(
"0",
"0",
"predictions:stops",
"phx_reply",
emptyMap(),
"""{"status":"ok","response":{}}"""
),
Message("0", "0", "predictions:stops", "phx_reply", mapOf("status" to "ok"), "{}"),
decodeMessage(
"""["0","0","predictions:stops","phx_reply",{"status":"ok","response":{}}]"""
)
Expand Down

0 comments on commit f8218eb

Please sign in to comment.