Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add delete room admin endpoint #7613

Merged
merged 15 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion changelog.d/7613.feature
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add delete room admin endpoint (`POST /_synapse/admin/v1/rooms/<room_id>/delete`).
Add delete room admin endpoint (`POST /_synapse/admin/v1/rooms/<room_id>/delete`). Contributed by @dklimpel.
4 changes: 3 additions & 1 deletion docs/admin_api/rooms.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ The following JSON body parameters are available:
* `new_room_user_id` - Optional. If set, a new room will be created with this user ID
as the creator and admin, and all users in the old room will be moved into that
room. If not set, no new room will be created and the users will just be removed
from the old room.
from the old room. The user ID must be from local server but it does not have to exist.
richvdh marked this conversation as resolved.
Show resolved Hide resolved
* `room_name` - Optional. A string representing the name of the room that new users will be
invited to. Defaults to `Content Violation Notification`
* `message` - Optional. A string containing the first message that will be sent as
Expand All @@ -400,6 +400,8 @@ The following JSON body parameters are available:
* `block` - Optional. If set to `true`, this room will be added to a blocking list, preventing future attempts to
join the room. Defaults to `false`.

The JSON body must not be empty. The body must be at least `{}`.

## Response

The following fields are returned in the JSON response body:
Expand Down
11 changes: 4 additions & 7 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ async def shutdown_room(
If set to `true`, this room will be added to a blocking list,
preventing future attempts to join the room. Defaults to `false`.

Returns:
Returns: a dict containing the following keys:
kicked_users: An array of users (`user_id`) that were kicked.
failed_to_kick_users:
An array of users (`user_id`) that that were not kicked.
Expand All @@ -1173,29 +1173,26 @@ async def shutdown_room(
if not RoomID.is_valid(room_id):
raise SynapseError(400, "%s is not a legal room ID" % (room_id,))

if not await self.store.get_room_with_stats(room_id):
if not await self.store.get_room(room_id):
raise NotFoundError("Unknown room id %s" % (room_id,))

# This will work even if the room is already blocked, but that is
# desirable in case the first attempt at blocking the room failed below.
if block:
await self.store.block_room(room_id, requester_user_id)

if new_room_user_id:
if new_room_user_id is not None:
if not self.hs.is_mine_id(new_room_user_id):
raise SynapseError(
400, "User must be our own: %s" % (new_room_user_id,)
)

if not await self.store.get_user_by_id(new_room_user_id):
raise NotFoundError("Unknown user %s" % (new_room_user_id,))

room_creator_requester = create_requester(new_room_user_id)

info, stream_id = await self._room_creation_handler.create_room(
room_creator_requester,
config={
"preset": "public_chat",
"preset": RoomCreationPreset.PUBLIC_CHAT,
"name": new_room_name,
"power_level_content_override": {"users_default": -10},
},
Expand Down
4 changes: 2 additions & 2 deletions synapse/rest/admin/rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def on_POST(self, request, room_id):

ret = await self.room_shutdown_handler.shutdown_room(
room_id=room_id,
new_room_user_id=content.get("new_room_user_id"),
new_room_user_id=content["new_room_user_id"],
new_room_name=content.get("room_name"),
message=content.get("message"),
requester_user_id=requester.user.to_string(),
Expand Down Expand Up @@ -93,7 +93,7 @@ async def on_POST(self, request, room_id):
requester = await self.auth.get_user_by_req(request)
await assert_user_is_admin(self.auth, requester.user)

content = parse_json_object_from_request(request, allow_empty_body=True)
content = parse_json_object_from_request(request)

block = content.get("block", False)
if not isinstance(block, bool):
Expand Down
15 changes: 9 additions & 6 deletions tests/rest/admin/test_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def test_requester_is_no_admin(self):
"""

request, channel = self.make_request(
"POST", self.url, access_token=self.other_user_tok,
"POST", self.url, json.dumps({}), access_token=self.other_user_tok,
)
self.render(request)

Expand All @@ -204,7 +204,7 @@ def test_room_does_not_exist(self):
url = "/_synapse/admin/v1/rooms/!unknown:test/delete"

request, channel = self.make_request(
"POST", url, access_token=self.admin_user_tok,
"POST", url, json.dumps({}), access_token=self.admin_user_tok,
)
self.render(request)

Expand All @@ -218,7 +218,7 @@ def test_room_is_not_valid(self):
url = "/_synapse/admin/v1/rooms/invalidroom/delete"

request, channel = self.make_request(
"POST", url, access_token=self.admin_user_tok,
"POST", url, json.dumps({}), access_token=self.admin_user_tok,
)
self.render(request)

Expand All @@ -229,7 +229,7 @@ def test_room_is_not_valid(self):

def test_new_room_user_does_not_exist(self):
"""
Tests that a lookup for a user that does not exist returns a 404
Tests that the user ID must be from local server but it does not have to exist.
"""
body = json.dumps({"new_room_user_id": "@unknown:test"})

Expand All @@ -241,8 +241,11 @@ def test_new_room_user_does_not_exist(self):
)
self.render(request)

self.assertEqual(404, int(channel.result["code"]), msg=channel.result["body"])
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
self.assertIn("new_room_id", channel.json_body)
self.assertIn("kicked_users", channel.json_body)
self.assertIn("failed_to_kick_users", channel.json_body)
self.assertIn("local_aliases", channel.json_body)

def test_new_room_user_is_not_local(self):
"""
Expand Down
4 changes: 4 additions & 0 deletions tests/storage/test_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def test_get_room(self):
(yield self.store.get_room(self.room.to_string())),
)

@defer.inlineCallbacks
def test_get_room_unknown_room(self):
self.assertIsNone((yield self.store.get_room("!uknown:test")),)

@defer.inlineCallbacks
def test_get_room_with_stats(self):
self.assertDictContainsSubset(
Expand Down