PushRuleService.getPushRules() now returns a RuleSet. Use getAllRules() on this object to get all the rules.

This commit is contained in:
Benoit Marty 2020-02-28 14:46:42 +01:00
parent 039924436f
commit 1c35d07acc
9 changed files with 25 additions and 14 deletions

View file

@ -14,7 +14,7 @@ Translations 🗣:
-
SDK API changes ⚠️:
-
- PushRuleService.getPushRules() now returns a RuleSet. Use getAllRules() on this object to get all the rules.
Build 🧱:
-

View file

@ -17,6 +17,7 @@ package im.vector.matrix.android.api.pushrules
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.pushrules.rest.PushRule
import im.vector.matrix.android.api.pushrules.rest.RuleSet
import im.vector.matrix.android.api.session.events.model.Event
import im.vector.matrix.android.api.util.Cancelable
@ -27,8 +28,7 @@ interface PushRuleService {
*/
fun fetchPushRules(scope: String = RuleScope.GLOBAL)
// TODO get push rule set
fun getPushRules(scope: String = RuleScope.GLOBAL): List<PushRule>
fun getPushRules(scope: String = RuleScope.GLOBAL): RuleSet
// TODO update rule

View file

@ -22,7 +22,7 @@ import com.squareup.moshi.JsonClass
* Ref: https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-pushrules
*/
@JsonClass(generateAdapter = true)
internal data class RuleSet(
data class RuleSet(
@Json(name = "content")
val content: List<PushRule>? = null,
@Json(name = "override")
@ -33,4 +33,9 @@ internal data class RuleSet(
val sender: List<PushRule>? = null,
@Json(name = "underride")
val underride: List<PushRule>? = null
)
) {
fun getAllRules(): List<PushRule> {
// Ref. for the order: https://matrix.org/docs/spec/client_server/latest#push-rules
return override.orEmpty() + content.orEmpty() + room.orEmpty() + sender.orEmpty() + underride.orEmpty()
}
}

View file

@ -22,6 +22,7 @@ import im.vector.matrix.android.api.pushrules.RuleKind
import im.vector.matrix.android.api.pushrules.RuleSetKey
import im.vector.matrix.android.api.pushrules.getActions
import im.vector.matrix.android.api.pushrules.rest.PushRule
import im.vector.matrix.android.api.pushrules.rest.RuleSet
import im.vector.matrix.android.api.session.events.model.Event
import im.vector.matrix.android.api.util.Cancelable
import im.vector.matrix.android.internal.database.mapper.PushRulesMapper
@ -55,7 +56,7 @@ internal class DefaultPushRuleService @Inject constructor(
.executeBy(taskExecutor)
}
override fun getPushRules(scope: String): List<PushRule> {
override fun getPushRules(scope: String): RuleSet {
var contentRules: List<PushRule> = emptyList()
var overrideRules: List<PushRule> = emptyList()
var roomRules: List<PushRule> = emptyList()
@ -90,8 +91,13 @@ internal class DefaultPushRuleService @Inject constructor(
}
}
// Ref. for the order: https://matrix.org/docs/spec/client_server/latest#push-rules
return overrideRules + contentRules + roomRules + senderRules + underrideRules
return RuleSet(
content = contentRules,
override = overrideRules,
room = roomRules,
sender = senderRules,
underride = underrideRules
)
}
override fun updatePushRuleEnableStatus(kind: RuleKind, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback<Unit>): Cancelable {

View file

@ -120,7 +120,7 @@ internal class SyncResponseHandler @Inject constructor(private val monarchy: Mon
return
} // nothing on initial sync
val rules = pushRuleService.getPushRules(RuleScope.GLOBAL)
val rules = pushRuleService.getPushRules(RuleScope.GLOBAL).getAllRules()
processEventForPushTask.execute(ProcessEventForPushTask.Params(roomsSyncResponse, rules))
Timber.v("[PushRules] <-- Push task scheduled")
}

View file

@ -42,7 +42,7 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
override fun bindPref() {
findPreference<VectorSwitchPreference>(VectorPreferences.SETTINGS_ENABLE_ALL_NOTIF_PREFERENCE_KEY)!!.let { pref ->
val pushRuleService = session
val mRuleMaster = pushRuleService.getPushRules()
val mRuleMaster = pushRuleService.getPushRules().getAllRules()
.find { it.ruleId == RuleIds.RULE_ID_DISABLE_ALL }
if (mRuleMaster == null) {
@ -105,7 +105,7 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
private fun updateEnabledForAccount(preference: Preference?) {
val pushRuleService = session
val switchPref = preference as SwitchPreference
pushRuleService.getPushRules()
pushRuleService.getPushRules().getAllRules()
.find { it.ruleId == RuleIds.RULE_ID_DISABLE_ALL }
?.let {
// Trick, we must enable this room to disable notifications

View file

@ -35,7 +35,7 @@ class PushRulesViewModel(initialState: PushRulesViewState)
override fun initialState(viewModelContext: ViewModelContext): PushRulesViewState? {
val session = (viewModelContext.activity as HasScreenInjector).injector().activeSessionHolder().getActiveSession()
val rules = session.getPushRules()
val rules = session.getPushRules().getAllRules()
return PushRulesViewState(rules)
}
}

View file

@ -32,7 +32,7 @@ class TestAccountSettings @Inject constructor(private val stringProvider: String
override fun perform() {
val session = activeSessionHolder.getSafeActiveSession() ?: return
val defaultRule = session.getPushRules()
val defaultRule = session.getPushRules().getAllRules()
.find { it.ruleId == RuleIds.RULE_ID_DISABLE_ALL }
if (defaultRule != null) {

View file

@ -40,7 +40,7 @@ class TestBingRulesSettings @Inject constructor(private val activeSessionHolder:
override fun perform() {
val session = activeSessionHolder.getSafeActiveSession() ?: return
val pushRules = session.getPushRules()
val pushRules = session.getPushRules().getAllRules()
var oneOrMoreRuleIsOff = false
var oneOrMoreRuleAreSilent = false
testedRules.forEach { ruleId ->