FastLane: handle push rules

This commit is contained in:
Benoit Marty 2021-03-25 15:30:00 +01:00 committed by Benoit Marty
parent f0f66cbd0e
commit 3a1b8bc33d
3 changed files with 53 additions and 14 deletions

View file

@ -39,6 +39,8 @@ interface PushRuleService {
fun removePushRuleListener(listener: PushRuleListener) fun removePushRuleListener(listener: PushRuleListener)
fun getActions(event: Event): List<Action>
// fun fulfilledBingRule(event: Event, rules: List<PushRule>): PushRule? // fun fulfilledBingRule(event: Event, rules: List<PushRule>): PushRule?
interface PushRuleListener { interface PushRuleListener {

View file

@ -16,8 +16,11 @@
package org.matrix.android.sdk.internal.session.notification package org.matrix.android.sdk.internal.session.notification
import com.zhuinden.monarchy.Monarchy import com.zhuinden.monarchy.Monarchy
import org.matrix.android.sdk.api.pushrules.Action
import org.matrix.android.sdk.api.pushrules.ConditionResolver
import org.matrix.android.sdk.api.pushrules.PushRuleService import org.matrix.android.sdk.api.pushrules.PushRuleService
import org.matrix.android.sdk.api.pushrules.RuleKind import org.matrix.android.sdk.api.pushrules.RuleKind
import org.matrix.android.sdk.api.pushrules.RuleScope
import org.matrix.android.sdk.api.pushrules.RuleSetKey import org.matrix.android.sdk.api.pushrules.RuleSetKey
import org.matrix.android.sdk.api.pushrules.getActions import org.matrix.android.sdk.api.pushrules.getActions
import org.matrix.android.sdk.api.pushrules.rest.PushRule import org.matrix.android.sdk.api.pushrules.rest.PushRule
@ -45,6 +48,7 @@ internal class DefaultPushRuleService @Inject constructor(
private val addPushRuleTask: AddPushRuleTask, private val addPushRuleTask: AddPushRuleTask,
private val updatePushRuleActionsTask: UpdatePushRuleActionsTask, private val updatePushRuleActionsTask: UpdatePushRuleActionsTask,
private val removePushRuleTask: RemovePushRuleTask, private val removePushRuleTask: RemovePushRuleTask,
private val conditionResolver: ConditionResolver,
private val taskExecutor: TaskExecutor, private val taskExecutor: TaskExecutor,
@SessionDatabase private val monarchy: Monarchy @SessionDatabase private val monarchy: Monarchy
) : PushRuleService { ) : PushRuleService {
@ -130,6 +134,22 @@ internal class DefaultPushRuleService @Inject constructor(
} }
} }
override fun getActions(event: Event): List<Action> {
val rules = getPushRules(RuleScope.GLOBAL).getAllRules()
return fulfilledBingRule(event, rules)?.getActions().orEmpty()
}
// TODO This is a copy paste, try to have only once this code
private fun fulfilledBingRule(event: Event, rules: List<PushRule>): PushRule? {
return rules.firstOrNull { rule ->
// All conditions must hold true for an event in order to apply the action for the event.
rule.enabled && rule.conditions?.all {
it.asExecutableCondition(rule)?.isSatisfied(event, conditionResolver) ?: false
} ?: false
}
}
// fun processEvents(events: List<Event>) { // fun processEvents(events: List<Event>) {
// var hasDoneSomething = false // var hasDoneSomething = false
// events.forEach { event -> // events.forEach { event ->

View file

@ -93,22 +93,39 @@ class NotifiableEventResolver @Inject constructor(
// Ignore message edition // Ignore message edition
if (event.isEdition()) return null if (event.isEdition()) return null
val user = session.getUser(event.senderId!!) ?: return null val actions = session.getActions(event)
val notificationAction = actions.toNotificationAction()
val timelineEvent = TimelineEvent( return if (notificationAction.shouldNotify) {
root = event, val user = session.getUser(event.senderId!!) ?: return null
localId = -1,
eventId = event.eventId!!,
displayIndex = 0,
senderInfo = SenderInfo(
userId = user.userId,
displayName = user.getBestName(),
isUniqueDisplayName = true,
avatarUrl = user.avatarUrl
)
)
return resolveMessageEvent(timelineEvent, session) val timelineEvent = TimelineEvent(
root = event,
localId = -1,
eventId = event.eventId!!,
displayIndex = 0,
senderInfo = SenderInfo(
userId = user.userId,
displayName = user.getBestName(),
isUniqueDisplayName = true,
avatarUrl = user.avatarUrl
)
)
val notifiableEvent = resolveMessageEvent(timelineEvent, session)
if (notifiableEvent == null) {
Timber.d("## Failed to resolve event")
// TODO
null
} else {
notifiableEvent.noisy = !notificationAction.soundName.isNullOrBlank()
notifiableEvent
}
} else {
Timber.d("Matched push rule is set to not notify")
null
}
} }
private fun resolveMessageEvent(event: TimelineEvent, session: Session): NotifiableEvent? { private fun resolveMessageEvent(event: TimelineEvent, session: Session): NotifiableEvent? {