Skip to content

Add admin API for fetching room reports #18253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/18253.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add admin API for fetching (paginated) room reports.
76 changes: 76 additions & 0 deletions docs/admin_api/room_reports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Show reported rooms

This API returns information about reported rooms.

To use it, you will need to authenticate by providing an `access_token`
for a server admin: see [Admin API](../usage/administration/admin_api/).

The api is:
```
GET /_synapse/admin/v1/room_reports?from=0&limit=10
```

It returns a JSON body like the following:

```json
{
"room_reports": [
{
"id": 2,
"reason": "foo",
"received_ts": 1570897107409,
"canonical_alias": "#alias1:matrix.org",
"room_id": "!ERAgBpSOcCCuTJqQPk:matrix.org",
"name": "Matrix HQ",
"user_id": "@foo:matrix.org"
},
{
"id": 3,
"reason": "bar",
"received_ts": 1598889612059,
"canonical_alias": "#alias2:matrix.org",
"room_id": "!eGvUQuTCkHGVwNMOjv:matrix.org",
"name": "Your room name here",
"user_id": "@bar:matrix.org"
}
],
"next_token": 2,
"total": 4
}
```

To paginate, check for `next_token` and if present, call the endpoint again with `from`
set to the value of `next_token`. This will return a new page.

If the endpoint does not return a `next_token` then there are no more reports to
paginate through.

**URL parameters:**

* `limit`: integer - Is optional but is used for pagination, denoting the maximum number
of items to return in this call. Defaults to `100`.
* `from`: integer - Is optional but used for pagination, denoting the offset in the
returned results. This should be treated as an opaque value and not explicitly set to
anything other than the return value of `next_token` from a previous call. Defaults to `0`.
* `dir`: string - Direction of event report order. Whether to fetch the most recent
first (`b`) or the oldest first (`f`). Defaults to `b`.
* `user_id`: optional string - Filter by the user ID of the reporter. This is the user who reported the event
and wrote the reason.
* `room_id`: optional string - Filter by (reported) room id.

**Response**

The following fields are returned in the JSON response body:

