2018-08-30 17:19:58 +03:00
|
|
|
# Copyright 2018 New Vector 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.
|
|
|
|
|
2021-03-08 17:21:36 +03:00
|
|
|
from synapse.api.errors import NotFoundError, SynapseError
|
2021-08-17 14:57:58 +03:00
|
|
|
from synapse.rest.client import room
|
2018-08-30 17:19:58 +03:00
|
|
|
|
|
|
|
from tests.unittest import HomeserverTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class PurgeTests(HomeserverTestCase):
|
|
|
|
|
|
|
|
user_id = "@red:server"
|
|
|
|
servlets = [room.register_servlets]
|
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock):
|
2020-12-02 19:09:24 +03:00
|
|
|
hs = self.setup_test_homeserver("server", federation_http_client=None)
|
2018-08-30 17:19:58 +03:00
|
|
|
return hs
|
|
|
|
|
|
|
|
def prepare(self, reactor, clock, hs):
|
|
|
|
self.room_id = self.helper.create_room_as(self.user_id)
|
|
|
|
|
2022-02-23 14:04:02 +03:00
|
|
|
self.store = hs.get_datastores().main
|
2022-05-31 15:17:50 +03:00
|
|
|
self._storage_controllers = self.hs.get_storage_controllers()
|
2021-03-08 17:21:36 +03:00
|
|
|
|
|
|
|
def test_purge_history(self):
|
2018-08-30 17:19:58 +03:00
|
|
|
"""
|
2021-03-08 17:21:36 +03:00
|
|
|
Purging a room history will delete everything before the topological point.
|
2018-08-30 17:19:58 +03:00
|
|
|
"""
|
|
|
|
# Send four messages to the room
|
|
|
|
first = self.helper.send(self.room_id, body="test1")
|
|
|
|
second = self.helper.send(self.room_id, body="test2")
|
|
|
|
third = self.helper.send(self.room_id, body="test3")
|
|
|
|
last = self.helper.send(self.room_id, body="test4")
|
|
|
|
|
|
|
|
# Get the topological token
|
2020-09-30 22:29:19 +03:00
|
|
|
token = self.get_success(
|
2021-03-08 17:21:36 +03:00
|
|
|
self.store.get_topological_token_for_event(last["event_id"])
|
2020-07-28 23:09:53 +03:00
|
|
|
)
|
2022-02-23 14:04:02 +03:00
|
|
|
token_str = self.get_success(token.to_string(self.hs.get_datastores().main))
|
2018-08-30 17:19:58 +03:00
|
|
|
|
2020-08-17 14:24:46 +03:00
|
|
|
# Purge everything before this topological token
|
2020-09-30 22:29:19 +03:00
|
|
|
self.get_success(
|
2022-05-31 15:17:50 +03:00
|
|
|
self._storage_controllers.purge_events.purge_history(
|
|
|
|
self.room_id, token_str, True
|
|
|
|
)
|
2020-09-30 22:29:19 +03:00
|
|
|
)
|
2018-08-30 17:19:58 +03:00
|
|
|
|
|
|
|
# 1-3 should fail and last will succeed, meaning that 1-3 are deleted
|
|
|
|
# and last is not.
|
2021-03-08 17:21:36 +03:00
|
|
|
self.get_failure(self.store.get_event(first["event_id"]), NotFoundError)
|
|
|
|
self.get_failure(self.store.get_event(second["event_id"]), NotFoundError)
|
|
|
|
self.get_failure(self.store.get_event(third["event_id"]), NotFoundError)
|
|
|
|
self.get_success(self.store.get_event(last["event_id"]))
|
2018-08-30 17:19:58 +03:00
|
|
|
|
2021-03-08 17:21:36 +03:00
|
|
|
def test_purge_history_wont_delete_extrems(self):
|
2018-08-30 17:19:58 +03:00
|
|
|
"""
|
2021-03-08 17:21:36 +03:00
|
|
|
Purging a room history will delete everything before the topological point.
|
2018-08-30 17:19:58 +03:00
|
|
|
"""
|
|
|
|
# Send four messages to the room
|
|
|
|
first = self.helper.send(self.room_id, body="test1")
|
|
|
|
second = self.helper.send(self.room_id, body="test2")
|
|
|
|
third = self.helper.send(self.room_id, body="test3")
|
|
|
|
last = self.helper.send(self.room_id, body="test4")
|
|
|
|
|
|
|
|
# Set the topological token higher than it should be
|
2020-09-29 23:48:33 +03:00
|
|
|
token = self.get_success(
|
2021-03-08 17:21:36 +03:00
|
|
|
self.store.get_topological_token_for_event(last["event_id"])
|
2020-08-17 14:24:46 +03:00
|
|
|
)
|
2021-07-13 13:43:15 +03:00
|
|
|
event = f"t{token.topological + 1}-{token.stream + 1}"
|
2018-08-30 17:19:58 +03:00
|
|
|
|
|
|
|
# Purge everything before this topological token
|
2021-03-08 17:21:36 +03:00
|
|
|
f = self.get_failure(
|
2022-05-31 15:17:50 +03:00
|
|
|
self._storage_controllers.purge_events.purge_history(
|
|
|
|
self.room_id, event, True
|
|
|
|
),
|
2021-03-08 17:21:36 +03:00
|
|
|
SynapseError,
|
|
|
|
)
|
2018-08-30 17:19:58 +03:00
|
|
|
self.assertIn("greater than forward", f.value.args[0])
|
|
|
|
|
|
|
|
# Try and get the events
|
2021-03-08 17:21:36 +03:00
|
|
|
self.get_success(self.store.get_event(first["event_id"]))
|
|
|
|
self.get_success(self.store.get_event(second["event_id"]))
|
|
|
|
self.get_success(self.store.get_event(third["event_id"]))
|
|
|
|
self.get_success(self.store.get_event(last["event_id"]))
|
|
|
|
|
|
|
|
def test_purge_room(self):
|
|
|
|
"""
|
|
|
|
Purging a room will delete everything about it.
|
|
|
|
"""
|
|
|
|
# Send four messages to the room
|
|
|
|
first = self.helper.send(self.room_id, body="test1")
|
|
|
|
|
|
|
|
# Get the current room state.
|
|
|
|
create_event = self.get_success(
|
2022-06-06 11:24:12 +03:00
|
|
|
self._storage_controllers.state.get_current_state_event(
|
|
|
|
self.room_id, "m.room.create", ""
|
|
|
|
)
|
2021-03-08 17:21:36 +03:00
|
|
|
)
|
|
|
|
self.assertIsNotNone(create_event)
|
|
|
|
|
|
|
|
# Purge everything before this topological token
|
2022-05-31 15:17:50 +03:00
|
|
|
self.get_success(
|
|
|
|
self._storage_controllers.purge_events.purge_room(self.room_id)
|
|
|
|
)
|
2021-03-08 17:21:36 +03:00
|
|
|
|
|
|
|
# The events aren't found.
|
2022-07-15 12:30:46 +03:00
|
|
|
self.store._invalidate_local_get_event_cache(create_event.event_id)
|
2021-03-08 17:21:36 +03:00
|
|
|
self.get_failure(self.store.get_event(create_event.event_id), NotFoundError)
|
|
|
|
self.get_failure(self.store.get_event(first["event_id"]), NotFoundError)
|