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

Commit b1ecd19

Browse files
authored
Fix 'delete room' admin api to work on incomplete rooms (#11523)
If, for some reason, we don't have the create event, we should still be able to purge a room.
1 parent 9c55ded commit b1ecd19

File tree

5 files changed

+33
-37
lines changed

5 files changed

+33
-37
lines changed

changelog.d/11523.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Extend the "delete room" admin api to work correctly on rooms which have previously been partially deleted.

synapse/handlers/pagination.py

-3
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,6 @@ async def purge_room(self, room_id: str, force: bool = False) -> None:
406406
force: set true to skip checking for joined users.
407407
"""
408408
with await self.pagination_lock.write(room_id):
409-
# check we know about the room
410-
await self.store.get_room_version_id(room_id)
411-
412409
# first check that we have no users in this room
413410
if not force:
414411
joined = await self.store.is_host_joined(room_id, self._server_name)

synapse/handlers/room.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -1535,20 +1535,13 @@ async def shutdown_room(
15351535
await self.store.block_room(room_id, requester_user_id)
15361536

15371537
if not await self.store.get_room(room_id):
1538-
if block:
1539-
# We allow you to block an unknown room.
1540-
return {
1541-
"kicked_users": [],
1542-
"failed_to_kick_users": [],
1543-
"local_aliases": [],
1544-
"new_room_id": None,
1545-
}
1546-
else:
1547-
# But if you don't want to preventatively block another room,
1548-
# this function can't do anything useful.
1549-
raise NotFoundError(
1550-
"Cannot shut down room: unknown room id %s" % (room_id,)
1551-
)
1538+
# if we don't know about the room, there is nothing left to do.
1539+
return {
1540+
"kicked_users": [],
1541+
"failed_to_kick_users": [],
1542+
"local_aliases": [],
1543+
"new_room_id": None,
1544+
}
15521545

15531546
if new_room_user_id is not None:
15541547
if not self.hs.is_mine_id(new_room_user_id):

synapse/rest/admin/rooms.py

-3
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,6 @@ async def on_DELETE(
106106
HTTPStatus.BAD_REQUEST, "%s is not a legal room ID" % (room_id,)
107107
)
108108

109-
if not await self._store.get_room(room_id):
110-
raise NotFoundError("Unknown room id %s" % (room_id,))
111-
112109
delete_id = self._pagination_handler.start_shutdown_and_purge_room(
113110
room_id=room_id,
114111
new_room_user_id=content.get("new_room_user_id"),

tests/rest/admin/test_room.py

+25-17
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_requester_is_no_admin(self):
8383

8484
def test_room_does_not_exist(self):
8585
"""
86-
Check that unknown rooms/server return error HTTPStatus.NOT_FOUND.
86+
Check that unknown rooms/server return 200
8787
"""
8888
url = "/_synapse/admin/v1/rooms/%s" % "!unknown:test"
8989

@@ -94,8 +94,7 @@ def test_room_does_not_exist(self):
9494
access_token=self.admin_user_tok,
9595
)
9696

97-
self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
98-
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
97+
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
9998

10099
def test_room_is_not_valid(self):
101100
"""
@@ -508,27 +507,36 @@ def test_requester_is_no_admin(self, method: str, url: str):
508507
self.assertEqual(HTTPStatus.FORBIDDEN, channel.code, msg=channel.json_body)
509508
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
510509

511-
@parameterized.expand(
512-
[
513-
("DELETE", "/_synapse/admin/v2/rooms/%s"),
514-
("GET", "/_synapse/admin/v2/rooms/%s/delete_status"),
515-
("GET", "/_synapse/admin/v2/rooms/delete_status/%s"),
516-
]
517-
)
518-
def test_room_does_not_exist(self, method: str, url: str):
519-
"""
520-
Check that unknown rooms/server return error HTTPStatus.NOT_FOUND.
510+
def test_room_does_not_exist(self):
521511
"""
512+
Check that unknown rooms/server return 200
522513
514+
This is important, as it allows incomplete vestiges of rooms to be cleared up
515+
even if the create event/etc is missing.
516+
"""
517+
room_id = "!unknown:test"
523518
channel = self.make_request(
524-
method,
525-
url % "!unknown:test",
519+
"DELETE",
520+
f"/_synapse/admin/v2/rooms/{room_id}",
526521
content={},
527522
access_token=self.admin_user_tok,
528523
)
529524

530-
self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
531-
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
525+
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
526+
self.assertIn("delete_id", channel.json_body)
527+
delete_id = channel.json_body["delete_id"]
528+
529+
# get status
530+
channel = self.make_request(
531+
"GET",
532+
f"/_synapse/admin/v2/rooms/{room_id}/delete_status",
533+
access_token=self.admin_user_tok,
534+
)
535+
536+
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
537+
self.assertEqual(1, len(channel.json_body["results"]))
538+
self.assertEqual("complete", channel.json_body["results"][0]["status"])
539+
self.assertEqual(delete_id, channel.json_body["results"][0]["delete_id"])
532540

533541
@parameterized.expand(
534542
[

0 commit comments

Comments
 (0)