From 64dae751babd39fff4d87e8ba62772d1fbb355d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Mon, 17 Apr 2023 15:39:15 +0200 Subject: [PATCH 1/9] Add get_room_alias to module API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Boxdot Signed-off-by: Gabriel FĂ©ron --- synapse/module_api/__init__.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index 595c23e78d7e..684836dade8d 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -120,6 +120,7 @@ JsonMapping, Requester, RoomAlias, + RoomID, StateMap, UserID, UserInfo, @@ -1520,6 +1521,26 @@ async def get_monthly_active_users_by_service( start_timestamp, end_timestamp ) + async def get_room_alias(self, room_id: RoomID) -> Optional[RoomAlias]: + """ + Get the room alias associated with a room ID. + + Added in Synapse v1.82.0. + + Args: + room_id: The Room ID to find the alias of. + + Returns: + The optional room alias + """ + from synapse.api.constants import EventTypes + + alias_event = await self._storage_controllers.state.get_current_state_event( + room_id, EventTypes.CanonicalAlias, "" + ) + if alias_event: + return alias_event.content.get("alias") + async def lookup_room_alias(self, room_alias: str) -> Tuple[str, List[str]]: """ Get the room ID associated with a room alias. From 46651afcc2a1a89911ff64dd5c9424cfc051a1bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Mon, 17 Apr 2023 15:43:46 +0200 Subject: [PATCH 2/9] Add changelog entry --- changelog.d/15450.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/15450.feature diff --git a/changelog.d/15450.feature b/changelog.d/15450.feature new file mode 100644 index 000000000000..e7cb33485695 --- /dev/null +++ b/changelog.d/15450.feature @@ -0,0 +1 @@ +Allow resolving room aliases in module API. \ No newline at end of file From a037e76687baadecefd0081e8a523ce4609b1baa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Thu, 25 May 2023 11:18:33 +0200 Subject: [PATCH 3/9] Apply suggestions from code review Co-authored-by: David Robertson --- changelog.d/15450.feature | 2 +- synapse/module_api/__init__.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/changelog.d/15450.feature b/changelog.d/15450.feature index e7cb33485695..0ebf344916e4 100644 --- a/changelog.d/15450.feature +++ b/changelog.d/15450.feature @@ -1 +1 @@ -Allow resolving room aliases in module API. \ No newline at end of file +Allow resolving room aliases via the module API. \ No newline at end of file diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index 684836dade8d..0d1eaedabdf6 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -1521,9 +1521,13 @@ async def get_monthly_active_users_by_service( start_timestamp, end_timestamp ) - async def get_room_alias(self, room_id: RoomID) -> Optional[RoomAlias]: + async def get_canonical_room_alias(self, room_id: RoomID) -> Optional[RoomAlias]: """ - Get the room alias associated with a room ID. + 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.82.0. @@ -1531,7 +1535,8 @@ async def get_room_alias(self, room_id: RoomID) -> Optional[RoomAlias]: room_id: The Room ID to find the alias of. Returns: - The optional room alias + None if the room ID does not exist, or if the room exists but has no canonical alias. + Otherwise, the parsed room alias. """ from synapse.api.constants import EventTypes From 105977f9e1c255e0aa1fd51c8112f41eaf0ab0d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Thu, 25 May 2023 11:27:20 +0200 Subject: [PATCH 4/9] Address review comments --- synapse/module_api/__init__.py | 12 ++++-------- synapse/storage/controllers/state.py | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index f83fe340fe05..2d0b1c4b7b6e 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -1572,7 +1572,7 @@ async def get_canonical_room_alias(self, room_id: RoomID) -> Optional[RoomAlias] preferred alias to use when referring to the room. This function retrieves that alias from the room's state. - Added in Synapse v1.82.0. + Added in Synapse v1.84.0. Args: room_id: The Room ID to find the alias of. @@ -1581,13 +1581,9 @@ async def get_canonical_room_alias(self, room_id: RoomID) -> Optional[RoomAlias] None if the room ID does not exist, or if the room exists but has no canonical alias. Otherwise, the parsed room alias. """ - from synapse.api.constants import EventTypes - - alias_event = await self._storage_controllers.state.get_current_state_event( - room_id, EventTypes.CanonicalAlias, "" - ) - if alias_event: - return alias_event.content.get("alias") + room_alias_str = await self._storage_controllers.state.get_canonical_alias_for_room(room_id) + if room_alias_str: + return RoomAlias.from_string(room_alias_str) async def lookup_room_alias(self, room_alias: str) -> Tuple[str, List[str]]: """ diff --git a/synapse/storage/controllers/state.py b/synapse/storage/controllers/state.py index 7089b0a1d85d..233df7cce24e 100644 --- a/synapse/storage/controllers/state.py +++ b/synapse/storage/controllers/state.py @@ -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") @trace @tag_args From bf408c0a2a2db1e3735b88cd29c112b9e12797ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Thu, 25 May 2023 11:33:03 +0200 Subject: [PATCH 5/9] Format --- synapse/module_api/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index 2d0b1c4b7b6e..e12b7164a3a1 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -1581,7 +1581,9 @@ async def get_canonical_room_alias(self, room_id: RoomID) -> Optional[RoomAlias] 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) + room_alias_str = ( + await self._storage_controllers.state.get_canonical_alias_for_room(room_id) + ) if room_alias_str: return RoomAlias.from_string(room_alias_str) From ce13fd97bb7f9da33a2db7331dc41c6c78b656c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Wed, 31 May 2023 11:56:07 +0200 Subject: [PATCH 6/9] Apply suggestions from code review Co-authored-by: Patrick Cloke --- synapse/module_api/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index e12b7164a3a1..179ba5c11877 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -1572,20 +1572,21 @@ async def get_canonical_room_alias(self, room_id: RoomID) -> Optional[RoomAlias] preferred alias to use when referring to the room. This function retrieves that alias from the room's state. - Added in Synapse v1.84.0. + 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. + 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) ) if room_alias_str: return RoomAlias.from_string(room_alias_str) + return None async def lookup_room_alias(self, room_alias: str) -> Tuple[str, List[str]]: """ From e0b9d5c86d7d6c88fea332472888dbccbac50498 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 31 May 2023 07:31:01 -0400 Subject: [PATCH 7/9] Update changelog. --- changelog.d/15450.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/15450.feature b/changelog.d/15450.feature index 0ebf344916e4..2102381143ba 100644 --- a/changelog.d/15450.feature +++ b/changelog.d/15450.feature @@ -1 +1 @@ -Allow resolving room aliases via the module API. \ No newline at end of file +Support resolving a room's [canonical alias](https://spec.matrix.org/v1.7/client-server-api/#mroomcanonical_alias) via the module API. \ No newline at end of file From 0c431483e640c241cd384d1de3b7e469881c2a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Wed, 31 May 2023 13:57:00 +0200 Subject: [PATCH 8/9] Fix input type --- 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 b2ba15e8f40a..0951fbd4fde9 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -1589,7 +1589,7 @@ async def get_canonical_room_alias(self, room_id: RoomID) -> Optional[RoomAlias] Otherwise, the parsed room alias. """ room_alias_str = ( - await self._storage_controllers.state.get_canonical_alias_for_room(room_id) + 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) From 9d8684d82a5957c02847fa1cfa809aeeb5bd4878 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 31 May 2023 08:22:12 -0400 Subject: [PATCH 9/9] Lint --- synapse/module_api/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index 0951fbd4fde9..a8d6224a4528 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -1589,7 +1589,9 @@ async def get_canonical_room_alias(self, room_id: RoomID) -> Optional[RoomAlias] Otherwise, the parsed room alias. """ room_alias_str = ( - await self._storage_controllers.state.get_canonical_alias_for_room(room_id.to_string()) + 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)