Skip to content

Commit

Permalink
Merge pull request #7692 from vector-im/feature/mna/listen-notificati…
Browse files Browse the repository at this point in the history
…on-account-data

Update notifications setting when m.local_notification_settings.<device-id> event changes for current device (PSG-874)
mnaturel authored Dec 6, 2022
2 parents b5f6dc0 + 7b830d1 commit 6f0a95b
Showing 11 changed files with 377 additions and 29 deletions.
1 change: 1 addition & 0 deletions changelog.d/7632.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update notifications setting when m.local_notification_settings.<device-id> event changes for current device
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.devices.v2.notification

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import org.matrix.android.sdk.api.session.Session
import javax.inject.Inject

class CanToggleNotificationsViaAccountDataUseCase @Inject constructor(
private val getNotificationSettingsAccountDataUpdatesUseCase: GetNotificationSettingsAccountDataUpdatesUseCase,
) {

fun execute(session: Session, deviceId: String): Flow<Boolean> {
return getNotificationSettingsAccountDataUpdatesUseCase.execute(session, deviceId)
.map { it?.isSilenced != null }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.devices.v2.notification

import androidx.lifecycle.asFlow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
import org.matrix.android.sdk.api.session.events.model.toModel
import javax.inject.Inject

class GetNotificationSettingsAccountDataUpdatesUseCase @Inject constructor() {

fun execute(session: Session, deviceId: String): Flow<LocalNotificationSettingsContent?> {
return session
.accountDataService()
.getLiveUserAccountDataEvent(UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId)
.asFlow()
.map { it.getOrNull()?.content?.toModel<LocalNotificationSettingsContent>() }
}
}
Original file line number Diff line number Diff line change
@@ -31,20 +31,30 @@ import javax.inject.Inject

class GetNotificationsStatusUseCase @Inject constructor(
private val canToggleNotificationsViaPusherUseCase: CanToggleNotificationsViaPusherUseCase,
private val checkIfCanToggleNotificationsViaAccountDataUseCase: CheckIfCanToggleNotificationsViaAccountDataUseCase,
private val canToggleNotificationsViaAccountDataUseCase: CanToggleNotificationsViaAccountDataUseCase,
) {

fun execute(session: Session, deviceId: String): Flow<NotificationsStatus> {
return when {
checkIfCanToggleNotificationsViaAccountDataUseCase.execute(session, deviceId) -> {
session.flow()
.liveUserAccountData(UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId)
.unwrap()
.map { it.content.toModel<LocalNotificationSettingsContent>()?.isSilenced?.not() }
.map { if (it == true) NotificationsStatus.ENABLED else NotificationsStatus.DISABLED }
.distinctUntilChanged()
}
else -> canToggleNotificationsViaPusherUseCase.execute(session)
return canToggleNotificationsViaAccountDataUseCase.execute(session, deviceId)
.flatMapLatest { canToggle ->
if (canToggle) {
notificationStatusFromAccountData(session, deviceId)
} else {
notificationStatusFromPusher(session, deviceId)
}
}
.distinctUntilChanged()
}

private fun notificationStatusFromAccountData(session: Session, deviceId: String) =
session.flow()
.liveUserAccountData(UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + deviceId)
.unwrap()
.map { it.content.toModel<LocalNotificationSettingsContent>()?.isSilenced?.not() }
.map { if (it == true) NotificationsStatus.ENABLED else NotificationsStatus.DISABLED }

private fun notificationStatusFromPusher(session: Session, deviceId: String) =
canToggleNotificationsViaPusherUseCase.execute(session)
.flatMapLatest { canToggle ->
if (canToggle) {
session.flow()
@@ -63,6 +73,4 @@ class GetNotificationsStatusUseCase @Inject constructor(
flowOf(NotificationsStatus.NOT_SUPPORTED)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@

package im.vector.app.features.settings.notifications

import android.content.SharedPreferences
import androidx.annotation.VisibleForTesting
import com.airbnb.mvrx.MavericksViewModelFactory
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
@@ -50,6 +52,31 @@ class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor(

companion object : MavericksViewModelFactory<VectorSettingsNotificationPreferenceViewModel, VectorDummyViewState> by hiltMavericksViewModelFactory()

@VisibleForTesting
val notificationsPreferenceListener: SharedPreferences.OnSharedPreferenceChangeListener =
SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (key == VectorPreferences.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY) {
if (vectorPreferences.areNotificationEnabledForDevice()) {
_viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceEnabled)
} else {
_viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceDisabled)
}
}
}

init {
observeNotificationsEnabledPreference()
}

private fun observeNotificationsEnabledPreference() {
vectorPreferences.subscribeToChanges(notificationsPreferenceListener)
}

override fun onCleared() {
vectorPreferences.unsubscribeToChanges(notificationsPreferenceListener)
super.onCleared()
}

override fun handle(action: VectorSettingsNotificationPreferenceViewAction) {
when (action) {
VectorSettingsNotificationPreferenceViewAction.DisableNotificationsForDevice -> handleDisableNotificationsForDevice()
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.devices.v2.notification

import im.vector.app.test.fakes.FakeSession
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBe
import org.junit.Test
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent

class CanToggleNotificationsViaAccountDataUseCaseTest {

private val fakeGetNotificationSettingsAccountDataUpdatesUseCase = mockk<GetNotificationSettingsAccountDataUpdatesUseCase>()

private val canToggleNotificationsViaAccountDataUseCase = CanToggleNotificationsViaAccountDataUseCase(
getNotificationSettingsAccountDataUpdatesUseCase = fakeGetNotificationSettingsAccountDataUpdatesUseCase,
)

@Test
fun `given current session and content for account data when execute then true is returned`() = runTest {
// Given
val aSession = FakeSession()
val aDeviceId = "aDeviceId"
val localNotificationSettingsContent = LocalNotificationSettingsContent(
isSilenced = true,
)
every { fakeGetNotificationSettingsAccountDataUpdatesUseCase.execute(any(), any()) } returns flowOf(localNotificationSettingsContent)

// When
val result = canToggleNotificationsViaAccountDataUseCase.execute(aSession, aDeviceId).firstOrNull()

// Then
result shouldBe true
verify { fakeGetNotificationSettingsAccountDataUpdatesUseCase.execute(aSession, aDeviceId) }
}

@Test
fun `given current session and empty content for account data when execute then false is returned`() = runTest {
// Given
val aSession = FakeSession()
val aDeviceId = "aDeviceId"
val localNotificationSettingsContent = LocalNotificationSettingsContent(
isSilenced = null,
)
every { fakeGetNotificationSettingsAccountDataUpdatesUseCase.execute(any(), any()) } returns flowOf(localNotificationSettingsContent)

// When
val result = canToggleNotificationsViaAccountDataUseCase.execute(aSession, aDeviceId).firstOrNull()

// Then
result shouldBe false
verify { fakeGetNotificationSettingsAccountDataUpdatesUseCase.execute(aSession, aDeviceId) }
}

@Test
fun `given current session and no related account data when execute then false is returned`() = runTest {
// Given
val aSession = FakeSession()
val aDeviceId = "aDeviceId"
every { fakeGetNotificationSettingsAccountDataUpdatesUseCase.execute(any(), any()) } returns flowOf(null)

// When
val result = canToggleNotificationsViaAccountDataUseCase.execute(aSession, aDeviceId).firstOrNull()

// Then
result shouldBe false
verify { fakeGetNotificationSettingsAccountDataUpdatesUseCase.execute(aSession, aDeviceId) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.devices.v2.notification

import im.vector.app.test.fakes.FakeFlowLiveDataConversions
import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.fakes.givenAsFlow
import io.mockk.unmockkAll
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.matrix.android.sdk.api.account.LocalNotificationSettingsContent
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
import org.matrix.android.sdk.api.session.events.model.toContent

class GetNotificationSettingsAccountDataUpdatesUseCaseTest {

private val fakeFlowLiveDataConversions = FakeFlowLiveDataConversions()
private val getNotificationSettingsAccountDataUpdatesUseCase = GetNotificationSettingsAccountDataUpdatesUseCase()

@Before
fun setUp() {
fakeFlowLiveDataConversions.setup()
}

@After
fun tearDown() {
unmockkAll()
}

@Test
fun `given a device id when execute then retrieve the account data event corresponding to this id if any`() = runTest {
// Given
val aDeviceId = "device-id"
val aSession = FakeSession()
val expectedContent = LocalNotificationSettingsContent(isSilenced = true)
aSession
.accountDataService()
.givenGetLiveUserAccountDataEventReturns(
type = UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + aDeviceId,
content = expectedContent.toContent(),
)
.givenAsFlow()

// When
val result = getNotificationSettingsAccountDataUpdatesUseCase.execute(aSession, aDeviceId).firstOrNull()

// Then
result shouldBeEqualTo expectedContent
}

@Test
fun `given a device id and no content for account data when execute then retrieve the account data event corresponding to this id if any`() = runTest {
// Given
val aDeviceId = "device-id"
val aSession = FakeSession()
aSession
.accountDataService()
.givenGetLiveUserAccountDataEventReturns(
type = UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + aDeviceId,
content = null,
)
.givenAsFlow()

// When
val result = getNotificationSettingsAccountDataUpdatesUseCase.execute(aSession, aDeviceId).firstOrNull()

// Then
result shouldBeEqualTo null
}

@Test
fun `given a device id and empty content for account data when execute then retrieve the account data event corresponding to this id if any`() = runTest {
// Given
val aDeviceId = "device-id"
val aSession = FakeSession()
val expectedContent = LocalNotificationSettingsContent(isSilenced = null)
aSession
.accountDataService()
.givenGetLiveUserAccountDataEventReturns(
type = UserAccountDataTypes.TYPE_LOCAL_NOTIFICATION_SETTINGS + aDeviceId,
content = expectedContent.toContent(),
)
.givenAsFlow()

// When
val result = getNotificationSettingsAccountDataUpdatesUseCase.execute(aSession, aDeviceId).firstOrNull()

// Then
result shouldBeEqualTo expectedContent
}
}
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ import im.vector.app.test.fixtures.PusherFixture
import im.vector.app.test.testDispatcher
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import io.mockk.verifyOrder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.firstOrNull
@@ -46,14 +47,14 @@ class GetNotificationsStatusUseCaseTest {
val instantTaskExecutorRule = InstantTaskExecutorRule()

private val fakeSession = FakeSession()
private val fakeCheckIfCanToggleNotificationsViaAccountDataUseCase =
mockk<CheckIfCanToggleNotificationsViaAccountDataUseCase>()
private val fakeCanToggleNotificationsViaAccountDataUseCase =
mockk<CanToggleNotificationsViaAccountDataUseCase>()
private val fakeCanToggleNotificationsViaPusherUseCase =
mockk<CanToggleNotificationsViaPusherUseCase>()

private val getNotificationsStatusUseCase =
GetNotificationsStatusUseCase(
checkIfCanToggleNotificationsViaAccountDataUseCase = fakeCheckIfCanToggleNotificationsViaAccountDataUseCase,
canToggleNotificationsViaAccountDataUseCase = fakeCanToggleNotificationsViaAccountDataUseCase,
canToggleNotificationsViaPusherUseCase = fakeCanToggleNotificationsViaPusherUseCase,
)

@@ -70,7 +71,7 @@ class GetNotificationsStatusUseCaseTest {
@Test
fun `given current session and toggle is not supported when execute then resulting flow contains NOT_SUPPORTED value`() = runTest {
// Given
every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns false
every { fakeCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns flowOf(false)
every { fakeCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns flowOf(false)

// When
@@ -80,7 +81,7 @@ class GetNotificationsStatusUseCaseTest {
result.firstOrNull() shouldBeEqualTo NotificationsStatus.NOT_SUPPORTED
verifyOrder {
// we should first check account data
fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID)
fakeCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID)
fakeCanToggleNotificationsViaPusherUseCase.execute(fakeSession)
}
}
@@ -95,7 +96,7 @@ class GetNotificationsStatusUseCaseTest {
)
)
fakeSession.pushersService().givenPushersLive(pushers)
every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns false
every { fakeCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns flowOf(false)
every { fakeCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns flowOf(true)

// When
@@ -109,7 +110,7 @@ class GetNotificationsStatusUseCaseTest {
fun `given toggle via pusher is supported and no registered pusher when execute then resulting flow contains NOT_SUPPORTED value`() = runTest {
// Given
fakeSession.pushersService().givenPushersLive(emptyList())
every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns false
every { fakeCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns flowOf(false)
every { fakeCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns flowOf(true)

// When
@@ -120,7 +121,7 @@ class GetNotificationsStatusUseCaseTest {
}

@Test
fun `given current session and toggle via account data is supported when execute then resulting flow contains status based on settings value`() = runTest {
fun `given current session and toggle via account data is supported when execute then resulting flow contains status based on account data`() = runTest {
// Given
fakeSession
.accountDataService()
@@ -130,13 +131,19 @@ class GetNotificationsStatusUseCaseTest {
isSilenced = false
).toContent(),
)
every { fakeCheckIfCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns true
every { fakeCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID) } returns flowOf(true)
every { fakeCanToggleNotificationsViaPusherUseCase.execute(fakeSession) } returns flowOf(false)

// When
val result = getNotificationsStatusUseCase.execute(fakeSession, A_DEVICE_ID)

// Then
result.firstOrNull() shouldBeEqualTo NotificationsStatus.ENABLED
verify {
fakeCanToggleNotificationsViaAccountDataUseCase.execute(fakeSession, A_DEVICE_ID)
}
verify(inverse = true) {
fakeCanToggleNotificationsViaPusherUseCase.execute(fakeSession)
}
}
}
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ import im.vector.app.core.platform.VectorDummyViewState
import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase
import im.vector.app.core.pushers.RegisterUnifiedPushUseCase
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
import im.vector.app.features.settings.VectorPreferences.Companion.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY
import im.vector.app.test.fakes.FakePushersManager
import im.vector.app.test.fakes.FakeVectorPreferences
import im.vector.app.test.test
@@ -60,6 +61,40 @@ class VectorSettingsNotificationPreferenceViewModelTest {
toggleNotificationsForCurrentSessionUseCase = fakeToggleNotificationsForCurrentSessionUseCase,
)

@Test
fun `given view model init when notifications are enabled in preferences then view event is posted`() {
// Given
fakeVectorPreferences.givenAreNotificationsEnabledForDevice(true)
val expectedEvent = VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceEnabled
val viewModel = createViewModel()

// When
val viewModelTest = viewModel.test()
viewModel.notificationsPreferenceListener.onSharedPreferenceChanged(mockk(), SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY)

// Then
viewModelTest
.assertEvent { event -> event == expectedEvent }
.finish()
}

@Test
fun `given view model init when notifications are disabled in preferences then view event is posted`() {
// Given
fakeVectorPreferences.givenAreNotificationsEnabledForDevice(false)
val expectedEvent = VectorSettingsNotificationPreferenceViewEvent.NotificationsForDeviceDisabled
val viewModel = createViewModel()

// When
val viewModelTest = viewModel.test()
viewModel.notificationsPreferenceListener.onSharedPreferenceChanged(mockk(), SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY)

// Then
viewModelTest
.assertEvent { event -> event == expectedEvent }
.finish()
}

@Test
fun `given DisableNotificationsForDevice action when handling action then disable use case is called`() {
// Given
Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@

package im.vector.app.test.fakes

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
@@ -25,13 +27,22 @@ import io.mockk.runs
import org.matrix.android.sdk.api.session.accountdata.SessionAccountDataService
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataEvent
import org.matrix.android.sdk.api.session.events.model.Content
import org.matrix.android.sdk.api.util.Optional
import org.matrix.android.sdk.api.util.toOptional

class FakeSessionAccountDataService : SessionAccountDataService by mockk(relaxed = true) {

fun givenGetUserAccountDataEventReturns(type: String, content: Content?) {
every { getUserAccountDataEvent(type) } returns content?.let { UserAccountDataEvent(type, it) }
}

fun givenGetLiveUserAccountDataEventReturns(type: String, content: Content?): LiveData<Optional<UserAccountDataEvent>> {
return MutableLiveData(content?.let { UserAccountDataEvent(type, it) }.toOptional())
.also {
every { getLiveUserAccountDataEvent(type) } returns it
}
}

fun givenUpdateUserAccountDataEventSucceeds() {
coEvery { updateUserAccountData(any(), any()) } just runs
}
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@

package im.vector.app.test.fakes

import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import im.vector.app.features.settings.BackgroundSyncMode
import im.vector.app.features.settings.VectorPreferences
import io.mockk.every
@@ -78,10 +77,4 @@ class FakeVectorPreferences {
fun givenIsBackgroundSyncEnabled(isEnabled: Boolean) {
every { instance.isBackgroundSyncEnabled() } returns isEnabled
}

fun givenChangeOnPreference(key: String) {
every { instance.subscribeToChanges(any()) } answers {
firstArg<OnSharedPreferenceChangeListener>().onSharedPreferenceChanged(mockk(), key)
}
}
}

0 comments on commit 6f0a95b

Please sign in to comment.