diff --git a/docs/admin_api/rooms.md b/docs/admin_api/rooms.md index e98fd6b1b57d..328853a9e1fa 100644 --- a/docs/admin_api/rooms.md +++ b/docs/admin_api/rooms.md @@ -38,10 +38,14 @@ The following query parameters are available: - `history_visibility` - Rooms are ordered alphabetically by visibility of history of the room. - `state_events` - Rooms are ordered by number of state events. Largest to smallest. * `dir` - Direction of room order. Either `f` for forwards or `b` for backwards. Setting - this value to `b` will reverse the above sort order. Defaults to `f`. + this value to `b` will reverse the above sort order. Defaults to `f`. * `search_term` - Filter rooms by their room name, canonical alias and room id. - Search term can be contained in any part of the room name and - local part of canonical alias or room id. Defaults to no filtering. + Specifically, rooms are selected if the search term is contained in + - the room's name, + - the local part of the room's canonical alias, or + - the local part of the room's id. + + Defaults to no filtering. **Response** diff --git a/synapse/storage/databases/main/room.py b/synapse/storage/databases/main/room.py index ae73cbb82654..b56c3d88ec83 100644 --- a/synapse/storage/databases/main/room.py +++ b/synapse/storage/databases/main/room.py @@ -417,6 +417,7 @@ async def get_rooms_paginate( """ # Filter room names by a string where_statement = "" + search_pattern = "" if search_term: where_statement = """ WHERE LOWER(state.name) LIKE ? @@ -429,7 +430,7 @@ async def get_rooms_paginate( # HOWEVER, if you put a % into your SQL then everything goes wibbly. # To get around this, we're going to surround search_term with %'s # before giving it to the database in python instead - search_term = [ + search_pattern = [ "%" + search_term.lower() + "%", "#%" + search_term.lower() + "%:%", "!%" + search_term.lower() + "%:%", @@ -527,9 +528,9 @@ async def get_rooms_paginate( def _get_rooms_paginate_txn(txn): # Execute the data query sql_values = [limit, start] - if search_term: + if search_pattern: # Add the search term into the WHERE clause - sql_values = search_term + sql_values + sql_values = search_pattern + sql_values txn.execute(info_sql, sql_values) # Refactor room query data into a structured dictionary @@ -557,7 +558,7 @@ def _get_rooms_paginate_txn(txn): # Execute the count query # Add the search term into the WHERE clause if present - sql_values = search_term if search_term else () + sql_values = search_pattern if search_pattern else () txn.execute(count_sql, sql_values) room_count = txn.fetchone() diff --git a/tests/rest/admin/test_room.py b/tests/rest/admin/test_room.py index a84c2c37318f..1ddad0d249c8 100644 --- a/tests/rest/admin/test_room.py +++ b/tests/rest/admin/test_room.py @@ -895,7 +895,7 @@ def _search_test( r = rooms[0] self.assertEqual(expected_room_id, r["room_id"]) - # Perform search tests + # Test searching by room name _search_test(room_id_1, "something") _search_test(room_id_1, "thing")