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

Commit 37935b5

Browse files
author
Adam
authored
Move methods that call add_push_rule to PushRuleStore (#12772)
Signed-off-by: Adam Roddick <ajroddick@tuta.io>
1 parent 0d17357 commit 37935b5

2 files changed

Lines changed: 52 additions & 51 deletions

File tree

changelog.d/12772.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Move methods that call `add_push_rule` to the `PushRuleStore` class.

synapse/storage/databases/main/push_rule.py

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -243,57 +243,6 @@ async def bulk_get_push_rules(
243243

244244
return results
245245

246-
async def copy_push_rule_from_room_to_room(
247-
self, new_room_id: str, user_id: str, rule: dict
248-
) -> None:
249-
"""Copy a single push rule from one room to another for a specific user.
250-
251-
Args:
252-
new_room_id: ID of the new room.
253-
user_id : ID of user the push rule belongs to.
254-
rule: A push rule.
255-
"""
256-
# Create new rule id
257-
rule_id_scope = "/".join(rule["rule_id"].split("/")[:-1])
258-
new_rule_id = rule_id_scope + "/" + new_room_id
259-
260-
# Change room id in each condition
261-
for condition in rule.get("conditions", []):
262-
if condition.get("key") == "room_id":
263-
condition["pattern"] = new_room_id
264-
265-
# Add the rule for the new room
266-
await self.add_push_rule( # type: ignore[attr-defined]
267-
user_id=user_id,
268-
rule_id=new_rule_id,
269-
priority_class=rule["priority_class"],
270-
conditions=rule["conditions"],
271-
actions=rule["actions"],
272-
)
273-
274-
async def copy_push_rules_from_room_to_room_for_user(
275-
self, old_room_id: str, new_room_id: str, user_id: str
276-
) -> None:
277-
"""Copy all of the push rules from one room to another for a specific
278-
user.
279-
280-
Args:
281-
old_room_id: ID of the old room.
282-
new_room_id: ID of the new room.
283-
user_id: ID of user to copy push rules for.
284-
"""
285-
# Retrieve push rules for this user
286-
user_push_rules = await self.get_push_rules_for_user(user_id)
287-
288-
# Get rules relating to the old room and copy them to the new room
289-
for rule in user_push_rules:
290-
conditions = rule.get("conditions", [])
291-
if any(
292-
(c.get("key") == "room_id" and c.get("pattern") == old_room_id)
293-
for c in conditions
294-
):
295-
await self.copy_push_rule_from_room_to_room(new_room_id, user_id, rule)
296-
297246
@cachedList(
298247
cached_method_name="get_push_rules_enabled_for_user",
299248
list_name="user_ids",
@@ -866,3 +815,54 @@ def _insert_push_rules_update_txn(
866815

867816
def get_max_push_rules_stream_id(self) -> int:
868817
return self._push_rules_stream_id_gen.get_current_token()
818+
819+
async def copy_push_rule_from_room_to_room(
820+
self, new_room_id: str, user_id: str, rule: dict
821+
) -> None:
822+
"""Copy a single push rule from one room to another for a specific user.
823+
824+
Args:
825+
new_room_id: ID of the new room.
826+
user_id : ID of user the push rule belongs to.
827+
rule: A push rule.
828+
"""
829+
# Create new rule id
830+
rule_id_scope = "/".join(rule["rule_id"].split("/")[:-1])
831+
new_rule_id = rule_id_scope + "/" + new_room_id
832+
833+
# Change room id in each condition
834+
for condition in rule.get("conditions", []):
835+
if condition.get("key") == "room_id":
836+
condition["pattern"] = new_room_id
837+
838+
# Add the rule for the new room
839+
await self.add_push_rule(
840+
user_id=user_id,
841+
rule_id=new_rule_id,
842+
priority_class=rule["priority_class"],
843+
conditions=rule["conditions"],
844+
actions=rule["actions"],
845+
)
846+
847+
async def copy_push_rules_from_room_to_room_for_user(
848+
self, old_room_id: str, new_room_id: str, user_id: str
849+
) -> None:
850+
"""Copy all of the push rules from one room to another for a specific
851+
user.
852+
853+
Args:
854+
old_room_id: ID of the old room.
855+
new_room_id: ID of the new room.
856+
user_id: ID of user to copy push rules for.
857+
"""
858+
# Retrieve push rules for this user
859+
user_push_rules = await self.get_push_rules_for_user(user_id)
860+
861+
# Get rules relating to the old room and copy them to the new room
862+
for rule in user_push_rules:
863+
conditions = rule.get("conditions", [])
864+
if any(
865+
(c.get("key") == "room_id" and c.get("pattern") == old_room_id)
866+
for c in conditions
867+
):
868+
await self.copy_push_rule_from_room_to_room(new_room_id, user_id, rule)

0 commit comments

Comments
 (0)