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

Commit 956b43f

Browse files
reivilibreH-Shay
authored andcommitted
Fix a long-standing bug where the user directory would return 1 more row than requested. (#14631)
1 parent 6baed0b commit 956b43f

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

changelog.d/14631.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where the user directory would return 1 more row than requested.

synapse/rest/client/user_directory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonMapping]:
6363

6464
body = parse_json_object_from_request(request)
6565

66-
limit = body.get("limit", 10)
67-
limit = min(limit, 50)
66+
limit = int(body.get("limit", 10))
67+
limit = max(min(limit, 50), 0)
6868

6969
try:
7070
search_term = body["search_term"]

synapse/storage/databases/main/user_directory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ async def search_user_dir(
886886

887887
limited = len(results) > limit
888888

889-
return {"limited": limited, "results": results}
889+
return {"limited": limited, "results": results[0:limit]}
890890

891891

892892
def _parse_query_sqlite(search_term: str) -> str:

tests/storage/test_user_directory.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,12 @@ def test_search_user_dir_all_users(self) -> None:
448448
{"user_id": BOBBY, "display_name": "bobby", "avatar_url": None},
449449
)
450450

451+
@override_config({"user_directory": {"search_all_users": True}})
452+
def test_search_user_limit_correct(self) -> None:
453+
r = self.get_success(self.store.search_user_dir(ALICE, "bob", 1))
454+
self.assertTrue(r["limited"])
455+
self.assertEqual(1, len(r["results"]))
456+
451457
@override_config({"user_directory": {"search_all_users": True}})
452458
def test_search_user_dir_stop_words(self) -> None:
453459
"""Tests that a user can look up another user by searching for the start if its

0 commit comments

Comments
 (0)