2019-11-20 06:49:11 -05:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# 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]
|
2019-11-20 06:49:11 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
import logging
|
2020-07-14 07:36:23 -04:00
|
|
|
from http import HTTPStatus
|
2021-11-09 08:11:47 -05:00
|
|
|
from typing import TYPE_CHECKING, List, Optional, Tuple, cast
|
2021-01-28 10:53:40 -05:00
|
|
|
from urllib import parse as urlparse
|
2019-11-20 06:49:11 -05:00
|
|
|
|
2023-10-31 13:13:28 -04:00
|
|
|
import attr
|
|
|
|
|
2023-02-01 16:35:24 -05:00
|
|
|
from synapse.api.constants import Direction, EventTypes, JoinRules, Membership
|
2020-12-18 10:37:19 -05:00
|
|
|
from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
|
2021-01-28 10:53:40 -05:00
|
|
|
from synapse.api.filtering import Filter
|
2023-09-15 09:37:44 -04:00
|
|
|
from synapse.handlers.pagination import (
|
|
|
|
PURGE_ROOM_ACTION_NAME,
|
|
|
|
SHUTDOWN_AND_PURGE_ROOM_ACTION_NAME,
|
|
|
|
)
|
2019-11-20 06:49:11 -05:00
|
|
|
from synapse.http.servlet import (
|
2021-08-16 10:49:12 -04:00
|
|
|
ResolveRoomIdMixin,
|
2019-11-20 06:49:11 -05:00
|
|
|
RestServlet,
|
|
|
|
assert_params_in_dict,
|
2023-02-01 16:35:24 -05:00
|
|
|
parse_enum,
|
2020-01-22 08:36:43 -05:00
|
|
|
parse_integer,
|
2019-11-20 06:49:11 -05:00
|
|
|
parse_json_object_from_request,
|
2020-01-22 08:36:43 -05:00
|
|
|
parse_string,
|
2019-11-20 06:49:11 -05:00
|
|
|
)
|
2020-12-11 05:42:47 -05:00
|
|
|
from synapse.http.site import SynapseRequest
|
2019-11-20 06:49:11 -05:00
|
|
|
from synapse.rest.admin._base import (
|
2020-01-22 08:36:43 -05:00
|
|
|
admin_patterns,
|
2020-05-07 15:33:07 -04:00
|
|
|
assert_requester_is_admin,
|
2019-11-20 06:49:11 -05:00
|
|
|
assert_user_is_admin,
|
|
|
|
)
|
2020-08-05 16:38:57 -04:00
|
|
|
from synapse.storage.databases.main.room import RoomSortOrder
|
2022-09-07 05:54:44 -04:00
|
|
|
from synapse.streams.config import PaginationConfig
|
2023-09-15 09:37:44 -04:00
|
|
|
from synapse.types import JsonDict, RoomID, ScheduledTask, UserID, create_requester
|
2022-12-12 11:19:30 -05:00
|
|
|
from synapse.types.state import StateFilter
|
2021-01-28 10:53:40 -05:00
|
|
|
from synapse.util import json_decoder
|
2020-12-11 05:42:47 -05:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2021-04-29 05:31:45 -04:00
|
|
|
from synapse.api.auth import Auth
|
|
|
|
from synapse.handlers.pagination import PaginationHandler
|
|
|
|
from synapse.handlers.room import RoomShutdownHandler
|
2020-12-11 05:42:47 -05:00
|
|
|
from synapse.server import HomeServer
|
2019-11-20 06:49:11 -05:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-11-12 07:35:31 -05:00
|
|
|
class RoomRestV2Servlet(RestServlet):
|
|
|
|
"""Delete a room from server asynchronously with a background task.
|
|
|
|
|
|
|
|
It is a combination and improvement of shutdown and purge room.
|
|
|
|
|
|
|
|
Shuts down a room by removing all local users from the room.
|
|
|
|
Blocking all future invites and joins to the room is optional.
|
|
|
|
|
|
|
|
If desired any local aliases will be repointed to a new room
|
|
|
|
created by `new_room_user_id` and kicked users will be auto-
|
|
|
|
joined to the new room.
|
|
|
|
|
|
|
|
If 'purge' is true, it will remove all traces of a room from the database.
|
|
|
|
"""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/rooms/(?P<room_id>[^/]*)$", "v2")
|
2021-11-12 07:35:31 -05:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self._auth = hs.get_auth()
|
2022-02-23 06:04:02 -05:00
|
|
|
self._store = hs.get_datastores().main
|
2021-11-12 07:35:31 -05:00
|
|
|
self._pagination_handler = hs.get_pagination_handler()
|
2023-05-04 10:18:22 -04:00
|
|
|
self._third_party_rules = hs.get_module_api_callbacks().third_party_event_rules
|
2021-11-12 07:35:31 -05:00
|
|
|
|
|
|
|
async def on_DELETE(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
requester = await self._auth.get_user_by_req(request)
|
2022-08-22 09:17:59 -04:00
|
|
|
await assert_user_is_admin(self._auth, requester)
|
2021-11-12 07:35:31 -05:00
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
|
|
|
|
|
|
|
block = content.get("block", False)
|
|
|
|
if not isinstance(block, bool):
|
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
"Param 'block' must be a boolean, if given",
|
|
|
|
Codes.BAD_JSON,
|
|
|
|
)
|
|
|
|
|
|
|
|
purge = content.get("purge", True)
|
|
|
|
if not isinstance(purge, bool):
|
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
"Param 'purge' must be a boolean, if given",
|
|
|
|
Codes.BAD_JSON,
|
|
|
|
)
|
|
|
|
|
|
|
|
force_purge = content.get("force_purge", False)
|
|
|
|
if not isinstance(force_purge, bool):
|
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
"Param 'force_purge' must be a boolean, if given",
|
|
|
|
Codes.BAD_JSON,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not RoomID.is_valid(room_id):
|
2021-11-29 17:19:45 -05:00
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST, "%s is not a legal room ID" % (room_id,)
|
|
|
|
)
|
2021-11-12 07:35:31 -05:00
|
|
|
|
2022-03-09 13:23:57 -05:00
|
|
|
# Check this here, as otherwise we'll only fail after the background job has been started.
|
|
|
|
if not await self._third_party_rules.check_can_shutdown_room(
|
|
|
|
requester.user.to_string(), room_id
|
|
|
|
):
|
|
|
|
raise SynapseError(
|
|
|
|
403, "Shutdown of this room is forbidden", Codes.FORBIDDEN
|
|
|
|
)
|
|
|
|
|
2023-09-15 09:37:44 -04:00
|
|
|
delete_id = await self._pagination_handler.start_shutdown_and_purge_room(
|
2021-11-12 07:35:31 -05:00
|
|
|
room_id=room_id,
|
2023-09-15 09:37:44 -04:00
|
|
|
shutdown_params={
|
|
|
|
"new_room_user_id": content.get("new_room_user_id"),
|
|
|
|
"new_room_name": content.get("room_name"),
|
|
|
|
"message": content.get("message"),
|
|
|
|
"requester_user_id": requester.user.to_string(),
|
|
|
|
"block": block,
|
|
|
|
"purge": purge,
|
|
|
|
"force_purge": force_purge,
|
|
|
|
},
|
2021-11-12 07:35:31 -05:00
|
|
|
)
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {"delete_id": delete_id}
|
2021-11-12 07:35:31 -05:00
|
|
|
|
|
|
|
|
2023-09-15 09:37:44 -04:00
|
|
|
def _convert_delete_task_to_response(task: ScheduledTask) -> JsonDict:
|
|
|
|
return {
|
|
|
|
"delete_id": task.id,
|
|
|
|
"status": task.status,
|
|
|
|
"shutdown_room": task.result,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-12 07:35:31 -05:00
|
|
|
class DeleteRoomStatusByRoomIdRestServlet(RestServlet):
|
|
|
|
"""Get the status of the delete room background task."""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/rooms/(?P<room_id>[^/]*)/delete_status$", "v2")
|
2021-11-12 07:35:31 -05:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self._auth = hs.get_auth()
|
|
|
|
self._pagination_handler = hs.get_pagination_handler()
|
|
|
|
|
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
await assert_requester_is_admin(self._auth, request)
|
|
|
|
|
|
|
|
if not RoomID.is_valid(room_id):
|
2021-11-29 17:19:45 -05:00
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST, "%s is not a legal room ID" % (room_id,)
|
|
|
|
)
|
2021-11-12 07:35:31 -05:00
|
|
|
|
2023-09-15 09:37:44 -04:00
|
|
|
delete_tasks = await self._pagination_handler.get_delete_tasks_by_room(room_id)
|
2021-11-12 07:35:31 -05:00
|
|
|
|
2023-09-15 09:37:44 -04:00
|
|
|
if delete_tasks:
|
|
|
|
return HTTPStatus.OK, {
|
|
|
|
"results": [
|
|
|
|
_convert_delete_task_to_response(task) for task in delete_tasks
|
|
|
|
],
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
raise NotFoundError("No delete task for room_id '%s' found" % room_id)
|
2021-11-12 07:35:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteRoomStatusByDeleteIdRestServlet(RestServlet):
|
|
|
|
"""Get the status of the delete room background task."""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/rooms/delete_status/(?P<delete_id>[^/]*)$", "v2")
|
2021-11-12 07:35:31 -05:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self._auth = hs.get_auth()
|
|
|
|
self._pagination_handler = hs.get_pagination_handler()
|
|
|
|
|
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, delete_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
await assert_requester_is_admin(self._auth, request)
|
|
|
|
|
2023-09-15 09:37:44 -04:00
|
|
|
delete_task = await self._pagination_handler.get_delete_task(delete_id)
|
|
|
|
if delete_task is None or (
|
|
|
|
delete_task.action != PURGE_ROOM_ACTION_NAME
|
|
|
|
and delete_task.action != SHUTDOWN_AND_PURGE_ROOM_ACTION_NAME
|
|
|
|
):
|
2021-11-12 07:35:31 -05:00
|
|
|
raise NotFoundError("delete id '%s' not found" % delete_id)
|
|
|
|
|
2023-09-15 09:37:44 -04:00
|
|
|
return HTTPStatus.OK, _convert_delete_task_to_response(delete_task)
|
2021-11-12 07:35:31 -05:00
|
|
|
|
|
|
|
|
2020-01-22 08:36:43 -05:00
|
|
|
class ListRoomRestServlet(RestServlet):
|
|
|
|
"""
|
|
|
|
List all rooms that are known to the homeserver. Results are returned
|
|
|
|
in a dictionary containing room information. Supports pagination.
|
|
|
|
"""
|
|
|
|
|
2020-05-07 15:33:07 -04:00
|
|
|
PATTERNS = admin_patterns("/rooms$")
|
2020-01-22 08:36:43 -05:00
|
|
|
|
2020-12-11 05:42:47 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2020-01-22 08:36:43 -05:00
|
|
|
self.auth = hs.get_auth()
|
2020-10-09 07:24:34 -04:00
|
|
|
self.admin_handler = hs.get_admin_handler()
|
2020-01-22 08:36:43 -05:00
|
|
|
|
2020-12-11 05:42:47 -05:00
|
|
|
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2021-12-08 11:59:40 -05:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2020-01-22 08:36:43 -05:00
|
|
|
|
|
|
|
# Extract query parameters
|
|
|
|
start = parse_integer(request, "from", default=0)
|
|
|
|
limit = parse_integer(request, "limit", default=100)
|
2021-12-08 11:59:40 -05:00
|
|
|
order_by = parse_string(
|
|
|
|
request,
|
|
|
|
"order_by",
|
|
|
|
default=RoomSortOrder.NAME.value,
|
|
|
|
allowed_values=[sort_order.value for sort_order in RoomSortOrder],
|
|
|
|
)
|
2020-01-22 08:36:43 -05:00
|
|
|
|
2021-09-21 11:04:35 -04:00
|
|
|
search_term = parse_string(request, "search_term", encoding="utf-8")
|
2020-01-22 08:36:43 -05:00
|
|
|
if search_term == "":
|
|
|
|
raise SynapseError(
|
2021-11-29 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2020-01-22 08:36:43 -05:00
|
|
|
"search_term cannot be an empty string",
|
|
|
|
errcode=Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
2023-02-01 16:35:24 -05:00
|
|
|
direction = parse_enum(request, "dir", Direction, default=Direction.FORWARDS)
|
|
|
|
reverse_order = True if direction == Direction.BACKWARDS else False
|
2020-01-22 08:36:43 -05:00
|
|
|
|
|
|
|
# Return list of rooms according to parameters
|
|
|
|
rooms, total_rooms = await self.store.get_rooms_paginate(
|
|
|
|
start, limit, order_by, reverse_order, search_term
|
|
|
|
)
|
|
|
|
response = {
|
|
|
|
# next_token should be opaque, so return a value the client can parse
|
|
|
|
"offset": start,
|
|
|
|
"rooms": rooms,
|
|
|
|
"total_rooms": total_rooms,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Are there more rooms to paginate through after this?
|
|
|
|
if (start + limit) < total_rooms:
|
|
|
|
# There are. Calculate where the query should start from next time
|
|
|
|
# to get the next part of the list
|
|
|
|
response["next_batch"] = start + limit
|
|
|
|
|
|
|
|
# Is it possible to paginate backwards? Check if we currently have an
|
|
|
|
# offset
|
|
|
|
if start > 0:
|
|
|
|
if start > limit:
|
|
|
|
# Going back one iteration won't take us to the start.
|
|
|
|
# Calculate new offset
|
|
|
|
response["prev_batch"] = start - limit
|
|
|
|
else:
|
|
|
|
response["prev_batch"] = 0
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, response
|
2020-03-27 15:16:43 -04:00
|
|
|
|
|
|
|
|
2020-05-07 15:33:07 -04:00
|
|
|
class RoomRestServlet(RestServlet):
|
2021-04-29 05:31:45 -04:00
|
|
|
"""Manage a room.
|
|
|
|
|
|
|
|
On GET : Get details of a room.
|
|
|
|
|
|
|
|
On DELETE : Delete a room from server.
|
|
|
|
|
|
|
|
It is a combination and improvement of shutdown and purge room.
|
|
|
|
|
|
|
|
Shuts down a room by removing all local users from the room.
|
|
|
|
Blocking all future invites and joins to the room is optional.
|
|
|
|
|
|
|
|
If desired any local aliases will be repointed to a new room
|
|
|
|
created by `new_room_user_id` and kicked users will be auto-
|
|
|
|
joined to the new room.
|
|
|
|
|
|
|
|
If 'purge' is true, it will remove all traces of a room from the database.
|
2020-05-07 15:33:07 -04:00
|
|
|
|
|
|
|
TODO: Add on_POST to allow room creation without joining the room
|
|
|
|
"""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/rooms/(?P<room_id>[^/]*)$")
|
2020-05-07 15:33:07 -04:00
|
|
|
|
2020-12-11 05:42:47 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-05-07 15:33:07 -04:00
|
|
|
self.auth = hs.get_auth()
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2021-04-29 05:31:45 -04:00
|
|
|
self.room_shutdown_handler = hs.get_room_shutdown_handler()
|
|
|
|
self.pagination_handler = hs.get_pagination_handler()
|
2020-05-07 15:33:07 -04:00
|
|
|
|
2020-12-11 05:42:47 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-05-07 15:33:07 -04:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
|
|
|
|
|
|
|
ret = await self.store.get_room_with_stats(room_id)
|
|
|
|
if not ret:
|
|
|
|
raise NotFoundError("Room not found")
|
|
|
|
|
2020-12-11 05:42:47 -05:00
|
|
|
members = await self.store.get_users_in_room(room_id)
|
2023-10-31 13:13:28 -04:00
|
|
|
result = attr.asdict(ret)
|
|
|
|
result["joined_local_devices"] = await self.store.count_devices_by_users(
|
|
|
|
members
|
|
|
|
)
|
|
|
|
result["forgotten"] = await self.store.is_locally_forgotten_room(room_id)
|
2020-12-11 05:42:47 -05:00
|
|
|
|
2023-10-31 13:13:28 -04:00
|
|
|
return HTTPStatus.OK, result
|
2020-05-07 15:33:07 -04:00
|
|
|
|
2021-04-29 05:31:45 -04:00
|
|
|
async def on_DELETE(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-11-01 11:11:24 -04:00
|
|
|
return await self._delete_room(
|
2021-04-29 05:31:45 -04:00
|
|
|
request,
|
|
|
|
room_id,
|
|
|
|
self.auth,
|
|
|
|
self.room_shutdown_handler,
|
|
|
|
self.pagination_handler,
|
|
|
|
)
|
|
|
|
|
2021-11-01 11:11:24 -04:00
|
|
|
async def _delete_room(
|
|
|
|
self,
|
|
|
|
request: SynapseRequest,
|
|
|
|
room_id: str,
|
|
|
|
auth: "Auth",
|
|
|
|
room_shutdown_handler: "RoomShutdownHandler",
|
|
|
|
pagination_handler: "PaginationHandler",
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
requester = await auth.get_user_by_req(request)
|
2022-08-22 09:17:59 -04:00
|
|
|
await assert_user_is_admin(auth, requester)
|
2021-11-01 11:11:24 -04:00
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
|
|
|
|
|
|
|
block = content.get("block", False)
|
|
|
|
if not isinstance(block, bool):
|
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
"Param 'block' must be a boolean, if given",
|
|
|
|
Codes.BAD_JSON,
|
|
|
|
)
|
|
|
|
|
|
|
|
purge = content.get("purge", True)
|
|
|
|
if not isinstance(purge, bool):
|
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
"Param 'purge' must be a boolean, if given",
|
|
|
|
Codes.BAD_JSON,
|
|
|
|
)
|
|
|
|
|
|
|
|
force_purge = content.get("force_purge", False)
|
|
|
|
if not isinstance(force_purge, bool):
|
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
"Param 'force_purge' must be a boolean, if given",
|
|
|
|
Codes.BAD_JSON,
|
|
|
|
)
|
|
|
|
|
|
|
|
ret = await room_shutdown_handler.shutdown_room(
|
|
|
|
room_id=room_id,
|
2023-09-15 09:37:44 -04:00
|
|
|
params={
|
|
|
|
"new_room_user_id": content.get("new_room_user_id"),
|
|
|
|
"new_room_name": content.get("room_name"),
|
|
|
|
"message": content.get("message"),
|
|
|
|
"requester_user_id": requester.user.to_string(),
|
|
|
|
"block": block,
|
|
|
|
"purge": purge,
|
|
|
|
"force_purge": force_purge,
|
|
|
|
},
|
2021-11-01 11:11:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# Purge room
|
|
|
|
if purge:
|
2021-11-09 08:11:47 -05:00
|
|
|
try:
|
|
|
|
await pagination_handler.purge_room(room_id, force=force_purge)
|
|
|
|
except NotFoundError:
|
|
|
|
if block:
|
|
|
|
# We can block unknown rooms with this endpoint, in which case
|
|
|
|
# a failed purge is expected.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# But otherwise, we expect this purge to have succeeded.
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Cast safety: cast away the knowledge that this is a TypedDict.
|
|
|
|
# See https://github.com/python/mypy/issues/4976#issuecomment-579883622
|
|
|
|
# for some discussion on why this is necessary. Either way,
|
|
|
|
# `ret` is an opaque dictionary blob as far as the rest of the app cares.
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, cast(JsonDict, ret)
|
2021-11-01 11:11:24 -04:00
|
|
|
|
2020-05-07 15:33:07 -04:00
|
|
|
|
2020-07-16 16:43:23 -04:00
|
|
|
class RoomMembersRestServlet(RestServlet):
|
|
|
|
"""
|
|
|
|
Get members list of a room.
|
|
|
|
"""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/rooms/(?P<room_id>[^/]*)/members$")
|
2020-07-16 16:43:23 -04:00
|
|
|
|
2020-12-11 05:42:47 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-07-16 16:43:23 -04:00
|
|
|
self.auth = hs.get_auth()
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2020-07-16 16:43:23 -04:00
|
|
|
|
2020-12-11 05:42:47 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-07-16 16:43:23 -04:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
|
|
|
|
2023-11-09 11:13:31 -05:00
|
|
|
room = await self.store.get_room(room_id)
|
|
|
|
if not room:
|
2020-07-16 16:43:23 -04:00
|
|
|
raise NotFoundError("Room not found")
|
|
|
|
|
|
|
|
members = await self.store.get_users_in_room(room_id)
|
|
|
|
ret = {"members": members, "total": len(members)}
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, ret
|
2020-07-16 16:43:23 -04:00
|
|
|
|
|
|
|
|
2021-02-02 06:16:29 -05:00
|
|
|
class RoomStateRestServlet(RestServlet):
|
|
|
|
"""
|
|
|
|
Get full state within a room.
|
|
|
|
"""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/rooms/(?P<room_id>[^/]*)/state$")
|
2021-02-02 06:16:29 -05:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self.auth = hs.get_auth()
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2022-06-01 11:02:53 -04:00
|
|
|
self._storage_controllers = hs.get_storage_controllers()
|
2021-02-02 06:16:29 -05:00
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self._event_serializer = hs.get_event_client_serializer()
|
|
|
|
|
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-12-08 11:59:40 -05:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2021-02-02 06:16:29 -05:00
|
|
|
|
2023-11-09 11:13:31 -05:00
|
|
|
room = await self.store.get_room(room_id)
|
|
|
|
if not room:
|
2021-02-02 06:16:29 -05:00
|
|
|
raise NotFoundError("Room not found")
|
|
|
|
|
2022-06-01 11:02:53 -04:00
|
|
|
event_ids = await self._storage_controllers.state.get_current_state_ids(room_id)
|
2021-02-02 06:16:29 -05:00
|
|
|
events = await self.store.get_events(event_ids.values())
|
|
|
|
now = self.clock.time_msec()
|
2023-10-27 05:04:08 -04:00
|
|
|
room_state = await self._event_serializer.serialize_events(events.values(), now)
|
2021-02-02 06:16:29 -05:00
|
|
|
ret = {"state": room_state}
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, ret
|
2021-02-02 06:16:29 -05:00
|
|
|
|
|
|
|
|
2021-03-01 13:59:01 -05:00
|
|
|
class JoinRoomAliasServlet(ResolveRoomIdMixin, RestServlet):
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/join/(?P<room_identifier>[^/]*)$")
|
2020-03-27 15:16:43 -04:00
|
|
|
|
2020-12-11 05:42:47 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-03-01 13:59:01 -05:00
|
|
|
super().__init__(hs)
|
2020-03-27 15:16:43 -04:00
|
|
|
self.auth = hs.get_auth()
|
2020-10-09 07:24:34 -04:00
|
|
|
self.admin_handler = hs.get_admin_handler()
|
2022-06-06 04:24:12 -04:00
|
|
|
self.store = hs.get_datastores().main
|
|
|
|
self._storage_controllers = hs.get_storage_controllers()
|
2021-12-08 11:59:40 -05:00
|
|
|
self.is_mine = hs.is_mine
|
2020-03-27 15:16:43 -04:00
|
|
|
|
2020-12-11 05:42:47 -05:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, room_identifier: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-03-26 12:49:46 -04:00
|
|
|
# This will always be set by the time Twisted calls us.
|
|
|
|
assert request.args is not None
|
|
|
|
|
2020-03-27 15:16:43 -04:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2022-08-22 09:17:59 -04:00
|
|
|
await assert_user_is_admin(self.auth, requester)
|
2020-03-27 15:16:43 -04:00
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
|
|
|
|
|
|
|
assert_params_in_dict(content, ["user_id"])
|
|
|
|
target_user = UserID.from_string(content["user_id"])
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
if not self.is_mine(target_user):
|
2021-11-29 17:19:45 -05:00
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
"This endpoint can only be used with local users",
|
|
|
|
)
|
2020-03-27 15:16:43 -04:00
|
|
|
|
|
|
|
if not await self.admin_handler.get_user(target_user):
|
|
|
|
raise NotFoundError("User not found")
|
|
|
|
|
2021-03-01 13:59:01 -05:00
|
|
|
# Get the room ID from the identifier.
|
|
|
|
try:
|
2021-07-16 13:22:36 -04:00
|
|
|
remote_room_hosts: Optional[List[str]] = [
|
2021-03-01 13:59:01 -05:00
|
|
|
x.decode("ascii") for x in request.args[b"server_name"]
|
2021-07-16 13:22:36 -04:00
|
|
|
]
|
2021-03-01 13:59:01 -05:00
|
|
|
except Exception:
|
|
|
|
remote_room_hosts = None
|
|
|
|
room_id, remote_room_hosts = await self.resolve_room_id(
|
|
|
|
room_identifier, remote_room_hosts
|
|
|
|
)
|
2020-03-27 15:16:43 -04:00
|
|
|
|
2020-11-17 05:51:25 -05:00
|
|
|
fake_requester = create_requester(
|
|
|
|
target_user, authenticated_entity=requester.authenticated_entity
|
|
|
|
)
|
2020-03-27 15:16:43 -04:00
|
|
|
|
|
|
|
# send invite if room has "JoinRules.INVITE"
|
2022-06-06 04:24:12 -04:00
|
|
|
join_rules_event = (
|
|
|
|
await self._storage_controllers.state.get_current_state_event(
|
|
|
|
room_id, EventTypes.JoinRules, ""
|
|
|
|
)
|
|
|
|
)
|
2020-03-27 15:16:43 -04:00
|
|
|
if join_rules_event:
|
|
|
|
if not (join_rules_event.content.get("join_rule") == JoinRules.PUBLIC):
|
2020-08-20 15:07:42 -04:00
|
|
|
# update_membership with an action of "invite" can raise a
|
|
|
|
# ShadowBanError. This is not handled since it is assumed that
|
|
|
|
# an admin isn't going to call this API with a shadow-banned user.
|
2020-03-27 15:16:43 -04:00
|
|
|
await self.room_member_handler.update_membership(
|
|
|
|
requester=requester,
|
|
|
|
target=fake_requester.user,
|
|
|
|
room_id=room_id,
|
|
|
|
action="invite",
|
|
|
|
remote_room_hosts=remote_room_hosts,
|
|
|
|
ratelimit=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
await self.room_member_handler.update_membership(
|
|
|
|
requester=fake_requester,
|
|
|
|
target=fake_requester.user,
|
|
|
|
room_id=room_id,
|
|
|
|
action="join",
|
|
|
|
remote_room_hosts=remote_room_hosts,
|
|
|
|
ratelimit=False,
|
|
|
|
)
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {"room_id": room_id}
|
2020-12-18 10:37:19 -05:00
|
|
|
|
|
|
|
|
2021-03-01 13:59:01 -05:00
|
|
|
class MakeRoomAdminRestServlet(ResolveRoomIdMixin, RestServlet):
|
2020-12-18 10:37:19 -05:00
|
|
|
"""Allows a server admin to get power in a room if a local user has power in
|
|
|
|
a room. Will also invite the user if they're not in the room and it's a
|
|
|
|
private room. Can specify another user (rather than the admin user) to be
|
|
|
|
granted power, e.g.:
|
|
|
|
|
|
|
|
POST/_synapse/admin/v1/rooms/<room_id_or_alias>/make_room_admin
|
|
|
|
{
|
|
|
|
"user_id": "@foo:example.com"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/rooms/(?P<room_identifier>[^/]*)/make_room_admin$")
|
2020-12-18 10:37:19 -05:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-03-01 13:59:01 -05:00
|
|
|
super().__init__(hs)
|
2020-12-18 10:37:19 -05:00
|
|
|
self.auth = hs.get_auth()
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2022-06-06 04:24:12 -04:00
|
|
|
self._state_storage_controller = hs.get_storage_controllers().state
|
2020-12-18 10:37:19 -05:00
|
|
|
self.event_creation_handler = hs.get_event_creation_handler()
|
|
|
|
self.state_handler = hs.get_state_handler()
|
|
|
|
self.is_mine_id = hs.is_mine_id
|
|
|
|
|
2021-03-01 13:59:01 -05:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, room_identifier: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2020-12-18 10:37:19 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2022-08-22 09:17:59 -04:00
|
|
|
await assert_user_is_admin(self.auth, requester)
|
2020-12-18 10:37:19 -05:00
|
|
|
content = parse_json_object_from_request(request, allow_empty_body=True)
|
|
|
|
|
2021-03-01 13:59:01 -05:00
|
|
|
room_id, _ = await self.resolve_room_id(room_identifier)
|
2020-12-18 10:37:19 -05:00
|
|
|
|
|
|
|
# Which user to grant room admin rights to.
|
|
|
|
user_to_add = content.get("user_id", requester.user.to_string())
|
|
|
|
|
|
|
|
# Figure out which local users currently have power in the room, if any.
|
2022-06-06 04:24:12 -04:00
|
|
|
filtered_room_state = await self._state_storage_controller.get_current_state(
|
|
|
|
room_id,
|
|
|
|
StateFilter.from_types(
|
|
|
|
[
|
|
|
|
(EventTypes.Create, ""),
|
|
|
|
(EventTypes.PowerLevels, ""),
|
|
|
|
(EventTypes.JoinRules, ""),
|
|
|
|
(EventTypes.Member, user_to_add),
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if not filtered_room_state:
|
2021-11-29 17:19:45 -05:00
|
|
|
raise SynapseError(HTTPStatus.BAD_REQUEST, "Server not in room")
|
2020-12-18 10:37:19 -05:00
|
|
|
|
2022-06-06 04:24:12 -04:00
|
|
|
create_event = filtered_room_state[(EventTypes.Create, "")]
|
|
|
|
power_levels = filtered_room_state.get((EventTypes.PowerLevels, ""))
|
2020-12-18 10:37:19 -05:00
|
|
|
|
|
|
|
if power_levels is not None:
|
|
|
|
# We pick the local user with the highest power.
|
|
|
|
user_power = power_levels.content.get("users", {})
|
|
|
|
admin_users = [
|
|
|
|
user_id for user_id in user_power if self.is_mine_id(user_id)
|
|
|
|
]
|
|
|
|
admin_users.sort(key=lambda user: user_power[user])
|
|
|
|
|
|
|
|
if not admin_users:
|
2021-11-29 17:19:45 -05:00
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST, "No local admin user in room"
|
|
|
|
)
|
2020-12-18 10:37:19 -05:00
|
|
|
|
2021-01-27 12:38:08 -05:00
|
|
|
admin_user_id = None
|
|
|
|
|
|
|
|
for admin_user in reversed(admin_users):
|
2021-07-13 06:53:45 -04:00
|
|
|
(
|
|
|
|
current_membership_type,
|
|
|
|
_,
|
|
|
|
) = await self.store.get_local_current_membership_for_user_in_room(
|
|
|
|
admin_user, room_id
|
|
|
|
)
|
|
|
|
if current_membership_type == "join":
|
2021-01-27 12:38:08 -05:00
|
|
|
admin_user_id = admin_user
|
|
|
|
break
|
|
|
|
|
|
|
|
if not admin_user_id:
|
|
|
|
raise SynapseError(
|
2021-11-29 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2021-01-27 12:38:08 -05:00
|
|
|
"No local admin user in room",
|
|
|
|
)
|
2020-12-18 10:37:19 -05:00
|
|
|
|
|
|
|
pl_content = power_levels.content
|
|
|
|
else:
|
|
|
|
# If there is no power level events then the creator has rights.
|
|
|
|
pl_content = {}
|
|
|
|
admin_user_id = create_event.sender
|
|
|
|
if not self.is_mine_id(admin_user_id):
|
|
|
|
raise SynapseError(
|
2021-11-29 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
2020-12-18 10:37:19 -05:00
|
|
|
"No local admin user in room",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Grant the user power equal to the room admin by attempting to send an
|
|
|
|
# updated power level event.
|
|
|
|
new_pl_content = dict(pl_content)
|
|
|
|
new_pl_content["users"] = dict(pl_content.get("users", {}))
|
|
|
|
new_pl_content["users"][user_to_add] = new_pl_content["users"][admin_user_id]
|
|
|
|
|
|
|
|
fake_requester = create_requester(
|
|
|
|
admin_user_id,
|
|
|
|
authenticated_entity=requester.authenticated_entity,
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await self.event_creation_handler.create_and_send_nonmember_event(
|
|
|
|
fake_requester,
|
|
|
|
event_dict={
|
|
|
|
"content": new_pl_content,
|
|
|
|
"sender": admin_user_id,
|
|
|
|
"type": EventTypes.PowerLevels,
|
|
|
|
"state_key": "",
|
|
|
|
"room_id": room_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
except AuthError:
|
|
|
|
# The admin user we found turned out not to have enough power.
|
|
|
|
raise SynapseError(
|
2021-11-29 17:19:45 -05:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
"No local admin user in room with power to update power levels.",
|
2020-12-18 10:37:19 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
# Now we check if the user we're granting admin rights to is already in
|
|
|
|
# the room. If not and it's not a public room we invite them.
|
2022-06-06 04:24:12 -04:00
|
|
|
member_event = filtered_room_state.get((EventTypes.Member, user_to_add))
|
2020-12-18 10:37:19 -05:00
|
|
|
is_joined = False
|
|
|
|
if member_event:
|
|
|
|
is_joined = member_event.content["membership"] in (
|
|
|
|
Membership.JOIN,
|
|
|
|
Membership.INVITE,
|
|
|
|
)
|
|
|
|
|
|
|
|
if is_joined:
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {}
|
2020-12-18 10:37:19 -05:00
|
|
|
|
2022-06-06 04:24:12 -04:00
|
|
|
join_rules = filtered_room_state.get((EventTypes.JoinRules, ""))
|
2020-12-18 10:37:19 -05:00
|
|
|
is_public = False
|
|
|
|
if join_rules:
|
|
|
|
is_public = join_rules.content.get("join_rule") == JoinRules.PUBLIC
|
|
|
|
|
|
|
|
if is_public:
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {}
|
2020-12-18 10:37:19 -05:00
|
|
|
|
|
|
|
await self.room_member_handler.update_membership(
|
|
|
|
fake_requester,
|
|
|
|
target=UserID.from_string(user_to_add),
|
|
|
|
room_id=room_id,
|
|
|
|
action=Membership.INVITE,
|
|
|
|
)
|
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {}
|
2021-01-07 16:01:59 -05:00
|
|
|
|
|
|
|
|
2021-03-01 13:59:01 -05:00
|
|
|
class ForwardExtremitiesRestServlet(ResolveRoomIdMixin, RestServlet):
|
2021-01-07 16:01:59 -05:00
|
|
|
"""Allows a server admin to get or clear forward extremities.
|
|
|
|
|
|
|
|
Clearing does not require restarting the server.
|
|
|
|
|
|
|
|
Clear forward extremities:
|
|
|
|
DELETE /_synapse/admin/v1/rooms/<room_id_or_alias>/forward_extremities
|
|
|
|
|
|
|
|
Get forward_extremities:
|
|
|
|
GET /_synapse/admin/v1/rooms/<room_id_or_alias>/forward_extremities
|
|
|
|
"""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/rooms/(?P<room_identifier>[^/]*)/forward_extremities$")
|
2021-01-07 16:01:59 -05:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-03-01 13:59:01 -05:00
|
|
|
super().__init__(hs)
|
2021-01-07 16:01:59 -05:00
|
|
|
self.auth = hs.get_auth()
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2021-01-07 16:01:59 -05:00
|
|
|
|
2021-03-01 13:59:01 -05:00
|
|
|
async def on_DELETE(
|
|
|
|
self, request: SynapseRequest, room_identifier: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-12-08 11:59:40 -05:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2021-01-07 16:01:59 -05:00
|
|
|
|
2021-03-01 13:59:01 -05:00
|
|
|
room_id, _ = await self.resolve_room_id(room_identifier)
|
2021-01-07 16:01:59 -05:00
|
|
|
|
2021-01-07 17:12:23 -05:00
|
|
|
deleted_count = await self.store.delete_forward_extremities_for_room(room_id)
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, {"deleted": deleted_count}
|
2021-01-07 17:12:23 -05:00
|
|
|
|
2021-03-01 13:59:01 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, room_identifier: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-12-08 11:59:40 -05:00
|
|
|
await assert_requester_is_admin(self.auth, request)
|
2021-01-07 16:01:59 -05:00
|
|
|
|
2021-03-01 13:59:01 -05:00
|
|
|
room_id, _ = await self.resolve_room_id(room_identifier)
|
2021-01-07 16:01:59 -05:00
|
|
|
|
|
|
|
extremities = await self.store.get_forward_extremities_for_room(room_id)
|
2023-10-26 15:12:28 -04:00
|
|
|
result = [
|
|
|
|
{
|
|
|
|
"event_id": ex[0],
|
|
|
|
"state_group": ex[1],
|
|
|
|
"depth": ex[2],
|
|
|
|
"received_ts": ex[3],
|
|
|
|
}
|
|
|
|
for ex in extremities
|
|
|
|
]
|
|
|
|
|
|
|
|
return HTTPStatus.OK, {"count": len(extremities), "results": result}
|
2021-01-18 09:02:22 -05:00
|
|
|
|
2021-01-28 10:53:40 -05:00
|
|
|
|
2021-01-18 09:02:22 -05:00
|
|
|
class RoomEventContextServlet(RestServlet):
|
2021-01-28 06:19:39 -05:00
|
|
|
"""
|
|
|
|
Provide the context for an event.
|
|
|
|
This API is designed to be used when system administrators wish to look at
|
|
|
|
an abuse report and understand what happened during and immediately prior
|
|
|
|
to this event.
|
|
|
|
"""
|
2021-01-28 10:53:40 -05:00
|
|
|
|
2021-01-18 09:02:22 -05:00
|
|
|
PATTERNS = admin_patterns("/rooms/(?P<room_id>[^/]*)/context/(?P<event_id>[^/]*)$")
|
|
|
|
|
2021-03-01 13:59:01 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-01-18 09:02:22 -05:00
|
|
|
super().__init__()
|
2021-11-09 08:10:58 -05:00
|
|
|
self._hs = hs
|
2021-01-18 09:02:22 -05:00
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.room_context_handler = hs.get_room_context_handler()
|
|
|
|
self._event_serializer = hs.get_event_client_serializer()
|
|
|
|
self.auth = hs.get_auth()
|
|
|
|
|
2021-03-01 13:59:01 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, room_id: str, event_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2021-01-28 06:18:07 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request, allow_guest=False)
|
2022-08-22 09:17:59 -04:00
|
|
|
await assert_user_is_admin(self.auth, requester)
|
2021-01-18 09:02:22 -05:00
|
|
|
|
|
|
|
limit = parse_integer(request, "limit", default=10)
|
|
|
|
|
|
|
|
# picking the API shape for symmetry with /messages
|
2021-06-08 08:30:48 -04:00
|
|
|
filter_str = parse_string(request, "filter", encoding="utf-8")
|
2021-01-18 09:02:22 -05:00
|
|
|
if filter_str:
|
|
|
|
filter_json = urlparse.unquote(filter_str)
|
2021-11-09 08:10:58 -05:00
|
|
|
event_filter: Optional[Filter] = Filter(
|
|
|
|
self._hs, json_decoder.decode(filter_json)
|
|
|
|
)
|
2021-01-18 09:02:22 -05:00
|
|
|
else:
|
|
|
|
event_filter = None
|
|
|
|
|
2022-01-26 08:27:04 -05:00
|
|
|
event_context = await self.room_context_handler.get_event_context(
|
2021-01-28 06:27:30 -05:00
|
|
|
requester,
|
2021-01-18 09:02:22 -05:00
|
|
|
room_id,
|
|
|
|
event_id,
|
|
|
|
limit,
|
|
|
|
event_filter,
|
|
|
|
use_admin_priviledge=True,
|
|
|
|
)
|
|
|
|
|
2022-01-26 08:27:04 -05:00
|
|
|
if not event_context:
|
2021-11-29 17:19:45 -05:00
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.NOT_FOUND, "Event not found.", errcode=Codes.NOT_FOUND
|
|
|
|
)
|
2021-01-18 09:02:22 -05:00
|
|
|
|
|
|
|
time_now = self.clock.time_msec()
|
2022-01-26 08:27:04 -05:00
|
|
|
results = {
|
2023-10-27 05:04:08 -04:00
|
|
|
"events_before": await self._event_serializer.serialize_events(
|
2022-01-26 08:27:04 -05:00
|
|
|
event_context.events_before,
|
|
|
|
time_now,
|
|
|
|
bundle_aggregations=event_context.aggregations,
|
|
|
|
),
|
2023-10-27 05:04:08 -04:00
|
|
|
"event": await self._event_serializer.serialize_event(
|
2022-01-26 08:27:04 -05:00
|
|
|
event_context.event,
|
|
|
|
time_now,
|
|
|
|
bundle_aggregations=event_context.aggregations,
|
|
|
|
),
|
2023-10-27 05:04:08 -04:00
|
|
|
"events_after": await self._event_serializer.serialize_events(
|
2022-01-26 08:27:04 -05:00
|
|
|
event_context.events_after,
|
|
|
|
time_now,
|
|
|
|
bundle_aggregations=event_context.aggregations,
|
|
|
|
),
|
2023-10-27 05:04:08 -04:00
|
|
|
"state": await self._event_serializer.serialize_events(
|
2022-01-26 08:27:04 -05:00
|
|
|
event_context.state, time_now
|
|
|
|
),
|
|
|
|
"start": event_context.start,
|
|
|
|
"end": event_context.end,
|
|
|
|
}
|
2021-01-18 09:02:22 -05:00
|
|
|
|
2021-11-29 17:19:45 -05:00
|
|
|
return HTTPStatus.OK, results
|
2021-11-18 12:43:49 -05:00
|
|
|
|
|
|
|
|
|
|
|
class BlockRoomRestServlet(RestServlet):
|
|
|
|
"""
|
|
|
|
Manage blocking of rooms.
|
|
|
|
On PUT: Add or remove a room from blocking list.
|
|
|
|
On GET: Get blocking status of room and user who has blocked this room.
|
|
|
|
"""
|
|
|
|
|
2021-12-08 11:59:40 -05:00
|
|
|
PATTERNS = admin_patterns("/rooms/(?P<room_id>[^/]*)/block$")
|
2021-11-18 12:43:49 -05:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self._auth = hs.get_auth()
|
2022-02-23 06:04:02 -05:00
|
|
|
self._store = hs.get_datastores().main
|
2021-11-18 12:43:49 -05:00
|
|
|
|
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
await assert_requester_is_admin(self._auth, request)
|
|
|
|
|
|
|
|
if not RoomID.is_valid(room_id):
|
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST, "%s is not a legal room ID" % (room_id,)
|
|
|
|
)
|
|
|
|
|
|
|
|
blocked_by = await self._store.room_is_blocked_by(room_id)
|
|
|
|
# Test `not None` if `user_id` is an empty string
|
|
|
|
# if someone add manually an entry in database
|
|
|
|
if blocked_by is not None:
|
|
|
|
response = {"block": True, "user_id": blocked_by}
|
|
|
|
else:
|
|
|
|
response = {"block": False}
|
|
|
|
|
|
|
|
return HTTPStatus.OK, response
|
|
|
|
|
|
|
|
async def on_PUT(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
requester = await self._auth.get_user_by_req(request)
|
2022-08-22 09:17:59 -04:00
|
|
|
await assert_user_is_admin(self._auth, requester)
|
2021-11-18 12:43:49 -05:00
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
|
|
|
|
|
|
|
if not RoomID.is_valid(room_id):
|
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST, "%s is not a legal room ID" % (room_id,)
|
|
|
|
)
|
|
|
|
|
|
|
|
assert_params_in_dict(content, ["block"])
|
|
|
|
block = content.get("block")
|
|
|
|
if not isinstance(block, bool):
|
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
"Param 'block' must be a boolean.",
|
|
|
|
Codes.BAD_JSON,
|
|
|
|
)
|
|
|
|
|
|
|
|
if block:
|
|
|
|
await self._store.block_room(room_id, requester.user.to_string())
|
|
|
|
else:
|
|
|
|
await self._store.unblock_room(room_id)
|
|
|
|
|
|
|
|
return HTTPStatus.OK, {"block": block}
|
2022-09-07 05:54:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
class RoomMessagesRestServlet(RestServlet):
|
|
|
|
"""
|
|
|
|
Get messages list of a room.
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = admin_patterns("/rooms/(?P<room_id>[^/]*)/messages$")
|
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self._hs = hs
|
|
|
|
self._clock = hs.get_clock()
|
|
|
|
self._pagination_handler = hs.get_pagination_handler()
|
|
|
|
self._auth = hs.get_auth()
|
|
|
|
self._store = hs.get_datastores().main
|
|
|
|
|
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
requester = await self._auth.get_user_by_req(request)
|
|
|
|
await assert_user_is_admin(self._auth, requester)
|
|
|
|
|
|
|
|
pagination_config = await PaginationConfig.from_request(
|
|
|
|
self._store, request, default_limit=10
|
|
|
|
)
|
|
|
|
# Twisted will have processed the args by now.
|
|
|
|
assert request.args is not None
|
|
|
|
as_client_event = b"raw" not in request.args
|
|
|
|
filter_str = parse_string(request, "filter", encoding="utf-8")
|
|
|
|
if filter_str:
|
|
|
|
filter_json = urlparse.unquote(filter_str)
|
|
|
|
event_filter: Optional[Filter] = Filter(
|
|
|
|
self._hs, json_decoder.decode(filter_json)
|
|
|
|
)
|
|
|
|
if (
|
|
|
|
event_filter
|
|
|
|
and event_filter.filter_json.get("event_format", "client")
|
|
|
|
== "federation"
|
|
|
|
):
|
|
|
|
as_client_event = False
|
|
|
|
else:
|
|
|
|
event_filter = None
|
|
|
|
|
|
|
|
msgs = await self._pagination_handler.get_messages(
|
|
|
|
room_id=room_id,
|
|
|
|
requester=requester,
|
|
|
|
pagin_config=pagination_config,
|
|
|
|
as_client_event=as_client_event,
|
|
|
|
event_filter=event_filter,
|
|
|
|
use_admin_priviledge=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
return HTTPStatus.OK, msgs
|
|
|
|
|
|
|
|
|
|
|
|
class RoomTimestampToEventRestServlet(RestServlet):
|
|
|
|
"""
|
|
|
|
API endpoint to fetch the `event_id` of the closest event to the given
|
|
|
|
timestamp (`ts` query parameter) in the given direction (`dir` query
|
|
|
|
parameter).
|
|
|
|
|
|
|
|
Useful for cases like jump to date so you can start paginating messages from
|
|
|
|
a given date in the archive.
|
|
|
|
|
|
|
|
`ts` is a timestamp in milliseconds where we will find the closest event in
|
|
|
|
the given direction.
|
|
|
|
|
|
|
|
`dir` can be `f` or `b` to indicate forwards and backwards in time from the
|
|
|
|
given timestamp.
|
|
|
|
|
|
|
|
GET /_synapse/admin/v1/rooms/<roomID>/timestamp_to_event?ts=<timestamp>&dir=<direction>
|
|
|
|
{
|
|
|
|
"event_id": ...
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
PATTERNS = admin_patterns("/rooms/(?P<room_id>[^/]*)/timestamp_to_event$")
|
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self._auth = hs.get_auth()
|
|
|
|
self._store = hs.get_datastores().main
|
|
|
|
self._timestamp_lookup_handler = hs.get_timestamp_lookup_handler()
|
|
|
|
|
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
|
|
|
requester = await self._auth.get_user_by_req(request)
|
|
|
|
await assert_user_is_admin(self._auth, requester)
|
|
|
|
|
|
|
|
timestamp = parse_integer(request, "ts", required=True)
|
2023-02-01 16:35:24 -05:00
|
|
|
direction = parse_enum(request, "dir", Direction, default=Direction.FORWARDS)
|
2022-09-07 05:54:44 -04:00
|
|
|
|
|
|
|
(
|
|
|
|
event_id,
|
|
|
|
origin_server_ts,
|
|
|
|
) = await self._timestamp_lookup_handler.get_event_for_timestamp(
|
|
|
|
requester, room_id, timestamp, direction
|
|
|
|
)
|
|
|
|
|
|
|
|
return HTTPStatus.OK, {
|
|
|
|
"event_id": event_id,
|
|
|
|
"origin_server_ts": origin_server_ts,
|
|
|
|
}
|