From cdfb728a73b1609b168cc5aab49e889f75f4c2df Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 1 Jun 2022 19:51:26 +0200 Subject: [PATCH] Clarify the data classes for the Json parsing --- .../core/pushers/VectorMessagingReceiver.kt | 49 +++++----------- .../vector/app/core/pushers/model/PushData.kt | 23 ++++++++ .../app/core/pushers/model/PushDataFcm.kt | 44 ++++++++++++++ .../core/pushers/model/PushDataUnifiedPush.kt | 58 +++++++++++++++++++ 4 files changed, 140 insertions(+), 34 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/core/pushers/model/PushData.kt create mode 100644 vector/src/main/java/im/vector/app/core/pushers/model/PushDataFcm.kt create mode 100644 vector/src/main/java/im/vector/app/core/pushers/model/PushDataUnifiedPush.kt diff --git a/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt b/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt index 10335d0f75..bb2f253abd 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/VectorMessagingReceiver.kt @@ -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() diff --git a/vector/src/main/java/im/vector/app/core/pushers/model/PushData.kt b/vector/src/main/java/im/vector/app/core/pushers/model/PushData.kt new file mode 100644 index 0000000000..9f7b710c91 --- /dev/null +++ b/vector/src/main/java/im/vector/app/core/pushers/model/PushData.kt @@ -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, +) diff --git a/vector/src/main/java/im/vector/app/core/pushers/model/PushDataFcm.kt b/vector/src/main/java/im/vector/app/core/pushers/model/PushDataFcm.kt new file mode 100644 index 0000000000..bf81377ccb --- /dev/null +++ b/vector/src/main/java/im/vector/app/core/pushers/model/PushDataFcm.kt @@ -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: + *
+ * {
+ *     "event_id":"$anEventId",
+ *     "room_id":"!aRoomId",
+ *     "unread":"1",
+ *     "prio":"high",
+ * }
+ * 
+ */ +@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 +) diff --git a/vector/src/main/java/im/vector/app/core/pushers/model/PushDataUnifiedPush.kt b/vector/src/main/java/im/vector/app/core/pushers/model/PushDataUnifiedPush.kt new file mode 100644 index 0000000000..3174165218 --- /dev/null +++ b/vector/src/main/java/im/vector/app/core/pushers/model/PushDataUnifiedPush.kt @@ -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: + *
+ * {
+ *     "notification":{
+ *         "event_id":"$anEventId",
+ *         "room_id":"!aRoomId",
+ *         "counts":{
+ *             "unread":1
+ *         },
+ *         "prio":"high",
+ *     }
+ * }
+ * 
+ */ +@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 +)