From 831938b8f557b44cba2e8e0f590b8625057bb4bc Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 26 Apr 2023 19:00:24 +0200 Subject: [PATCH] fix: Skip rules with unknown conditions Without this, when new rules are added with new conditions, we would always match on them. This would mean all messages now notify. It is better to skip them instead. --- lib/src/utils/pushrule_evaluator.dart | 2 ++ test/pushevaluator_test.dart | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/src/utils/pushrule_evaluator.dart b/lib/src/utils/pushrule_evaluator.dart index 505be3de..a531325f 100644 --- a/lib/src/utils/pushrule_evaluator.dart +++ b/lib/src/utils/pushrule_evaluator.dart @@ -186,6 +186,8 @@ class _OptimizedRules { notificationPermissions.add(key); } break; + default: + throw Exception('Unknown push condition: ${condition.kind}'); } } actions = EvaluatedPushRuleAction.fromActions(rule.actions); diff --git a/test/pushevaluator_test.dart b/test/pushevaluator_test.dart index c02e8697..1d155595 100644 --- a/test/pushevaluator_test.dart +++ b/test/pushevaluator_test.dart @@ -208,6 +208,29 @@ void main() { testNotMatch(override_ruleset2, event); }); + test('invalid push condition', () async { + final invalid_ruleset = PushRuleSet(override: [ + PushRule(ruleId: 'my.rule', default$: false, enabled: true, actions: [ + 'notify', + {'set_tweak': 'highlight', 'value': true}, + {'set_tweak': 'sound', 'value': 'goose.wav'}, + ], conditions: [ + PushCondition( + kind: 'invalidcondition', pattern: 'fox', key: 'content.body'), + ]) + ]); + + expect(() => PushruleEvaluator.fromRuleset(invalid_ruleset), + returnsNormally); + + final evaluator = PushruleEvaluator.fromRuleset(invalid_ruleset); + final event = Event.fromJson(jsonObj, room); + final actions = evaluator.match(event); + expect(actions.highlight, false); + expect(actions.sound, null); + expect(actions.notify, false); + }); + test('match_display_name rule', () async { final event = Event.fromJson(jsonObj, room); (event.room.states[EventTypes.RoomMember] ??= {})[client.userID!] =