@@ -896,6 +896,7 @@ def _test_bundled_aggregations(
896896 relation_type : str ,
897897 assertion_callable : Callable [[JsonDict ], None ],
898898 expected_db_txn_for_event : int ,
899+ access_token : Optional [str ] = None ,
899900 ) -> None :
900901 """
901902 Makes requests to various endpoints which should include bundled aggregations
@@ -907,7 +908,9 @@ def _test_bundled_aggregations(
907908 for relation-specific assertions.
908909 expected_db_txn_for_event: The number of database transactions which
909910 are expected for a call to /event/.
911+ access_token: The access token to user, defaults to self.user_token.
910912 """
913+ access_token = access_token or self .user_token
911914
912915 def assert_bundle (event_json : JsonDict ) -> None :
913916 """Assert the expected values of the bundled aggregations."""
@@ -921,7 +924,7 @@ def assert_bundle(event_json: JsonDict) -> None:
921924 channel = self .make_request (
922925 "GET" ,
923926 f"/rooms/{ self .room } /event/{ self .parent_id } " ,
924- access_token = self . user_token ,
927+ access_token = access_token ,
925928 )
926929 self .assertEqual (200 , channel .code , channel .json_body )
927930 assert_bundle (channel .json_body )
@@ -932,7 +935,7 @@ def assert_bundle(event_json: JsonDict) -> None:
932935 channel = self .make_request (
933936 "GET" ,
934937 f"/rooms/{ self .room } /messages?dir=b" ,
935- access_token = self . user_token ,
938+ access_token = access_token ,
936939 )
937940 self .assertEqual (200 , channel .code , channel .json_body )
938941 assert_bundle (self ._find_event_in_chunk (channel .json_body ["chunk" ]))
@@ -941,15 +944,15 @@ def assert_bundle(event_json: JsonDict) -> None:
941944 channel = self .make_request (
942945 "GET" ,
943946 f"/rooms/{ self .room } /context/{ self .parent_id } " ,
944- access_token = self . user_token ,
947+ access_token = access_token ,
945948 )
946949 self .assertEqual (200 , channel .code , channel .json_body )
947950 assert_bundle (channel .json_body ["event" ])
948951
949952 # Request sync.
950953 filter = urllib .parse .quote_plus (b'{"room": {"timeline": {"limit": 4}}}' )
951954 channel = self .make_request (
952- "GET" , f"/sync?filter={ filter } " , access_token = self . user_token
955+ "GET" , f"/sync?filter={ filter } " , access_token = access_token
953956 )
954957 self .assertEqual (200 , channel .code , channel .json_body )
955958 room_timeline = channel .json_body ["rooms" ]["join" ][self .room ]["timeline" ]
@@ -962,7 +965,7 @@ def assert_bundle(event_json: JsonDict) -> None:
962965 "/search" ,
963966 # Search term matches the parent message.
964967 content = {"search_categories" : {"room_events" : {"search_term" : "Hi" }}},
965- access_token = self . user_token ,
968+ access_token = access_token ,
966969 )
967970 self .assertEqual (200 , channel .code , channel .json_body )
968971 chunk = [
@@ -1037,30 +1040,60 @@ def test_thread(self) -> None:
10371040 """
10381041 Test that threads get correctly bundled.
10391042 """
1040- self ._send_relation (RelationTypes .THREAD , "m.room.test" )
1041- channel = self ._send_relation (RelationTypes .THREAD , "m.room.test" )
1043+ # The root message is from "user", send replies as "user2".
1044+ self ._send_relation (
1045+ RelationTypes .THREAD , "m.room.test" , access_token = self .user2_token
1046+ )
1047+ channel = self ._send_relation (
1048+ RelationTypes .THREAD , "m.room.test" , access_token = self .user2_token
1049+ )
10421050 thread_2 = channel .json_body ["event_id" ]
10431051
1044- def assert_thread (bundled_aggregations : JsonDict ) -> None :
1045- self .assertEqual (2 , bundled_aggregations .get ("count" ))
1046- self .assertTrue (bundled_aggregations .get ("current_user_participated" ))
1047- # The latest thread event has some fields that don't matter.
1048- self .assert_dict (
1049- {
1050- "content" : {
1051- "m.relates_to" : {
1052- "event_id" : self .parent_id ,
1053- "rel_type" : RelationTypes .THREAD ,
1054- }
1052+ # This needs two assertion functions which are identical except for whether
1053+ # the current_user_participated flag is True, create a factory for the
1054+ # two versions.
1055+ def _gen_assert (participated : bool ) -> Callable [[JsonDict ], None ]:
1056+ def assert_thread (bundled_aggregations : JsonDict ) -> None :
1057+ self .assertEqual (2 , bundled_aggregations .get ("count" ))
1058+ self .assertEqual (
1059+ participated , bundled_aggregations .get ("current_user_participated" )
1060+ )
1061+ # The latest thread event has some fields that don't matter.
1062+ self .assert_dict (
1063+ {
1064+ "content" : {
1065+ "m.relates_to" : {
1066+ "event_id" : self .parent_id ,
1067+ "rel_type" : RelationTypes .THREAD ,
1068+ }
1069+ },
1070+ "event_id" : thread_2 ,
1071+ "sender" : self .user2_id ,
1072+ "type" : "m.room.test" ,
10551073 },
1056- "event_id" : thread_2 ,
1057- "sender" : self .user_id ,
1058- "type" : "m.room.test" ,
1059- },
1060- bundled_aggregations .get ("latest_event" ),
1061- )
1074+ bundled_aggregations .get ("latest_event" ),
1075+ )
10621076
1063- self ._test_bundled_aggregations (RelationTypes .THREAD , assert_thread , 9 )
1077+ return assert_thread
1078+
1079+ # The "user" sent the root event and is making queries for the bundled
1080+ # aggregations: they have participated.
1081+ self ._test_bundled_aggregations (RelationTypes .THREAD , _gen_assert (True ), 8 )
1082+ # The "user2" sent replies in the thread and is making queries for the
1083+ # bundled aggregations: they have participated.
1084+ #
1085+ # Note that this re-uses some cached values, so the total number of
1086+ # queries is much smaller.
1087+ self ._test_bundled_aggregations (
1088+ RelationTypes .THREAD , _gen_assert (True ), 2 , access_token = self .user2_token
1089+ )
1090+
1091+ # A user with no interactions with the thread: they have not participated.
1092+ user3_id , user3_token = self ._create_user ("charlie" )
1093+ self .helper .join (self .room , user = user3_id , tok = user3_token )
1094+ self ._test_bundled_aggregations (
1095+ RelationTypes .THREAD , _gen_assert (False ), 2 , access_token = user3_token
1096+ )
10641097
10651098 def test_thread_with_bundled_aggregations_for_latest (self ) -> None :
10661099 """
@@ -1106,7 +1139,7 @@ def assert_thread(bundled_aggregations: JsonDict) -> None:
11061139 bundled_aggregations ["latest_event" ].get ("unsigned" ),
11071140 )
11081141
1109- self ._test_bundled_aggregations (RelationTypes .THREAD , assert_thread , 9 )
1142+ self ._test_bundled_aggregations (RelationTypes .THREAD , assert_thread , 8 )
11101143
11111144 def test_nested_thread (self ) -> None :
11121145 """
0 commit comments