Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add a module API to allow modules to edit push rule actions #12406

Merged
merged 14 commits into from
Apr 27, 2022
11 changes: 6 additions & 5 deletions synapse/handlers/push_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ class PushRulesHandler:

def __init__(self, hs: "HomeServer"):
self._notifier = hs.get_notifier()
self._store = hs.get_datastores().main
self._main_store = hs.get_datastores().main

async def set_rule_attr(
self, user_id: str, spec: RuleSpec, val: Union[bool, JsonDict]
) -> None:
"""Set an attribute (enabled or actions) on an existing push rule.
"""Set an attribute (enabled or actions) on an existing push rule, and notify
listeners (e.g. sync handler) of the change.
babolivier marked this conversation as resolved.
Show resolved Hide resolved

Args:
user_id: the user for which to modify the push rule.
Expand Down Expand Up @@ -76,7 +77,7 @@ async def set_rule_attr(
# This should *actually* take a dict, but many clients pass
# bools directly, so let's not break them.
raise SynapseError(400, "Value for 'enabled' must be boolean")
await self._store.set_push_rule_enabled(
await self._main_store.set_push_rule_enabled(
user_id, namespaced_rule_id, val, is_default_rule
)
elif spec.attr == "actions":
Expand All @@ -93,7 +94,7 @@ async def set_rule_attr(
raise RuleNotFoundException(
"Unknown rule %r" % (namespaced_rule_id,)
)
await self._store.set_push_rule_actions(
await self._main_store.set_push_rule_actions(
user_id, namespaced_rule_id, actions, is_default_rule
)
else:
Expand All @@ -107,7 +108,7 @@ def notify_user(self, user_id: str) -> None:
Args:
user_id: the user ID the change is for.
"""
stream_id = self._store.get_max_push_rules_stream_id()
stream_id = self._main_store.get_max_push_rules_stream_id()
self._notifier.on_new_event("push_rules_key", stream_id, users=[user_id])


Expand Down
8 changes: 4 additions & 4 deletions synapse/storage/databases/main/push_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ async def set_push_rule_enabled(
are always stored in the database `push_rules` table).

Raises:
NotFoundError if the rule does not exist.
RuleNotFoundException if the rule does not exist.
"""
async with self._push_rules_stream_id_gen.get_next() as stream_id:
event_stream_ordering = self._stream_id_gen.get_current_token()
Expand Down Expand Up @@ -697,9 +697,6 @@ async def set_push_rule_actions(
"""
Sets the `actions` state of a push rule.

Will throw NotFoundError if the rule does not exist; the Code for this
is NOT_FOUND.

Args:
user_id: the user ID of the user who wishes to enable/disable the rule
e.g. '@tina:example.org'
Expand All @@ -711,6 +708,9 @@ async def set_push_rule_actions(
is_default_rule: True if and only if this is a server-default rule.
This skips the check for existence (as only user-created rules
are always stored in the database `push_rules` table).

Raises:
RuleNotFoundException if the rule does not exist.
"""
actions_json = json_encoder.encode(actions)

Expand Down