-
Notifications
You must be signed in to change notification settings - Fork 731
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 #8130 from vector-im/feature/fre/poll_sync_push_ru…
…les_after_creation [Poll] Synchronize polls and message push rules after creation (PSG-1137)
- Loading branch information
Showing
13 changed files
with
317 additions
and
33 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 @@ | ||
[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
51 changes: 51 additions & 0 deletions
51
vector/src/main/java/im/vector/app/features/settings/notifications/RuleIdsExt.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,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<String> { | ||
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 | ||
} | ||
} |
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 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.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
Oops, something went wrong.