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

Commit

Permalink
Add an option to disable purge in delete room admin API (#7964)
Browse files Browse the repository at this point in the history
Add option ```purge``` to ```POST /_synapse/admin/v1/rooms/<room_id>/delete```
Fixes: #3761

Signed-off-by: Dirk Klimpel dirk@klimpel.org
  • Loading branch information
dklimpel authored Jul 28, 2020
1 parent 8a25332 commit e866e3b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog.d/7964.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add an option to purge room or not with delete room admin endpoint (`POST /_synapse/admin/v1/rooms/<room_id>/delete`). Contributed by @dklimpel.
13 changes: 9 additions & 4 deletions docs/admin_api/rooms.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,9 @@ to the new room will have power level `-10` by default, and thus be unable to sp
If `block` is `True` it prevents new joins to the old room.

This API will remove all trace of the old room from your database after removing
all local users.
all local users. If `purge` is `true` (the default), all traces of the old room will
be removed from your database after removing all local users. If you do not want
this to happen, set `purge` to `false`.
Depending on the amount of history being purged a call to the API may take
several minutes or longer.

Expand All @@ -388,7 +390,8 @@ with a body of:
"new_room_user_id": "@someuser:example.com",
"room_name": "Content Violation Notification",
"message": "Bad Room has been shutdown due to content violations on this server. Please review our Terms of Service.",
"block": true
"block": true,
"purge": true
}
```

Expand Down Expand Up @@ -430,8 +433,10 @@ The following JSON body parameters are available:
`new_room_user_id` in the new room. Ideally this will clearly convey why the
original room was shut down. Defaults to `Sharing illegal content on this server
is not permitted and rooms in violation will be blocked.`
* `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`.
* `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`.
* `purge` - Optional. If set to `true`, it will remove all traces of the room from your database.
Defaults to `true`.

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

Expand Down
11 changes: 10 additions & 1 deletion synapse/rest/admin/rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ async def on_POST(self, request, room_id):
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,
)

ret = await self.room_shutdown_handler.shutdown_room(
room_id=room_id,
new_room_user_id=content.get("new_room_user_id"),
Expand All @@ -113,7 +121,8 @@ async def on_POST(self, request, room_id):
)

# Purge room
await self.pagination_handler.purge_room(room_id)
if purge:
await self.pagination_handler.purge_room(room_id)

return (200, ret)

Expand Down
57 changes: 55 additions & 2 deletions tests/rest/admin/test_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,23 @@ def test_block_is_not_bool(self):
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
self.assertEqual(Codes.BAD_JSON, channel.json_body["errcode"])

def test_purge_is_not_bool(self):
"""
If parameter `purge` is not boolean, return an error
"""
body = json.dumps({"purge": "NotBool"})

request, channel = self.make_request(
"POST",
self.url,
content=body.encode(encoding="utf_8"),
access_token=self.admin_user_tok,
)
self.render(request)

self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"])
self.assertEqual(Codes.BAD_JSON, channel.json_body["errcode"])

def test_purge_room_and_block(self):
"""Test to purge a room and block it.
Members will not be moved to a new room and will not receive a message.
Expand All @@ -297,7 +314,7 @@ def test_purge_room_and_block(self):
# Assert one user in room
self._is_member(room_id=self.room_id, user_id=self.other_user)

body = json.dumps({"block": True})
body = json.dumps({"block": True, "purge": True})

request, channel = self.make_request(
"POST",
Expand Down Expand Up @@ -331,7 +348,7 @@ def test_purge_room_and_not_block(self):
# Assert one user in room
self._is_member(room_id=self.room_id, user_id=self.other_user)

body = json.dumps({"block": False})
body = json.dumps({"block": False, "purge": True})

request, channel = self.make_request(
"POST",
Expand All @@ -351,6 +368,42 @@ def test_purge_room_and_not_block(self):
self._is_blocked(self.room_id, expect=False)
self._has_no_members(self.room_id)

def test_block_room_and_not_purge(self):
"""Test to block a room without purging it.
Members will not be moved to a new room and will not receive a message.
The room will not be purged.
"""
# Test that room is not purged
with self.assertRaises(AssertionError):
self._is_purged(self.room_id)

# Test that room is not blocked
self._is_blocked(self.room_id, expect=False)

# Assert one user in room
self._is_member(room_id=self.room_id, user_id=self.other_user)

body = json.dumps({"block": False, "purge": False})

request, channel = self.make_request(
"POST",
self.url.encode("ascii"),
content=body.encode(encoding="utf_8"),
access_token=self.admin_user_tok,
)
self.render(request)

self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
self.assertEqual(None, channel.json_body["new_room_id"])
self.assertEqual(self.other_user, channel.json_body["kicked_users"][0])
self.assertIn("failed_to_kick_users", channel.json_body)
self.assertIn("local_aliases", channel.json_body)

with self.assertRaises(AssertionError):
self._is_purged(self.room_id)
self._is_blocked(self.room_id, expect=False)
self._has_no_members(self.room_id)

def test_shutdown_room_consent(self):
"""Test that we can shutdown rooms with local users who have not
yet accepted the privacy policy. This used to fail when we tried to
Expand Down

0 comments on commit e866e3b

Please sign in to comment.