From 521b2a397b7a2049f898ab7563cb5add9a5d39c0 Mon Sep 17 00:00:00 2001 From: MattC Date: Mon, 1 Aug 2022 14:38:30 +1000 Subject: [PATCH 1/6] Add - module API method to translate a room alias to a room ID. --- synapse/module_api/__init__.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index 6d8bf5408364..0e606e6873e1 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -1452,6 +1452,28 @@ async def get_monthly_active_users_by_service( start_timestamp, end_timestamp ) + async def lookup_room_alias(self, room_alias: str) -> Tuple[str, List[str]]: + """ + Get the room ID associated with a room alias. + + Added in Synapse v1.65.0. + + Args: + room_alias: The alias to look up. + Returns: + A tuple of: + The room ID (str). + Hosts likely to be participating in the room ([str]). + Raises: + SynapseError if room alias is invalid or could not be found. + """ + alias = RoomAlias.from_string(room_alias) + (room_id, hosts) = await self._hs.get_room_member_handler().lookup_room_alias( + alias + ) + + return room_id.to_string(), hosts + class PublicRoomListManager: """Contains methods for adding to, removing from and querying whether a room From be9a783386f39e242b771a3ea51420f6b3f1eb20 Mon Sep 17 00:00:00 2001 From: MattC Date: Mon, 1 Aug 2022 14:54:50 +1000 Subject: [PATCH 2/6] Document - Add changelog entry for 13428. --- changelog.d/13428.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/13428.feature diff --git a/changelog.d/13428.feature b/changelog.d/13428.feature new file mode 100644 index 000000000000..9b4823aec43b --- /dev/null +++ b/changelog.d/13428.feature @@ -0,0 +1 @@ +Add module API method to translate a room alias to a room ID. From 83de48b344d9ba6c8064c42d0e8aef8d53a1c494 Mon Sep 17 00:00:00 2001 From: MattC Date: Tue, 2 Aug 2022 12:59:33 +1000 Subject: [PATCH 3/6] Add - unit test for the module API "lookup_room_alias" method. --- tests/module_api/test_api.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/module_api/test_api.py b/tests/module_api/test_api.py index 169e29b59001..8e05590230d3 100644 --- a/tests/module_api/test_api.py +++ b/tests/module_api/test_api.py @@ -635,6 +635,25 @@ def test_check_push_rules_actions(self) -> None: [{"set_tweak": "sound", "value": "default"}] ) + def test_lookup_room_alias(self) -> None: + """Test that modules can resolve a room alias to a room ID.""" + password = "password" + user_id = self.register_user("user", password) + access_token = self.login(user_id, password) + room_alias = "my-alias" + reference_room_id = self.helper.create_room_as( + tok=access_token, extra_content={"room_alias_name": room_alias} + ) + self.assertIsNotNone(reference_room_id) + + (room_id, _) = self.get_success( + self.module_api.lookup_room_alias( + f"#{room_alias}:{self.module_api.server_name}" + ) + ) + + self.assertEqual(room_id, reference_room_id) + class ModuleApiWorkerTestCase(BaseMultiWorkerStreamTestCase): """For testing ModuleApi functionality in a multi-worker setup""" From 5ff3e02670f49e6c23de082ef1710b0e4029055c Mon Sep 17 00:00:00 2001 From: MattC Date: Wed, 3 Aug 2022 12:45:38 +1000 Subject: [PATCH 4/6] Document - add some spacing to the module API "lookup_room_alias" docstring. --- synapse/module_api/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index 0e606e6873e1..18d6d1058a3d 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -1460,10 +1460,12 @@ async def lookup_room_alias(self, room_alias: str) -> Tuple[str, List[str]]: Args: room_alias: The alias to look up. + Returns: A tuple of: The room ID (str). Hosts likely to be participating in the room ([str]). + Raises: SynapseError if room alias is invalid or could not be found. """ From 353ee92e02407a4e51b45570431e151b64f37ccd Mon Sep 17 00:00:00 2001 From: MattC Date: Wed, 3 Aug 2022 12:51:47 +1000 Subject: [PATCH 5/6] Document - Alter module API "lookup_room_alias" method docstring so that expected Synapse version release is more realistic. --- synapse/module_api/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index 18d6d1058a3d..798586b64926 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -1456,7 +1456,7 @@ async def lookup_room_alias(self, room_alias: str) -> Tuple[str, List[str]]: """ Get the room ID associated with a room alias. - Added in Synapse v1.65.0. + Added in Synapse v1.66.0. Args: room_alias: The alias to look up. From 8e02d37501ac230f9feecd642f1ae6e033581b73 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Wed, 3 Aug 2022 10:46:05 +0200 Subject: [PATCH 6/6] Apply suggestions from code review --- changelog.d/13428.feature | 2 +- synapse/module_api/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/changelog.d/13428.feature b/changelog.d/13428.feature index 9b4823aec43b..085b61483f6a 100644 --- a/changelog.d/13428.feature +++ b/changelog.d/13428.feature @@ -1 +1 @@ -Add module API method to translate a room alias to a room ID. +Add a module API method to translate a room alias into a room ID. diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index 798586b64926..18d6d1058a3d 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -1456,7 +1456,7 @@ async def lookup_room_alias(self, room_alias: str) -> Tuple[str, List[str]]: """ Get the room ID associated with a room alias. - Added in Synapse v1.66.0. + Added in Synapse v1.65.0. Args: room_alias: The alias to look up.