diff --git a/changelog.d/3681.removal b/changelog.d/3681.removal new file mode 100644 index 0000000000..314b58c41d --- /dev/null +++ b/changelog.d/3681.removal @@ -0,0 +1 @@ +updatePushRuleActions signature has been updated to more explicitly enabled/disable the rule and update the actions. It's behaviour has also been changed to match the web with the enable/disable requests being sent on every invocation and actions sent when needed(not null). \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt index 05d40f43d3..4534368679 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt @@ -31,6 +31,12 @@ interface PushRuleService { suspend fun addPushRule(kind: RuleKind, pushRule: PushRule) + /** + * Enables/Disables a push rule and updates the actions if necessary + * @param enable Enables/Disables the rule + * @param actions Actions to update if not null + */ + suspend fun updatePushRuleActions(kind: RuleKind, ruleId: String, enable: Boolean, actions: List?) suspend fun removePushRule(kind: RuleKind, pushRule: PushRule) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt index b95d5217a4..154209731e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt @@ -18,6 +18,7 @@ package org.matrix.android.sdk.api.pushrules.rest import com.squareup.moshi.Json import com.squareup.moshi.JsonClass +import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.pushrules.Action import org.matrix.android.sdk.api.pushrules.getActions import org.matrix.android.sdk.api.pushrules.toJson @@ -104,7 +105,7 @@ data class PushRule( * Get the highlight status. As spec mentions assume false if no tweak present. */ fun getHighlight(): Boolean { - return (getActions().firstOrNull { it is Action.Highlight } as? Action.Highlight)?.highlight ?: false + return (getActions().firstOrNull { it is Action.Highlight } as? Action.Highlight)?.highlight.orFalse() } /** diff --git a/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt b/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt index d49d7c3407..2471c5f66c 100755 --- a/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt +++ b/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt @@ -28,7 +28,7 @@ class PushRulePreference : VectorPreference { enum class NotificationIndex(val index: Int) { OFF(0), SILENT(1), - NOISEY(2); + NOISY(2); companion object { fun fromInt(index: Int) = values().first { it.index == index } @@ -67,8 +67,8 @@ class PushRulePreference : VectorPreference { private fun refreshSummary() { summary = context.getString(when (index) { NotificationIndex.OFF -> R.string.notification_off - NotificationIndex.SILENT -> R.string.notification_silent - NotificationIndex.NOISEY, null -> R.string.notification_noisy + NotificationIndex.SILENT -> R.string.notification_silent + NotificationIndex.NOISY, null -> R.string.notification_noisy }) } @@ -86,10 +86,10 @@ class PushRulePreference : VectorPreference { NotificationIndex.OFF -> { radioGroup?.check(R.id.bingPreferenceRadioBingRuleOff) } - NotificationIndex.SILENT -> { + NotificationIndex.SILENT -> { radioGroup?.check(R.id.bingPreferenceRadioBingRuleSilent) } - NotificationIndex.NOISEY -> { + NotificationIndex.NOISY -> { radioGroup?.check(R.id.bingPreferenceRadioBingRuleNoisy) } } @@ -103,7 +103,7 @@ class PushRulePreference : VectorPreference { onPreferenceChangeListener?.onPreferenceChange(this, NotificationIndex.SILENT) } R.id.bingPreferenceRadioBingRuleNoisy -> { - onPreferenceChangeListener?.onPreferenceChange(this, NotificationIndex.NOISEY) + onPreferenceChangeListener?.onPreferenceChange(this, NotificationIndex.NOISY) } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/PushRuleDefinitions.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/PushRuleDefinitions.kt index 155986cd8a..2d1bd4d5e5 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/PushRuleDefinitions.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/PushRuleDefinitions.kt @@ -19,73 +19,73 @@ package im.vector.app.features.settings.notifications import im.vector.app.core.preference.PushRulePreference import org.matrix.android.sdk.api.pushrules.RuleIds -fun getStandardAction(ruleId: String, index: PushRulePreference.NotificationIndex): StandardAction? { +fun getStandardAction(ruleId: String, index: PushRulePreference.NotificationIndex): StandardActions? { return when (ruleId) { RuleIds.RULE_ID_CONTAIN_DISPLAY_NAME -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.HighlightDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.HighlightDefaultSound } RuleIds.RULE_ID_CONTAIN_USER_NAME -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.HighlightDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.HighlightDefaultSound } RuleIds.RULE_ID_ROOM_NOTIF -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.Highlight + PushRulePreference.NotificationIndex.OFF -> StandardActions.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.Highlight } RuleIds.RULE_ID_ONE_TO_ONE_ROOM -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyDefaultSound } RuleIds.RULE_ID_ONE_TO_ONE_ENCRYPTED_ROOM -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyDefaultSound } RuleIds.RULE_ID_ALL_OTHER_MESSAGES_ROOMS -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyDefaultSound } RuleIds.RULE_ID_ENCRYPTED -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyDefaultSound } RuleIds.RULE_ID_INVITE_ME -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyDefaultSound } RuleIds.RULE_ID_CALL -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyRingSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyRingSound } RuleIds.RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Disabled - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Disabled + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyDefaultSound } RuleIds.RULE_ID_TOMBSTONE -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.Highlight + PushRulePreference.NotificationIndex.OFF -> StandardActions.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.Highlight } else -> null } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/StandardAction.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/StandardActions.kt similarity index 51% rename from vector/src/main/java/im/vector/app/features/settings/notifications/StandardAction.kt rename to vector/src/main/java/im/vector/app/features/settings/notifications/StandardActions.kt index 3f482bf626..d6b6165bd9 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/StandardAction.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/StandardActions.kt @@ -18,14 +18,14 @@ package im.vector.app.features.settings.notifications import org.matrix.android.sdk.api.pushrules.Action -sealed class StandardAction( +sealed class StandardActions( val actions: List? ) { - object Notify : StandardAction(actions = listOf(Action.Notify)) - object NotifyDefaultSound : StandardAction(actions = listOf(Action.Notify, Action.Sound())) - object NotifyRingSound : StandardAction(actions = listOf(Action.Notify, Action.Sound(sound = Action.ACTION_OBJECT_VALUE_VALUE_RING))) - object Highlight : StandardAction(actions = listOf(Action.Notify, Action.Highlight(highlight = true))) - object HighlightDefaultSound : StandardAction(actions = listOf(Action.Notify, Action.Highlight(highlight = true), Action.Sound())) - object DontNotify : StandardAction(actions = listOf(Action.DoNotNotify)) - object Disabled : StandardAction(actions = null) + object Notify : StandardActions(actions = listOf(Action.Notify)) + object NotifyDefaultSound : StandardActions(actions = listOf(Action.Notify, Action.Sound())) + object NotifyRingSound : StandardActions(actions = listOf(Action.Notify, Action.Sound(sound = Action.ACTION_OBJECT_VALUE_VALUE_RING))) + object Highlight : StandardActions(actions = listOf(Action.Notify, Action.Highlight(highlight = true))) + object HighlightDefaultSound : StandardActions(actions = listOf(Action.Notify, Action.Highlight(highlight = true), Action.Sound())) + object DontNotify : StandardActions(actions = listOf(Action.DoNotNotify)) + object Disabled : StandardActions(actions = null) } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt index d6a3fea1bf..c6f7e5db89 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt @@ -55,7 +55,7 @@ class VectorSettingsAdvancedNotificationPreferenceFragment @Inject constructor() val newIndex = newValue as NotificationIndex val standardAction = getStandardAction(ruleAndKind.pushRule.ruleId, newIndex) if (standardAction != null) { - val enabled = standardAction != StandardAction.Disabled + val enabled = standardAction != StandardActions.Disabled val newActions = standardAction.actions displayLoadingView() @@ -93,7 +93,7 @@ class VectorSettingsAdvancedNotificationPreferenceFragment @Inject constructor() val standardAction = getStandardAction(rule.ruleId, it) ?: return@firstOrNull false val indexActions = standardAction.actions ?: listOf() // Check if the input rule matches a rule generated from the static rule definitions - val targetRule = rule.copy(enabled = standardAction != StandardAction.Disabled, actions = indexActions.toJson()) + val targetRule = rule.copy(enabled = standardAction != StandardActions.Disabled, actions = indexActions.toJson()) ruleMatches(rule, targetRule) } }