From 1f4c098d8b6836209bae170c1c7f88d7b5bc364a Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Mon, 13 Feb 2023 18:39:35 +0100 Subject: [PATCH 1/5] Synchronize polls and message push rules after creation --- .../sdk/api/session/pushrules/RuleIds.kt | 46 ++++++++++------ .../app/core/notification/PushRulesUpdater.kt | 54 ++++++++++++++++++ .../ConfigureAndStartSessionUseCase.kt | 3 + ...orSettingsPushRuleNotificationViewModel.kt | 1 + .../usecase/UpdatePushRulesIfNeededUseCase.kt | 55 +++++++++++++++++++ .../ConfigureAndStartSessionUseCaseTest.kt | 1 + 6 files changed, 144 insertions(+), 16 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/core/notification/PushRulesUpdater.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCase.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushrules/RuleIds.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushrules/RuleIds.kt index a3755f85b7..484344e6ea 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushrules/RuleIds.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushrules/RuleIds.kt @@ -61,22 +61,36 @@ object RuleIds { const val RULE_ID_FALLBACK = ".m.rule.fallback" const val RULE_ID_REACTION = ".m.rule.reaction" +} - fun getSyncedRules(ruleId: String): List { - return when (ruleId) { - RULE_ID_ONE_TO_ONE_ROOM -> listOf( - RULE_ID_POLL_START_ONE_TO_ONE, - RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE, - RULE_ID_POLL_END_ONE_TO_ONE, - RULE_ID_POLL_END_ONE_TO_ONE_UNSTABLE, - ) - RULE_ID_ALL_OTHER_MESSAGES_ROOMS -> listOf( - RULE_ID_POLL_START, - RULE_ID_POLL_START_UNSTABLE, - RULE_ID_POLL_END, - RULE_ID_POLL_END_UNSTABLE, - ) - else -> emptyList() - } +fun RuleIds.getSyncedRules(ruleId: String): List { + return when (ruleId) { + RULE_ID_ONE_TO_ONE_ROOM -> listOf( + RULE_ID_POLL_START_ONE_TO_ONE, + RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE, + RULE_ID_POLL_END_ONE_TO_ONE, + RULE_ID_POLL_END_ONE_TO_ONE_UNSTABLE, + ) + RULE_ID_ALL_OTHER_MESSAGES_ROOMS -> listOf( + RULE_ID_POLL_START, + RULE_ID_POLL_START_UNSTABLE, + RULE_ID_POLL_END, + RULE_ID_POLL_END_UNSTABLE, + ) + else -> emptyList() + } +} + +fun RuleIds.getParentRule(ruleId: String): String? { + return when (ruleId) { + RULE_ID_POLL_START_ONE_TO_ONE, + RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE, + RULE_ID_POLL_END_ONE_TO_ONE, + RULE_ID_POLL_END_ONE_TO_ONE_UNSTABLE -> RULE_ID_ONE_TO_ONE_ROOM + RULE_ID_POLL_START, + RULE_ID_POLL_START_UNSTABLE, + RULE_ID_POLL_END, + RULE_ID_POLL_END_UNSTABLE -> RULE_ID_ALL_OTHER_MESSAGES_ROOMS + else -> null } } diff --git a/vector/src/main/java/im/vector/app/core/notification/PushRulesUpdater.kt b/vector/src/main/java/im/vector/app/core/notification/PushRulesUpdater.kt new file mode 100644 index 0000000000..4925941f92 --- /dev/null +++ b/vector/src/main/java/im/vector/app/core/notification/PushRulesUpdater.kt @@ -0,0 +1,54 @@ +/* + * 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 im.vector.app.core.notification + +import im.vector.app.features.session.coroutineScope +import im.vector.app.features.settings.notifications.usecase.UpdatePushRulesIfNeededUseCase +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch +import org.matrix.android.sdk.api.session.Session +import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes +import org.matrix.android.sdk.flow.flow +import javax.inject.Inject +import javax.inject.Singleton + +/** + * Listen changes in Account Data to update the push rules if needed. + */ +@Singleton +class PushRulesUpdater @Inject constructor( + private val updatePushRulesIfNeededUseCase: UpdatePushRulesIfNeededUseCase, +) { + + private var job: Job? = null + + fun onSessionStarted(session: Session) { + updatePushRulesOnChange(session) + } + + private fun updatePushRulesOnChange(session: Session) { + job?.cancel() + job = session.coroutineScope.launch { + session.flow() + .liveUserAccountData(UserAccountDataTypes.TYPE_PUSH_RULES) + .onEach { updatePushRulesIfNeededUseCase.execute(session) } + .collect() + } + } +} diff --git a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt index c6a2635e6c..b9573e9292 100644 --- a/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt +++ b/vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt @@ -20,6 +20,7 @@ import android.content.Context import dagger.hilt.android.qualifiers.ApplicationContext import im.vector.app.core.extensions.startSyncing import im.vector.app.core.notification.NotificationsSettingUpdater +import im.vector.app.core.notification.PushRulesUpdater import im.vector.app.core.session.clientinfo.UpdateMatrixClientInfoUseCase import im.vector.app.features.call.webrtc.WebRtcCallManager import im.vector.app.features.session.coroutineScope @@ -37,6 +38,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor( private val vectorPreferences: VectorPreferences, private val notificationsSettingUpdater: NotificationsSettingUpdater, private val updateNotificationSettingsAccountDataUseCase: UpdateNotificationSettingsAccountDataUseCase, + private val pushRulesUpdater: PushRulesUpdater, ) { fun execute(session: Session, startSyncing: Boolean = true) { @@ -50,6 +52,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor( updateMatrixClientInfoIfNeeded(session) createNotificationSettingsAccountDataIfNeeded(session) notificationsSettingUpdater.onSessionStarted(session) + pushRulesUpdater.onSessionStarted(session) } private fun updateMatrixClientInfoIfNeeded(session: Session) { diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewModel.kt index 39969ec13e..daf9c044d1 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewModel.kt @@ -32,6 +32,7 @@ import org.matrix.android.sdk.api.failure.MatrixError import org.matrix.android.sdk.api.session.pushrules.Action import org.matrix.android.sdk.api.session.pushrules.RuleIds import org.matrix.android.sdk.api.session.pushrules.RuleKind +import org.matrix.android.sdk.api.session.pushrules.getSyncedRules import org.matrix.android.sdk.api.session.pushrules.rest.PushRuleAndKind private typealias ViewModel = VectorSettingsPushRuleNotificationViewModel diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCase.kt new file mode 100644 index 0000000000..9397c9e74c --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCase.kt @@ -0,0 +1,55 @@ +/* + * 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 im.vector.app.features.settings.notifications.usecase + +import org.matrix.android.sdk.api.session.Session +import org.matrix.android.sdk.api.session.pushrules.RuleIds +import org.matrix.android.sdk.api.session.pushrules.getActions +import org.matrix.android.sdk.api.session.pushrules.getParentRule +import org.matrix.android.sdk.api.session.pushrules.rest.PushRule +import org.matrix.android.sdk.api.session.pushrules.rest.PushRuleAndKind +import javax.inject.Inject + +class UpdatePushRulesIfNeededUseCase @Inject constructor() { + + suspend fun execute(session: Session) { + val ruleSet = session.pushRuleService().getPushRules() + val pushRules = ruleSet.getAllRules() + val rulesToUpdate = pushRules.mapNotNull { rule -> + val parent = RuleIds.getParentRule(rule.ruleId)?.let { ruleId -> ruleSet.findDefaultRule(ruleId) } + if (parent != null && (rule.enabled != parent.pushRule.enabled || rule.actions != parent.pushRule.actions)) { + PushRuleWithParent(rule, parent) + } else { + null + } + } + + rulesToUpdate.forEach { + session.pushRuleService().updatePushRuleActions( + kind = it.parent.kind, + ruleId = it.rule.ruleId, + enable = it.parent.pushRule.enabled, + actions = it.parent.pushRule.getActions(), + ) + } + } + + private data class PushRuleWithParent( + val rule: PushRule, + val parent: PushRuleAndKind, + ) +} diff --git a/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt index d72500b520..786b200ef5 100644 --- a/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt @@ -56,6 +56,7 @@ class ConfigureAndStartSessionUseCaseTest { vectorPreferences = fakeVectorPreferences.instance, notificationsSettingUpdater = fakeNotificationsSettingUpdater.instance, updateNotificationSettingsAccountDataUseCase = fakeUpdateNotificationSettingsAccountDataUseCase, + pushRulesUpdater = mockk(), ) @Before From 5bb78c995ee8d157cc3a1b69ae453a6f58b77f19 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Tue, 14 Feb 2023 10:20:38 +0100 Subject: [PATCH 2/5] Add unit test for UpdatePushRulesIfNeededUseCaseTest --- ...tificationsForCurrentSessionUseCaseTest.kt | 6 +- ...tificationsForCurrentSessionUseCaseTest.kt | 6 +- ...tificationsForCurrentSessionUseCaseTest.kt | 5 +- .../UpdatePushRulesIfNeededUseCaseTest.kt | 106 ++++++++++++++++++ 4 files changed, 112 insertions(+), 11 deletions(-) rename vector/src/test/java/im/vector/app/features/settings/notifications/{ => usecase}/DisableNotificationsForCurrentSessionUseCaseTest.kt (87%) rename vector/src/test/java/im/vector/app/features/settings/notifications/{ => usecase}/EnableNotificationsForCurrentSessionUseCaseTest.kt (92%) rename vector/src/test/java/im/vector/app/features/settings/notifications/{ => usecase}/ToggleNotificationsForCurrentSessionUseCaseTest.kt (96%) create mode 100644 vector/src/test/java/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCaseTest.kt diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/usecase/DisableNotificationsForCurrentSessionUseCaseTest.kt similarity index 87% rename from vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt rename to vector/src/test/java/im/vector/app/features/settings/notifications/usecase/DisableNotificationsForCurrentSessionUseCaseTest.kt index ceb6fd9217..958c120083 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/usecase/DisableNotificationsForCurrentSessionUseCaseTest.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 New Vector Ltd + * 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. @@ -14,11 +14,9 @@ * limitations under the License. */ -package im.vector.app.features.settings.notifications +package im.vector.app.features.settings.notifications.usecase import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase -import im.vector.app.features.settings.notifications.usecase.DisableNotificationsForCurrentSessionUseCase -import im.vector.app.features.settings.notifications.usecase.ToggleNotificationsForCurrentSessionUseCase import im.vector.app.test.fakes.FakePushersManager import io.mockk.coJustRun import io.mockk.coVerify diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/usecase/EnableNotificationsForCurrentSessionUseCaseTest.kt similarity index 92% rename from vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt rename to vector/src/test/java/im/vector/app/features/settings/notifications/usecase/EnableNotificationsForCurrentSessionUseCaseTest.kt index fa5d0a8bd1..1439dbad74 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/usecase/EnableNotificationsForCurrentSessionUseCaseTest.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 New Vector Ltd + * 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. @@ -14,12 +14,10 @@ * limitations under the License. */ -package im.vector.app.features.settings.notifications +package im.vector.app.features.settings.notifications.usecase import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase import im.vector.app.core.pushers.RegisterUnifiedPushUseCase -import im.vector.app.features.settings.notifications.usecase.EnableNotificationsForCurrentSessionUseCase -import im.vector.app.features.settings.notifications.usecase.ToggleNotificationsForCurrentSessionUseCase import im.vector.app.test.fakes.FakePushersManager import io.mockk.coJustRun import io.mockk.coVerify diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/usecase/ToggleNotificationsForCurrentSessionUseCaseTest.kt similarity index 96% rename from vector/src/test/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCaseTest.kt rename to vector/src/test/java/im/vector/app/features/settings/notifications/usecase/ToggleNotificationsForCurrentSessionUseCaseTest.kt index 83a4265ace..005687fbf7 100644 --- a/vector/src/test/java/im/vector/app/features/settings/notifications/ToggleNotificationsForCurrentSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/usecase/ToggleNotificationsForCurrentSessionUseCaseTest.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 New Vector Ltd + * 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. @@ -14,12 +14,11 @@ * limitations under the License. */ -package im.vector.app.features.settings.notifications +package im.vector.app.features.settings.notifications.usecase 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 im.vector.app.features.settings.notifications.usecase.ToggleNotificationsForCurrentSessionUseCase import im.vector.app.test.fakes.FakeActiveSessionHolder import im.vector.app.test.fakes.FakeUnifiedPushHelper import im.vector.app.test.fixtures.PusherFixture diff --git a/vector/src/test/java/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCaseTest.kt new file mode 100644 index 0000000000..7dcbe68d4b --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCaseTest.kt @@ -0,0 +1,106 @@ +/* + * 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 im.vector.app.features.settings.notifications.usecase + +import im.vector.app.test.fakes.FakeSession +import io.mockk.coVerifySequence +import io.mockk.every +import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.unmockkAll +import kotlinx.coroutines.test.runTest +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.matrix.android.sdk.api.session.pushrules.Action +import org.matrix.android.sdk.api.session.pushrules.RuleIds +import org.matrix.android.sdk.api.session.pushrules.getActions +import org.matrix.android.sdk.api.session.pushrules.rest.PushRule +import org.matrix.android.sdk.api.session.pushrules.rest.PushRuleAndKind + +internal class UpdatePushRulesIfNeededUseCaseTest { + + private val fakeSession = FakeSession() + private val updatePushRulesIfNeededUseCase = UpdatePushRulesIfNeededUseCase() + + @Before + fun setup() { + mockkStatic("org.matrix.android.sdk.api.session.pushrules.ActionKt") + } + + @After + fun tearDown() { + unmockkAll() + } + + @Test + fun test() = runTest { + // Given + val firstActions = listOf(Action.Notify) + val secondActions = listOf(Action.DoNotNotify) + val rules = listOf( + // first set of related rules + givenARuleId(RuleIds.RULE_ID_ONE_TO_ONE_ROOM, true, firstActions), + givenARuleId(RuleIds.RULE_ID_POLL_START_ONE_TO_ONE, true, listOf(Action.DoNotNotify)), // diff + givenARuleId(RuleIds.RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE, true, emptyList()), // diff + givenARuleId(RuleIds.RULE_ID_POLL_END_ONE_TO_ONE, false, listOf(Action.Notify)), // diff + givenARuleId(RuleIds.RULE_ID_POLL_END_ONE_TO_ONE_UNSTABLE, true, listOf(Action.Notify)), + // second set of related rules + givenARuleId(RuleIds.RULE_ID_ALL_OTHER_MESSAGES_ROOMS, false, secondActions), + givenARuleId(RuleIds.RULE_ID_POLL_START, true, listOf(Action.Notify)), // diff + givenARuleId(RuleIds.RULE_ID_POLL_START_UNSTABLE, false, listOf(Action.DoNotNotify)), + givenARuleId(RuleIds.RULE_ID_POLL_END, false, listOf(Action.Notify)), // diff + givenARuleId(RuleIds.RULE_ID_POLL_END_UNSTABLE, true, listOf()), // diff + // Another rule + givenARuleId(RuleIds.RULE_ID_CONTAIN_USER_NAME, true, listOf(Action.Notify)), + ) + every { fakeSession.fakePushRuleService.getPushRules().getAllRules() } returns rules + + // When + updatePushRulesIfNeededUseCase.execute(fakeSession) + + // Then + coVerifySequence { + fakeSession.fakePushRuleService.getPushRules() + // first set + fakeSession.fakePushRuleService.updatePushRuleActions(any(), RuleIds.RULE_ID_POLL_START_ONE_TO_ONE, true, firstActions) + fakeSession.fakePushRuleService.updatePushRuleActions(any(), RuleIds.RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE, true, firstActions) + fakeSession.fakePushRuleService.updatePushRuleActions(any(), RuleIds.RULE_ID_POLL_END_ONE_TO_ONE, true, firstActions) + // second set + fakeSession.fakePushRuleService.updatePushRuleActions(any(), RuleIds.RULE_ID_POLL_START, false, secondActions) + fakeSession.fakePushRuleService.updatePushRuleActions(any(), RuleIds.RULE_ID_POLL_END, false, secondActions) + fakeSession.fakePushRuleService.updatePushRuleActions(any(), RuleIds.RULE_ID_POLL_END_UNSTABLE, false, secondActions) + } + } + + private fun givenARuleId(ruleId: String, enabled: Boolean, actions: List): PushRule { + val pushRule = mockk { + every { this@mockk.ruleId } returns ruleId + every { this@mockk.enabled } returns enabled + every { this@mockk.actions } returns actions + every { getActions() } returns actions + } + val ruleAndKind = mockk { + every { this@mockk.pushRule } returns pushRule + every { kind } returns mockk() + } + + every { fakeSession.fakePushRuleService.getPushRules().findDefaultRule(ruleId) } returns ruleAndKind + + return pushRule + } +} From 529f640554d3294fd2622da3b64ae0a97f97acb0 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Tue, 14 Feb 2023 17:27:50 +0100 Subject: [PATCH 3/5] Changelog --- changelog.d/8130.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/8130.feature diff --git a/changelog.d/8130.feature b/changelog.d/8130.feature new file mode 100644 index 0000000000..80b7b1debd --- /dev/null +++ b/changelog.d/8130.feature @@ -0,0 +1 @@ +[Poll] Synchronize polls and message push rules From dcd43d6e7fc8ab1949fb5163bf6ddedf26b44559 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Fri, 17 Feb 2023 10:12:56 +0100 Subject: [PATCH 4/5] Move push rule ids extensions to vector module --- .../sdk/api/session/pushrules/RuleIds.kt | 32 ------------ .../settings/notifications/RuleIdsExt.kt | 51 +++++++++++++++++++ ...orSettingsPushRuleNotificationViewModel.kt | 1 - .../usecase/UpdatePushRulesIfNeededUseCase.kt | 2 +- 4 files changed, 52 insertions(+), 34 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/settings/notifications/RuleIdsExt.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushrules/RuleIds.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushrules/RuleIds.kt index 484344e6ea..34581b613a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushrules/RuleIds.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushrules/RuleIds.kt @@ -62,35 +62,3 @@ object RuleIds { const val RULE_ID_REACTION = ".m.rule.reaction" } - -fun RuleIds.getSyncedRules(ruleId: String): List { - return when (ruleId) { - RULE_ID_ONE_TO_ONE_ROOM -> listOf( - RULE_ID_POLL_START_ONE_TO_ONE, - RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE, - RULE_ID_POLL_END_ONE_TO_ONE, - RULE_ID_POLL_END_ONE_TO_ONE_UNSTABLE, - ) - RULE_ID_ALL_OTHER_MESSAGES_ROOMS -> listOf( - RULE_ID_POLL_START, - RULE_ID_POLL_START_UNSTABLE, - RULE_ID_POLL_END, - RULE_ID_POLL_END_UNSTABLE, - ) - else -> emptyList() - } -} - -fun RuleIds.getParentRule(ruleId: String): String? { - return when (ruleId) { - RULE_ID_POLL_START_ONE_TO_ONE, - RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE, - RULE_ID_POLL_END_ONE_TO_ONE, - RULE_ID_POLL_END_ONE_TO_ONE_UNSTABLE -> RULE_ID_ONE_TO_ONE_ROOM - RULE_ID_POLL_START, - RULE_ID_POLL_START_UNSTABLE, - RULE_ID_POLL_END, - RULE_ID_POLL_END_UNSTABLE -> RULE_ID_ALL_OTHER_MESSAGES_ROOMS - else -> null - } -} diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/RuleIdsExt.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/RuleIdsExt.kt new file mode 100644 index 0000000000..1e14ef9157 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/RuleIdsExt.kt @@ -0,0 +1,51 @@ +/* + * 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 im.vector.app.features.settings.notifications + +import org.matrix.android.sdk.api.session.pushrules.RuleIds + +fun RuleIds.getSyncedRules(ruleId: String): List { + return when (ruleId) { + RULE_ID_ONE_TO_ONE_ROOM -> listOf( + RULE_ID_POLL_START_ONE_TO_ONE, + RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE, + RULE_ID_POLL_END_ONE_TO_ONE, + RULE_ID_POLL_END_ONE_TO_ONE_UNSTABLE, + ) + RULE_ID_ALL_OTHER_MESSAGES_ROOMS -> listOf( + RULE_ID_POLL_START, + RULE_ID_POLL_START_UNSTABLE, + RULE_ID_POLL_END, + RULE_ID_POLL_END_UNSTABLE, + ) + else -> emptyList() + } +} + +fun RuleIds.getParentRule(ruleId: String): String? { + return when (ruleId) { + RULE_ID_POLL_START_ONE_TO_ONE, + RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE, + RULE_ID_POLL_END_ONE_TO_ONE, + RULE_ID_POLL_END_ONE_TO_ONE_UNSTABLE -> RULE_ID_ONE_TO_ONE_ROOM + RULE_ID_POLL_START, + RULE_ID_POLL_START_UNSTABLE, + RULE_ID_POLL_END, + RULE_ID_POLL_END_UNSTABLE -> RULE_ID_ALL_OTHER_MESSAGES_ROOMS + else -> null + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewModel.kt index daf9c044d1..39969ec13e 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsPushRuleNotificationViewModel.kt @@ -32,7 +32,6 @@ import org.matrix.android.sdk.api.failure.MatrixError import org.matrix.android.sdk.api.session.pushrules.Action import org.matrix.android.sdk.api.session.pushrules.RuleIds import org.matrix.android.sdk.api.session.pushrules.RuleKind -import org.matrix.android.sdk.api.session.pushrules.getSyncedRules import org.matrix.android.sdk.api.session.pushrules.rest.PushRuleAndKind private typealias ViewModel = VectorSettingsPushRuleNotificationViewModel diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCase.kt index 9397c9e74c..6998065f01 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCase.kt @@ -16,10 +16,10 @@ package im.vector.app.features.settings.notifications.usecase +import im.vector.app.features.settings.notifications.getParentRule import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.pushrules.RuleIds import org.matrix.android.sdk.api.session.pushrules.getActions -import org.matrix.android.sdk.api.session.pushrules.getParentRule import org.matrix.android.sdk.api.session.pushrules.rest.PushRule import org.matrix.android.sdk.api.session.pushrules.rest.PushRuleAndKind import javax.inject.Inject From 8bf46b136a5a2fe119d95f94153c14d01bc7117a Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Fri, 17 Feb 2023 12:12:55 +0100 Subject: [PATCH 5/5] Fix broken test --- .../ConfigureAndStartSessionUseCaseTest.kt | 13 +++++--- .../fakes/FakeNotificationsSettingUpdater.kt | 2 +- .../app/test/fakes/FakePushRulesUpdater.kt | 31 +++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/test/fakes/FakePushRulesUpdater.kt diff --git a/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt b/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt index 786b200ef5..54e249f6b0 100644 --- a/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt @@ -22,6 +22,7 @@ import im.vector.app.features.session.coroutineScope import im.vector.app.features.settings.devices.v2.notification.UpdateNotificationSettingsAccountDataUseCase import im.vector.app.test.fakes.FakeContext import im.vector.app.test.fakes.FakeNotificationsSettingUpdater +import im.vector.app.test.fakes.FakePushRulesUpdater import im.vector.app.test.fakes.FakeSession import im.vector.app.test.fakes.FakeVectorPreferences import im.vector.app.test.fakes.FakeWebRtcCallManager @@ -47,6 +48,7 @@ class ConfigureAndStartSessionUseCaseTest { private val fakeUpdateMatrixClientInfoUseCase = mockk() private val fakeVectorPreferences = FakeVectorPreferences() private val fakeNotificationsSettingUpdater = FakeNotificationsSettingUpdater() + private val fakePushRulesUpdater = FakePushRulesUpdater() private val fakeUpdateNotificationSettingsAccountDataUseCase = mockk() private val configureAndStartSessionUseCase = ConfigureAndStartSessionUseCase( @@ -56,7 +58,7 @@ class ConfigureAndStartSessionUseCaseTest { vectorPreferences = fakeVectorPreferences.instance, notificationsSettingUpdater = fakeNotificationsSettingUpdater.instance, updateNotificationSettingsAccountDataUseCase = fakeUpdateNotificationSettingsAccountDataUseCase, - pushRulesUpdater = mockk(), + pushRulesUpdater = fakePushRulesUpdater.instance, ) @Before @@ -79,7 +81,8 @@ class ConfigureAndStartSessionUseCaseTest { coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) } coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) } fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = true) - fakeNotificationsSettingUpdater.givenOnSessionsStarted(aSession) + fakeNotificationsSettingUpdater.givenOnSessionStarted(aSession) + fakePushRulesUpdater.givenOnSessionStarted(aSession) // When configureAndStartSessionUseCase.execute(aSession, startSyncing = true) @@ -103,7 +106,8 @@ class ConfigureAndStartSessionUseCaseTest { fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds() coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) } fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = false) - fakeNotificationsSettingUpdater.givenOnSessionsStarted(aSession) + fakeNotificationsSettingUpdater.givenOnSessionStarted(aSession) + fakePushRulesUpdater.givenOnSessionStarted(aSession) // When configureAndStartSessionUseCase.execute(aSession, startSyncing = true) @@ -130,7 +134,8 @@ class ConfigureAndStartSessionUseCaseTest { coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) } coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) } fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = true) - fakeNotificationsSettingUpdater.givenOnSessionsStarted(aSession) + fakeNotificationsSettingUpdater.givenOnSessionStarted(aSession) + fakePushRulesUpdater.givenOnSessionStarted(aSession) // When configureAndStartSessionUseCase.execute(aSession, startSyncing = false) diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeNotificationsSettingUpdater.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeNotificationsSettingUpdater.kt index 2e397763f8..1fbd59afc9 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeNotificationsSettingUpdater.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeNotificationsSettingUpdater.kt @@ -25,7 +25,7 @@ class FakeNotificationsSettingUpdater { val instance = mockk() - fun givenOnSessionsStarted(session: Session) { + fun givenOnSessionStarted(session: Session) { justRun { instance.onSessionStarted(session) } } } diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakePushRulesUpdater.kt b/vector/src/test/java/im/vector/app/test/fakes/FakePushRulesUpdater.kt new file mode 100644 index 0000000000..b6e5b8f2ae --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakePushRulesUpdater.kt @@ -0,0 +1,31 @@ +/* + * 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.test.fakes + +import im.vector.app.core.notification.PushRulesUpdater +import io.mockk.justRun +import io.mockk.mockk +import org.matrix.android.sdk.api.session.Session + +class FakePushRulesUpdater { + + val instance = mockk() + + fun givenOnSessionStarted(session: Session) { + justRun { instance.onSessionStarted(session) } + } +}