This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Transfer user's push rules on room upgrade #4838
Merged
anoadragon453
merged 12 commits into
develop
from
anoa/notification_settings_room_upgrade
Mar 12, 2019
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4761df8
Transfer local user's push rules on room upgrade
anoadragon453 77a4ed5
Copy push rules when a user joins a room
anoadragon453 201ae7d
Add changelog
anoadragon453 639ca4d
Remove old work
anoadragon453 4db0b2b
Merge branch 'develop' into anoa/notification_settings_room_upgrade
anoadragon453 78d494e
Only copy room push rules per user
anoadragon453 695cc17
Fix comment
anoadragon453 50d3243
Fixes
anoadragon453 b3a278e
Split copying out into new function
anoadragon453 0efd53f
Rename functions to move. Cleaner logic
anoadragon453 e95b6fe
copy -> move
anoadragon453 d90fc7d
Ensure things don't blow up
anoadragon453 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Transfer a user's notification settings (push rules) on room upgrade. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,6 +185,63 @@ def bulk_get_push_rules(self, user_ids): | |
|
||
defer.returnValue(results) | ||
|
||
@defer.inlineCallbacks | ||
def move_push_rule_from_room_to_room( | ||
self, new_room_id, user_id, rule, | ||
): | ||
"""Move a single push rule from one room to another for a specific user. | ||
|
||
Args: | ||
new_room_id (str): ID of the new room. | ||
user_id (str): ID of user the push rule belongs to. | ||
rule (Dict): 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["conditions"]: | ||
if condition.get("key") == "room_id": | ||
condition["pattern"] = new_room_id | ||
|
||
# Add the rule for the new room | ||
yield self.add_push_rule( | ||
user_id=user_id, | ||
rule_id=new_rule_id, | ||
priority_class=rule["priority_class"], | ||
conditions=rule["conditions"], | ||
actions=rule["actions"], | ||
) | ||
|
||
# Delete push rule for the old room | ||
yield self.delete_push_rule(user_id, rule["rule_id"]) | ||
|
||
@defer.inlineCallbacks | ||
def move_push_rules_from_room_to_room_for_user( | ||
self, old_room_id, new_room_id, user_id, | ||
): | ||
"""Move all of the push rules from one room to another for a specific | ||
user. | ||
|
||
Args: | ||
old_room_id (str): ID of the old room. | ||
new_room_id (str): ID of the new room. | ||
user_id (str): ID of user to copy push rules for. | ||
""" | ||
# Retrieve push rules for this user | ||
user_push_rules = yield self.get_push_rules_for_user(user_id) | ||
|
||
# Get rules relating to the old room, move them to the new room, then | ||
# delete them from the old room | ||
for rule in user_push_rules: | ||
for condition in rule["conditions"]: | ||
if condition.get("key") == "room_id": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we probably need to worry about what happens if somebody makes a rule that has two conditions that match: I suspect your code will blow up. an easy fix might be: conditions = rule.get("conditions", [])
if any((c.get("key") == "room_id" and c.get("pattern") == old_room_id) for c in conditions):
# ... move There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works and is very pythonic, thanks. |
||
if condition.get("pattern") == old_room_id: | ||
self.move_push_rule_from_room_to_room( | ||
new_room_id, user_id, rule, | ||
) | ||
|
||
@defer.inlineCallbacks | ||
def bulk_get_push_rules_for_room(self, event, context): | ||
state_group = context.state_group | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
conditions is apparently optional: