Firebase provides Map<String, String?>

This commit is contained in:
Benoit Marty 2022-09-09 16:26:52 +02:00
parent bdff23a74d
commit e19647f6ca
3 changed files with 12 additions and 19 deletions

View file

@ -57,10 +57,8 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage) { override fun onMessageReceived(message: RemoteMessage) {
Timber.tag(loggerTag.value).d("New Firebase message") Timber.tag(loggerTag.value).d("New Firebase message")
pushParser.parsePushDataFcm(message.data)?.let { pushParser.parsePushDataFcm(message.data).let {
vectorPushHandler.handle(it) vectorPushHandler.handle(it)
} ?: run {
Timber.tag(loggerTag.value).w("Invalid received data Json format")
} }
} }
} }

View file

@ -46,16 +46,11 @@ class PushParser @Inject constructor() {
} }
} }
fun parsePushDataFcm(message: Map<*, *>): PushData? { fun parsePushDataFcm(message: Map<String, String?>): 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
}
val pushDataFcm = PushDataFcm( val pushDataFcm = PushDataFcm(
eventId = message["event_id"] as String?, eventId = message["event_id"],
roomId = message["room_id"] as String?, roomId = message["room_id"],
unread = message["unread"] as Int?, unread = message["unread"]?.let { tryOrNull { Integer.parseInt(it) } },
) )
return pushDataFcm.toPushData() return pushDataFcm.toPushData()
} }

View file

@ -38,13 +38,11 @@ class PushParserTest {
fun `test edge cases Firebase`() { fun `test edge cases Firebase`() {
val pushParser = PushParser() val pushParser = PushParser()
// Empty Json // Empty Json
pushParser.parsePushDataFcm(emptyMap<String, Any>()) shouldBeEqualTo emptyData pushParser.parsePushDataFcm(emptyMap()) shouldBeEqualTo emptyData
// Bad Json // Bad Json
pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("room_id", 5)) shouldBe null pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("unread", "str")) shouldBeEqualTo validData.copy(unread = null)
pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("event_id", 5)) shouldBe null
pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("unread", "str")) shouldBe null
// Extra data // Extra data
pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("extra", 5)) shouldBeEqualTo validData pushParser.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("extra", "5")) shouldBeEqualTo validData
} }
@Test @Test
@ -74,6 +72,7 @@ class PushParserTest {
fun `test empty roomId`() { fun `test empty roomId`() {
val pushParser = PushParser() val pushParser = PushParser()
val expected = validData.copy(roomId = null) 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.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("room_id", "")) shouldBeEqualTo expected
pushParser.parsePushDataUnifiedPush(UNIFIED_PUSH_DATA.replace("!aRoomId:domain", "").toByteArray()) shouldBeEqualTo expected pushParser.parsePushDataUnifiedPush(UNIFIED_PUSH_DATA.replace("!aRoomId:domain", "").toByteArray()) shouldBeEqualTo expected
} }
@ -90,6 +89,7 @@ class PushParserTest {
fun `test empty eventId`() { fun `test empty eventId`() {
val pushParser = PushParser() val pushParser = PushParser()
val expected = validData.copy(eventId = null) 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.parsePushDataFcm(FIREBASE_PUSH_DATA.mutate("event_id", "")) shouldBeEqualTo expected
pushParser.parsePushDataUnifiedPush(UNIFIED_PUSH_DATA.mutate("\$anEventId", "")) shouldBeEqualTo expected pushParser.parsePushDataUnifiedPush(UNIFIED_PUSH_DATA.mutate("\$anEventId", "")) shouldBeEqualTo expected
} }
@ -108,13 +108,13 @@ class PushParserTest {
private val FIREBASE_PUSH_DATA = mapOf( private val FIREBASE_PUSH_DATA = mapOf(
"event_id" to "\$anEventId", "event_id" to "\$anEventId",
"room_id" to "!aRoomId:domain", "room_id" to "!aRoomId:domain",
"unread" to 1, "unread" to "1",
"prio" to "high", "prio" to "high",
) )
} }
} }
private fun <K, V> Map<K, V?>.mutate(key: K, value: V?): Map<K, V?> { private fun Map<String, String?>.mutate(key: String, value: String?): Map<String, String?> {
return toMutableMap().apply { put(key, value) } return toMutableMap().apply { put(key, value) }
} }