summary refs log tree commit diff
diff options
context:
space:
mode:
authorEric Eastwood <eric.eastwood@beta.gouv.fr>2024-08-07 18:07:53 -0500
committerEric Eastwood <eric.eastwood@beta.gouv.fr>2024-08-07 18:07:53 -0500
commit61cea4e9b7f3a6f0c1f6748a020fa4089efea772 (patch)
tree80f0a6394d9165e147978b087780e8e94d62454e
parentChange to updating the latest membership in the room (diff)
downloadsynapse-61cea4e9b7f3a6f0c1f6748a020fa4089efea772.tar.xz
Closer to right
-rw-r--r--synapse/storage/databases/main/events.py23
-rw-r--r--tests/storage/test_events.py144
2 files changed, 156 insertions, 11 deletions
diff --git a/synapse/storage/databases/main/events.py b/synapse/storage/databases/main/events.py
index 6f9ab3c31f..913b6f2b87 100644
--- a/synapse/storage/databases/main/events.py
+++ b/synapse/storage/databases/main/events.py
@@ -1324,9 +1324,10 @@ class PersistEventsStore:
                     )
                     sliding_sync_joined_rooms_insert_map["room_type"] = room_type
                 elif event_id == room_encryption_event_id:
-                    is_encrypted = event_json.get("content", {}).get(
+                    encryption_algorithm = event_json.get("content", {}).get(
                         EventContentFields.ENCRYPTION_ALGORITHM
                     )
+                    is_encrypted = encryption_algorithm is not None
                     sliding_sync_joined_rooms_insert_map["is_encrypted"] = is_encrypted
                 elif event_id == room_name_event_id:
                     room_name = event_json.get("content", {}).get(
@@ -1459,9 +1460,10 @@ class PersistEventsStore:
                             room_type
                         )
                     elif event_type == EventTypes.RoomEncryption:
-                        is_encrypted = event_json.get("content", {}).get(
+                        encryption_algorithm = event_json.get("content", {}).get(
                             EventContentFields.ENCRYPTION_ALGORITHM
                         )
+                        is_encrypted = encryption_algorithm is not None
                         sliding_sync_non_joined_rooms_insert_map["is_encrypted"] = (
                             is_encrypted
                         )
@@ -1483,14 +1485,18 @@ class PersistEventsStore:
                 # TODO: Only do this for non-join membership
                 txn.execute_batch(
                     f"""
+                    WITH data_table (room_id, user_id, membership_event_id, membership, event_stream_ordering, {", ".join(insert_keys)}) AS (
+                        VALUES (
+                            ?, ?, ?,
+                            (SELECT membership FROM room_memberships WHERE event_id = ?),
+                            (SELECT stream_ordering FROM events WHERE event_id = ?),
+                            {", ".join("?" for _ in insert_values)}
+                        )
+                    )
                     INSERT INTO sliding_sync_non_join_memberships
                         (room_id, user_id, membership_event_id, membership, event_stream_ordering, {", ".join(insert_keys)})
-                    VALUES (
-                        ?, ?, ?,
-                        (SELECT membership FROM room_memberships WHERE event_id = ?),
-                        (SELECT stream_ordering FROM events WHERE event_id = ?),
-                        {", ".join("?" for _ in insert_values)}
-                    )
+                    SELECT * FROM data_table
+                    WHERE membership != ?
                     ON CONFLICT (room_id, user_id)
                     DO UPDATE SET
                         membership_event_id = EXCLUDED.membership_event_id,
@@ -1505,6 +1511,7 @@ class PersistEventsStore:
                             membership_event_id,
                             membership_event_id,
                             membership_event_id,
+                            Membership.JOIN,
                         ]
                         + list(insert_values)
                         for membership_event_id, user_id in membership_event_id_to_user_id_map.items()
diff --git a/tests/storage/test_events.py b/tests/storage/test_events.py
index 39f55022dd..7639eded2d 100644
--- a/tests/storage/test_events.py
+++ b/tests/storage/test_events.py
@@ -24,7 +24,7 @@ from typing import List, Optional
 
 from twisted.test.proto_helpers import MemoryReactor
 
-from synapse.api.constants import EventTypes, Membership
+from synapse.api.constants import EventTypes, Membership, EventContentFields, RoomTypes
 from synapse.api.room_versions import RoomVersions
 from synapse.events import EventBase
 from synapse.federation.federation_base import event_from_pdu_json
@@ -499,7 +499,46 @@ class SlidingSyncPrePopulatedTablesTestCase(HomeserverTestCase):
         room.register_servlets,
     ]
 
-    def test_TODO(self) -> None:
+    def prepare(
+        self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
+    ) -> None:
+        self.store = self.hs.get_datastores().main
+
+    def test_room_with_no_info(self) -> None:
+        """
+        Test room that doesn't have a room type, encryption, or name.
+        """
+        user1_id = self.register_user("user1", "pass")
+        user1_tok = self.login(user1_id, "pass")
+        user2_id = self.register_user("user2", "pass")
+        user2_tok = self.login(user2_id, "pass")
+
+        room_id1 = self.helper.create_room_as(user2_id, tok=user2_tok)
+
+        # User1 joins the room
+        self.helper.join(room_id1, user1_id, tok=user1_tok)
+
+        sliding_sync_joined_rooms_results = self.get_success(
+            self.store.db_pool.simple_select_list(
+                "sliding_sync_joined_rooms", None, retcols=("*",)
+            )
+        )
+        logger.info(
+            "sliding_sync_joined_rooms %s",
+            sliding_sync_joined_rooms_results,
+        )
+
+        sliding_sync_non_join_memberships_results = self.get_success(
+            self.store.db_pool.simple_select_list(
+                "sliding_sync_non_join_memberships", None, retcols=("*",)
+            )
+        )
+        logger.info(
+            "sliding_sync_non_join_memberships %s",
+            sliding_sync_non_join_memberships_results,
+        )
+
+    def test_room_with_info(self) -> None:
         """
         TODO
         """
@@ -509,10 +548,91 @@ class SlidingSyncPrePopulatedTablesTestCase(HomeserverTestCase):
         user2_tok = self.login(user2_id, "pass")
 
         room_id1 = self.helper.create_room_as(user2_id, tok=user2_tok)
+        # Add a room name
+        self.helper.send_state(
+            room_id1,
+            EventTypes.Name,
+            {"name": "my super duper room"},
+            tok=user2_tok,
+        )
+        # Encrypt the room
+        self.helper.send_state(
+            room_id1,
+            EventTypes.RoomEncryption,
+            {EventContentFields.ENCRYPTION_ALGORITHM: "m.megolm.v1.aes-sha2"},
+            tok=user2_tok,
+        )
 
         # User1 joins the room
         self.helper.join(room_id1, user1_id, tok=user1_tok)
 
+        sliding_sync_joined_rooms_results = self.get_success(
+            self.store.db_pool.simple_select_list(
+                "sliding_sync_joined_rooms", None, retcols=("*",)
+            )
+        )
+        logger.info(
+            "sliding_sync_joined_rooms %s",
+            sliding_sync_joined_rooms_results,
+        )
+
+        sliding_sync_non_join_memberships_results = self.get_success(
+            self.store.db_pool.simple_select_list(
+                "sliding_sync_non_join_memberships", None, retcols=("*",)
+            )
+        )
+        logger.info(
+            "sliding_sync_non_join_memberships %s",
+            sliding_sync_non_join_memberships_results,
+        )
+
+    def test_space_room_with_info(self) -> None:
+        """
+        TODO
+        """
+        user1_id = self.register_user("user1", "pass")
+        user1_tok = self.login(user1_id, "pass")
+        user2_id = self.register_user("user2", "pass")
+        user2_tok = self.login(user2_id, "pass")
+
+        space_room_id = self.helper.create_room_as(
+            user2_id,
+            tok=user2_tok,
+            extra_content={
+                "creation_content": {EventContentFields.ROOM_TYPE: RoomTypes.SPACE}
+            },
+        )
+        # Add a room name
+        self.helper.send_state(
+            space_room_id,
+            EventTypes.Name,
+            {"name": "my super duper space"},
+            tok=user2_tok,
+        )
+
+        # User1 joins the room
+        self.helper.join(space_room_id, user1_id, tok=user1_tok)
+
+        sliding_sync_joined_rooms_results = self.get_success(
+            self.store.db_pool.simple_select_list(
+                "sliding_sync_joined_rooms", None, retcols=("*",)
+            )
+        )
+        logger.info(
+            "sliding_sync_joined_rooms %s",
+            sliding_sync_joined_rooms_results,
+        )
+
+        sliding_sync_non_join_memberships_results = self.get_success(
+            self.store.db_pool.simple_select_list(
+                "sliding_sync_non_join_memberships", None, retcols=("*",)
+            )
+        )
+        logger.info(
+            "sliding_sync_non_join_memberships %s",
+            sliding_sync_non_join_memberships_results,
+        )
+
     def test_server_left_room(self) -> None:
         """
         TODO
@@ -533,4 +653,22 @@ class SlidingSyncPrePopulatedTablesTestCase(HomeserverTestCase):
         # User1 leaves the room
         self.helper.leave(room_id1, user1_id, tok=user1_tok)
 
-    # TODO: Server left room test
+        sliding_sync_joined_rooms_results = self.get_success(
+            self.store.db_pool.simple_select_list(
+                "sliding_sync_joined_rooms", None, retcols=("*",)
+            )
+        )
+        logger.info(
+            "sliding_sync_joined_rooms %s",
+            sliding_sync_joined_rooms_results,
+        )
+
+        sliding_sync_non_join_memberships_results = self.get_success(
+            self.store.db_pool.simple_select_list(
+                "sliding_sync_non_join_memberships", None, retcols=("*",)
+            )
+        )
+        logger.info(
+            "sliding_sync_non_join_memberships %s",
+            sliding_sync_non_join_memberships_results,
+        )