Refactor: Condition is now an interface

This commit is contained in:
Benoit Marty 2020-08-25 16:06:26 +02:00 committed by Benoit Marty
parent 558e11a364
commit 7b5c74f81c
5 changed files with 11 additions and 22 deletions

View File

@ -18,11 +18,8 @@ package org.matrix.android.sdk.api.pushrules
import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.session.events.model.Event
abstract class Condition(val kind: Kind) { interface Condition {
fun isSatisfied(event: Event, conditionResolver: ConditionResolver): Boolean
abstract fun isSatisfied(event: Event, conditionResolver: ConditionResolver): Boolean fun technicalDescription(): String
open fun technicalDescription(): String {
return "Kind: $kind"
}
} }

View File

@ -22,15 +22,13 @@ import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.room.model.message.MessageContent import org.matrix.android.sdk.api.session.room.model.message.MessageContent
import timber.log.Timber import timber.log.Timber
class ContainsDisplayNameCondition : Condition(Kind.ContainsDisplayName) { class ContainsDisplayNameCondition : Condition {
override fun isSatisfied(event: Event, conditionResolver: ConditionResolver): Boolean { override fun isSatisfied(event: Event, conditionResolver: ConditionResolver): Boolean {
return conditionResolver.resolveContainsDisplayNameCondition(event, this) return conditionResolver.resolveContainsDisplayNameCondition(event, this)
} }
override fun technicalDescription(): String { override fun technicalDescription() = "User is mentioned"
return "User is mentioned"
}
fun isSatisfied(event: Event, displayName: String): Boolean { fun isSatisfied(event: Event, displayName: String): Boolean {
val message = when (event.type) { val message = when (event.type) {

View File

@ -30,15 +30,13 @@ class EventMatchCondition(
* be treated as having asterisks prepended and appended when testing the condition. * be treated as having asterisks prepended and appended when testing the condition.
*/ */
val pattern: String val pattern: String
) : Condition(Kind.EventMatch) { ) : Condition {
override fun isSatisfied(event: Event, conditionResolver: ConditionResolver): Boolean { override fun isSatisfied(event: Event, conditionResolver: ConditionResolver): Boolean {
return conditionResolver.resolveEventMatchCondition(event, this) return conditionResolver.resolveEventMatchCondition(event, this)
} }
override fun technicalDescription(): String { override fun technicalDescription() = "'$key' Matches '$pattern'"
return "'$key' Matches '$pattern'"
}
fun isSatisfied(event: Event): Boolean { fun isSatisfied(event: Event): Boolean {
// TODO encrypted events? // TODO encrypted events?

View File

@ -29,15 +29,13 @@ class RoomMemberCountCondition(
* If no prefix is present, this parameter defaults to ==. * If no prefix is present, this parameter defaults to ==.
*/ */
val iz: String val iz: String
) : Condition(Kind.RoomMemberCount) { ) : Condition {
override fun isSatisfied(event: Event, conditionResolver: ConditionResolver): Boolean { override fun isSatisfied(event: Event, conditionResolver: ConditionResolver): Boolean {
return conditionResolver.resolveRoomMemberCountCondition(event, this) return conditionResolver.resolveRoomMemberCountCondition(event, this)
} }
override fun technicalDescription(): String { override fun technicalDescription() = "Room member count is $iz"
return "Room member count is $iz"
}
internal fun isSatisfied(event: Event, roomGetter: RoomGetter): Boolean { internal fun isSatisfied(event: Event, roomGetter: RoomGetter): Boolean {
// sanity checks // sanity checks

View File

@ -28,15 +28,13 @@ class SenderNotificationPermissionCondition(
* type from the notifications object in the power level event content. * type from the notifications object in the power level event content.
*/ */
val key: String val key: String
) : Condition(Kind.SenderNotificationPermission) { ) : Condition {
override fun isSatisfied(event: Event, conditionResolver: ConditionResolver): Boolean { override fun isSatisfied(event: Event, conditionResolver: ConditionResolver): Boolean {
return conditionResolver.resolveSenderNotificationPermissionCondition(event, this) return conditionResolver.resolveSenderNotificationPermissionCondition(event, this)
} }
override fun technicalDescription(): String { override fun technicalDescription() = "User power level <$key>"
return "User power level <$key>"
}
fun isSatisfied(event: Event, powerLevels: PowerLevelsContent): Boolean { fun isSatisfied(event: Event, powerLevels: PowerLevelsContent): Boolean {
val powerLevelsHelper = PowerLevelsHelper(powerLevels) val powerLevelsHelper = PowerLevelsHelper(powerLevels)