Skip to content

Commit 7d16c86

Browse files
authored
Merge pull request #8130 from vector-im/feature/fre/poll_sync_push_rules_after_creation
[Poll] Synchronize polls and message push rules after creation (PSG-1137)
2 parents f887acd + 8bf46b1 commit 7d16c86

File tree

13 files changed

+317
-33
lines changed

13 files changed

+317
-33
lines changed

changelog.d/8130.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[Poll] Synchronize polls and message push rules

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/pushrules/RuleIds.kt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,4 @@ object RuleIds {
6161
const val RULE_ID_FALLBACK = ".m.rule.fallback"
6262

6363
const val RULE_ID_REACTION = ".m.rule.reaction"
64-
65-
fun getSyncedRules(ruleId: String): List<String> {
66-
return when (ruleId) {
67-
RULE_ID_ONE_TO_ONE_ROOM -> listOf(
68-
RULE_ID_POLL_START_ONE_TO_ONE,
69-
RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE,
70-
RULE_ID_POLL_END_ONE_TO_ONE,
71-
RULE_ID_POLL_END_ONE_TO_ONE_UNSTABLE,
72-
)
73-
RULE_ID_ALL_OTHER_MESSAGES_ROOMS -> listOf(
74-
RULE_ID_POLL_START,
75-
RULE_ID_POLL_START_UNSTABLE,
76-
RULE_ID_POLL_END,
77-
RULE_ID_POLL_END_UNSTABLE,
78-
)
79-
else -> emptyList()
80-
}
81-
}
8264
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2023 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+
* http://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 im.vector.app.core.notification
18+
19+
import im.vector.app.features.session.coroutineScope
20+
import im.vector.app.features.settings.notifications.usecase.UpdatePushRulesIfNeededUseCase
21+
import kotlinx.coroutines.Job
22+
import kotlinx.coroutines.flow.collect
23+
import kotlinx.coroutines.flow.onEach
24+
import kotlinx.coroutines.launch
25+
import org.matrix.android.sdk.api.session.Session
26+
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
27+
import org.matrix.android.sdk.flow.flow
28+
import javax.inject.Inject
29+
import javax.inject.Singleton
30+
31+
/**
32+
* Listen changes in Account Data to update the push rules if needed.
33+
*/
34+
@Singleton
35+
class PushRulesUpdater @Inject constructor(
36+
private val updatePushRulesIfNeededUseCase: UpdatePushRulesIfNeededUseCase,
37+
) {
38+
39+
private var job: Job? = null
40+
41+
fun onSessionStarted(session: Session) {
42+
updatePushRulesOnChange(session)
43+
}
44+
45+
private fun updatePushRulesOnChange(session: Session) {
46+
job?.cancel()
47+
job = session.coroutineScope.launch {
48+
session.flow()
49+
.liveUserAccountData(UserAccountDataTypes.TYPE_PUSH_RULES)
50+
.onEach { updatePushRulesIfNeededUseCase.execute(session) }
51+
.collect()
52+
}
53+
}
54+
}

vector/src/main/java/im/vector/app/core/session/ConfigureAndStartSessionUseCase.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.content.Context
2020
import dagger.hilt.android.qualifiers.ApplicationContext
2121
import im.vector.app.core.extensions.startSyncing
2222
import im.vector.app.core.notification.NotificationsSettingUpdater
23+
import im.vector.app.core.notification.PushRulesUpdater
2324
import im.vector.app.core.session.clientinfo.UpdateMatrixClientInfoUseCase
2425
import im.vector.app.features.call.webrtc.WebRtcCallManager
2526
import im.vector.app.features.session.coroutineScope
@@ -37,6 +38,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor(
3738
private val vectorPreferences: VectorPreferences,
3839
private val notificationsSettingUpdater: NotificationsSettingUpdater,
3940
private val updateNotificationSettingsAccountDataUseCase: UpdateNotificationSettingsAccountDataUseCase,
41+
private val pushRulesUpdater: PushRulesUpdater,
4042
) {
4143

4244
fun execute(session: Session, startSyncing: Boolean = true) {
@@ -50,6 +52,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor(
5052
updateMatrixClientInfoIfNeeded(session)
5153
createNotificationSettingsAccountDataIfNeeded(session)
5254
notificationsSettingUpdater.onSessionStarted(session)
55+
pushRulesUpdater.onSessionStarted(session)
5356
}
5457

5558
private fun updateMatrixClientInfoIfNeeded(session: Session) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2023 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+
* http://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 im.vector.app.features.settings.notifications
18+
19+
import org.matrix.android.sdk.api.session.pushrules.RuleIds
20+
21+
fun RuleIds.getSyncedRules(ruleId: String): List<String> {
22+
return when (ruleId) {
23+
RULE_ID_ONE_TO_ONE_ROOM -> listOf(
24+
RULE_ID_POLL_START_ONE_TO_ONE,
25+
RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE,
26+
RULE_ID_POLL_END_ONE_TO_ONE,
27+
RULE_ID_POLL_END_ONE_TO_ONE_UNSTABLE,
28+
)
29+
RULE_ID_ALL_OTHER_MESSAGES_ROOMS -> listOf(
30+
RULE_ID_POLL_START,
31+
RULE_ID_POLL_START_UNSTABLE,
32+
RULE_ID_POLL_END,
33+
RULE_ID_POLL_END_UNSTABLE,
34+
)
35+
else -> emptyList()
36+
}
37+
}
38+
39+
fun RuleIds.getParentRule(ruleId: String): String? {
40+
return when (ruleId) {
41+
RULE_ID_POLL_START_ONE_TO_ONE,
42+
RULE_ID_POLL_START_ONE_TO_ONE_UNSTABLE,
43+
RULE_ID_POLL_END_ONE_TO_ONE,
44+
RULE_ID_POLL_END_ONE_TO_ONE_UNSTABLE -> RULE_ID_ONE_TO_ONE_ROOM
45+
RULE_ID_POLL_START,
46+
RULE_ID_POLL_START_UNSTABLE,
47+
RULE_ID_POLL_END,
48+
RULE_ID_POLL_END_UNSTABLE -> RULE_ID_ALL_OTHER_MESSAGES_ROOMS
49+
else -> null
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2023 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+
* http://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 im.vector.app.features.settings.notifications.usecase
18+
19+
import im.vector.app.features.settings.notifications.getParentRule
20+
import org.matrix.android.sdk.api.session.Session
21+
import org.matrix.android.sdk.api.session.pushrules.RuleIds
22+
import org.matrix.android.sdk.api.session.pushrules.getActions
23+
import org.matrix.android.sdk.api.session.pushrules.rest.PushRule
24+
import org.matrix.android.sdk.api.session.pushrules.rest.PushRuleAndKind
25+
import javax.inject.Inject
26+
27+
class UpdatePushRulesIfNeededUseCase @Inject constructor() {
28+
29+
suspend fun execute(session: Session) {
30+
val ruleSet = session.pushRuleService().getPushRules()
31+
val pushRules = ruleSet.getAllRules()
32+
val rulesToUpdate = pushRules.mapNotNull { rule ->
33+
val parent = RuleIds.getParentRule(rule.ruleId)?.let { ruleId -> ruleSet.findDefaultRule(ruleId) }
34+
if (parent != null && (rule.enabled != parent.pushRule.enabled || rule.actions != parent.pushRule.actions)) {
35+
PushRuleWithParent(rule, parent)
36+
} else {
37+
null
38+
}
39+
}
40+
41+
rulesToUpdate.forEach {
42+
session.pushRuleService().updatePushRuleActions(
43+
kind = it.parent.kind,
44+
ruleId = it.rule.ruleId,
45+
enable = it.parent.pushRule.enabled,
46+
actions = it.parent.pushRule.getActions(),
47+
)
48+
}
49+
}
50+
51+
private data class PushRuleWithParent(
52+
val rule: PushRule,
53+
val parent: PushRuleAndKind,
54+
)
55+
}

vector/src/test/java/im/vector/app/core/session/ConfigureAndStartSessionUseCaseTest.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import im.vector.app.features.session.coroutineScope
2222
import im.vector.app.features.settings.devices.v2.notification.UpdateNotificationSettingsAccountDataUseCase
2323
import im.vector.app.test.fakes.FakeContext
2424
import im.vector.app.test.fakes.FakeNotificationsSettingUpdater
25+
import im.vector.app.test.fakes.FakePushRulesUpdater
2526
import im.vector.app.test.fakes.FakeSession
2627
import im.vector.app.test.fakes.FakeVectorPreferences
2728
import im.vector.app.test.fakes.FakeWebRtcCallManager
@@ -47,6 +48,7 @@ class ConfigureAndStartSessionUseCaseTest {
4748
private val fakeUpdateMatrixClientInfoUseCase = mockk<UpdateMatrixClientInfoUseCase>()
4849
private val fakeVectorPreferences = FakeVectorPreferences()
4950
private val fakeNotificationsSettingUpdater = FakeNotificationsSettingUpdater()
51+
private val fakePushRulesUpdater = FakePushRulesUpdater()
5052
private val fakeUpdateNotificationSettingsAccountDataUseCase = mockk<UpdateNotificationSettingsAccountDataUseCase>()
5153

5254
private val configureAndStartSessionUseCase = ConfigureAndStartSessionUseCase(
@@ -56,6 +58,7 @@ class ConfigureAndStartSessionUseCaseTest {
5658
vectorPreferences = fakeVectorPreferences.instance,
5759
notificationsSettingUpdater = fakeNotificationsSettingUpdater.instance,
5860
updateNotificationSettingsAccountDataUseCase = fakeUpdateNotificationSettingsAccountDataUseCase,
61+
pushRulesUpdater = fakePushRulesUpdater.instance,
5962
)
6063

6164
@Before
@@ -78,7 +81,8 @@ class ConfigureAndStartSessionUseCaseTest {
7881
coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) }
7982
coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) }
8083
fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = true)
81-
fakeNotificationsSettingUpdater.givenOnSessionsStarted(aSession)
84+
fakeNotificationsSettingUpdater.givenOnSessionStarted(aSession)
85+
fakePushRulesUpdater.givenOnSessionStarted(aSession)
8286

8387
// When
8488
configureAndStartSessionUseCase.execute(aSession, startSyncing = true)
@@ -102,7 +106,8 @@ class ConfigureAndStartSessionUseCaseTest {
102106
fakeWebRtcCallManager.givenCheckForProtocolsSupportIfNeededSucceeds()
103107
coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) }
104108
fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = false)
105-
fakeNotificationsSettingUpdater.givenOnSessionsStarted(aSession)
109+
fakeNotificationsSettingUpdater.givenOnSessionStarted(aSession)
110+
fakePushRulesUpdater.givenOnSessionStarted(aSession)
106111

107112
// When
108113
configureAndStartSessionUseCase.execute(aSession, startSyncing = true)
@@ -129,7 +134,8 @@ class ConfigureAndStartSessionUseCaseTest {
129134
coJustRun { fakeUpdateMatrixClientInfoUseCase.execute(any()) }
130135
coJustRun { fakeUpdateNotificationSettingsAccountDataUseCase.execute(any()) }
131136
fakeVectorPreferences.givenIsClientInfoRecordingEnabled(isEnabled = true)
132-
fakeNotificationsSettingUpdater.givenOnSessionsStarted(aSession)
137+
fakeNotificationsSettingUpdater.givenOnSessionStarted(aSession)
138+
fakePushRulesUpdater.givenOnSessionStarted(aSession)
133139

134140
// When
135141
configureAndStartSessionUseCase.execute(aSession, startSyncing = false)
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 New Vector Ltd
2+
* Copyright (c) 2023 New Vector Ltd
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,11 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package im.vector.app.features.settings.notifications
17+
package im.vector.app.features.settings.notifications.usecase
1818

1919
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
20-
import im.vector.app.features.settings.notifications.usecase.DisableNotificationsForCurrentSessionUseCase
21-
import im.vector.app.features.settings.notifications.usecase.ToggleNotificationsForCurrentSessionUseCase
2220
import im.vector.app.test.fakes.FakePushersManager
2321
import io.mockk.coJustRun
2422
import io.mockk.coVerify
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 New Vector Ltd
2+
* Copyright (c) 2023 New Vector Ltd
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,12 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package im.vector.app.features.settings.notifications
17+
package im.vector.app.features.settings.notifications.usecase
1818

1919
import im.vector.app.core.pushers.EnsureFcmTokenIsRetrievedUseCase
2020
import im.vector.app.core.pushers.RegisterUnifiedPushUseCase
21-
import im.vector.app.features.settings.notifications.usecase.EnableNotificationsForCurrentSessionUseCase
22-
import im.vector.app.features.settings.notifications.usecase.ToggleNotificationsForCurrentSessionUseCase
2321
import im.vector.app.test.fakes.FakePushersManager
2422
import io.mockk.coJustRun
2523
import io.mockk.coVerify
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 New Vector Ltd
2+
* Copyright (c) 2023 New Vector Ltd
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,12 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
package im.vector.app.features.settings.notifications
17+
package im.vector.app.features.settings.notifications.usecase
1818

1919
import im.vector.app.features.settings.devices.v2.notification.CheckIfCanToggleNotificationsViaPusherUseCase
2020
import im.vector.app.features.settings.devices.v2.notification.DeleteNotificationSettingsAccountDataUseCase
2121
import im.vector.app.features.settings.devices.v2.notification.SetNotificationSettingsAccountDataUseCase
22-
import im.vector.app.features.settings.notifications.usecase.ToggleNotificationsForCurrentSessionUseCase
2322
import im.vector.app.test.fakes.FakeActiveSessionHolder
2423
import im.vector.app.test.fakes.FakeUnifiedPushHelper
2524
import im.vector.app.test.fixtures.PusherFixture

0 commit comments

Comments
 (0)