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

Add get_canonical_room_alias to module API #15450

Merged
merged 12 commits into from
May 31, 2023
1 change: 1 addition & 0 deletions changelog.d/15450.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support resolving a room's [canonical alias](https://spec.matrix.org/v1.7/client-server-api/#mroomcanonical_alias) via the module API.
27 changes: 27 additions & 0 deletions synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
JsonMapping,
Requester,
RoomAlias,
RoomID,
StateMap,
UserID,
UserInfo,
Expand Down Expand Up @@ -1570,6 +1571,32 @@ async def get_monthly_active_users_by_service(
start_timestamp, end_timestamp
)

async def get_canonical_room_alias(self, room_id: RoomID) -> Optional[RoomAlias]:
"""
Retrieve the given room's current canonical alias.

A room may declare an alias as "canonical", meaning that it is the
preferred alias to use when referring to the room. This function
retrieves that alias from the room's state.

Added in Synapse v1.86.0.

Args:
room_id: The Room ID to find the alias of.

Returns:
None if the room ID does not exist, or if the room exists but has no canonical alias.
Otherwise, the parsed room alias.
"""
room_alias_str = (
await self._storage_controllers.state.get_canonical_alias_for_room(
room_id.to_string()
)
)
if room_alias_str:
return RoomAlias.from_string(room_alias_str)
gferon marked this conversation as resolved.
Show resolved Hide resolved
return None

async def lookup_room_alias(self, room_alias: str) -> Tuple[str, List[str]]:
"""
Get the room ID associated with a room alias.
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/controllers/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ async def get_canonical_alias_for_room(self, room_id: str) -> Optional[str]:
if not event:
return None

return event.content.get("canonical_alias")
return event.content.get("alias")
clokep marked this conversation as resolved.
Show resolved Hide resolved

@trace
@tag_args
Expand Down