Skip to content
Snippets Groups Projects
Unverified Commit 57feeab3 authored by reivilibre's avatar reivilibre Committed by GitHub
Browse files

Don't ignore `set_tweak` actions with no explicit `value`. (#7766)

* Fix spec compliance; tweaks without values are valid

(default to True, which is only concretely specified for
`highlight`, but it seems only reasonable to generalise)

* Changelog for 7766.

* Add documentation to `tweaks_for_actions`

May as well tidy up when I'm here.

* Add a test for `tweaks_for_actions`
parent 4e118742
No related branches found
No related tags found
No related merge requests found
Fix to not ignore `set_tweak` actions in Push Rules that have no `value`, as permitted by the specification.
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
import logging import logging
import re import re
from typing import Pattern from typing import Any, Dict, List, Pattern, Union
from synapse.events import EventBase from synapse.events import EventBase
from synapse.types import UserID from synapse.types import UserID
...@@ -72,13 +72,36 @@ def _test_ineq_condition(condition, number): ...@@ -72,13 +72,36 @@ def _test_ineq_condition(condition, number):
return False return False
def tweaks_for_actions(actions): def tweaks_for_actions(actions: List[Union[str, Dict]]) -> Dict[str, Any]:
"""
Converts a list of actions into a `tweaks` dict (which can then be passed to
the push gateway).
This function ignores all actions other than `set_tweak` actions, and treats
absent `value`s as `True`, which agrees with the only spec-defined treatment
of absent `value`s (namely, for `highlight` tweaks).
Args:
actions: list of actions
e.g. [
{"set_tweak": "a", "value": "AAA"},
{"set_tweak": "b", "value": "BBB"},
{"set_tweak": "highlight"},
"notify"
]
Returns:
dictionary of tweaks for those actions
e.g. {"a": "AAA", "b": "BBB", "highlight": True}
"""
tweaks = {} tweaks = {}
for a in actions: for a in actions:
if not isinstance(a, dict): if not isinstance(a, dict):
continue continue
if "set_tweak" in a and "value" in a: if "set_tweak" in a:
tweaks[a["set_tweak"]] = a["value"] # value is allowed to be absent in which case the value assumed
# should be True.
tweaks[a["set_tweak"]] = a.get("value", True)
return tweaks return tweaks
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
from synapse.api.room_versions import RoomVersions from synapse.api.room_versions import RoomVersions
from synapse.events import FrozenEvent from synapse.events import FrozenEvent
from synapse.push import push_rule_evaluator
from synapse.push.push_rule_evaluator import PushRuleEvaluatorForEvent from synapse.push.push_rule_evaluator import PushRuleEvaluatorForEvent
from tests import unittest from tests import unittest
...@@ -84,3 +85,19 @@ class PushRuleEvaluatorTestCase(unittest.TestCase): ...@@ -84,3 +85,19 @@ class PushRuleEvaluatorTestCase(unittest.TestCase):
for body in (1, True, {"foo": "bar"}): for body in (1, True, {"foo": "bar"}):
evaluator = self._get_evaluator({"body": body}) evaluator = self._get_evaluator({"body": body})
self.assertFalse(evaluator.matches(condition, "@user:test", "foo")) self.assertFalse(evaluator.matches(condition, "@user:test", "foo"))
def test_tweaks_for_actions(self):
"""
This tests the behaviour of tweaks_for_actions.
"""
actions = [
{"set_tweak": "sound", "value": "default"},
{"set_tweak": "highlight"},
"notify",
]
self.assertEqual(
push_rule_evaluator.tweaks_for_actions(actions),
{"sound": "default", "highlight": True},
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment