2018-08-22 03:57:54 -06:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
|
|
|
#
|
|
|
|
# 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
|
2021-08-23 06:14:17 -06:00
|
|
|
from typing import TYPE_CHECKING, Tuple
|
2018-08-22 03:57:54 -06:00
|
|
|
|
2020-08-24 11:58:56 -06:00
|
|
|
from synapse.api.errors import Codes, ShadowBanError, SynapseError
|
2019-04-01 03:24:38 -06:00
|
|
|
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
|
2021-08-23 06:14:17 -06:00
|
|
|
from synapse.http.server import HttpServer
|
2018-08-22 03:57:54 -06:00
|
|
|
from synapse.http.servlet import (
|
|
|
|
RestServlet,
|
|
|
|
assert_params_in_dict,
|
|
|
|
parse_json_object_from_request,
|
|
|
|
)
|
2021-08-23 06:14:17 -06:00
|
|
|
from synapse.http.site import SynapseRequest
|
|
|
|
from synapse.types import JsonDict
|
2020-08-24 11:58:56 -06:00
|
|
|
from synapse.util import stringutils
|
2018-08-22 03:57:54 -06:00
|
|
|
|
2019-06-03 05:28:59 -06:00
|
|
|
from ._base import client_patterns
|
2018-08-22 03:57:54 -06:00
|
|
|
|
2021-08-23 06:14:17 -06:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
2018-08-22 03:57:54 -06:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class RoomUpgradeRestServlet(RestServlet):
|
2021-02-12 09:01:48 -07:00
|
|
|
"""Handler for room upgrade requests.
|
2018-10-26 10:10:30 -06: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 03:32:02 -06:00
|
|
|
|
2019-06-03 05:28:59 -06:00
|
|
|
PATTERNS = client_patterns(
|
2018-08-22 03:57:54 -06:00
|
|
|
# /rooms/$roomid/upgrade
|
|
|
|
"/rooms/(?P<room_id>[^/]*)/upgrade$"
|
|
|
|
)
|
|
|
|
|
2021-08-23 06:14:17 -06:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 07:56:44 -06:00
|
|
|
super().__init__()
|
2018-08-22 03:57:54 -06:00
|
|
|
self._hs = hs
|
|
|
|
self._room_creation_handler = hs.get_room_creation_handler()
|
|
|
|
self._auth = hs.get_auth()
|
|
|
|
|
2021-08-23 06:14:17 -06:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, room_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 09:46:37 -07:00
|
|
|
requester = await self._auth.get_user_by_req(request)
|
2018-08-22 03:57:54 -06:00
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
|
|
|
assert_params_in_dict(content, ("new_version",))
|
|
|
|
|
2020-01-27 07:30:57 -07:00
|
|
|
new_version = KNOWN_ROOM_VERSIONS.get(content["new_version"])
|
|
|
|
if new_version is None:
|
2018-08-22 03:57:54 -06:00
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"Your homeserver does not support this room version",
|
|
|
|
Codes.UNSUPPORTED_ROOM_VERSION,
|
|
|
|
)
|
|
|
|
|
2020-08-24 11:58:56 -06:00
|
|
|
try:
|
|
|
|
new_room_id = await self._room_creation_handler.upgrade_room(
|
|
|
|
requester, room_id, new_version
|
|
|
|
)
|
|
|
|
except ShadowBanError:
|
|
|
|
# Generate a random room ID.
|
|
|
|
new_room_id = stringutils.random_string(18)
|
2018-08-22 03:57:54 -06:00
|
|
|
|
|
|
|
ret = {"replacement_room": new_room_id}
|
|
|
|
|
2019-08-30 09:28:26 -06:00
|
|
|
return 200, ret
|
2018-08-22 03:57:54 -06:00
|
|
|
|
|
|
|
|
2021-08-23 06:14:17 -06:00
|
|
|
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
|
2018-08-22 03:57:54 -06:00
|
|
|
RoomUpgradeRestServlet(hs).register(http_server)
|