From 404459f40fee94a843f5188ca891f92c16fdd00c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 29 Jul 2022 07:29:15 +0200 Subject: [PATCH 01/10] Add background update to remove unstable private read receipts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- synapse/_scripts/synapse_port_db.py | 2 + synapse/storage/databases/main/receipts.py | 60 +++++++++++++- synapse/storage/schema/__init__.py | 7 +- ...1remove_unstable_private_read_receipts.sql | 17 ++++ tests/storage/test_receipts.py | 81 +++++++++++++++++++ 5 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 synapse/storage/schema/main/delta/73/01remove_unstable_private_read_receipts.sql diff --git a/synapse/_scripts/synapse_port_db.py b/synapse/_scripts/synapse_port_db.py index 543bba27c29e..30983c47fbb7 100755 --- a/synapse/_scripts/synapse_port_db.py +++ b/synapse/_scripts/synapse_port_db.py @@ -67,6 +67,7 @@ ) from synapse.storage.databases.main.presence import PresenceBackgroundUpdateStore from synapse.storage.databases.main.pusher import PusherWorkerStore +from synapse.storage.databases.main.receipts import ReceiptsBackgroundUpdateStore from synapse.storage.databases.main.registration import ( RegistrationBackgroundUpdateStore, find_max_generated_user_id_localpart, @@ -203,6 +204,7 @@ class Store( PushRuleStore, PusherWorkerStore, PresenceBackgroundUpdateStore, + ReceiptsBackgroundUpdateStore, ): def execute(self, f: Callable[..., R], *args: Any, **kwargs: Any) -> Awaitable[R]: return self.db_pool.runInteraction(f.__name__, f, *args, **kwargs) diff --git a/synapse/storage/databases/main/receipts.py b/synapse/storage/databases/main/receipts.py index 0090c9f22512..ce441449ee1d 100644 --- a/synapse/storage/databases/main/receipts.py +++ b/synapse/storage/databases/main/receipts.py @@ -26,7 +26,7 @@ cast, ) -from synapse.api.constants import EduTypes +from synapse.api.constants import EduTypes, ReceiptTypes from synapse.replication.slave.storage._slaved_id_tracker import SlavedIdTracker from synapse.replication.tcp.streams import ReceiptsStream from synapse.storage._base import SQLBaseStore, db_to_json, make_in_list_sql_clause @@ -834,5 +834,61 @@ def _insert_graph_receipt_txn( ) -class ReceiptsStore(ReceiptsWorkerStore): +class _BackgroundUpdates: + REMOVE_UNSTABLE_PRIVATE_READ_RECEIPTS = "remove_unstable_private_read_receipts" + + +class ReceiptsBackgroundUpdateStore(SQLBaseStore): + def __init__( + self, + database: DatabasePool, + db_conn: LoggingDatabaseConnection, + hs: "HomeServer", + ): + super().__init__(database, db_conn, hs) + + self.db_pool.updates.register_background_update_handler( + _BackgroundUpdates.REMOVE_UNSTABLE_PRIVATE_READ_RECEIPTS, + self._background_remove_unstable_private_read_receipts, + ) + + async def _background_remove_unstable_private_read_receipts( + self, progress: JsonDict, batch_size: int + ) -> int: + """ + Background update to go and remove unstable private read receipts + (org.matrix.msc2285.read.private) from the `receipts_linearized` and + `receipts_graph` tables. + """ + + def _background_remove_unstable_private_read_receipts_txn( + txn: LoggingTransaction, + ) -> bool: + self.db_pool.simple_delete_txn( + txn, + "receipts_linearized", + {"receipt_type": ReceiptTypes.READ_PRIVATE_UNSTABLE}, + ) + self.db_pool.simple_delete_txn( + txn, + "receipts_graph", + {"receipt_type": ReceiptTypes.READ_PRIVATE_UNSTABLE}, + ) + + return True + + end = await self.db_pool.runInteraction( + "_background_remove_unstable_private_read_receipts", + _background_remove_unstable_private_read_receipts_txn, + ) + + if end: + await self.db_pool.updates._end_background_update( + _BackgroundUpdates.REMOVE_UNSTABLE_PRIVATE_READ_RECEIPTS + ) + + return 0 + + +class ReceiptsStore(ReceiptsBackgroundUpdateStore, ReceiptsWorkerStore): pass diff --git a/synapse/storage/schema/__init__.py b/synapse/storage/schema/__init__.py index a9a88c8bfd8b..6dd3e205fe35 100644 --- a/synapse/storage/schema/__init__.py +++ b/synapse/storage/schema/__init__.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -SCHEMA_VERSION = 72 # remember to update the list below when updating +SCHEMA_VERSION = 73 # remember to update the list below when updating """Represents the expectations made by the codebase about the database schema This should be incremented whenever the codebase changes its requirements on the @@ -75,13 +75,16 @@ Changes in SCHEMA_VERSION = 72: - event_edges.(room_id, is_state) are no longer written to. - Tables related to groups are dropped. + +Changes in SCHEMA_VERSION = 73: + - Remove unstable private read receipts """ SCHEMA_COMPAT_VERSION = ( # The groups tables are no longer accessible, so synapses with SCHEMA_VERSION < 72 # could break. - 72 + 73 ) """Limit on how far the synapse codebase can be rolled back without breaking db compat diff --git a/synapse/storage/schema/main/delta/73/01remove_unstable_private_read_receipts.sql b/synapse/storage/schema/main/delta/73/01remove_unstable_private_read_receipts.sql new file mode 100644 index 000000000000..1e1b02269dad --- /dev/null +++ b/synapse/storage/schema/main/delta/73/01remove_unstable_private_read_receipts.sql @@ -0,0 +1,17 @@ +/* Copyright 2022 The Matrix.org Foundation C.I.C + * + * 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. + */ + +INSERT INTO background_updates (update_name, progress_json) + VALUES ('remove_unstable_private_read_receipts', '{}'); diff --git a/tests/storage/test_receipts.py b/tests/storage/test_receipts.py index b1a8f8bba7d3..f06610836348 100644 --- a/tests/storage/test_receipts.py +++ b/tests/storage/test_receipts.py @@ -13,6 +13,7 @@ # limitations under the License. from synapse.api.constants import ReceiptTypes +from synapse.storage.databases.main.receipts import _BackgroundUpdates from synapse.types import UserID, create_requester from tests.test_utils.event_injection import create_event @@ -259,3 +260,83 @@ def test_get_last_receipt_event_id_for_user(self): ) ) self.assertEqual(res, event2_1_id) + + +class ReceiptsBackgroundUpdateStoreTestCase(HomeserverTestCase): + def prepare(self, reactor, clock, hs): + self.store = hs.get_datastores().main + + def test_background_remove_unstable_private_read_receipts(self): + """ + Test that the background update removes unstable private read receipts + (org.matrix.msc2285.read.private) from the `receipts_linearized` and + `receipts_graph` tables. + """ + + # Fake a receipt of unstable private read receipt type in + # `receipt_linearized` + self.get_success( + self.store.db_pool.simple_insert( + table="receipts_linearized", + values={ + "room_id": "room_id", + "receipt_type": ReceiptTypes.READ_PRIVATE_UNSTABLE, + "user_id": OUR_USER_ID, + "stream_id": "stream_id", + "event_id": "event_id", + "data": "{}", + }, + ) + ) + # Fake a receipt of unstable private read receipt type in + # `receipt_graph` + self.get_success( + self.store.db_pool.simple_insert( + table="receipts_graph", + values={ + "room_id": "room_id", + "receipt_type": ReceiptTypes.READ_PRIVATE_UNSTABLE, + "user_id": OUR_USER_ID, + "event_ids": "{}", + "data": "{}", + }, + ) + ) + + # Insert and run the background update + self.get_success( + self.store.db_pool.simple_insert( + "background_updates", + { + "update_name": _BackgroundUpdates.REMOVE_UNSTABLE_PRIVATE_READ_RECEIPTS, + "progress_json": "{}", + }, + ) + ) + + # ... and tell the DataStore that it hasn't finished all updates yet + self.store.db_pool.updates._all_done = False + + # Now let's actually drive the updates to completion + self.wait_for_background_updates() + + # Make sure the background update removed unstable private read receipt + # from `receipts_linearized` + receipts_linearized = self.get_success( + self.store.db_pool.simple_select_list( + table="receipts_linearized", + keyvalues={"receipt_type": ReceiptTypes.READ_PRIVATE_UNSTABLE}, + retcols={"room_id"}, + ) + ) + # Make sure the background update removed unstable private read receipt + # from `receipts_graph` + self.assertEqual(receipts_linearized, []) + receipts_graph = self.get_success( + self.store.db_pool.simple_select_list( + table="receipts_graph", + keyvalues={"receipt_type": ReceiptTypes.READ_PRIVATE_UNSTABLE}, + retcols={"room_id"}, + ) + ) + self.assertEqual(receipts_graph, []) From 57251c8744b2b81944b38d382365ebaffa74be70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 29 Jul 2022 07:39:00 +0200 Subject: [PATCH 02/10] Remove handling for unstable private read receipts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- synapse/handlers/receipts.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/synapse/handlers/receipts.py b/synapse/handlers/receipts.py index e5c516695815..bb856c92ff78 100644 --- a/synapse/handlers/receipts.py +++ b/synapse/handlers/receipts.py @@ -203,27 +203,20 @@ def filter_out_private_receipts( for event_id, orig_event_content in room.get("content", {}).items(): event_content = orig_event_content # If there are private read receipts, additional logic is necessary. - if ( - ReceiptTypes.READ_PRIVATE in event_content - or ReceiptTypes.READ_PRIVATE_UNSTABLE in event_content - ): + if ReceiptTypes.READ_PRIVATE in event_content: # Make a copy without private read receipts to avoid leaking # other user's private read receipts.. event_content = { receipt_type: receipt_value for receipt_type, receipt_value in event_content.items() - if receipt_type - not in ( - ReceiptTypes.READ_PRIVATE, - ReceiptTypes.READ_PRIVATE_UNSTABLE, - ) + if receipt_type not in ReceiptTypes.READ_PRIVATE } # Copy the current user's private read receipt from the # original content, if it exists. - user_private_read_receipt = orig_event_content.get( - ReceiptTypes.READ_PRIVATE, {} - ).get(user_id, None) + user_private_read_receipt = orig_event_content[ + ReceiptTypes.READ_PRIVATE + ].get(user_id, None) if user_private_read_receipt: event_content[ReceiptTypes.READ_PRIVATE] = { user_id: user_private_read_receipt From e12706ca72431888aec7042a3aebb71416f1e6e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 29 Jul 2022 13:32:05 +0200 Subject: [PATCH 03/10] Don't bump `SCHEMA_VERSION` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- synapse/storage/schema/__init__.py | 6 ++---- .../04remove_unstable_private_read_receipts.sql} | 0 2 files changed, 2 insertions(+), 4 deletions(-) rename synapse/storage/schema/main/delta/{73/01remove_unstable_private_read_receipts.sql => 72/04remove_unstable_private_read_receipts.sql} (100%) diff --git a/synapse/storage/schema/__init__.py b/synapse/storage/schema/__init__.py index 6dd3e205fe35..37a1ff97ca62 100644 --- a/synapse/storage/schema/__init__.py +++ b/synapse/storage/schema/__init__.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -SCHEMA_VERSION = 73 # remember to update the list below when updating +SCHEMA_VERSION = 72 # remember to update the list below when updating """Represents the expectations made by the codebase about the database schema This should be incremented whenever the codebase changes its requirements on the @@ -75,8 +75,6 @@ Changes in SCHEMA_VERSION = 72: - event_edges.(room_id, is_state) are no longer written to. - Tables related to groups are dropped. - -Changes in SCHEMA_VERSION = 73: - Remove unstable private read receipts """ @@ -84,7 +82,7 @@ SCHEMA_COMPAT_VERSION = ( # The groups tables are no longer accessible, so synapses with SCHEMA_VERSION < 72 # could break. - 73 + 72 ) """Limit on how far the synapse codebase can be rolled back without breaking db compat diff --git a/synapse/storage/schema/main/delta/73/01remove_unstable_private_read_receipts.sql b/synapse/storage/schema/main/delta/72/04remove_unstable_private_read_receipts.sql similarity index 100% rename from synapse/storage/schema/main/delta/73/01remove_unstable_private_read_receipts.sql rename to synapse/storage/schema/main/delta/72/04remove_unstable_private_read_receipts.sql From c8cf10e450fc0b90c209f2859c9a0560fdb81242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 29 Jul 2022 13:34:27 +0200 Subject: [PATCH 04/10] Remove `READ_PRIVATE_UNSTABLE` from `ReceiptTypes` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- synapse/api/constants.py | 1 - synapse/storage/databases/main/receipts.py | 8 +++++--- tests/storage/test_receipts.py | 10 ++++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/synapse/api/constants.py b/synapse/api/constants.py index 6cc96bbc5399..5d68796f7bc1 100644 --- a/synapse/api/constants.py +++ b/synapse/api/constants.py @@ -258,7 +258,6 @@ class GuestAccess: class ReceiptTypes: READ: Final = "m.read" READ_PRIVATE: Final = "m.read.private" - READ_PRIVATE_UNSTABLE: Final = "org.matrix.msc2285.read.private" FULLY_READ: Final = "m.fully_read" diff --git a/synapse/storage/databases/main/receipts.py b/synapse/storage/databases/main/receipts.py index ce441449ee1d..46b41ed2a86c 100644 --- a/synapse/storage/databases/main/receipts.py +++ b/synapse/storage/databases/main/receipts.py @@ -26,7 +26,7 @@ cast, ) -from synapse.api.constants import EduTypes, ReceiptTypes +from synapse.api.constants import EduTypes from synapse.replication.slave.storage._slaved_id_tracker import SlavedIdTracker from synapse.replication.tcp.streams import ReceiptsStream from synapse.storage._base import SQLBaseStore, db_to_json, make_in_list_sql_clause @@ -861,18 +861,20 @@ async def _background_remove_unstable_private_read_receipts( `receipts_graph` tables. """ + READ_PRIVATE_UNSTABLE = "org.matrix.msc2285.read.private" + def _background_remove_unstable_private_read_receipts_txn( txn: LoggingTransaction, ) -> bool: self.db_pool.simple_delete_txn( txn, "receipts_linearized", - {"receipt_type": ReceiptTypes.READ_PRIVATE_UNSTABLE}, + {"receipt_type": READ_PRIVATE_UNSTABLE}, ) self.db_pool.simple_delete_txn( txn, "receipts_graph", - {"receipt_type": ReceiptTypes.READ_PRIVATE_UNSTABLE}, + {"receipt_type": READ_PRIVATE_UNSTABLE}, ) return True diff --git a/tests/storage/test_receipts.py b/tests/storage/test_receipts.py index f06610836348..4ab1d85ec713 100644 --- a/tests/storage/test_receipts.py +++ b/tests/storage/test_receipts.py @@ -273,6 +273,8 @@ def test_background_remove_unstable_private_read_receipts(self): `receipts_graph` tables. """ + READ_PRIVATE_UNSTABLE = "org.matrix.msc2285.read.private" + # Fake a receipt of unstable private read receipt type in # `receipt_linearized` self.get_success( @@ -280,7 +282,7 @@ def test_background_remove_unstable_private_read_receipts(self): table="receipts_linearized", values={ "room_id": "room_id", - "receipt_type": ReceiptTypes.READ_PRIVATE_UNSTABLE, + "receipt_type": READ_PRIVATE_UNSTABLE, "user_id": OUR_USER_ID, "stream_id": "stream_id", "event_id": "event_id", @@ -295,7 +297,7 @@ def test_background_remove_unstable_private_read_receipts(self): table="receipts_graph", values={ "room_id": "room_id", - "receipt_type": ReceiptTypes.READ_PRIVATE_UNSTABLE, + "receipt_type": READ_PRIVATE_UNSTABLE, "user_id": OUR_USER_ID, "event_ids": "{}", "data": "{}", @@ -325,7 +327,7 @@ def test_background_remove_unstable_private_read_receipts(self): receipts_linearized = self.get_success( self.store.db_pool.simple_select_list( table="receipts_linearized", - keyvalues={"receipt_type": ReceiptTypes.READ_PRIVATE_UNSTABLE}, + keyvalues={"receipt_type": READ_PRIVATE_UNSTABLE}, retcols={"room_id"}, ) ) @@ -335,7 +337,7 @@ def test_background_remove_unstable_private_read_receipts(self): receipts_graph = self.get_success( self.store.db_pool.simple_select_list( table="receipts_graph", - keyvalues={"receipt_type": ReceiptTypes.READ_PRIVATE_UNSTABLE}, + keyvalues={"receipt_type": READ_PRIVATE_UNSTABLE}, retcols={"room_id"}, ) ) From 84299f6f609637e19aa854adeeea06bbf98e2968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 29 Jul 2022 13:37:08 +0200 Subject: [PATCH 05/10] Assume return value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- synapse/storage/databases/main/receipts.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/synapse/storage/databases/main/receipts.py b/synapse/storage/databases/main/receipts.py index 46b41ed2a86c..500228ad5f01 100644 --- a/synapse/storage/databases/main/receipts.py +++ b/synapse/storage/databases/main/receipts.py @@ -879,15 +879,13 @@ def _background_remove_unstable_private_read_receipts_txn( return True - end = await self.db_pool.runInteraction( + await self.db_pool.runInteraction( "_background_remove_unstable_private_read_receipts", _background_remove_unstable_private_read_receipts_txn, ) - - if end: - await self.db_pool.updates._end_background_update( - _BackgroundUpdates.REMOVE_UNSTABLE_PRIVATE_READ_RECEIPTS - ) + await self.db_pool.updates._end_background_update( + _BackgroundUpdates.REMOVE_UNSTABLE_PRIVATE_READ_RECEIPTS + ) return 0 From 208e97acb9628f8dad613231a8d09f941f3094b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 29 Jul 2022 13:57:19 +0200 Subject: [PATCH 06/10] Remove non-relevant test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- tests/handlers/test_receipts.py | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/tests/handlers/test_receipts.py b/tests/handlers/test_receipts.py index 6fcbf20666b3..a95868b5c055 100644 --- a/tests/handlers/test_receipts.py +++ b/tests/handlers/test_receipts.py @@ -296,26 +296,6 @@ def test_leaves_our_private_and_their_public(self): ], ) - def test_filters_out_unstable_private_receipt(self): - self._test_filters_private( - [ - { - "content": { - "$1435641916114394fHBLK:matrix.org": { - "org.matrix.msc2285.read.private": { - "@rikj:jki.re": { - "ts": 1436451550453, - } - } - } - }, - "room_id": "!jEsUZKDJdhlrceRyVU:example.org", - "type": EduTypes.RECEIPT, - } - ], - [], - ) - def test_we_do_not_mutate(self): """Ensure the input values are not modified.""" events = [ From a7c3a1c80c4e6e54d2326f5ce685eb1ce47c1622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 29 Jul 2022 14:16:04 +0200 Subject: [PATCH 07/10] Don't bother with a migration script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- synapse/_scripts/synapse_port_db.py | 2 - synapse/storage/databases/main/receipts.py | 58 +------------ ...4remove_unstable_private_read_receipts.sql | 4 +- tests/storage/test_receipts.py | 83 ------------------- 4 files changed, 3 insertions(+), 144 deletions(-) diff --git a/synapse/_scripts/synapse_port_db.py b/synapse/_scripts/synapse_port_db.py index 30983c47fbb7..543bba27c29e 100755 --- a/synapse/_scripts/synapse_port_db.py +++ b/synapse/_scripts/synapse_port_db.py @@ -67,7 +67,6 @@ ) from synapse.storage.databases.main.presence import PresenceBackgroundUpdateStore from synapse.storage.databases.main.pusher import PusherWorkerStore -from synapse.storage.databases.main.receipts import ReceiptsBackgroundUpdateStore from synapse.storage.databases.main.registration import ( RegistrationBackgroundUpdateStore, find_max_generated_user_id_localpart, @@ -204,7 +203,6 @@ class Store( PushRuleStore, PusherWorkerStore, PresenceBackgroundUpdateStore, - ReceiptsBackgroundUpdateStore, ): def execute(self, f: Callable[..., R], *args: Any, **kwargs: Any) -> Awaitable[R]: return self.db_pool.runInteraction(f.__name__, f, *args, **kwargs) diff --git a/synapse/storage/databases/main/receipts.py b/synapse/storage/databases/main/receipts.py index 500228ad5f01..0090c9f22512 100644 --- a/synapse/storage/databases/main/receipts.py +++ b/synapse/storage/databases/main/receipts.py @@ -834,61 +834,5 @@ def _insert_graph_receipt_txn( ) -class _BackgroundUpdates: - REMOVE_UNSTABLE_PRIVATE_READ_RECEIPTS = "remove_unstable_private_read_receipts" - - -class ReceiptsBackgroundUpdateStore(SQLBaseStore): - def __init__( - self, - database: DatabasePool, - db_conn: LoggingDatabaseConnection, - hs: "HomeServer", - ): - super().__init__(database, db_conn, hs) - - self.db_pool.updates.register_background_update_handler( - _BackgroundUpdates.REMOVE_UNSTABLE_PRIVATE_READ_RECEIPTS, - self._background_remove_unstable_private_read_receipts, - ) - - async def _background_remove_unstable_private_read_receipts( - self, progress: JsonDict, batch_size: int - ) -> int: - """ - Background update to go and remove unstable private read receipts - (org.matrix.msc2285.read.private) from the `receipts_linearized` and - `receipts_graph` tables. - """ - - READ_PRIVATE_UNSTABLE = "org.matrix.msc2285.read.private" - - def _background_remove_unstable_private_read_receipts_txn( - txn: LoggingTransaction, - ) -> bool: - self.db_pool.simple_delete_txn( - txn, - "receipts_linearized", - {"receipt_type": READ_PRIVATE_UNSTABLE}, - ) - self.db_pool.simple_delete_txn( - txn, - "receipts_graph", - {"receipt_type": READ_PRIVATE_UNSTABLE}, - ) - - return True - - await self.db_pool.runInteraction( - "_background_remove_unstable_private_read_receipts", - _background_remove_unstable_private_read_receipts_txn, - ) - await self.db_pool.updates._end_background_update( - _BackgroundUpdates.REMOVE_UNSTABLE_PRIVATE_READ_RECEIPTS - ) - - return 0 - - -class ReceiptsStore(ReceiptsBackgroundUpdateStore, ReceiptsWorkerStore): +class ReceiptsStore(ReceiptsWorkerStore): pass diff --git a/synapse/storage/schema/main/delta/72/04remove_unstable_private_read_receipts.sql b/synapse/storage/schema/main/delta/72/04remove_unstable_private_read_receipts.sql index 1e1b02269dad..9c9e63edaeac 100644 --- a/synapse/storage/schema/main/delta/72/04remove_unstable_private_read_receipts.sql +++ b/synapse/storage/schema/main/delta/72/04remove_unstable_private_read_receipts.sql @@ -13,5 +13,5 @@ * limitations under the License. */ -INSERT INTO background_updates (update_name, progress_json) - VALUES ('remove_unstable_private_read_receipts', '{}'); +DELETE FROM receipts_linearized WHERE receipt_type = "org.matrix.msc2285.read.private"; +DELETE FROM receipts_graph WHERE receipt_type = "org.matrix.msc2285.read.private"; diff --git a/tests/storage/test_receipts.py b/tests/storage/test_receipts.py index 4ab1d85ec713..b1a8f8bba7d3 100644 --- a/tests/storage/test_receipts.py +++ b/tests/storage/test_receipts.py @@ -13,7 +13,6 @@ # limitations under the License. from synapse.api.constants import ReceiptTypes -from synapse.storage.databases.main.receipts import _BackgroundUpdates from synapse.types import UserID, create_requester from tests.test_utils.event_injection import create_event @@ -260,85 +259,3 @@ def test_get_last_receipt_event_id_for_user(self): ) ) self.assertEqual(res, event2_1_id) - - -class ReceiptsBackgroundUpdateStoreTestCase(HomeserverTestCase): - def prepare(self, reactor, clock, hs): - self.store = hs.get_datastores().main - - def test_background_remove_unstable_private_read_receipts(self): - """ - Test that the background update removes unstable private read receipts - (org.matrix.msc2285.read.private) from the `receipts_linearized` and - `receipts_graph` tables. - """ - - READ_PRIVATE_UNSTABLE = "org.matrix.msc2285.read.private" - - # Fake a receipt of unstable private read receipt type in - # `receipt_linearized` - self.get_success( - self.store.db_pool.simple_insert( - table="receipts_linearized", - values={ - "room_id": "room_id", - "receipt_type": READ_PRIVATE_UNSTABLE, - "user_id": OUR_USER_ID, - "stream_id": "stream_id", - "event_id": "event_id", - "data": "{}", - }, - ) - ) - # Fake a receipt of unstable private read receipt type in - # `receipt_graph` - self.get_success( - self.store.db_pool.simple_insert( - table="receipts_graph", - values={ - "room_id": "room_id", - "receipt_type": READ_PRIVATE_UNSTABLE, - "user_id": OUR_USER_ID, - "event_ids": "{}", - "data": "{}", - }, - ) - ) - - # Insert and run the background update - self.get_success( - self.store.db_pool.simple_insert( - "background_updates", - { - "update_name": _BackgroundUpdates.REMOVE_UNSTABLE_PRIVATE_READ_RECEIPTS, - "progress_json": "{}", - }, - ) - ) - - # ... and tell the DataStore that it hasn't finished all updates yet - self.store.db_pool.updates._all_done = False - - # Now let's actually drive the updates to completion - self.wait_for_background_updates() - - # Make sure the background update removed unstable private read receipt - # from `receipts_linearized` - receipts_linearized = self.get_success( - self.store.db_pool.simple_select_list( - table="receipts_linearized", - keyvalues={"receipt_type": READ_PRIVATE_UNSTABLE}, - retcols={"room_id"}, - ) - ) - # Make sure the background update removed unstable private read receipt - # from `receipts_graph` - self.assertEqual(receipts_linearized, []) - receipts_graph = self.get_success( - self.store.db_pool.simple_select_list( - table="receipts_graph", - keyvalues={"receipt_type": READ_PRIVATE_UNSTABLE}, - retcols={"room_id"}, - ) - ) - self.assertEqual(receipts_graph, []) From 252146df9dc9aa0bc65554bb0660f1932d4ada7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 29 Jul 2022 14:22:35 +0200 Subject: [PATCH 08/10] Remove non-schema thing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- synapse/storage/schema/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/synapse/storage/schema/__init__.py b/synapse/storage/schema/__init__.py index 37a1ff97ca62..a9a88c8bfd8b 100644 --- a/synapse/storage/schema/__init__.py +++ b/synapse/storage/schema/__init__.py @@ -75,7 +75,6 @@ Changes in SCHEMA_VERSION = 72: - event_edges.(room_id, is_state) are no longer written to. - Tables related to groups are dropped. - - Remove unstable private read receipts """ From 3cbd80b2e1efe8869b181cdc463de5632e1ffc4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 29 Jul 2022 14:23:07 +0200 Subject: [PATCH 09/10] Only use single `'` Co-authored-by: Patrick Cloke --- .../main/delta/72/04remove_unstable_private_read_receipts.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/storage/schema/main/delta/72/04remove_unstable_private_read_receipts.sql b/synapse/storage/schema/main/delta/72/04remove_unstable_private_read_receipts.sql index 9c9e63edaeac..409dc918d53d 100644 --- a/synapse/storage/schema/main/delta/72/04remove_unstable_private_read_receipts.sql +++ b/synapse/storage/schema/main/delta/72/04remove_unstable_private_read_receipts.sql @@ -13,5 +13,5 @@ * limitations under the License. */ -DELETE FROM receipts_linearized WHERE receipt_type = "org.matrix.msc2285.read.private"; -DELETE FROM receipts_graph WHERE receipt_type = "org.matrix.msc2285.read.private"; +DELETE FROM receipts_linearized WHERE receipt_type = 'org.matrix.msc2285.read.private'; +DELETE FROM receipts_graph WHERE receipt_type = 'org.matrix.msc2285.read.private'; From 8854e357ff78900a6f02798e91f7395942978f43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 29 Jul 2022 14:29:02 +0200 Subject: [PATCH 10/10] Use `!=` Co-authored-by: Patrick Cloke --- synapse/handlers/receipts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/handlers/receipts.py b/synapse/handlers/receipts.py index bb856c92ff78..d2bdb9c8be79 100644 --- a/synapse/handlers/receipts.py +++ b/synapse/handlers/receipts.py @@ -209,7 +209,7 @@ def filter_out_private_receipts( event_content = { receipt_type: receipt_value for receipt_type, receipt_value in event_content.items() - if receipt_type not in ReceiptTypes.READ_PRIVATE + if receipt_type != ReceiptTypes.READ_PRIVATE } # Copy the current user's private read receipt from the