-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7692 from vector-im/feature/mna/listen-notificati…
…on-account-data Update notifications setting when m.local_notification_settings.<device-id> event changes for current device (PSG-874)
Showing
11 changed files
with
377 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
32 changes: 32 additions & 0 deletions
32
.../features/settings/devices/v2/notification/CanToggleNotificationsViaAccountDataUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ures/settings/devices/v2/notification/GetNotificationSettingsAccountDataUpdatesUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...tures/settings/devices/v2/notification/CanToggleNotificationsViaAccountDataUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
.../settings/devices/v2/notification/GetNotificationSettingsAccountDataUpdatesUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters