Skip to content
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

fix(Android): parse join responses correctly #598

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading