Skip to content

Commit 5da3f0a

Browse files
buffless-mattbabolivier
authored andcommitted
Add module API method to resolve a room alias to a room ID (matrix-org#13428)
Co-authored-by: MattC <[email protected]> Co-authored-by: Brendan Abolivier <[email protected]>
1 parent b1f03bb commit 5da3f0a

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

changelog.d/13428.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a module API method to translate a room alias into a room ID.

synapse/module_api/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,30 @@ async def get_monthly_active_users_by_service(
14521452
start_timestamp, end_timestamp
14531453
)
14541454

1455+
async def lookup_room_alias(self, room_alias: str) -> Tuple[str, List[str]]:
1456+
"""
1457+
Get the room ID associated with a room alias.
1458+
1459+
Added in Synapse v1.65.0.
1460+
1461+
Args:
1462+
room_alias: The alias to look up.
1463+
1464+
Returns:
1465+
A tuple of:
1466+
The room ID (str).
1467+
Hosts likely to be participating in the room ([str]).
1468+
1469+
Raises:
1470+
SynapseError if room alias is invalid or could not be found.
1471+
"""
1472+
alias = RoomAlias.from_string(room_alias)
1473+
(room_id, hosts) = await self._hs.get_room_member_handler().lookup_room_alias(
1474+
alias
1475+
)
1476+
1477+
return room_id.to_string(), hosts
1478+
14551479

14561480
class PublicRoomListManager:
14571481
"""Contains methods for adding to, removing from and querying whether a room

tests/module_api/test_api.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,25 @@ def test_check_push_rules_actions(self) -> None:
635635
[{"set_tweak": "sound", "value": "default"}]
636636
)
637637

638+
def test_lookup_room_alias(self) -> None:
639+
"""Test that modules can resolve a room alias to a room ID."""
640+
password = "password"
641+
user_id = self.register_user("user", password)
642+
access_token = self.login(user_id, password)
643+
room_alias = "my-alias"
644+
reference_room_id = self.helper.create_room_as(
645+
tok=access_token, extra_content={"room_alias_name": room_alias}
646+
)
647+
self.assertIsNotNone(reference_room_id)
648+
649+
(room_id, _) = self.get_success(
650+
self.module_api.lookup_room_alias(
651+
f"#{room_alias}:{self.module_api.server_name}"
652+
)
653+
)
654+
655+
self.assertEqual(room_id, reference_room_id)
656+
638657

639658
class ModuleApiWorkerTestCase(BaseMultiWorkerStreamTestCase):
640659
"""For testing ModuleApi functionality in a multi-worker setup"""

0 commit comments

Comments
 (0)