2018-08-22 05:57:54 -04: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]
|
2018-08-22 05:57:54 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import logging
|
2021-08-23 08:14:17 -04:00
|
|
|
from typing import TYPE_CHECKING, Tuple
|
2018-08-22 05:57:54 -04:00
|
|
|
|
2020-08-24 13:58:56 -04:00
|
|
|
from synapse.api.errors import Codes, ShadowBanError, SynapseError
|
2019-04-01 05:24:38 -04:00
|
|
|
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
|
2023-08-16 10:19:54 -04:00
|
|
|
from synapse.handlers.worker_lock import NEW_EVENT_DURING_PURGE_LOCK_NAME
|
2021-08-23 08:14:17 -04:00
|
|
|
from synapse.http.server import HttpServer
|
2018-08-22 05:57:54 -04:00
|
|
|
from synapse.http.servlet import (
|
|
|
|
RestServlet,
|
|
|
|
assert_params_in_dict,
|
|
|
|
parse_json_object_from_request,
|
|
|
|
)
|
2021-08-23 08:14:17 -04:00
|
|
|
from synapse.http.site import SynapseRequest
|
|
|
|
from synapse.types import JsonDict
|
2020-08-24 13:58:56 -04:00
|
|
|
from synapse.util import stringutils
|
2018-08-22 05:57:54 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
from ._base import client_patterns
|
2018-08-22 05:57:54 -04:00
|
|
|
|
2021-08-23 08:14:17 -04:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
2018-08-22 05:57:54 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class RoomUpgradeRestServlet(RestServlet):
|
2021-02-12 11:01:48 -05:00
|
|
|
"""Handler for room upgrade requests.
|
2018-10-26 12:10:30 -04:00
|
|
|
|
|
|
|
Handles requests of the form:
|
|
|
|
|
|
|
|
POST /_matrix/client/r0/rooms/$roomid/upgrade HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"new_version": "2",
|
|
|
|
}
|
|
|
|
|
|
|
|
Creates a new room and shuts down the old one. Returns the ID of the new room.
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns(
|
2018-08-22 05:57:54 -04:00
|
|
|
# /rooms/$roomid/upgrade
|
|
|
|
"/rooms/(?P<room_id>[^/]*)/upgrade$"
|
|
|
|
)
|
|
|
|
|
2021-08-23 08:14:17 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2018-08-22 05:57:54 -04:00
|
|
|
self._hs = hs
|
|
|
|
self._room_creation_handler = hs.get_room_creation_handler()
|
|
|
|
self._auth = hs.get_auth()
|
2023-07-31 05:58:03 -04:00
|
|
|
self._worker_lock_handler = hs.get_worker_locks_handler()
|
2018-08-22 05:57:54 -04:00
|
|
|
|
2021-08-23 08:14:17 -04:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self._auth.get_user_by_req(request)
|
2018-08-22 05:57:54 -04:00
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
|
|
|
assert_params_in_dict(content, ("new_version",))
|
|
|
|
|
2020-01-27 09:30:57 -05:00
|
|
|
new_version = KNOWN_ROOM_VERSIONS.get(content["new_version"])
|
|
|
|
if new_version is None:
|
2018-08-22 05:57:54 -04:00
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"Your homeserver does not support this room version",
|
|
|
|
Codes.UNSUPPORTED_ROOM_VERSION,
|
|
|
|
)
|
|
|
|
|
2020-08-24 13:58:56 -04:00
|
|
|
try:
|
2023-07-31 05:58:03 -04:00
|
|
|
async with self._worker_lock_handler.acquire_read_write_lock(
|
2023-08-16 10:19:54 -04:00
|
|
|
NEW_EVENT_DURING_PURGE_LOCK_NAME, room_id, write=False
|
2023-07-31 05:58:03 -04:00
|
|
|
):
|
|
|
|
new_room_id = await self._room_creation_handler.upgrade_room(
|
|
|
|
requester, room_id, new_version
|
|
|
|
)
|
2020-08-24 13:58:56 -04:00
|
|
|
except ShadowBanError:
|
|
|
|
# Generate a random room ID.
|
|
|
|
new_room_id = stringutils.random_string(18)
|
2018-08-22 05:57:54 -04:00
|
|
|
|
|
|
|
ret = {"replacement_room": new_room_id}
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, ret
|
2018-08-22 05:57:54 -04:00
|
|
|
|
|
|
|
|
2021-08-23 08:14:17 -04:00
|
|
|
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
|
2018-08-22 05:57:54 -04:00
|
|
|
RoomUpgradeRestServlet(hs).register(http_server)
|