From e19647f6cabdb45daa6ea7ffee294cba52e2f15e Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 9 Sep 2022 16:26:52 +0200 Subject: [PATCH] Firebase provides Map --- .../app/push/fcm/VectorFirebaseMessagingService.kt | 4 +--- .../java/im/vector/app/core/pushers/PushParser.kt | 13 ++++--------- .../im/vector/app/core/pushers/PushParserTest.kt | 14 +++++++------- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/vector/src/gplay/java/im/vector/app/push/fcm/VectorFirebaseMessagingService.kt b/vector/src/gplay/java/im/vector/app/push/fcm/VectorFirebaseMessagingService.kt index ddbe8b1292..7fd55bd165 100644 --- a/vector/src/gplay/java/im/vector/app/push/fcm/VectorFirebaseMessagingService.kt +++ b/vector/src/gplay/java/im/vector/app/push/fcm/VectorFirebaseMessagingService.kt @@ -57,10 +57,8 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() { override fun onMessageReceived(message: RemoteMessage) { Timber.tag(loggerTag.value).d("New Firebase message") - pushParser.parsePushDataFcm(message.data)?.let { + pushParser.parsePushDataFcm(message.data).let { vectorPushHandler.handle(it) - } ?: run { - Timber.tag(loggerTag.value).w("Invalid received data Json format") } } } diff --git a/vector/src/main/java/im/vector/app/core/pushers/PushParser.kt b/vector/src/main/java/im/vector/app/core/pushers/PushParser.kt index 6de8935315..8c18295c45 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/PushParser.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/PushParser.kt @@ -46,16 +46,11 @@ class PushParser @Inject constructor() { } } - fun parsePushDataFcm(message: Map<*, *>): PushData? { - if ((message.containsKey("event_id") && message["event_id"] !is String) || - message.containsKey("room_id") && message["room_id"] !is String || - message.containsKey("unread") && message["unread"] !is Int) { - return null - } + fun parsePushDataFcm(message: Map): PushData { val pushDataFcm = PushDataFcm( - eventId = message["event_id"] as String?, - roomId = message["room_id"] as String?, - unread = message["unread"] as Int?, + eventId = message["event_id"], + roomId = message["room_id"], + unread = message["unread"]?.let { tryOrNull { Integer.parseInt(it) } }, ) return pushDataFcm.toPushData() } diff --git a/vector/src/test/java/im/vector/app/core/pushers/PushParserTest.kt b/vector/src/test/java/im/vector/app/core/pushers/PushParserTest.kt index 9dcaa992f8..c0f6c33da8 100644 --- a/vector/src/test/java/im/vector/app/core/pushers/PushParserTest.kt +++ b/vector/src/test/java/im/vector/app/core/pushers/PushParserTest.kt @@ -38,13 +38,11 @@ class PushParserTest { fun `test edge cases Firebase`() { val pushParser = PushParser() // Empty Json - pushParser.parsePushDataFcm(emptyMap()) shouldBeEqualTo emptyData + pushParser.parsePushDataFcm(emptyMap()) shouldBeEqualTo emptyData // Bad Json - pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("room_id", 5)) shouldBe null - pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("event_id", 5)) shouldBe null - pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("unread", "str")) shouldBe null + pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("unread", "str")) shouldBeEqualTo validData.copy(unread = null) // Extra data - pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("extra", 5)) shouldBeEqualTo validData + pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("extra", "5")) shouldBeEqualTo validData } @Test @@ -74,6 +72,7 @@ class PushParserTest { fun `test empty roomId`() { val pushParser = PushParser() val expected = validData.copy(roomId = null) + pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("room_id", null)) shouldBeEqualTo expected pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("room_id", "")) shouldBeEqualTo expected pushParser.parsePushDataUnifiedPush(UNIFIED_PUSH_DATA.replace("!aRoomId:domain", "").toByteArray()) shouldBeEqualTo expected } @@ -90,6 +89,7 @@ class PushParserTest { fun `test empty eventId`() { val pushParser = PushParser() val expected = validData.copy(eventId = null) + pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("event_id", null)) shouldBeEqualTo expected pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("event_id", "")) shouldBeEqualTo expected pushParser.parsePushDataUnifiedPush(UNIFIED_PUSH_DATA.mutate("\$anEventId", "")) shouldBeEqualTo expected } @@ -108,13 +108,13 @@ class PushParserTest { private val FIREBASE_PUSH_DATA = mapOf( "event_id" to "\$anEventId", "room_id" to "!aRoomId:domain", - "unread" to 1, + "unread" to "1", "prio" to "high", ) } } -private fun Map.mutate(key: K, value: V?): Map { +private fun Map.mutate(key: String, value: String?): Map { return toMutableMap().apply { put(key, value) } }