Fixing the toggle notifications use case for current session

This commit is contained in:
Maxime NATUREL 2022-12-01 16:47:10 +01:00
parent 8973f3892a
commit 3f5147ddce
10 changed files with 92 additions and 34 deletions

View File

@ -20,7 +20,7 @@ import im.vector.app.core.di.ActiveSessionHolder
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
import javax.inject.Inject
class ToggleNotificationUseCase @Inject constructor(
class ToggleNotificationsUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
private val checkIfCanToggleNotificationsViaPusherUseCase: CheckIfCanToggleNotificationsViaPusherUseCase,
private val checkIfCanToggleNotificationsViaAccountDataUseCase: CheckIfCanToggleNotificationsViaAccountDataUseCase,

View File

@ -31,7 +31,7 @@ import im.vector.app.features.settings.devices.v2.RefreshDevicesUseCase
import im.vector.app.features.settings.devices.v2.ToggleIpAddressVisibilityUseCase
import im.vector.app.features.settings.devices.v2.VectorSessionsListViewModel
import im.vector.app.features.settings.devices.v2.notification.GetNotificationsStatusUseCase
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationsUseCase
import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsReAuthNeeded
import im.vector.app.features.settings.devices.v2.signout.SignoutSessionsUseCase
import im.vector.app.features.settings.devices.v2.verification.CheckIfCurrentSessionCanBeVerifiedUseCase
@ -52,7 +52,7 @@ class SessionOverviewViewModel @AssistedInject constructor(
private val signoutSessionsUseCase: SignoutSessionsUseCase,
private val pendingAuthHandler: PendingAuthHandler,
private val activeSessionHolder: ActiveSessionHolder,
private val toggleNotificationUseCase: ToggleNotificationUseCase,
private val toggleNotificationsUseCase: ToggleNotificationsUseCase,
private val getNotificationsStatusUseCase: GetNotificationsStatusUseCase,
refreshDevicesUseCase: RefreshDevicesUseCase,
private val vectorPreferences: VectorPreferences,
@ -226,7 +226,7 @@ class SessionOverviewViewModel @AssistedInject constructor(
private fun handleTogglePusherAction(action: SessionOverviewAction.TogglePushNotifications) {
viewModelScope.launch {
toggleNotificationUseCase.execute(action.deviceId, action.enabled)
toggleNotificationsUseCase.execute(action.deviceId, action.enabled)
}
}
}

View File

@ -20,21 +20,22 @@ import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.pushers.PushersManager
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
import im.vector.app.features.settings.devices.v2.notification.CheckIfCanToggleNotificationsViaPusherUseCase
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase
import javax.inject.Inject
class DisableNotificationsForCurrentSessionUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
private val pushersManager: PushersManager,
private val checkIfCanToggleNotificationsViaPusherUseCase: CheckIfCanToggleNotificationsViaPusherUseCase,
private val toggleNotificationUseCase: ToggleNotificationUseCase,
private val toggleNotificationsForCurrentSessionUseCase: ToggleNotificationsForCurrentSessionUseCase,
private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase,
) {
// TODO update unit tests
suspend fun execute() {
val session = activeSessionHolder.getSafeActiveSession() ?: return
val deviceId = session.sessionParams.deviceId ?: return
toggleNotificationUseCase.execute(deviceId, enabled = false)
toggleNotificationsForCurrentSessionUseCase.execute(enabled = false)
// handle case when server does not support toggle of pusher
if (!checkIfCanToggleNotificationsViaPusherUseCase.execute(session)) {
unregisterUnifiedPushUseCase.execute(pushersManager)
}

View File

@ -16,17 +16,14 @@
package im.vector.app.features.settings.notifications
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase
import im.vector.app.core.pushers.PushersManager
import im.vector.app.core.pushers.RegisterUnifiedPushUseCase
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase
import javax.inject.Inject
class EnableNotificationsForCurrentSessionUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
private val pushersManager: PushersManager,
private val toggleNotificationUseCase: ToggleNotificationUseCase,
private val toggleNotificationsForCurrentSessionUseCase: ToggleNotificationsForCurrentSessionUseCase,
private val registerUnifiedPushUseCase: RegisterUnifiedPushUseCase,
private val ensureFcmTokenIsRetrievedUseCase: EnsureFcmTokenIsRetrievedUseCase,
) {
@ -37,6 +34,7 @@ class EnableNotificationsForCurrentSessionUseCase @Inject constructor(
object NeedToAskUserForDistributor : EnableNotificationsResult
}
// TODO update unit tests
suspend fun execute(distributor: String = ""): EnableNotificationsResult {
val pusherForCurrentSession = pushersManager.getPusherForCurrentSession()
if (pusherForCurrentSession == null) {
@ -50,9 +48,7 @@ class EnableNotificationsForCurrentSessionUseCase @Inject constructor(
}
}
val session = activeSessionHolder.getSafeActiveSession() ?: return EnableNotificationsResult.Failure
val deviceId = session.sessionParams.deviceId ?: return EnableNotificationsResult.Failure
toggleNotificationUseCase.execute(deviceId, enabled = true)
toggleNotificationsForCurrentSessionUseCase.execute(enabled = true)
return EnableNotificationsResult.Success
}

View File

@ -0,0 +1,57 @@
/*
* 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.features.settings.notifications
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.pushers.UnifiedPushHelper
import im.vector.app.features.settings.devices.v2.notification.CheckIfCanToggleNotificationsViaPusherUseCase
import im.vector.app.features.settings.devices.v2.notification.DeleteNotificationSettingsAccountDataUseCase
import im.vector.app.features.settings.devices.v2.notification.SetNotificationSettingsAccountDataUseCase
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
import timber.log.Timber
import javax.inject.Inject
class ToggleNotificationsForCurrentSessionUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
private val unifiedPushHelper: UnifiedPushHelper,
private val checkIfCanToggleNotificationsViaPusherUseCase: CheckIfCanToggleNotificationsViaPusherUseCase,
private val setNotificationSettingsAccountDataUseCase: SetNotificationSettingsAccountDataUseCase,
private val deleteNotificationSettingsAccountDataUseCase: DeleteNotificationSettingsAccountDataUseCase,
) {
// TODO add unit tests
suspend fun execute(enabled: Boolean) {
val session = activeSessionHolder.getSafeActiveSession() ?: return
val deviceId = session.sessionParams.deviceId ?: return
if (unifiedPushHelper.isBackgroundSync()) {
Timber.d("background sync is enabled, setting account data event")
val newNotificationSettingsContent = LocalNotificationSettingsContent(isSilenced = !enabled)
setNotificationSettingsAccountDataUseCase.execute(session, deviceId, newNotificationSettingsContent)
} else {
Timber.d("push notif is enabled, deleting any account data and updating pusher")
deleteNotificationSettingsAccountDataUseCase.execute(session)
if (checkIfCanToggleNotificationsViaPusherUseCase.execute(session)) {
val devicePusher = session.pushersService().getPushers().firstOrNull { it.deviceId == deviceId }
devicePusher?.let { pusher ->
session.pushersService().togglePusher(pusher, enabled)
}
}
}
}
}

View File

@ -40,6 +40,7 @@ class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor(
private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase,
private val registerUnifiedPushUseCase: RegisterUnifiedPushUseCase,
private val ensureFcmTokenIsRetrievedUseCase: EnsureFcmTokenIsRetrievedUseCase,
private val toggleNotificationsForCurrentSessionUseCase: ToggleNotificationsForCurrentSessionUseCase,
) : VectorViewModel<VectorDummyViewState, VectorSettingsNotificationPreferenceViewAction, VectorSettingsNotificationPreferenceViewEvent>(initialState) {
@AssistedFactory
@ -80,6 +81,7 @@ class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor(
}
}
// TODO update unit tests
private fun handleRegisterPushDistributor(distributor: String) {
viewModelScope.launch {
unregisterUnifiedPushUseCase.execute(pushersManager)
@ -88,7 +90,9 @@ class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor(
_viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.AskUserForPushDistributor)
}
RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success -> {
ensureFcmTokenIsRetrievedUseCase.execute(pushersManager, registerPusher = vectorPreferences.areNotificationEnabledForDevice())
val areNotificationsEnabled = vectorPreferences.areNotificationEnabledForDevice()
ensureFcmTokenIsRetrievedUseCase.execute(pushersManager, registerPusher = areNotificationsEnabled)
toggleNotificationsForCurrentSessionUseCase.execute(enabled = areNotificationsEnabled)
_viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.NotificationMethodChanged)
}
}

View File

@ -26,7 +26,7 @@ import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
class ToggleNotificationUseCaseTest {
class ToggleNotificationsUseCaseTest {
private val activeSessionHolder = FakeActiveSessionHolder()
private val fakeCheckIfCanToggleNotificationsViaPusherUseCase =
@ -36,8 +36,8 @@ class ToggleNotificationUseCaseTest {
private val fakeSetNotificationSettingsAccountDataUseCase =
mockk<SetNotificationSettingsAccountDataUseCase>()
private val toggleNotificationUseCase =
ToggleNotificationUseCase(
private val toggleNotificationsUseCase =
ToggleNotificationsUseCase(
activeSessionHolder = activeSessionHolder.instance,
checkIfCanToggleNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase,
checkIfCanToggleNotificationsViaAccountDataUseCase = fakeCheckIfCanToggleNotificationsViaAccountDataUseCase,
@ -59,7 +59,7 @@ class ToggleNotificationUseCaseTest {
every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, sessionId) } returns false
// When
toggleNotificationUseCase.execute(sessionId, true)
toggleNotificationsUseCase.execute(sessionId, true)
// Then
activeSessionHolder.fakeSession.pushersService().verifyTogglePusherCalled(pushers.first(), true)
@ -78,7 +78,7 @@ class ToggleNotificationUseCaseTest {
)
// When
toggleNotificationUseCase.execute(sessionId, true)
toggleNotificationsUseCase.execute(sessionId, true)
// Then
coVerify {

View File

@ -18,7 +18,7 @@ package im.vector.app.features.settings.notifications
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
import im.vector.app.features.settings.devices.v2.notification.CheckIfCanToggleNotificationsViaPusherUseCase
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationsUseCase
import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakePushersManager
import io.mockk.coJustRun
@ -35,14 +35,14 @@ class DisableNotificationsForCurrentSessionUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
private val fakePushersManager = FakePushersManager()
private val fakeCheckIfCanToggleNotificationsViaPusherUseCase = mockk<CheckIfCanToggleNotificationsViaPusherUseCase>()
private val fakeToggleNotificationUseCase = mockk<ToggleNotificationUseCase>()
private val fakeToggleNotificationsUseCase = mockk<ToggleNotificationsUseCase>()
private val fakeUnregisterUnifiedPushUseCase = mockk<UnregisterUnifiedPushUseCase>()
private val disableNotificationsForCurrentSessionUseCase = DisableNotificationsForCurrentSessionUseCase(
activeSessionHolder = fakeActiveSessionHolder.instance,
pushersManager = fakePushersManager.instance,
checkIfCanToggleNotificationsViaPusherUseCase = fakeCheckIfCanToggleNotificationsViaPusherUseCase,
toggleNotificationUseCase = fakeToggleNotificationUseCase,
toggleNotificationUseCase = fakeToggleNotificationsUseCase,
unregisterUnifiedPushUseCase = fakeUnregisterUnifiedPushUseCase,
)
@ -52,13 +52,13 @@ class DisableNotificationsForCurrentSessionUseCaseTest {
val fakeSession = fakeActiveSessionHolder.fakeSession
fakeSession.givenSessionId(A_SESSION_ID)
every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns true
coJustRun { fakeToggleNotificationUseCase.execute(A_SESSION_ID, any()) }
coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) }
// When
disableNotificationsForCurrentSessionUseCase.execute()
// Then
coVerify { fakeToggleNotificationUseCase.execute(A_SESSION_ID, false) }
coVerify { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, false) }
}
@Test
@ -67,7 +67,7 @@ class DisableNotificationsForCurrentSessionUseCaseTest {
val fakeSession = fakeActiveSessionHolder.fakeSession
fakeSession.givenSessionId(A_SESSION_ID)
every { fakeCheckIfCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns false
coJustRun { fakeToggleNotificationUseCase.execute(A_SESSION_ID, any()) }
coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) }
coJustRun { fakeUnregisterUnifiedPushUseCase.execute(any()) }
// When
@ -75,7 +75,7 @@ class DisableNotificationsForCurrentSessionUseCaseTest {
// Then
coVerify {
fakeToggleNotificationUseCase.execute(A_SESSION_ID, false)
fakeToggleNotificationsUseCase.execute(A_SESSION_ID, false)
fakeUnregisterUnifiedPushUseCase.execute(fakePushersManager.instance)
}
}

View File

@ -18,7 +18,7 @@ package im.vector.app.features.settings.notifications
import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase
import im.vector.app.core.pushers.RegisterUnifiedPushUseCase
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationsUseCase
import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakePushersManager
import io.mockk.coJustRun
@ -36,14 +36,14 @@ class EnableNotificationsForCurrentSessionUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
private val fakePushersManager = FakePushersManager()
private val fakeToggleNotificationUseCase = mockk<ToggleNotificationUseCase>()
private val fakeToggleNotificationsUseCase = mockk<ToggleNotificationsUseCase>()
private val fakeRegisterUnifiedPushUseCase = mockk<RegisterUnifiedPushUseCase>()
private val fakeEnsureFcmTokenIsRetrievedUseCase = mockk<EnsureFcmTokenIsRetrievedUseCase>()
private val enableNotificationsForCurrentSessionUseCase = EnableNotificationsForCurrentSessionUseCase(
activeSessionHolder = fakeActiveSessionHolder.instance,
pushersManager = fakePushersManager.instance,
toggleNotificationUseCase = fakeToggleNotificationUseCase,
toggleNotificationUseCase = fakeToggleNotificationsUseCase,
registerUnifiedPushUseCase = fakeRegisterUnifiedPushUseCase,
ensureFcmTokenIsRetrievedUseCase = fakeEnsureFcmTokenIsRetrievedUseCase,
)
@ -57,7 +57,7 @@ class EnableNotificationsForCurrentSessionUseCaseTest {
fakePushersManager.givenGetPusherForCurrentSessionReturns(null)
every { fakeRegisterUnifiedPushUseCase.execute(any()) } returns RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success
justRun { fakeEnsureFcmTokenIsRetrievedUseCase.execute(any(), any()) }
coJustRun { fakeToggleNotificationUseCase.execute(A_SESSION_ID, any()) }
coJustRun { fakeToggleNotificationsUseCase.execute(A_SESSION_ID, any()) }
// When
val result = enableNotificationsForCurrentSessionUseCase.execute(aDistributor)

View File

@ -16,14 +16,14 @@
package im.vector.app.test.fakes
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationUseCase
import im.vector.app.features.settings.devices.v2.notification.ToggleNotificationsUseCase
import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.mockk
class FakeToggleNotificationUseCase {
val instance = mockk<ToggleNotificationUseCase> {
val instance = mockk<ToggleNotificationsUseCase> {
coJustRun { execute(any(), any()) }
}