From 4d09f64605b63f5c1d52585d55597534e2046e9d Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 18 Feb 2020 17:34:18 +0000 Subject: [PATCH 1/7] Transfer alias mappings when joining an upgraded room --- synapse/handlers/room_member.py | 3 +++ synapse/storage/data_stores/main/directory.py | 22 ++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index 426042636921..aeca805ac790 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -519,6 +519,9 @@ def transfer_room_state_on_room_upgrade(self, old_room_id, room_id): yield self.store.set_room_is_public(old_room_id, False) yield self.store.set_room_is_public(room_id, True) + # Transfer alias mappings in the room directory + self.store.update_aliases_for_room(old_room_id, room_id) + # Check if any groups we own contain the predecessor room local_group_ids = yield self.store.get_local_groups_for_room(old_room_id) for group_id in local_group_ids: diff --git a/synapse/storage/data_stores/main/directory.py b/synapse/storage/data_stores/main/directory.py index c9e7de7d1248..31daf72a2194 100644 --- a/synapse/storage/data_stores/main/directory.py +++ b/synapse/storage/data_stores/main/directory.py @@ -159,10 +159,26 @@ def _delete_room_alias_txn(self, txn, room_alias): return room_id - def update_aliases_for_room(self, old_room_id, new_room_id, creator): + def update_aliases_for_room(self, old_room_id, new_room_id, creator=None): + """Repoint the aliases from one room to another + + Args: + old_room_id: + new_room_id: + creator: The creator of the alias mapping. If None, don't update + """ + def _update_aliases_for_room_txn(txn): - sql = "UPDATE room_aliases SET room_id = ?, creator = ? WHERE room_id = ?" - txn.execute(sql, (new_room_id, creator, old_room_id)) + update_creator_sql = "" + sql_params = (new_room_id, old_room_id) + if creator: + update_creator_sql = ", creator = ?" + sql_params = (new_room_id, creator, old_room_id) + + sql = "UPDATE room_aliases SET room_id = ? %s WHERE room_id = ?" % ( + update_creator_sql, + ) + txn.execute(sql, sql_params) self._invalidate_cache_and_stream( txn, self.get_aliases_for_room, (old_room_id,) ) From 5fd9b24143c9ae9df1633b4fe78b55a54bc676df Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 18 Feb 2020 17:36:36 +0000 Subject: [PATCH 2/7] Add changelog --- changelog.d/6946.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/6946.bugfix diff --git a/changelog.d/6946.bugfix b/changelog.d/6946.bugfix new file mode 100644 index 000000000000..a238c83a18f7 --- /dev/null +++ b/changelog.d/6946.bugfix @@ -0,0 +1 @@ +Transfer alias mappings on room upgrade. \ No newline at end of file From 277bc3bed175a96b10ce652c0d29f1927317295e Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Wed, 25 Mar 2020 13:52:20 +0000 Subject: [PATCH 3/7] Add missing yield --- synapse/handlers/room_member.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index aeca805ac790..c3ee8db4f009 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -520,7 +520,7 @@ def transfer_room_state_on_room_upgrade(self, old_room_id, room_id): yield self.store.set_room_is_public(room_id, True) # Transfer alias mappings in the room directory - self.store.update_aliases_for_room(old_room_id, room_id) + yield self.store.update_aliases_for_room(old_room_id, room_id) # Check if any groups we own contain the predecessor room local_group_ids = yield self.store.get_local_groups_for_room(old_room_id) From 1fedf87a60b931759b3554c50203a6436291474d Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Wed, 25 Mar 2020 14:46:26 +0000 Subject: [PATCH 4/7] Add typing to new method --- synapse/storage/data_stores/main/directory.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/synapse/storage/data_stores/main/directory.py b/synapse/storage/data_stores/main/directory.py index 31daf72a2194..0049c43d9c2a 100644 --- a/synapse/storage/data_stores/main/directory.py +++ b/synapse/storage/data_stores/main/directory.py @@ -15,6 +15,7 @@ from collections import namedtuple +from typing import Optional from twisted.internet import defer from synapse.api.errors import SynapseError @@ -159,7 +160,12 @@ def _delete_room_alias_txn(self, txn, room_alias): return room_id - def update_aliases_for_room(self, old_room_id, new_room_id, creator=None): + def update_aliases_for_room( + self, + old_room_id: str, + new_room_id: str, + creator: Optional[str] = None, + ): """Repoint the aliases from one room to another Args: From 7d121888475a570705443696a433d3620aa50b48 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Wed, 25 Mar 2020 14:50:45 +0000 Subject: [PATCH 5/7] lint --- synapse/storage/data_stores/main/directory.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/synapse/storage/data_stores/main/directory.py b/synapse/storage/data_stores/main/directory.py index 0049c43d9c2a..9640ec35ece5 100644 --- a/synapse/storage/data_stores/main/directory.py +++ b/synapse/storage/data_stores/main/directory.py @@ -14,8 +14,8 @@ # limitations under the License. from collections import namedtuple - from typing import Optional + from twisted.internet import defer from synapse.api.errors import SynapseError @@ -161,10 +161,7 @@ def _delete_room_alias_txn(self, txn, room_alias): return room_id def update_aliases_for_room( - self, - old_room_id: str, - new_room_id: str, - creator: Optional[str] = None, + self, old_room_id: str, new_room_id: str, creator: Optional[str] = None, ): """Repoint the aliases from one room to another From 08ee8c82f845a9686680543fa20c920e78ee62b8 Mon Sep 17 00:00:00 2001 From: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com> Date: Mon, 30 Mar 2020 17:04:31 +0100 Subject: [PATCH 6/7] Apply suggestions from code review Co-Authored-By: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> --- synapse/storage/data_stores/main/directory.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/synapse/storage/data_stores/main/directory.py b/synapse/storage/data_stores/main/directory.py index 9640ec35ece5..9bdf6e671bec 100644 --- a/synapse/storage/data_stores/main/directory.py +++ b/synapse/storage/data_stores/main/directory.py @@ -163,12 +163,13 @@ def _delete_room_alias_txn(self, txn, room_alias): def update_aliases_for_room( self, old_room_id: str, new_room_id: str, creator: Optional[str] = None, ): - """Repoint the aliases from one room to another + """Repoint all of the aliases for a given room, to a different room. Args: old_room_id: new_room_id: - creator: The creator of the alias mapping. If None, don't update + creator: The user to record as the creator of the new mapping. + If None, the creator will be left unchanged. """ def _update_aliases_for_room_txn(txn): From 50d45f18693637f882423d17287677330cd8ace4 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 30 Mar 2020 17:32:37 +0100 Subject: [PATCH 7/7] lint --- synapse/storage/data_stores/main/directory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/storage/data_stores/main/directory.py b/synapse/storage/data_stores/main/directory.py index 9bdf6e671bec..e1d1bc3e0586 100644 --- a/synapse/storage/data_stores/main/directory.py +++ b/synapse/storage/data_stores/main/directory.py @@ -168,7 +168,7 @@ def update_aliases_for_room( Args: old_room_id: new_room_id: - creator: The user to record as the creator of the new mapping. + creator: The user to record as the creator of the new mapping. If None, the creator will be left unchanged. """