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

Commit be4ea20

Browse files
authored
Add topic and name events to group of events that are batch persisted when creating a room. (#15229)
1 parent 88efc75 commit be4ea20

File tree

2 files changed

+53
-56
lines changed

2 files changed

+53
-56
lines changed

changelog.d/15229.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add topic and name events to group of events that are batch persisted when creating a room.

synapse/handlers/room.py

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ async def clone_existing_room(
569569
new_room_id,
570570
# we expect to override all the presets with initial_state, so this is
571571
# somewhat arbitrary.
572-
preset_config=RoomCreationPreset.PRIVATE_CHAT,
572+
room_config={"preset": RoomCreationPreset.PRIVATE_CHAT},
573573
invite_list=[],
574574
initial_state=initial_state,
575575
creation_content=creation_content,
@@ -904,13 +904,6 @@ async def create_room(
904904
check_membership=False,
905905
)
906906

907-
preset_config = config.get(
908-
"preset",
909-
RoomCreationPreset.PRIVATE_CHAT
910-
if visibility == "private"
911-
else RoomCreationPreset.PUBLIC_CHAT,
912-
)
913-
914907
raw_initial_state = config.get("initial_state", [])
915908

916909
initial_state = OrderedDict()
@@ -929,7 +922,7 @@ async def create_room(
929922
) = await self._send_events_for_new_room(
930923
requester,
931924
room_id,
932-
preset_config=preset_config,
925+
room_config=config,
933926
invite_list=invite_list,
934927
initial_state=initial_state,
935928
creation_content=creation_content,
@@ -938,48 +931,6 @@ async def create_room(
938931
creator_join_profile=creator_join_profile,
939932
)
940933

941-
if "name" in config:
942-
name = config["name"]
943-
(
944-
name_event,
945-
last_stream_id,
946-
) = await self.event_creation_handler.create_and_send_nonmember_event(
947-
requester,
948-
{
949-
"type": EventTypes.Name,
950-
"room_id": room_id,
951-
"sender": user_id,
952-
"state_key": "",
953-
"content": {"name": name},
954-
},
955-
ratelimit=False,
956-
prev_event_ids=[last_sent_event_id],
957-
depth=depth,
958-
)
959-
last_sent_event_id = name_event.event_id
960-
depth += 1
961-
962-
if "topic" in config:
963-
topic = config["topic"]
964-
(
965-
topic_event,
966-
last_stream_id,
967-
) = await self.event_creation_handler.create_and_send_nonmember_event(
968-
requester,
969-
{
970-
"type": EventTypes.Topic,
971-
"room_id": room_id,
972-
"sender": user_id,
973-
"state_key": "",
974-
"content": {"topic": topic},
975-
},
976-
ratelimit=False,
977-
prev_event_ids=[last_sent_event_id],
978-
depth=depth,
979-
)
980-
last_sent_event_id = topic_event.event_id
981-
depth += 1
982-
983934
# we avoid dropping the lock between invites, as otherwise joins can
984935
# start coming in and making the createRoom slow.
985936
#
@@ -1047,7 +998,7 @@ async def _send_events_for_new_room(
1047998
self,
1048999
creator: Requester,
10491000
room_id: str,
1050-
preset_config: str,
1001+
room_config: JsonDict,
10511002
invite_list: List[str],
10521003
initial_state: MutableStateMap,
10531004
creation_content: JsonDict,
@@ -1064,11 +1015,33 @@ async def _send_events_for_new_room(
10641015
10651016
Rate limiting should already have been applied by this point.
10661017
1018+
Args:
1019+
creator:
1020+
the user requesting the room creation
1021+
room_id:
1022+
room id for the room being created
1023+
room_config:
1024+
A dict of configuration options. This will be the body of
1025+
a /createRoom request; see
1026+
https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3createroom
1027+
invite_list:
1028+
a list of user ids to invite to the room
1029+
initial_state:
1030+
A list of state events to set in the new room.
1031+
creation_content:
1032+
Extra keys, such as m.federate, to be added to the content of the m.room.create event.
1033+
room_alias:
1034+
alias for the room
1035+
power_level_content_override:
1036+
The power level content to override in the default power level event.
1037+
creator_join_profile:
1038+
Set to override the displayname and avatar for the creating
1039+
user in this room.
1040+
10671041
Returns:
10681042
A tuple containing the stream ID, event ID and depth of the last
10691043
event sent to the room.
10701044
"""
1071-
10721045
creator_id = creator.user.to_string()
10731046
event_keys = {"room_id": room_id, "sender": creator_id, "state_key": ""}
10741047
depth = 1
@@ -1079,9 +1052,6 @@ async def _send_events_for_new_room(
10791052
# created (but not persisted to the db) to determine state for future created events
10801053
# (as this info can't be pulled from the db)
10811054
state_map: MutableStateMap[str] = {}
1082-
# current_state_group of last event created. Used for computing event context of
1083-
# events to be batched
1084-
current_state_group: Optional[int] = None
10851055

10861056
def create_event_dict(etype: str, content: JsonDict, **kwargs: Any) -> JsonDict:
10871057
e = {"type": etype, "content": content}
@@ -1135,6 +1105,14 @@ async def create_event(
11351105

11361106
return new_event, new_unpersisted_context
11371107

1108+
visibility = room_config.get("visibility", "private")
1109+
preset_config = room_config.get(
1110+
"preset",
1111+
RoomCreationPreset.PRIVATE_CHAT
1112+
if visibility == "private"
1113+
else RoomCreationPreset.PUBLIC_CHAT,
1114+
)
1115+
11381116
try:
11391117
config = self._presets_dict[preset_config]
11401118
except KeyError:
@@ -1286,6 +1264,24 @@ async def create_event(
12861264
)
12871265
events_to_send.append((encryption_event, encryption_context))
12881266

1267+
if "name" in room_config:
1268+
name = room_config["name"]
1269+
name_event, name_context = await create_event(
1270+
EventTypes.Name,
1271+
{"name": name},
1272+
True,
1273+
)
1274+
events_to_send.append((name_event, name_context))
1275+
1276+
if "topic" in room_config:
1277+
topic = room_config["topic"]
1278+
topic_event, topic_context = await create_event(
1279+
EventTypes.Topic,
1280+
{"topic": topic},
1281+
True,
1282+
)
1283+
events_to_send.append((topic_event, topic_context))
1284+
12891285
datastore = self.hs.get_datastores().state
12901286
events_and_context = (
12911287
await UnpersistedEventContext.batch_persist_unpersisted_contexts(

0 commit comments

Comments
 (0)