@@ -493,6 +493,7 @@ async def create_event(
493493 allow_no_prev_events : bool = False ,
494494 prev_event_ids : Optional [List [str ]] = None ,
495495 auth_event_ids : Optional [List [str ]] = None ,
496+ state_event_ids : Optional [List [str ]] = None ,
496497 require_consent : bool = True ,
497498 outlier : bool = False ,
498499 historical : bool = False ,
@@ -527,6 +528,15 @@ async def create_event(
527528
528529 If non-None, prev_event_ids must also be provided.
529530
531+ state_event_ids:
532+ The full state at a given event. This is used particularly by the MSC2716
533+ /batch_send endpoint. One use case is with insertion events which float at
534+ the beginning of a historical batch and don't have any `prev_events` to
535+ derive from; we add all of these state events as the explicit state so the
536+ rest of the historical batch can inherit the same state and state_group.
537+ This should normally be left as None, which will cause the auth_event_ids
538+ to be calculated based on the room state at the prev_events.
539+
530540 require_consent: Whether to check if the requester has
531541 consented to the privacy policy.
532542
@@ -612,6 +622,7 @@ async def create_event(
612622 allow_no_prev_events = allow_no_prev_events ,
613623 prev_event_ids = prev_event_ids ,
614624 auth_event_ids = auth_event_ids ,
625+ state_event_ids = state_event_ids ,
615626 depth = depth ,
616627 )
617628
@@ -772,6 +783,7 @@ async def create_and_send_nonmember_event(
772783 allow_no_prev_events : bool = False ,
773784 prev_event_ids : Optional [List [str ]] = None ,
774785 auth_event_ids : Optional [List [str ]] = None ,
786+ state_event_ids : Optional [List [str ]] = None ,
775787 ratelimit : bool = True ,
776788 txn_id : Optional [str ] = None ,
777789 ignore_shadow_ban : bool = False ,
@@ -801,6 +813,14 @@ async def create_and_send_nonmember_event(
801813 based on the room state at the prev_events.
802814
803815 If non-None, prev_event_ids must also be provided.
816+ state_event_ids:
817+ The full state at a given event. This is used particularly by the MSC2716
818+ /batch_send endpoint. One use case is with insertion events which float at
819+ the beginning of a historical batch and don't have any `prev_events` to
820+ derive from; we add all of these state events as the explicit state so the
821+ rest of the historical batch can inherit the same state and state_group.
822+ This should normally be left as None, which will cause the auth_event_ids
823+ to be calculated based on the room state at the prev_events.
804824 ratelimit: Whether to rate limit this send.
805825 txn_id: The transaction ID.
806826 ignore_shadow_ban: True if shadow-banned users should be allowed to
@@ -856,8 +876,10 @@ async def create_and_send_nonmember_event(
856876 requester ,
857877 event_dict ,
858878 txn_id = txn_id ,
879+ allow_no_prev_events = allow_no_prev_events ,
859880 prev_event_ids = prev_event_ids ,
860881 auth_event_ids = auth_event_ids ,
882+ state_event_ids = state_event_ids ,
861883 outlier = outlier ,
862884 historical = historical ,
863885 depth = depth ,
@@ -893,6 +915,7 @@ async def create_new_client_event(
893915 allow_no_prev_events : bool = False ,
894916 prev_event_ids : Optional [List [str ]] = None ,
895917 auth_event_ids : Optional [List [str ]] = None ,
918+ state_event_ids : Optional [List [str ]] = None ,
896919 depth : Optional [int ] = None ,
897920 ) -> Tuple [EventBase , EventContext ]:
898921 """Create a new event for a local client
@@ -915,38 +938,42 @@ async def create_new_client_event(
915938 Should normally be left as None, which will cause them to be calculated
916939 based on the room state at the prev_events.
917940
941+ state_event_ids:
942+ The full state at a given event. This is used particularly by the MSC2716
943+ /batch_send endpoint. One use case is with insertion events which float at
944+ the beginning of a historical batch and don't have any `prev_events` to
945+ derive from; we add all of these state events as the explicit state so the
946+ rest of the historical batch can inherit the same state and state_group.
947+ This should normally be left as None, which will cause the auth_event_ids
948+ to be calculated based on the room state at the prev_events.
949+
918950 depth: Override the depth used to order the event in the DAG.
919951 Should normally be set to None, which will cause the depth to be calculated
920952 based on the prev_events.
921953
922954 Returns:
923955 Tuple of created event, context
924956 """
925- # Strip down the auth_event_ids to only what we need to auth the event.
957+ # Strip down the state_event_ids to only what we need to auth the event.
926958 # For example, we don't need extra m.room.member that don't match event.sender
927- full_state_ids_at_event = None
928- if auth_event_ids is not None :
929- # If auth events are provided, prev events must be also .
959+ if state_event_ids is not None :
960+ # Do a quick check to make sure that prev_event_ids is present to
961+ # make the type-checking around `builder.build` happy .
930962 # prev_event_ids could be an empty array though.
931963 assert prev_event_ids is not None
932964
933- # Copy the full auth state before it stripped down
934- full_state_ids_at_event = auth_event_ids .copy ()
935-
936965 temp_event = await builder .build (
937966 prev_event_ids = prev_event_ids ,
938- auth_event_ids = auth_event_ids ,
967+ auth_event_ids = state_event_ids ,
939968 depth = depth ,
940969 )
941- auth_events = await self .store .get_events_as_list (auth_event_ids )
970+ state_events = await self .store .get_events_as_list (state_event_ids )
942971 # Create a StateMap[str]
943- auth_event_state_map = {
944- (e .type , e .state_key ): e .event_id for e in auth_events
945- }
946- # Actually strip down and use the necessary auth events
972+ state_map = {(e .type , e .state_key ): e .event_id for e in state_events }
973+ # Actually strip down and only use the necessary auth events
947974 auth_event_ids = self ._event_auth_handler .compute_auth_events (
948975 event = temp_event ,
949- current_state_ids = auth_event_state_map ,
976+ current_state_ids = state_map ,
950977 for_verification = False ,
951978 )
952979
@@ -989,12 +1016,16 @@ async def create_new_client_event(
9891016 context = EventContext .for_outlier ()
9901017 elif (
9911018 event .type == EventTypes .MSC2716_INSERTION
992- and full_state_ids_at_event
1019+ and state_event_ids
9931020 and builder .internal_metadata .is_historical ()
9941021 ):
1022+ # Add explicit state to the insertion event so it has state to derive
1023+ # from even though it's floating with no `prev_events`. The rest of
1024+ # the batch can derive from this state and state_group.
1025+ #
9951026 # TODO(faster_joins): figure out how this works, and make sure that the
9961027 # old state is complete.
997- old_state = await self .store .get_events_as_list (full_state_ids_at_event )
1028+ old_state = await self .store .get_events_as_list (state_event_ids )
9981029 context = await self .state .compute_event_context (event , old_state = old_state )
9991030 else :
10001031 context = await self .state .compute_event_context (event )
0 commit comments