From f8218eb30e9995f6d84bac774b9e77ced6837870 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Thu, 19 Dec 2024 09:06:22 -0700 Subject: [PATCH] fix(Android): parse join responses correctly (#598) --- .../tid/mbta_app/android/util/SocketMessage.kt | 15 +++++++++++++-- .../mbta_app/android/util/SocketMessageTest.kt | 9 +-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/androidApp/src/main/java/com/mbta/tid/mbta_app/android/util/SocketMessage.kt b/androidApp/src/main/java/com/mbta/tid/mbta_app/android/util/SocketMessage.kt index caf181c00..467e9f19b 100644 --- a/androidApp/src/main/java/com/mbta/tid/mbta_app/android/util/SocketMessage.kt +++ b/androidApp/src/main/java/com/mbta/tid/mbta_app/android/util/SocketMessage.kt @@ -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 @@ -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 ) } diff --git a/androidApp/src/test/java/com/mbta/tid/mbta_app/android/util/SocketMessageTest.kt b/androidApp/src/test/java/com/mbta/tid/mbta_app/android/util/SocketMessageTest.kt index 690659e02..4df73ca28 100644 --- a/androidApp/src/test/java/com/mbta/tid/mbta_app/android/util/SocketMessageTest.kt +++ b/androidApp/src/test/java/com/mbta/tid/mbta_app/android/util/SocketMessageTest.kt @@ -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":{}}]""" )