|
| 1 | +# |
| 2 | +# This file is licensed under the Affero General Public License (AGPL) version 3. |
| 3 | +# |
| 4 | +# Copyright (C) 2026 Element Creations, Ltd |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Affero General Public License as |
| 8 | +# published by the Free Software Foundation, either version 3 of the |
| 9 | +# License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# See the GNU Affero General Public License for more details: |
| 12 | +# <https://www.gnu.org/licenses/agpl-3.0.html>. |
| 13 | +# |
| 14 | + |
| 15 | + |
| 16 | +import logging |
| 17 | +from http import HTTPStatus |
| 18 | +from typing import TYPE_CHECKING |
| 19 | + |
| 20 | +from synapse.api.constants import Direction |
| 21 | +from synapse.api.errors import Codes, NotFoundError, SynapseError |
| 22 | +from synapse.http.servlet import RestServlet, parse_enum, parse_integer, parse_string |
| 23 | +from synapse.http.site import SynapseRequest |
| 24 | +from synapse.rest.admin._base import admin_patterns, assert_requester_is_admin |
| 25 | +from synapse.types import JsonDict |
| 26 | + |
| 27 | +if TYPE_CHECKING: |
| 28 | + from synapse.server import HomeServer |
| 29 | + |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +class UserReportsRestServlet(RestServlet): |
| 34 | + """ |
| 35 | + List all reported users that are known to the homeserver. Results are returned |
| 36 | + in a dictionary containing report information. Supports pagination. |
| 37 | + The requester must have administrator access in Synapse. |
| 38 | +
|
| 39 | + GET /_synapse/admin/v1/user_reports |
| 40 | + returns: |
| 41 | + 200 OK with list of reports if success otherwise an error. |
| 42 | +
|
| 43 | + Args: |
| 44 | + The parameters `from` and `limit` are required only for pagination. |
| 45 | + By default, a `limit` of 100 is used. |
| 46 | + The parameter `dir` can be used to define the order of results. |
| 47 | + The `user_id` query parameter filters by the user ID of the reporter of the target user. |
| 48 | + The `target_user_id` query parameter filters by user id of the target user. |
| 49 | + Returns: |
| 50 | + A list of user reprots and an integer representing the total number of user |
| 51 | + reports that exist given this query |
| 52 | + """ |
| 53 | + |
| 54 | + PATTERNS = admin_patterns("/user_reports$") |
| 55 | + |
| 56 | + def __init__(self, hs: "HomeServer"): |
| 57 | + self._auth = hs.get_auth() |
| 58 | + self._store = hs.get_datastores().main |
| 59 | + |
| 60 | + async def on_GET(self, request: SynapseRequest) -> tuple[int, JsonDict]: |
| 61 | + await assert_requester_is_admin(self._auth, request) |
| 62 | + |
| 63 | + start = parse_integer(request, "from", default=0) |
| 64 | + limit = parse_integer(request, "limit", default=100) |
| 65 | + direction = parse_enum(request, "dir", Direction, Direction.BACKWARDS) |
| 66 | + user_id = parse_string(request, "user_id") |
| 67 | + target_user_id = parse_string(request, "target_user_id") |
| 68 | + |
| 69 | + if start < 0: |
| 70 | + raise SynapseError( |
| 71 | + HTTPStatus.BAD_REQUEST, |
| 72 | + "The start parameter must be a positive integer.", |
| 73 | + errcode=Codes.INVALID_PARAM, |
| 74 | + ) |
| 75 | + |
| 76 | + if limit < 0: |
| 77 | + raise SynapseError( |
| 78 | + HTTPStatus.BAD_REQUEST, |
| 79 | + "The limit parameter must be a positive integer.", |
| 80 | + errcode=Codes.INVALID_PARAM, |
| 81 | + ) |
| 82 | + |
| 83 | + user_reports, total = await self._store.get_user_reports_paginate( |
| 84 | + start, limit, direction, user_id, target_user_id |
| 85 | + ) |
| 86 | + ret = {"user_reports": user_reports, "total": total} |
| 87 | + if (start + limit) < total: |
| 88 | + ret["next_token"] = start + len(user_reports) |
| 89 | + |
| 90 | + return HTTPStatus.OK, ret |
| 91 | + |
| 92 | + |
| 93 | +class UserReportDetailRestServlet(RestServlet): |
| 94 | + """ |
| 95 | + Get a specific user report that is known to the homeserver. Results are returned |
| 96 | + in a dictionary containing report information. |
| 97 | + The requester must have administrator access in Synapse. |
| 98 | +
|
| 99 | + GET /_synapse/admin/v1/user_reports/<report_id> |
| 100 | + returns: |
| 101 | + 200 OK with details report if success otherwise an error. |
| 102 | +
|
| 103 | + Args: |
| 104 | + The parameter `report_id` is the ID of the user report in the database. |
| 105 | + Returns: |
| 106 | + JSON blob of information about the user report |
| 107 | + """ |
| 108 | + |
| 109 | + PATTERNS = admin_patterns("/user_reports/(?P<report_id>[^/]*)$") |
| 110 | + |
| 111 | + def __init__(self, hs: "HomeServer"): |
| 112 | + self._auth = hs.get_auth() |
| 113 | + self._store = hs.get_datastores().main |
| 114 | + |
| 115 | + async def on_GET( |
| 116 | + self, request: SynapseRequest, report_id: str |
| 117 | + ) -> tuple[int, JsonDict]: |
| 118 | + await assert_requester_is_admin(self._auth, request) |
| 119 | + |
| 120 | + message = ( |
| 121 | + "The report_id parameter must be a string representing a positive integer." |
| 122 | + ) |
| 123 | + try: |
| 124 | + resolved_report_id = int(report_id) |
| 125 | + except ValueError: |
| 126 | + raise SynapseError( |
| 127 | + HTTPStatus.BAD_REQUEST, message, errcode=Codes.INVALID_PARAM |
| 128 | + ) |
| 129 | + |
| 130 | + if resolved_report_id < 0: |
| 131 | + raise SynapseError( |
| 132 | + HTTPStatus.BAD_REQUEST, message, errcode=Codes.INVALID_PARAM |
| 133 | + ) |
| 134 | + |
| 135 | + ret = await self._store.get_user_report(resolved_report_id) |
| 136 | + if not ret: |
| 137 | + raise NotFoundError("User report not found") |
| 138 | + |
| 139 | + id, received_ts, target_user_id, user_id, reason = ret |
| 140 | + response = { |
| 141 | + "id": id, |
| 142 | + "received_ts": received_ts, |
| 143 | + "target_user_id": target_user_id, |
| 144 | + "user_id": user_id, |
| 145 | + "reason": reason, |
| 146 | + } |
| 147 | + |
| 148 | + return HTTPStatus.OK, response |
| 149 | + |
| 150 | + async def on_DELETE( |
| 151 | + self, request: SynapseRequest, report_id: str |
| 152 | + ) -> tuple[int, JsonDict]: |
| 153 | + await assert_requester_is_admin(self._auth, request) |
| 154 | + |
| 155 | + message = ( |
| 156 | + "The report_id parameter must be a string representing a positive integer." |
| 157 | + ) |
| 158 | + try: |
| 159 | + resolved_report_id = int(report_id) |
| 160 | + except ValueError: |
| 161 | + raise SynapseError( |
| 162 | + HTTPStatus.BAD_REQUEST, message, errcode=Codes.INVALID_PARAM |
| 163 | + ) |
| 164 | + |
| 165 | + if resolved_report_id < 0: |
| 166 | + raise SynapseError( |
| 167 | + HTTPStatus.BAD_REQUEST, message, errcode=Codes.INVALID_PARAM |
| 168 | + ) |
| 169 | + |
| 170 | + if await self._store.delete_user_report(resolved_report_id): |
| 171 | + return HTTPStatus.OK, {} |
| 172 | + |
| 173 | + raise NotFoundError("User report not found") |
0 commit comments