2020-09-22 13:15:04 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2020 Dirk Klimpel
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 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]
|
2020-09-22 13:15:04 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import logging
|
2021-11-29 17:19:45 -05:00
|
|
|
from http import HTTPStatus
|
2021-03-02 04:31:12 -05:00
|
|
|
from typing import TYPE_CHECKING, Tuple
|
2020-09-22 13:15:04 -04:00
|
|
|
|
2023-02-01 16:35:24 -05:00
|
|
|
from synapse.api.constants import Direction
|
2020-10-26 14:16:37 -04:00
|
|
|
from synapse.api.errors import Codes, NotFoundError, SynapseError
|
2023-02-01 16:35:24 -05:00
|
|
|
from synapse.http.servlet import RestServlet, parse_enum, parse_integer, parse_string
|
2021-03-02 04:31:12 -05:00
|
|
|
from synapse.http.site import SynapseRequest
|
2020-09-22 13:15:04 -04:00
|
|
|
from synapse.rest.admin._base import admin_patterns, assert_requester_is_admin
|
2021-03-02 04:31:12 -05:00
|
|
|
from synapse.types import JsonDict
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
2020-09-22 13:15:04 -04:00
|
|
|
|
|
|
|
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$")
|
|
|
|
|
2021-03-02 04:31:12 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2023-02-28 07:09:10 -05:00
|
|
|
self._auth = hs.get_auth()
|
|
|
|
self._store = hs.get_datastores().main
|
2020-09-22 13:15:04 -04:00
|
|
|
|
2021-03-02 04:31:12 -05:00
|
|
|
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2023-02-28 07:09:10 -05:00
|
|
|
await assert_requester_is_admin(self._auth, request)
|
2020-09-22 13:15:04 -04:00
|
|
|
|
|
|
|
start = parse_integer(request, "from", default=0)
|
|
|
|
limit = parse_integer(request, "limit", default=100)
|
2023-02-01 16:35:24 -05:00
|
|
|
direction = parse_enum(request, "dir", Direction, Direction.BACKWARDS)
|
2020-09-22 13:15:04 -04:00
|
|
|
user_id = parse_string(request, "user_id")
|
|
|
|
room_id = parse_string(request, "room_id")
|
|
|
|
|
|
|
|
if start < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2020-09-22 13:15:04 -04:00
|
|
|
"The start parameter must be a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
if limit < 0:
|
|
|
|
raise SynapseError(
|
2021-11-29 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2020-09-22 13:15:04 -04:00
|
|
|
"The limit parameter must be a positive integer.",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
2023-02-28 07:09:10 -05:00
|
|
|
event_reports, total = await self._store.get_event_reports_paginate(
|
2020-09-22 13:15:04 -04:00
|
|
|
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)
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, ret
|
2020-10-26 14:16:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
class EventReportDetailRestServlet(RestServlet):
|
|
|
|
"""
|
|
|
|
Get a specific reported event that is known to the homeserver. Results are returned
|
|
|
|
in a dictionary containing report information.
|
|
|
|
The requester must have administrator access in Synapse.
|
|
|
|
|
|
|
|
GET /_synapse/admin/v1/event_reports/<report_id>
|
|
|
|
returns:
|
|
|
|
200 OK with details report if success otherwise an error.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
The parameter `report_id` is the ID of the event report in the database.
|
|
|
|
Returns:
|
|
|
|
JSON blob of information about the event report
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = admin_patterns("/event_reports/(?P<report_id>[^/]*)$")
|
|
|
|
|
2021-03-02 04:31:12 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2023-02-28 07:09:10 -05:00
|
|
|
self._auth = hs.get_auth()
|
|
|
|
self._store = hs.get_datastores().main
|
2020-10-26 14:16:37 -04:00
|
|
|
|
2021-03-02 04:31:12 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, report_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2023-02-28 07:09:10 -05:00
|
|
|
await assert_requester_is_admin(self._auth, request)
|
2020-10-26 14:16:37 -04:00
|
|
|
|
|
|
|
message = (
|
|
|
|
"The report_id parameter must be a string representing a positive integer."
|
|
|
|
)
|
|
|
|
try:
|
2021-03-02 04:31:12 -05:00
|
|
|
resolved_report_id = int(report_id)
|
2020-10-26 14:16:37 -04:00
|
|
|
except ValueError:
|
2021-11-29 17:19:45 -05:00
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST, message, errcode=Codes.INVALID_PARAM
|
|
|
|
)
|
2020-10-26 14:16:37 -04:00
|
|
|
|
2021-03-02 04:31:12 -05:00
|
|
|
if resolved_report_id < 0:
|
2021-11-29 17:19:45 -05:00
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST, message, errcode=Codes.INVALID_PARAM
|
|
|
|
)
|
2020-10-26 14:16:37 -04:00
|
|
|
|
2023-02-28 07:09:10 -05:00
|
|
|
ret = await self._store.get_event_report(resolved_report_id)
|
2020-10-26 14:16:37 -04:00
|
|
|
if not ret:
|
|
|
|
raise NotFoundError("Event report not found")
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, ret
|
2023-02-28 07:09:10 -05:00
|
|
|
|
|
|
|
async def on_DELETE(
|
|
|
|
self, request: SynapseRequest, report_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
await assert_requester_is_admin(self._auth, request)
|
|
|
|
|
|
|
|
message = (
|
|
|
|
"The report_id parameter must be a string representing a positive integer."
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
resolved_report_id = int(report_id)
|
|
|
|
except ValueError:
|
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST, message, errcode=Codes.INVALID_PARAM
|
|
|
|
)
|
|
|
|
|
|
|
|
if resolved_report_id < 0:
|
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST, message, errcode=Codes.INVALID_PARAM
|
|
|
|
)
|
|
|
|
|
|
|
|
if await self._store.delete_event_report(resolved_report_id):
|
|
|
|
return HTTPStatus.OK, {}
|
|
|
|
|
|
|
|
raise NotFoundError("Event report not found")
|