This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Admin API for reported events (#8217)
Add an admin API to read entries of table `event_reports`. API: `GET /_synapse/admin/v1/event_reports`
- Loading branch information
Showing
6 changed files
with
697 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add an admin API `GET /_synapse/admin/v1/event_reports` to read entries of table `event_reports`. Contributed by @dklimpel. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
Show reported events | ||
==================== | ||
|
||
This API returns information about reported events. | ||
|
||
The api is:: | ||
|
||
GET /_synapse/admin/v1/event_reports?from=0&limit=10 | ||
|
||
To use it, you will need to authenticate by providing an ``access_token`` for a | ||
server admin: see `README.rst <README.rst>`_. | ||
|
||
It returns a JSON body like the following: | ||
|
||
.. code:: jsonc | ||
{ | ||
"event_reports": [ | ||
{ | ||
"content": { | ||
"reason": "foo", | ||
"score": -100 | ||
}, | ||
"event_id": "$bNUFCwGzWca1meCGkjp-zwslF-GfVcXukvRLI1_FaVY", | ||
"event_json": { | ||
"auth_events": [ | ||
"$YK4arsKKcc0LRoe700pS8DSjOvUT4NDv0HfInlMFw2M", | ||
"$oggsNXxzPFRE3y53SUNd7nsj69-QzKv03a1RucHu-ws" | ||
], | ||
"content": { | ||
"body": "matrix.org: This Week in Matrix", | ||
"format": "org.matrix.custom.html", | ||
"formatted_body": "<strong>matrix.org</strong>:<br><a href=\"https://matrix.org/blog/\"><strong>This Week in Matrix</strong></a>", | ||
"msgtype": "m.notice" | ||
}, | ||
"depth": 546, | ||
"hashes": { | ||
"sha256": "xK1//xnmvHJIOvbgXlkI8eEqdvoMmihVDJ9J4SNlsAw" | ||
}, | ||
"origin": "matrix.org", | ||
"origin_server_ts": 1592291711430, | ||
"prev_events": [ | ||
"$YK4arsKKcc0LRoe700pS8DSjOvUT4NDv0HfInlMFw2M" | ||
], | ||
"prev_state": [], | ||
"room_id": "!ERAgBpSOcCCuTJqQPk:matrix.org", | ||
"sender": "@foobar:matrix.org", | ||
"signatures": { | ||
"matrix.org": { | ||
"ed25519:a_JaEG": "cs+OUKW/iHx5pEidbWxh0UiNNHwe46Ai9LwNz+Ah16aWDNszVIe2gaAcVZfvNsBhakQTew51tlKmL2kspXk/Dg" | ||
} | ||
}, | ||
"type": "m.room.message", | ||
"unsigned": { | ||
"age_ts": 1592291711430, | ||
} | ||
}, | ||
"id": 2, | ||
"reason": "foo", | ||
"received_ts": 1570897107409, | ||
"room_alias": "#alias1:matrix.org", | ||
"room_id": "!ERAgBpSOcCCuTJqQPk:matrix.org", | ||
"sender": "@foobar:matrix.org", | ||
"user_id": "@foo:matrix.org" | ||
}, | ||
{ | ||
"content": { | ||
"reason": "bar", | ||
"score": -100 | ||
}, | ||
"event_id": "$3IcdZsDaN_En-S1DF4EMCy3v4gNRKeOJs8W5qTOKj4I", | ||
"event_json": { | ||
// hidden items | ||
// see above | ||
}, | ||
"id": 3, | ||
"reason": "bar", | ||
"received_ts": 1598889612059, | ||
"room_alias": "#alias2:matrix.org", | ||
"room_id": "!eGvUQuTCkHGVwNMOjv:matrix.org", | ||
"sender": "@foobar:matrix.org", | ||
"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``: string - Is optional and filters to only return users with user IDs that contain this value. | ||
This is the user who reported the event and wrote the reason. | ||
- ``room_id``: string - Is optional and filters to only return rooms with room IDs that contain this value. | ||
|
||
**Response** | ||
|
||
The following fields are returned in the JSON response body: | ||
|
||
- ``id``: integer - ID of event 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 in which the event being reported is located. | ||
- ``event_id``: string - The ID of the reported event. | ||
- ``user_id``: string - This is the user who reported the event and wrote the reason. | ||
- ``reason``: string - Comment made by the ``user_id`` in this report. May be blank. | ||
- ``content``: object - Content of reported event. | ||
|
||
- ``reason``: string - Comment made by the ``user_id`` in this report. May be blank. | ||
- ``score``: integer - Content is reported based upon a negative score, where -100 is "most offensive" and 0 is "inoffensive". | ||
|
||
- ``sender``: string - This is the ID of the user who sent the original message/event that was reported. | ||
- ``room_alias``: string - The alias of the room. ``null`` if the room does not have a canonical alias set. | ||
- ``event_json``: object - Details of the original event that was reported. | ||
- ``next_token``: integer - Indication for pagination. See above. | ||
- ``total``: integer - Total number of event reports related to the query (``user_id`` and ``room_id``). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2020 Dirk Klimpel | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
|
||
from synapse.api.errors import Codes, SynapseError | ||
from synapse.http.servlet import RestServlet, parse_integer, parse_string | ||
from synapse.rest.admin._base import admin_patterns, assert_requester_is_admin | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class EventReportsRestServlet(RestServlet): | ||
""" | ||
List all reported events 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/event_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 parameter `user_id` can be used to filter by user id. | ||
The parameter `room_id` can be used to filter by room id. | ||
Returns: | ||
A list of reported events and an integer representing the total number of | ||
reported events that exist given this query | ||
""" | ||
|
||
PATTERNS = admin_patterns("/event_reports$") | ||
|
||
def __init__(self, hs): | ||
self.hs = hs | ||
self.auth = hs.get_auth() | ||
self.store = hs.get_datastore() | ||
|
||
async def on_GET(self, request): | ||
await assert_requester_is_admin(self.auth, request) | ||
|
||
start = parse_integer(request, "from", default=0) | ||
limit = parse_integer(request, "limit", default=100) | ||
direction = parse_string(request, "dir", default="b") | ||
user_id = parse_string(request, "user_id") | ||
room_id = parse_string(request, "room_id") | ||
|
||
if start < 0: | ||
raise SynapseError( | ||
400, | ||
"The start parameter must be a positive integer.", | ||
errcode=Codes.INVALID_PARAM, | ||
) | ||
|
||
if limit < 0: | ||
raise SynapseError( | ||
400, | ||
"The limit parameter must be a positive integer.", | ||
errcode=Codes.INVALID_PARAM, | ||
) | ||
|
||
if direction not in ("f", "b"): | ||
raise SynapseError( | ||
400, "Unknown direction: %s" % (direction,), errcode=Codes.INVALID_PARAM | ||
) | ||
|
||
event_reports, total = await self.store.get_event_reports_paginate( | ||
start, limit, direction, user_id, room_id | ||
) | ||
ret = {"event_reports": event_reports, "total": total} | ||
if (start + limit) < total: | ||
ret["next_token"] = start + len(event_reports) | ||
|
||
return 200, ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.