From 157fbbb1a0a2286a30ba615cf17accaf118dffbe Mon Sep 17 00:00:00 2001 From: Adam Roddick Date: Tue, 17 May 2022 21:15:54 +0100 Subject: [PATCH 1/3] Move methods that call add_push_rule to PushRuleStore Signed-off-by: Adam Roddick --- synapse/storage/databases/main/push_rule.py | 102 ++++++++++---------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/synapse/storage/databases/main/push_rule.py b/synapse/storage/databases/main/push_rule.py index 0e2855fb446c..ad67901cc1ac 100644 --- a/synapse/storage/databases/main/push_rule.py +++ b/synapse/storage/databases/main/push_rule.py @@ -243,57 +243,6 @@ async def bulk_get_push_rules( return results - async def copy_push_rule_from_room_to_room( - self, new_room_id: str, user_id: str, rule: dict - ) -> None: - """Copy a single push rule from one room to another for a specific user. - - Args: - new_room_id: ID of the new room. - user_id : ID of user the push rule belongs to. - rule: A push rule. - """ - # Create new rule id - rule_id_scope = "/".join(rule["rule_id"].split("/")[:-1]) - new_rule_id = rule_id_scope + "/" + new_room_id - - # Change room id in each condition - for condition in rule.get("conditions", []): - if condition.get("key") == "room_id": - condition["pattern"] = new_room_id - - # Add the rule for the new room - await self.add_push_rule( # type: ignore[attr-defined] - user_id=user_id, - rule_id=new_rule_id, - priority_class=rule["priority_class"], - conditions=rule["conditions"], - actions=rule["actions"], - ) - - async def copy_push_rules_from_room_to_room_for_user( - self, old_room_id: str, new_room_id: str, user_id: str - ) -> None: - """Copy all of the push rules from one room to another for a specific - user. - - Args: - old_room_id: ID of the old room. - new_room_id: ID of the new room. - user_id: ID of user to copy push rules for. - """ - # Retrieve push rules for this user - user_push_rules = await self.get_push_rules_for_user(user_id) - - # Get rules relating to the old room and copy them to the new room - for rule in user_push_rules: - conditions = rule.get("conditions", []) - if any( - (c.get("key") == "room_id" and c.get("pattern") == old_room_id) - for c in conditions - ): - await self.copy_push_rule_from_room_to_room(new_room_id, user_id, rule) - @cachedList( cached_method_name="get_push_rules_enabled_for_user", list_name="user_ids", @@ -866,3 +815,54 @@ def _insert_push_rules_update_txn( def get_max_push_rules_stream_id(self) -> int: return self._push_rules_stream_id_gen.get_current_token() + + async def copy_push_rule_from_room_to_room( + self, new_room_id: str, user_id: str, rule: dict + ) -> None: + """Copy a single push rule from one room to another for a specific user. + + Args: + new_room_id: ID of the new room. + user_id : ID of user the push rule belongs to. + rule: A push rule. + """ + # Create new rule id + rule_id_scope = "/".join(rule["rule_id"].split("/")[:-1]) + new_rule_id = rule_id_scope + "/" + new_room_id + + # Change room id in each condition + for condition in rule.get("conditions", []): + if condition.get("key") == "room_id": + condition["pattern"] = new_room_id + + # Add the rule for the new room + await self.add_push_rule( + user_id=user_id, + rule_id=new_rule_id, + priority_class=rule["priority_class"], + conditions=rule["conditions"], + actions=rule["actions"], + ) + + async def copy_push_rules_from_room_to_room_for_user( + self, old_room_id: str, new_room_id: str, user_id: str + ) -> None: + """Copy all of the push rules from one room to another for a specific + user. + + Args: + old_room_id: ID of the old room. + new_room_id: ID of the new room. + user_id: ID of user to copy push rules for. + """ + # Retrieve push rules for this user + user_push_rules = await self.get_push_rules_for_user(user_id) + + # Get rules relating to the old room and copy them to the new room + for rule in user_push_rules: + conditions = rule.get("conditions", []) + if any( + (c.get("key") == "room_id" and c.get("pattern") == old_room_id) + for c in conditions + ): + await self.copy_push_rule_from_room_to_room(new_room_id, user_id, rule) From 1870e801385aea3dc6989c6f114a7b51c60a27b5 Mon Sep 17 00:00:00 2001 From: Adam Roddick Date: Tue, 17 May 2022 21:24:13 +0100 Subject: [PATCH 2/3] Add changelog --- changelog.d/12772.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/12772.bugfix diff --git a/changelog.d/12772.bugfix b/changelog.d/12772.bugfix new file mode 100644 index 000000000000..dceb6bb5e0a2 --- /dev/null +++ b/changelog.d/12772.bugfix @@ -0,0 +1 @@ +Moves methods that call `add_push_rule` to the `PushRuleStore` class. From dab9bd6a84f35beec73bc5d21dc4da2db596a2b5 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 18 May 2022 10:12:12 +0100 Subject: [PATCH 3/3] Fixup changelog --- changelog.d/12772.bugfix | 1 - changelog.d/12772.misc | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 changelog.d/12772.bugfix create mode 100644 changelog.d/12772.misc diff --git a/changelog.d/12772.bugfix b/changelog.d/12772.bugfix deleted file mode 100644 index dceb6bb5e0a2..000000000000 --- a/changelog.d/12772.bugfix +++ /dev/null @@ -1 +0,0 @@ -Moves methods that call `add_push_rule` to the `PushRuleStore` class. diff --git a/changelog.d/12772.misc b/changelog.d/12772.misc new file mode 100644 index 000000000000..da66f376fe8e --- /dev/null +++ b/changelog.d/12772.misc @@ -0,0 +1 @@ +Move methods that call `add_push_rule` to the `PushRuleStore` class.