batch some events to send

This commit is contained in:
H. Shay 2022-08-11 21:13:59 -07:00
parent a08f32f8ed
commit e215109b74

View file

@ -1040,9 +1040,7 @@ class RoomCreationHandler:
""" """
creator_id = creator.user.to_string() creator_id = creator.user.to_string()
event_keys = {"room_id": room_id, "sender": creator_id, "state_key": ""} event_keys = {"room_id": room_id, "sender": creator_id, "state_key": ""}
depth = 1 depth = 1
last_sent_event_id: Optional[str] = None last_sent_event_id: Optional[str] = None
@ -1068,7 +1066,7 @@ class RoomCreationHandler:
prev_event_ids=[last_sent_event_id] if last_sent_event_id else [], prev_event_ids=[last_sent_event_id] if last_sent_event_id else [],
depth=depth, depth=depth,
) )
depth += 1
return event, context return event, context
async def send( async def send(
@ -1077,7 +1075,6 @@ class RoomCreationHandler:
creator: Requester, creator: Requester,
) -> int: ) -> int:
nonlocal last_sent_event_id nonlocal last_sent_event_id
nonlocal depth
assert self.hs.is_mine_id(event.sender), "User must be our own: %s" % ( assert self.hs.is_mine_id(event.sender), "User must be our own: %s" % (
event.sender, event.sender,
) )
@ -1091,7 +1088,6 @@ class RoomCreationHandler:
) )
last_sent_event_id = ev.event_id last_sent_event_id = ev.event_id
depth += 1
# we know it was persisted, so must have a stream ordering # we know it was persisted, so must have a stream ordering
assert ev.internal_metadata.stream_ordering assert ev.internal_metadata.stream_ordering
@ -1126,6 +1122,7 @@ class RoomCreationHandler:
depth=depth, depth=depth,
) )
last_sent_event_id = member_event_id last_sent_event_id = member_event_id
depth += 1
# We treat the power levels override specially as this needs to be one # We treat the power levels override specially as this needs to be one
# of the first events that get sent into a room. # of the first events that get sent into a room.
@ -1183,31 +1180,27 @@ class RoomCreationHandler:
) )
last_sent_stream_id = await send(pl_event, pl_context, creator) last_sent_stream_id = await send(pl_event, pl_context, creator)
events_to_send = []
if room_alias and (EventTypes.CanonicalAlias, "") not in initial_state: if room_alias and (EventTypes.CanonicalAlias, "") not in initial_state:
room_alias_event, room_alias_context = await create_event( room_alias_event, room_alias_context = await create_event(
etype=EventTypes.CanonicalAlias, etype=EventTypes.CanonicalAlias,
content={"alias": room_alias.to_string()}, content={"alias": room_alias.to_string()},
) )
last_sent_stream_id = await send( events_to_send.append((room_alias_event, room_alias_context))
room_alias_event, room_alias_context, creator
)
if (EventTypes.JoinRules, "") not in initial_state: if (EventTypes.JoinRules, "") not in initial_state:
join_rules_event, join_rules_context = await create_event( join_rules_event, join_rules_context = await create_event(
etype=EventTypes.JoinRules, content={"join_rule": config["join_rules"]} etype=EventTypes.JoinRules, content={"join_rule": config["join_rules"]}
) )
last_sent_stream_id = await send(
join_rules_event, join_rules_context, creator events_to_send.append((join_rules_event, join_rules_context))
)
if (EventTypes.RoomHistoryVisibility, "") not in initial_state: if (EventTypes.RoomHistoryVisibility, "") not in initial_state:
visibility_event, visibility_context = await create_event( visibility_event, visibility_context = await create_event(
etype=EventTypes.RoomHistoryVisibility, etype=EventTypes.RoomHistoryVisibility,
content={"history_visibility": config["history_visibility"]}, content={"history_visibility": config["history_visibility"]},
) )
last_sent_stream_id = await send( events_to_send.append((visibility_event, visibility_context))
visibility_event, visibility_context, creator
)
if config["guest_can_join"]: if config["guest_can_join"]:
if (EventTypes.GuestAccess, "") not in initial_state: if (EventTypes.GuestAccess, "") not in initial_state:
@ -1215,18 +1208,13 @@ class RoomCreationHandler:
etype=EventTypes.GuestAccess, etype=EventTypes.GuestAccess,
content={EventContentFields.GUEST_ACCESS: GuestAccess.CAN_JOIN}, content={EventContentFields.GUEST_ACCESS: GuestAccess.CAN_JOIN},
) )
last_sent_stream_id = await send( events_to_send.append((guest_access_event, guest_access_context))
guest_access_event, guest_access_context, creator
)
events = []
for (etype, state_key), content in initial_state.items(): for (etype, state_key), content in initial_state.items():
event, context = await create_event( event, context = await create_event(
etype=etype, state_key=state_key, content=content etype=etype, state_key=state_key, content=content
) )
events.append((event, context)) events_to_send.append((event, context))
for event, context in events:
last_sent_stream_id = await send(event, context, creator)
if config["encrypted"]: if config["encrypted"]:
encryption_event, encryption_context = await create_event( encryption_event, encryption_context = await create_event(
@ -1234,10 +1222,10 @@ class RoomCreationHandler:
state_key="", state_key="",
content={"algorithm": RoomEncryptionAlgorithms.DEFAULT}, content={"algorithm": RoomEncryptionAlgorithms.DEFAULT},
) )
last_sent_stream_id = await send( events_to_send.append((encryption_event, encryption_context))
encryption_event, encryption_context, creator
)
for event, context in events_to_send:
last_sent_stream_id = await send(event, context, creator)
return last_sent_stream_id, last_sent_event_id, depth return last_sent_stream_id, last_sent_event_id, depth
def _generate_room_id(self) -> str: def _generate_room_id(self) -> str: