-
Notifications
You must be signed in to change notification settings - Fork 731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Poll] Synchronize polls and message push rules after creation (PSG-1137) #8130
Merged
Florian14
merged 5 commits into
develop
from
feature/fre/poll_sync_push_rules_after_creation
Feb 17, 2023
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ | ||
[Poll] Synchronize polls and message push rules |
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
54 changes: 54 additions & 0 deletions
54
vector/src/main/java/im/vector/app/core/notification/PushRulesUpdater.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,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() | ||
} | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
...a/im/vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCase.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,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, | ||
) | ||
} |
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
106 changes: 106 additions & 0 deletions
106
.../vector/app/features/settings/notifications/usecase/UpdatePushRulesIfNeededUseCaseTest.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,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<Action>): PushRule { | ||
val pushRule = mockk<PushRule> { | ||
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<PushRuleAndKind> { | ||
every { this@mockk.pushRule } returns pushRule | ||
every { kind } returns mockk() | ||
} | ||
|
||
every { fakeSession.fakePushRuleService.getPushRules().findDefaultRule(ruleId) } returns ruleAndKind | ||
|
||
return pushRule | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be worth adding a comment here to explain what a parent rule is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm also wondering if this should be moved outside of the sdk, this relation between the push rules is mentioned in the MSC but their connection is done at the application side (the clients are not forced to apply this relation, but on the other hand it could also be interesting to share this information at the sdk level... wdyt?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find any mention of parent rules in the MSC3930. Is that the right place to look?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The term "parent" is not mentioned, I was talking about these references to
m.room.message
rule:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It sounds like more of a recommendation from the MSC about how the push rules might be related. In a different app, for example one that is based around polls rather than messages, I guess the parent relation might be reversed?
So I'm leaning towards moving this out of the SDK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're totally right, I moved the extensions to the vector module dcd43d6