mirror of
https://github.com/element-hq/element-android
synced 2024-11-24 02:15:35 +03:00
FastLane: handle push rules
This commit is contained in:
parent
f0f66cbd0e
commit
3a1b8bc33d
3 changed files with 53 additions and 14 deletions
|
@ -39,6 +39,8 @@ interface PushRuleService {
|
|||
|
||||
fun removePushRuleListener(listener: PushRuleListener)
|
||||
|
||||
fun getActions(event: Event): List<Action>
|
||||
|
||||
// fun fulfilledBingRule(event: Event, rules: List<PushRule>): PushRule?
|
||||
|
||||
interface PushRuleListener {
|
||||
|
|
|
@ -16,8 +16,11 @@
|
|||
package org.matrix.android.sdk.internal.session.notification
|
||||
|
||||
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.RuleKind
|
||||
import org.matrix.android.sdk.api.pushrules.RuleScope
|
||||
import org.matrix.android.sdk.api.pushrules.RuleSetKey
|
||||
import org.matrix.android.sdk.api.pushrules.getActions
|
||||
import org.matrix.android.sdk.api.pushrules.rest.PushRule
|
||||
|
@ -45,6 +48,7 @@ internal class DefaultPushRuleService @Inject constructor(
|
|||
private val addPushRuleTask: AddPushRuleTask,
|
||||
private val updatePushRuleActionsTask: UpdatePushRuleActionsTask,
|
||||
private val removePushRuleTask: RemovePushRuleTask,
|
||||
private val conditionResolver: ConditionResolver,
|
||||
private val taskExecutor: TaskExecutor,
|
||||
@SessionDatabase private val monarchy: Monarchy
|
||||
) : 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>) {
|
||||
// var hasDoneSomething = false
|
||||
// events.forEach { event ->
|
||||
|
|
|
@ -93,22 +93,39 @@ class NotifiableEventResolver @Inject constructor(
|
|||
// Ignore message edition
|
||||
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(
|
||||
root = event,
|
||||
localId = -1,
|
||||
eventId = event.eventId!!,
|
||||
displayIndex = 0,
|
||||
senderInfo = SenderInfo(
|
||||
userId = user.userId,
|
||||
displayName = user.getBestName(),
|
||||
isUniqueDisplayName = true,
|
||||
avatarUrl = user.avatarUrl
|
||||
)
|
||||
)
|
||||
return if (notificationAction.shouldNotify) {
|
||||
val user = session.getUser(event.senderId!!) ?: return null
|
||||
|
||||
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? {
|
||||
|
|
Loading…
Reference in a new issue