Ensure the sync is started when receiving a Push, to ensure that the encryption loop is running.

Fixes notification with endecrypted content (#1178)
This commit is contained in:
Benoit Marty 2023-09-04 12:50:37 +02:00
parent fedd0b39b1
commit 82f6f358a7
6 changed files with 75 additions and 16 deletions

View file

@ -26,6 +26,7 @@ import io.element.android.libraries.matrix.api.core.UserId
import io.element.android.libraries.matrix.api.notification.NotificationContent
import io.element.android.libraries.matrix.api.notification.NotificationData
import io.element.android.libraries.matrix.api.room.RoomMembershipState
import io.element.android.libraries.matrix.api.sync.StartSyncReason
import io.element.android.libraries.matrix.api.timeline.item.event.AudioMessageType
import io.element.android.libraries.matrix.api.timeline.item.event.EmoteMessageType
import io.element.android.libraries.matrix.api.timeline.item.event.FileMessageType
@ -44,6 +45,7 @@ import io.element.android.libraries.push.impl.notifications.model.NotifiableMess
import io.element.android.libraries.ui.strings.CommonStrings
import io.element.android.services.toolbox.api.strings.StringProvider
import io.element.android.services.toolbox.api.systemclock.SystemClock
import kotlinx.coroutines.delay
import timber.log.Timber
import javax.inject.Inject
@ -65,6 +67,14 @@ class NotifiableEventResolver @Inject constructor(
// Restore session
val client = matrixClientProvider.getOrRestore(sessionId).getOrNull() ?: return null
val notificationService = client.notificationService()
// Restart the sync service to ensure that the crypto sync handle the toDevice Events.
client.syncService().startSync(StartSyncReason.Notification(roomId, eventId))
// Wait for toDevice Event to be processed
// FIXME This delay can be removed when the Rust SDK will handle internal retry to get
// clear notification content.
delay(300)
val notificationData = notificationService.getNotification(
userId = sessionId,
roomId = roomId,
@ -73,6 +83,8 @@ class NotifiableEventResolver @Inject constructor(
Timber.tag(loggerTag.value).e(it, "Unable to resolve event: $eventId.")
}.getOrNull()
client.syncService().stopSync(StartSyncReason.Notification(roomId, eventId))
// TODO this notificationData is not always valid at the moment, sometimes the Rust SDK can't fetch the matching event
return notificationData?.asNotifiableEvent(sessionId)
}