Clarify the data classes for the Json parsing

This commit is contained in:
Benoit Marty 2022-06-01 19:51:26 +02:00 committed by Benoit Marty
parent 45768c5251
commit cdfb728a73
4 changed files with 140 additions and 34 deletions

View File

@ -24,12 +24,14 @@ import android.widget.Toast
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.BuildConfig
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.network.WifiDetector
import im.vector.app.core.pushers.model.PushData
import im.vector.app.core.pushers.model.PushDataFcm
import im.vector.app.core.pushers.model.PushDataUnifiedPush
import im.vector.app.core.pushers.model.toPushData
import im.vector.app.core.services.GuardServiceStarter
import im.vector.app.features.notifications.NotifiableEventResolver
import im.vector.app.features.notifications.NotificationDrawerManager
@ -51,24 +53,6 @@ import org.unifiedpush.android.connector.MessagingReceiver
import timber.log.Timber
import javax.inject.Inject
@JsonClass(generateAdapter = true)
data class UnifiedPushMessage(
val notification: Notification = Notification()
)
@JsonClass(generateAdapter = true)
data class Notification(
@Json(name = "event_id") val eventId: String = "",
@Json(name = "room_id") val roomId: String = "",
var unread: Int = 0,
val counts: Counts = Counts()
)
@JsonClass(generateAdapter = true)
data class Counts(
val unread: Int = 0
)
private val loggerTag = LoggerTag("Push", LoggerTag.SYNC)
/**
@ -113,17 +97,14 @@ class VectorMessagingReceiver : MessagingReceiver() {
}
val moshi = MatrixJsonParser.getMoshi()
val notification: Notification = if (unifiedPushHelper.isEmbeddedDistributor()) {
moshi.adapter(Notification::class.java).fromJson(sMessage)
val pushData = if (unifiedPushHelper.isEmbeddedDistributor()) {
moshi.adapter(PushDataFcm::class.java).fromJson(sMessage)?.toPushData()
} else {
val data = moshi.adapter(UnifiedPushMessage::class.java).fromJson(sMessage)
data?.notification?.also {
it.unread = it.counts.unread
}
} ?: return Unit.also { Timber.w("Invalid received data") }
moshi.adapter(PushDataUnifiedPush::class.java).fromJson(sMessage)?.toPushData()
} ?: return Unit.also { Timber.tag(loggerTag.value).w("Invalid received data Json format") }
// Diagnostic Push
if (notification.eventId == PushersManager.TEST_EVENT_ID) {
if (pushData.eventId == PushersManager.TEST_EVENT_ID) {
val intent = Intent(NotificationUtils.PUSH_ACTION)
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
return
@ -139,7 +120,7 @@ class VectorMessagingReceiver : MessagingReceiver() {
// we are in foreground, let the sync do the things?
Timber.tag(loggerTag.value).d("PUSH received in a foreground state, ignore")
} else {
onMessageReceivedInternal(notification)
onMessageReceivedInternal(pushData)
}
}
}
@ -197,12 +178,12 @@ class VectorMessagingReceiver : MessagingReceiver() {
/**
* Internal receive method.
*
* @param notification Notification containing message data.
* @param pushData Object containing message data.
*/
private fun onMessageReceivedInternal(notification: Notification) {
private fun onMessageReceivedInternal(pushData: PushData) {
try {
if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) {
Timber.tag(loggerTag.value).d("## onMessageReceivedInternal() : $notification")
Timber.tag(loggerTag.value).d("## onMessageReceivedInternal() : $pushData")
} else {
Timber.tag(loggerTag.value).d("## onMessageReceivedInternal()")
}
@ -212,12 +193,12 @@ class VectorMessagingReceiver : MessagingReceiver() {
if (session == null) {
Timber.tag(loggerTag.value).w("## Can't sync from push, no current session")
} else {
if (isEventAlreadyKnown(notification.eventId, notification.roomId)) {
if (isEventAlreadyKnown(pushData.eventId, pushData.roomId)) {
Timber.tag(loggerTag.value).d("Ignoring push, event already known")
} else {
// Try to get the Event content faster
Timber.tag(loggerTag.value).d("Requesting event in fast lane")
getEventFastLane(session, notification.roomId, notification.eventId)
getEventFastLane(session, pushData.roomId, pushData.eventId)
Timber.tag(loggerTag.value).d("Requesting background sync")
session.syncService().requireBackgroundSync()

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.core.pushers.model
data class PushData(
val eventId: String,
val roomId: String,
var unread: Int,
)

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.core.pushers.model
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
/**
* In this case, the format is:
* <pre>
* {
* "event_id":"$anEventId",
* "room_id":"!aRoomId",
* "unread":"1",
* "prio":"high",
* }
* </pre>
*/
@JsonClass(generateAdapter = true)
data class PushDataFcm(
@Json(name = "event_id") val eventId: String = "",
@Json(name = "room_id") val roomId: String = "",
@Json(name = "unread") var unread: Int = 0,
)
fun PushDataFcm.toPushData() = PushData(
eventId = eventId,
roomId = roomId,
unread = unread
)

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.core.pushers.model
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
/**
* In this case, the format is:
* <pre>
* {
* "notification":{
* "event_id":"$anEventId",
* "room_id":"!aRoomId",
* "counts":{
* "unread":1
* },
* "prio":"high",
* }
* }
* </pre>
*/
@JsonClass(generateAdapter = true)
data class PushDataUnifiedPush(
@Json(name = "notification") val notification: PushDataUnifiedPushNotification = PushDataUnifiedPushNotification()
)
@JsonClass(generateAdapter = true)
data class PushDataUnifiedPushNotification(
@Json(name = "event_id") val eventId: String = "",
@Json(name = "room_id") val roomId: String = "",
@Json(name = "counts") var counts: PushDataUnifiedPushCounts = PushDataUnifiedPushCounts(),
)
@JsonClass(generateAdapter = true)
data class PushDataUnifiedPushCounts(
@Json(name = "unread") val unread: Int = 0
)
fun PushDataUnifiedPush.toPushData() = PushData(
eventId = notification.eventId,
roomId = notification.roomId,
unread = notification.counts.unread
)