From 9bfc043225d62773b076b0a10d14cf8fecd22221 Mon Sep 17 00:00:00 2001 From: "H. Shay" Date: Wed, 8 Mar 2023 11:57:07 -0800 Subject: [PATCH 1/3] add topic and name events to group of events that are batch persisted when creating a room --- synapse/handlers/room.py | 89 +++++++++++++++------------------------- 1 file changed, 34 insertions(+), 55 deletions(-) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index 32451670f326..f91d6bd57d08 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -569,7 +569,7 @@ async def clone_existing_room( new_room_id, # we expect to override all the presets with initial_state, so this is # somewhat arbitrary. - preset_config=RoomCreationPreset.PRIVATE_CHAT, + room_config={"preset": RoomCreationPreset.PRIVATE_CHAT}, invite_list=[], initial_state=initial_state, creation_content=creation_content, @@ -904,13 +904,6 @@ async def create_room( check_membership=False, ) - preset_config = config.get( - "preset", - RoomCreationPreset.PRIVATE_CHAT - if visibility == "private" - else RoomCreationPreset.PUBLIC_CHAT, - ) - raw_initial_state = config.get("initial_state", []) initial_state = OrderedDict() @@ -929,7 +922,7 @@ async def create_room( ) = await self._send_events_for_new_room( requester, room_id, - preset_config=preset_config, + room_config=config, invite_list=invite_list, initial_state=initial_state, creation_content=creation_content, @@ -938,48 +931,6 @@ async def create_room( creator_join_profile=creator_join_profile, ) - if "name" in config: - name = config["name"] - ( - name_event, - last_stream_id, - ) = await self.event_creation_handler.create_and_send_nonmember_event( - requester, - { - "type": EventTypes.Name, - "room_id": room_id, - "sender": user_id, - "state_key": "", - "content": {"name": name}, - }, - ratelimit=False, - prev_event_ids=[last_sent_event_id], - depth=depth, - ) - last_sent_event_id = name_event.event_id - depth += 1 - - if "topic" in config: - topic = config["topic"] - ( - topic_event, - last_stream_id, - ) = await self.event_creation_handler.create_and_send_nonmember_event( - requester, - { - "type": EventTypes.Topic, - "room_id": room_id, - "sender": user_id, - "state_key": "", - "content": {"topic": topic}, - }, - ratelimit=False, - prev_event_ids=[last_sent_event_id], - depth=depth, - ) - last_sent_event_id = topic_event.event_id - depth += 1 - # we avoid dropping the lock between invites, as otherwise joins can # start coming in and making the createRoom slow. # @@ -1047,7 +998,7 @@ async def _send_events_for_new_room( self, creator: Requester, room_id: str, - preset_config: str, + room_config: JsonDict, invite_list: List[str], initial_state: MutableStateMap, creation_content: JsonDict, @@ -1068,6 +1019,13 @@ async def _send_events_for_new_room( A tuple containing the stream ID, event ID and depth of the last event sent to the room. """ + visibility = room_config.get("visibility", "private") + preset_config = room_config.get( + "preset", + RoomCreationPreset.PRIVATE_CHAT + if visibility == "private" + else RoomCreationPreset.PUBLIC_CHAT, + ) creator_id = creator.user.to_string() event_keys = {"room_id": room_id, "sender": creator_id, "state_key": ""} @@ -1079,9 +1037,6 @@ async def _send_events_for_new_room( # created (but not persisted to the db) to determine state for future created events # (as this info can't be pulled from the db) state_map: MutableStateMap[str] = {} - # current_state_group of last event created. Used for computing event context of - # events to be batched - current_state_group: Optional[int] = None def create_event_dict(etype: str, content: JsonDict, **kwargs: Any) -> JsonDict: e = {"type": etype, "content": content} @@ -1286,6 +1241,30 @@ async def create_event( ) events_to_send.append((encryption_event, encryption_context)) + if "name" in room_config: + name = room_config["name"] + name_event, name_context = await create_event( + EventTypes.Name, + {"name": name}, + True, + room_id=room_id, + sender=creator.user.to_string(), + state_key="", + ) + events_to_send.append((name_event, name_context)) + + if "topic" in room_config: + topic = room_config["topic"] + topic_event, topic_context = await create_event( + EventTypes.Topic, + {"topic": topic}, + True, + room_id=room_id, + sender=creator.user.to_string(), + state_key="", + ) + events_to_send.append((topic_event, topic_context)) + datastore = self.hs.get_datastores().state events_and_context = ( await UnpersistedEventContext.batch_persist_unpersisted_contexts( From 745d9663434e95e138a4a53d50b897650800df5c Mon Sep 17 00:00:00 2001 From: "H. Shay" Date: Wed, 8 Mar 2023 11:59:56 -0800 Subject: [PATCH 2/3] newsfragment --- changelog.d/15229.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/15229.misc diff --git a/changelog.d/15229.misc b/changelog.d/15229.misc new file mode 100644 index 000000000000..4d8ea03b27c6 --- /dev/null +++ b/changelog.d/15229.misc @@ -0,0 +1 @@ +Add topic and name events to group of events that are batch persisted when creating a room. From 33b4eec968e238f95955b353e4ff277b5cbb62e3 Mon Sep 17 00:00:00 2001 From: "H. Shay" Date: Wed, 8 Mar 2023 13:11:38 -0800 Subject: [PATCH 3/3] requested changes --- synapse/handlers/room.py | 45 +++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index f91d6bd57d08..be120cb12f36 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -1015,18 +1015,33 @@ async def _send_events_for_new_room( Rate limiting should already have been applied by this point. + Args: + creator: + the user requesting the room creation + room_id: + room id for the room being created + room_config: + A dict of configuration options. This will be the body of + a /createRoom request; see + https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3createroom + invite_list: + a list of user ids to invite to the room + initial_state: + A list of state events to set in the new room. + creation_content: + Extra keys, such as m.federate, to be added to the content of the m.room.create event. + room_alias: + alias for the room + power_level_content_override: + The power level content to override in the default power level event. + creator_join_profile: + Set to override the displayname and avatar for the creating + user in this room. + Returns: A tuple containing the stream ID, event ID and depth of the last event sent to the room. """ - visibility = room_config.get("visibility", "private") - preset_config = room_config.get( - "preset", - RoomCreationPreset.PRIVATE_CHAT - if visibility == "private" - else RoomCreationPreset.PUBLIC_CHAT, - ) - creator_id = creator.user.to_string() event_keys = {"room_id": room_id, "sender": creator_id, "state_key": ""} depth = 1 @@ -1090,6 +1105,14 @@ async def create_event( return new_event, new_unpersisted_context + visibility = room_config.get("visibility", "private") + preset_config = room_config.get( + "preset", + RoomCreationPreset.PRIVATE_CHAT + if visibility == "private" + else RoomCreationPreset.PUBLIC_CHAT, + ) + try: config = self._presets_dict[preset_config] except KeyError: @@ -1247,9 +1270,6 @@ async def create_event( EventTypes.Name, {"name": name}, True, - room_id=room_id, - sender=creator.user.to_string(), - state_key="", ) events_to_send.append((name_event, name_context)) @@ -1259,9 +1279,6 @@ async def create_event( EventTypes.Topic, {"topic": topic}, True, - room_id=room_id, - sender=creator.user.to_string(), - state_key="", ) events_to_send.append((topic_event, topic_context))