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 4fd44131f6
commit 74a444966e
6 changed files with 75 additions and 16 deletions

View File

@@ -60,6 +60,7 @@ import io.element.android.libraries.di.SessionScope
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.matrix.api.core.MAIN_SPACE
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.sync.StartSyncReason
import io.element.android.libraries.matrix.api.sync.SyncState
import io.element.android.libraries.push.api.notifications.NotificationDrawerManager
import io.element.android.services.appnavstate.api.AppNavigationStateService
@@ -124,7 +125,7 @@ class LoggedInFlowNode @AssistedInject constructor(
onStop = {
//Counterpart startSync is done in observeSyncStateAndNetworkStatus method.
coroutineScope.launch {
syncService.stopSync()
syncService.stopSync(StartSyncReason.AppInForeground)
}
},
onDestroy = {
@@ -150,7 +151,7 @@ class LoggedInFlowNode @AssistedInject constructor(
.collect { (syncState, networkStatus) ->
Timber.d("Sync state: $syncState, network status: $networkStatus")
if (syncState != SyncState.Running && networkStatus == NetworkStatus.Online) {
syncService.startSync()
syncService.startSync(StartSyncReason.AppInForeground)
}
}
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 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 io.element.android.libraries.matrix.api.sync
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.RoomId
sealed interface StartSyncReason {
data object AppInForeground : StartSyncReason
data class Notification(val roomId: RoomId, val eventId: EventId) : StartSyncReason
}

View File

@@ -22,12 +22,12 @@ interface SyncService {
/**
* Tries to start the sync. If already syncing it has no effect.
*/
suspend fun startSync(): Result<Unit>
suspend fun startSync(reason: StartSyncReason): Result<Unit>
/**
* Tries to stop the sync. If service is not syncing it has no effect.
*/
suspend fun stopSync(): Result<Unit>
suspend fun stopSync(reason: StartSyncReason): Result<Unit>
/**
* Flow of [SyncState]. Will be updated as soon as the current [SyncState] changes.

View File

@@ -16,6 +16,7 @@
package io.element.android.libraries.matrix.impl.sync
import io.element.android.libraries.matrix.api.sync.StartSyncReason
import io.element.android.libraries.matrix.api.sync.SyncService
import io.element.android.libraries.matrix.api.sync.SyncState
import kotlinx.coroutines.CoroutineScope
@@ -25,6 +26,8 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.matrix.rustcomponents.sdk.SyncServiceInterface
import org.matrix.rustcomponents.sdk.SyncServiceState
import timber.log.Timber
@@ -33,19 +36,36 @@ class RustSyncService(
private val innerSyncService: SyncServiceInterface,
sessionCoroutineScope: CoroutineScope
) : SyncService {
private val mutex = Mutex()
private val startSyncReasonSet = mutableSetOf<StartSyncReason>()
override suspend fun startSync() = runCatching {
Timber.i("Start sync")
innerSyncService.start()
}.onFailure {
Timber.d("Start sync failed: $it")
override suspend fun startSync(reason: StartSyncReason): Result<Unit> {
return mutex.withLock {
startSyncReasonSet.add(reason)
runCatching {
Timber.d("Start sync")
innerSyncService.start()
}.onFailure {
Timber.e("Start sync failed: $it")
}
}
}
override suspend fun stopSync() = runCatching {
Timber.i("Stop sync")
innerSyncService.stop()
}.onFailure {
Timber.d("Stop sync failed: $it")
override suspend fun stopSync(reason: StartSyncReason): Result<Unit> {
return mutex.withLock {
startSyncReasonSet.remove(reason)
if (startSyncReasonSet.isEmpty()) {
runCatching {
Timber.d("Stop sync")
innerSyncService.stop()
}.onFailure {
Timber.e("Stop sync failed: $it")
}
} else {
Timber.d("Stop sync skipped, still $startSyncReasonSet")
Result.success(Unit)
}
}
}
override val syncState: StateFlow<SyncState> =

View File

@@ -16,6 +16,7 @@
package io.element.android.libraries.matrix.test.sync
import io.element.android.libraries.matrix.api.sync.StartSyncReason
import io.element.android.libraries.matrix.api.sync.SyncService
import io.element.android.libraries.matrix.api.sync.SyncState
import kotlinx.coroutines.flow.MutableStateFlow
@@ -29,12 +30,12 @@ class FakeSyncService : SyncService {
syncStateFlow.value = SyncState.Error
}
override suspend fun startSync(): Result<Unit> {
override suspend fun startSync(reason: StartSyncReason): Result<Unit> {
syncStateFlow.value = SyncState.Running
return Result.success(Unit)
}
override suspend fun stopSync(): Result<Unit> {
override suspend fun stopSync(reason: StartSyncReason): Result<Unit> {
syncStateFlow.value = SyncState.Terminated
return Result.success(Unit)
}

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