* `id`: integer - ID of room report.
* `received_ts`: integer - The timestamp (in milliseconds since the unix epoch) when this
report was sent.
* `room_id`: string - The ID of the room being reported.
* `name`: string - The name of the room.
* `user_id`: string - This is the user who reported the room and wrote the reason.
* `reason`: string - Comment made by the `user_id` in this report. May be blank or `null`.
* `canonical_alias`: string - The canonical alias of the room. `null` if the room does not
have a canonical alias set.
* `next_token`: integer - Indication for pagination. See above.
* `total`: integer - Total number of room reports related to the query
(`user_id` and `room_id`).
2 changes: 2 additions & 0 deletions synapse/rest/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
NewRegistrationTokenRestServlet,
RegistrationTokenRestServlet,
)
from synapse.rest.admin.room_reports import RoomReportsRestServlet
from synapse.rest.admin.rooms import (
BlockRoomRestServlet,
DeleteRoomStatusByDeleteIdRestServlet,
Expand Down Expand Up @@ -302,6 +303,7 @@ def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
LargestRoomsStatistics(hs).register(http_server)
EventReportDetailRestServlet(hs).register(http_server)
EventReportsRestServlet(hs).register(http_server)
RoomReportsRestServlet(hs).register(http_server)
AccountDataRestServlet(hs).register(http_server)
PushersRestServlet(hs).register(http_server)
MakeRoomAdminRestServlet(hs).register(http_server)
Expand Down
96 changes: 96 additions & 0 deletions synapse/rest/admin/room_reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#
# This file is licensed under the Affero General Public License (AGPL) version 3.
#
# Copyright (C) 2025 New Vector, Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# See the GNU Affero General Public License for more details:
# <https://www.gnu.org/licenses/agpl-3.0.html>.
#
# Originally licensed under the Apache License, Version 2.0:
# <http://www.apache.org/licenses/LICENSE-2.0>.
#
# [This file includes modifications made by New Vector Limited]
#
#

import logging
from http import HTTPStatus
from typing import TYPE_CHECKING, Tuple

from synapse.api.constants import Direction
from synapse.api.errors import Codes, SynapseError
from synapse.http.servlet import RestServlet, parse_enum, parse_integer, parse_string
from synapse.http.site import SynapseRequest
from synapse.rest.admin._base import admin_patterns, assert_requester_is_admin
from synapse.types import JsonDict

if TYPE_CHECKING:
from synapse.server import HomeServer

logger = logging.getLogger(__name__)


# Based upon EventReportsRestServlet
class RoomReportsRestServlet(RestServlet):
"""
List all reported rooms that are known to the homeserver. Results are returned
in a dictionary containing report information. Supports pagination.
The requester must have administrator access in Synapse.

GET /_synapse/admin/v1/room_reports
returns:
200 OK with list of reports if success otherwise an error.

Args:
The parameters `from` and `limit` are required only for pagination.
By default, a `limit` of 100 is used.
The parameter `dir` can be used to define the order of results.
The `user_id` query parameter filters by the user ID of the reporter of the event.
The `room_id` query parameter filters by room id.
Returns:
A list of reported rooms and an integer representing the total number of
reported rooms that exist given this query
"""

PATTERNS = admin_patterns("/room_reports$")

def __init__(self, hs: "HomeServer"):
self._auth = hs.get_auth()
self._store = hs.get_datastores().main

async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
await assert_requester_is_admin(self._auth, request)

start = parse_integer(request, "from", default=0)
limit = parse_integer(request, "limit", default=100)
direction = parse_enum(request, "dir", Direction, Direction.BACKWARDS)
user_id = parse_string(request, "user_id")
room_id = parse_string(request, "room_id")

if start < 0:
raise SynapseError(
HTTPStatus.BAD_REQUEST,
"The start parameter must be a positive integer.",
errcode=Codes.INVALID_PARAM,
)

if limit < 0:
raise SynapseError(
HTTPStatus.BAD_REQUEST,
"The limit parameter must be a positive integer.",
errcode=Codes.INVALID_PARAM,
)

room_reports, total = await self._store.get_room_reports_paginate(
start, limit, direction, user_id, room_id
)
ret = {"room_reports": room_reports, "total": total}
if (start + limit) < total:
ret["next_token"] = start + len(room_reports)

return HTTPStatus.OK, ret
101 changes: 101 additions & 0 deletions synapse/storage/databases/main/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,107 @@ def _get_event_reports_paginate_txn(
"get_event_reports_paginate", _get_event_reports_paginate_txn
)

async def get_room_reports_paginate(
self,
start: int,
limit: int,
direction: Direction = Direction.BACKWARDS,
user_id: Optional[str] = None,
room_id: Optional[str] = None,
) -> Tuple[List[Dict[str, Any]], int]:
"""Retrieve a paginated list of room reports

Args:
start: room offset to begin the query from
limit: number of rows to retrieve
direction: Whether to fetch the most recent first (backwards) or the
oldest first (forwards)
user_id: search for user_id. Ignored if user_id is None
room_id: search for room_id. Ignored if room_id is None
Returns:
Tuple of:
json list of room reports
total number of room reports matching the filter criteria
"""

def _get_room_reports_paginate_txn(
txn: LoggingTransaction,
) -> Tuple[List[Dict[str, Any]], int]:
filters = []
args: List[object] = []

if user_id:
filters.append("er.user_id LIKE ?")
args.extend(["%" + user_id + "%"])
if room_id:
filters.append("er.room_id LIKE ?")
args.extend(["%" + room_id + "%"])

if direction == Direction.BACKWARDS:
order = "DESC"
else:
order = "ASC"

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

# We join on room_stats_state despite not using any columns from it
# because the join can influence the number of rows returned;
# e.g. a room that doesn't have state, maybe because it was deleted.
# The query returning the total count should be consistent with
# the query returning the results.
sql = """
SELECT COUNT(*) as total_room_reports
FROM room_reports AS rr
JOIN room_stats_state ON room_stats_state.room_id = rr.room_id
{}
""".format(where_clause)
txn.execute(sql, args)
count = cast(Tuple[int], txn.fetchone())[0]

sql = """
SELECT
rr.id,
rr.received_ts,
rr.room_id,
rr.user_id,
rr.reason,
room_stats_state.canonical_alias,
room_stats_state.name
FROM event_reports AS rr
JOIN room_stats_state
ON room_stats_state.room_id = rr.room_id
{where_clause}
ORDER BY rr.received_ts {order}
LIMIT ?
OFFSET ?
""".format(
where_clause=where_clause,
order=order,
)

args += [limit, start]
txn.execute(sql, args)

room_reports = []
for row in txn:
room_reports.append(
{
"id": row[0],
"received_ts": row[1],
"room_id": row[2],
"user_id": row[3],
"reason": row[4],
"canonical_alias": row[5],
"name": row[6],
}
)

return room_reports, count

return await self.db_pool.runInteraction(
"get_room_reports_paginate", _get_room_reports_paginate_txn
)

async def delete_event_report(self, report_id: int) -> bool:
"""Remove an event report from database.

Expand Down
Loading
Loading