Synchronize polls and message push rules after creation
This commit is contained in:
parent
0a0ad2d91e
commit
1f4c098d8b
|
@ -61,8 +61,9 @@ object RuleIds {
|
||||||
const val RULE_ID_FALLBACK = ".m.rule.fallback"
|
const val RULE_ID_FALLBACK = ".m.rule.fallback"
|
||||||
|
|
||||||
const val RULE_ID_REACTION = ".m.rule.reaction"
|
const val RULE_ID_REACTION = ".m.rule.reaction"
|
||||||
|
}
|
||||||
|
|
||||||
fun getSyncedRules(ruleId: String): List<String> {
|
fun RuleIds.getSyncedRules(ruleId: String): List<String> {
|
||||||
return when (ruleId) {
|
return when (ruleId) {
|
||||||
RULE_ID_ONE_TO_ONE_ROOM -> listOf(
|
RULE_ID_ONE_TO_ONE_ROOM -> listOf(
|
||||||
RULE_ID_POLL_START_ONE_TO_ONE,
|
RULE_ID_POLL_START_ONE_TO_ONE,
|
||||||
|
@ -79,4 +80,17 @@ object RuleIds {
|
||||||
else -> emptyList()
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,6 +20,7 @@ import android.content.Context
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import im.vector.app.core.extensions.startSyncing
|
import im.vector.app.core.extensions.startSyncing
|
||||||
import im.vector.app.core.notification.NotificationsSettingUpdater
|
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.core.session.clientinfo.UpdateMatrixClientInfoUseCase
|
||||||
import im.vector.app.features.call.webrtc.WebRtcCallManager
|
import im.vector.app.features.call.webrtc.WebRtcCallManager
|
||||||
import im.vector.app.features.session.coroutineScope
|
import im.vector.app.features.session.coroutineScope
|
||||||
|
@ -37,6 +38,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor(
|
||||||
private val vectorPreferences: VectorPreferences,
|
private val vectorPreferences: VectorPreferences,
|
||||||
private val notificationsSettingUpdater: NotificationsSettingUpdater,
|
private val notificationsSettingUpdater: NotificationsSettingUpdater,
|
||||||
private val updateNotificationSettingsAccountDataUseCase: UpdateNotificationSettingsAccountDataUseCase,
|
private val updateNotificationSettingsAccountDataUseCase: UpdateNotificationSettingsAccountDataUseCase,
|
||||||
|
private val pushRulesUpdater: PushRulesUpdater,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun execute(session: Session, startSyncing: Boolean = true) {
|
fun execute(session: Session, startSyncing: Boolean = true) {
|
||||||
|
@ -50,6 +52,7 @@ class ConfigureAndStartSessionUseCase @Inject constructor(
|
||||||
updateMatrixClientInfoIfNeeded(session)
|
updateMatrixClientInfoIfNeeded(session)
|
||||||
createNotificationSettingsAccountDataIfNeeded(session)
|
createNotificationSettingsAccountDataIfNeeded(session)
|
||||||
notificationsSettingUpdater.onSessionStarted(session)
|
notificationsSettingUpdater.onSessionStarted(session)
|
||||||
|
pushRulesUpdater.onSessionStarted(session)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateMatrixClientInfoIfNeeded(session: Session) {
|
private fun updateMatrixClientInfoIfNeeded(session: Session) {
|
||||||
|
|
|
@ -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.Action
|
||||||
import org.matrix.android.sdk.api.session.pushrules.RuleIds
|
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.RuleKind
|
||||||
|
import org.matrix.android.sdk.api.session.pushrules.getSyncedRules
|
||||||
import org.matrix.android.sdk.api.session.pushrules.rest.PushRuleAndKind
|
import org.matrix.android.sdk.api.session.pushrules.rest.PushRuleAndKind
|
||||||
|
|
||||||
private typealias ViewModel = VectorSettingsPushRuleNotificationViewModel
|
private typealias ViewModel = VectorSettingsPushRuleNotificationViewModel
|
||||||
|
|
|
@ -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,
|
||||||
|
)
|
||||||
|
}
|
|
@ -56,6 +56,7 @@ class ConfigureAndStartSessionUseCaseTest {
|
||||||
vectorPreferences = fakeVectorPreferences.instance,
|
vectorPreferences = fakeVectorPreferences.instance,
|
||||||
notificationsSettingUpdater = fakeNotificationsSettingUpdater.instance,
|
notificationsSettingUpdater = fakeNotificationsSettingUpdater.instance,
|
||||||
updateNotificationSettingsAccountDataUseCase = fakeUpdateNotificationSettingsAccountDataUseCase,
|
updateNotificationSettingsAccountDataUseCase = fakeUpdateNotificationSettingsAccountDataUseCase,
|
||||||
|
pushRulesUpdater = mockk(),
|
||||||
)
|
)
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
|
|
Loading…
Reference in New Issue