|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 New Vector Ltd |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.element.android.libraries.push.impl.push |
| 18 | + |
| 19 | +import io.element.android.libraries.core.coroutine.CoroutineDispatchers |
| 20 | +import io.element.android.libraries.featureflag.api.FeatureFlagService |
| 21 | +import io.element.android.libraries.featureflag.api.FeatureFlags |
| 22 | +import io.element.android.libraries.matrix.api.MatrixClientProvider |
| 23 | +import io.element.android.libraries.matrix.api.core.EventId |
| 24 | +import io.element.android.libraries.matrix.api.room.MatrixRoom |
| 25 | +import io.element.android.libraries.matrix.api.sync.SyncService |
| 26 | +import io.element.android.libraries.matrix.api.timeline.MatrixTimelineItem |
| 27 | +import io.element.android.libraries.push.impl.notifications.model.NotifiableEvent |
| 28 | +import io.element.android.services.appnavstate.api.AppForegroundStateService |
| 29 | +import kotlinx.coroutines.flow.first |
| 30 | +import kotlinx.coroutines.withContext |
| 31 | +import kotlinx.coroutines.withTimeoutOrNull |
| 32 | +import java.util.concurrent.atomic.AtomicInteger |
| 33 | +import javax.inject.Inject |
| 34 | +import kotlin.time.Duration |
| 35 | +import kotlin.time.Duration.Companion.seconds |
| 36 | + |
| 37 | +class SyncOnNotifiableEvent @Inject constructor( |
| 38 | + private val matrixClientProvider: MatrixClientProvider, |
| 39 | + private val featureFlagService: FeatureFlagService, |
| 40 | + private val appForegroundStateService: AppForegroundStateService, |
| 41 | + private val dispatchers: CoroutineDispatchers, |
| 42 | +) { |
| 43 | + private var syncCounter = AtomicInteger(0) |
| 44 | + |
| 45 | + suspend operator fun invoke(notifiableEvent: NotifiableEvent) = withContext(dispatchers.io) { |
| 46 | + if (!featureFlagService.isFeatureEnabled(FeatureFlags.SyncOnPush)) { |
| 47 | + return@withContext |
| 48 | + } |
| 49 | + val client = matrixClientProvider.getOrRestore(notifiableEvent.sessionId).getOrNull() ?: return@withContext |
| 50 | + client.getRoom(notifiableEvent.roomId)?.use { room -> |
| 51 | + room.subscribeToSync() |
| 52 | + |
| 53 | + // If the app is in foreground, sync is already running, so just add the subscription. |
| 54 | + if (!appForegroundStateService.isInForeground.value) { |
| 55 | + val syncService = client.syncService() |
| 56 | + syncService.startSyncIfNeeded() |
| 57 | + room.waitsUntilEventIsKnown(eventId = notifiableEvent.eventId, timeout = 10.seconds) |
| 58 | + syncService.stopSyncIfNeeded() |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private suspend fun MatrixRoom.waitsUntilEventIsKnown(eventId: EventId, timeout: Duration) { |
| 64 | + withTimeoutOrNull(timeout) { |
| 65 | + liveTimeline.timelineItems.first { timelineItems -> |
| 66 | + timelineItems.any { timelineItem -> |
| 67 | + when (timelineItem) { |
| 68 | + is MatrixTimelineItem.Event -> timelineItem.eventId == eventId |
| 69 | + else -> false |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private suspend fun SyncService.startSyncIfNeeded() { |
| 77 | + if (syncCounter.getAndIncrement() == 0) { |
| 78 | + startSync() |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private suspend fun SyncService.stopSyncIfNeeded() { |
| 83 | + if (syncCounter.decrementAndGet() == 0 && !appForegroundStateService.isInForeground.value) { |
| 84 | + stopSync() |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments