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

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