summary refs log tree commit diff
path: root/synapse/handlers/push_rules.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2024-03-27 11:44:50 +0000
committerErik Johnston <erik@matrix.org>2024-03-27 11:44:50 +0000
commiteb4513c78a9bab7c7c3ca1e474aa1cc894445ffe (patch)
tree71207560cf754e75251612c47a3b778b62eed2c1 /synapse/handlers/push_rules.py
parentFixup changelog (diff)
downloadsynapse-erikj/allow_set_push_rules_workers.tar.xz
Allow setting push rules to be handled on workers github/erikj/allow_set_push_rules_workers erikj/allow_set_push_rules_workers
Diffstat (limited to 'synapse/handlers/push_rules.py')
-rw-r--r--synapse/handlers/push_rules.py33
1 files changed, 22 insertions, 11 deletions
diff --git a/synapse/handlers/push_rules.py b/synapse/handlers/push_rules.py
index 4ef6a04c51..782765223c 100644
--- a/synapse/handlers/push_rules.py
+++ b/synapse/handlers/push_rules.py
@@ -18,15 +18,15 @@
 # [This file includes modifications made by New Vector Limited]
 #
 #
-from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
-
-import attr
+from typing import TYPE_CHECKING, Any, Dict, List, Union
 
 from synapse.api.errors import SynapseError, UnrecognizedRequestError
 from synapse.push.clientformat import format_push_rules_for_user
+from synapse.replication.http.push import PushSetRuleAttrRestServlet
 from synapse.storage.push_rule import RuleNotFoundException
 from synapse.synapse_rust.push import get_base_rule_ids
 from synapse.types import JsonDict, StreamKeyType, UserID
+from synapse.types.push import RuleSpec
 
 if TYPE_CHECKING:
     from synapse.server import HomeServer
@@ -35,14 +35,6 @@ if TYPE_CHECKING:
 BASE_RULE_IDS = get_base_rule_ids()
 
 
-@attr.s(slots=True, frozen=True, auto_attribs=True)
-class RuleSpec:
-    scope: str
-    template: str
-    rule_id: str
-    attr: Optional[str]
-
-
 class PushRulesHandler:
     """A class to handle changes in push rules for users."""
 
@@ -50,9 +42,28 @@ class PushRulesHandler:
         self._notifier = hs.get_notifier()
         self._main_store = hs.get_datastores().main
 
+        self._push_attr_repl_client = None
+        if hs.config.worker.worker_app is not None:
+            self._push_attr_repl_client = PushSetRuleAttrRestServlet.make_client(hs)
+
     async def set_rule_attr(
         self, user_id: str, spec: RuleSpec, val: Union[bool, JsonDict]
     ) -> None:
+        if self._push_attr_repl_client:
+            await self._push_attr_repl_client(
+                user_id=user_id,
+                scope=spec.scope,
+                template=spec.template,
+                rule_id=spec.rule_id,
+                attr=spec.attr,
+                val=val,
+            )
+        else:
+            await self._set_rule_attr(user_id, spec, val)
+
+    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.
 
         Notifies listeners (e.g. sync handler) of the change.