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) {
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")
}
}
}

View file

@ -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<String, String?>): 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()
}

View file

@ -38,13 +38,11 @@ class PushParserTest {
fun `test edge cases Firebase`() {
val pushParser = PushParser()
// Empty Json
pushParser.parsePushDataFcm(emptyMap<String, Any>()) 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 <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) }
}