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

Commit 8bdf2bd

Browse files
Fix a bug in the /event_reports Admin API which meant that the total count could be larger than the number of results you can actually query for. (#13525)
Co-authored-by: Brendan Abolivier <[email protected]>
1 parent 82a0752 commit 8bdf2bd

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

changelog.d/13525.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug in the `/event_reports` Admin API which meant that the total count could be larger than the number of results you can actually query for.

synapse/storage/databases/main/room.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,9 +2001,15 @@ def _get_event_reports_paginate_txn(
20012001

20022002
where_clause = "WHERE " + " AND ".join(filters) if len(filters) > 0 else ""
20032003

2004+
# We join on room_stats_state despite not using any columns from it
2005+
# because the join can influence the number of rows returned;
2006+
# e.g. a room that doesn't have state, maybe because it was deleted.
2007+
# The query returning the total count should be consistent with
2008+
# the query returning the results.
20042009
sql = """
20052010
SELECT COUNT(*) as total_event_reports
20062011
FROM event_reports AS er
2012+
JOIN room_stats_state ON room_stats_state.room_id = er.room_id
20072013
{}
20082014
""".format(
20092015
where_clause

tests/rest/admin/test_event_reports.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,33 @@ def _check_fields(self, content: List[JsonDict]) -> None:
410410
self.assertIn("score", c)
411411
self.assertIn("reason", c)
412412

413+
def test_count_correct_despite_table_deletions(self) -> None:
414+
"""
415+
Tests that the count matches the number of rows, even if rows in joined tables
416+
are missing.
417+
"""
418+
419+
# Delete rows from room_stats_state for one of our rooms.
420+
self.get_success(
421+
self.hs.get_datastores().main.db_pool.simple_delete(
422+
"room_stats_state", {"room_id": self.room_id1}, desc="_"
423+
)
424+
)
425+
426+
channel = self.make_request(
427+
"GET",
428+
self.url,
429+
access_token=self.admin_user_tok,
430+
)
431+
432+
self.assertEqual(200, channel.code, msg=channel.json_body)
433+
# The 'total' field is 10 because only 10 reports will actually
434+
# be retrievable since we deleted the rows in the room_stats_state
435+
# table.
436+
self.assertEqual(channel.json_body["total"], 10)
437+
# This is consistent with the number of rows actually returned.
438+
self.assertEqual(len(channel.json_body["event_reports"]), 10)
439+
413440

414441
class EventReportDetailTestCase(unittest.HomeserverTestCase):
415442
servlets = [

0 commit comments

Comments
 (0)