@@ -79,6 +79,97 @@ async fn test_subscribe_thread() {
79
79
assert_matches ! ( subscription, None ) ;
80
80
}
81
81
82
+ #[ async_test]
83
+ async fn test_subscribe_thread_if_needed ( ) {
84
+ let server = MatrixMockServer :: new ( ) . await ;
85
+ let client = server. client_builder ( ) . build ( ) . await ;
86
+
87
+ let room_id = room_id ! ( "!test:example.org" ) ;
88
+ let room = server. sync_joined_room ( & client, room_id) . await ;
89
+
90
+ // If there's no prior subscription, the function `subscribe_thread_if_needed`
91
+ // will automatically subscribe to the thread, whether the new subscription
92
+ // is automatic or not.
93
+ for ( root_id, automatic) in [
94
+ ( owned_event_id ! ( "$root" ) , None ) ,
95
+ ( owned_event_id ! ( "$woot" ) , Some ( owned_event_id ! ( "$woot" ) ) ) ,
96
+ ] {
97
+ server
98
+ . mock_put_thread_subscription ( )
99
+ . match_room_id ( room_id. to_owned ( ) )
100
+ . match_thread_id ( root_id. clone ( ) )
101
+ . ok ( )
102
+ . mock_once ( )
103
+ . mount ( )
104
+ . await ;
105
+
106
+ room. subscribe_thread_if_needed ( & root_id, automatic) . await . unwrap ( ) ;
107
+ }
108
+
109
+ // If there's a prior automatic subscription, the function
110
+ // `subscribe_thread_if_needed` will only subscribe to the thread if the new
111
+ // subscription is manual.
112
+ {
113
+ let root_id = owned_event_id ! ( "$toot" ) ;
114
+
115
+ server
116
+ . mock_get_thread_subscription ( )
117
+ . match_room_id ( room_id. to_owned ( ) )
118
+ . match_thread_id ( root_id. clone ( ) )
119
+ . ok ( true )
120
+ . mock_once ( )
121
+ . mount ( )
122
+ . await ;
123
+
124
+ server
125
+ . mock_put_thread_subscription ( )
126
+ . match_room_id ( room_id. to_owned ( ) )
127
+ . match_thread_id ( root_id. clone ( ) )
128
+ . ok ( )
129
+ . mock_once ( )
130
+ . mount ( )
131
+ . await ;
132
+
133
+ room. subscribe_thread_if_needed ( & root_id, None ) . await . unwrap ( ) ;
134
+ }
135
+
136
+ // Otherwise, it will be a no-op.
137
+ {
138
+ let root_id = owned_event_id ! ( "$foot" ) ;
139
+
140
+ server
141
+ . mock_get_thread_subscription ( )
142
+ . match_room_id ( room_id. to_owned ( ) )
143
+ . match_thread_id ( root_id. clone ( ) )
144
+ . ok ( true )
145
+ . mock_once ( )
146
+ . mount ( )
147
+ . await ;
148
+
149
+ room. subscribe_thread_if_needed ( & root_id, Some ( owned_event_id ! ( "$foot" ) ) ) . await . unwrap ( ) ;
150
+ }
151
+
152
+ // The function `subscribe_thread_if_needed` is a no-op if there's a prior
153
+ // manual subscription, whether the new subscription is automatic or not.
154
+ for ( root_id, automatic) in [
155
+ ( owned_event_id ! ( "$root" ) , None ) ,
156
+ ( owned_event_id ! ( "$woot" ) , Some ( owned_event_id ! ( "$woot" ) ) ) ,
157
+ ] {
158
+ server
159
+ . mock_get_thread_subscription ( )
160
+ . match_room_id ( room_id. to_owned ( ) )
161
+ . match_thread_id ( root_id. clone ( ) )
162
+ . ok ( false )
163
+ . mock_once ( )
164
+ . mount ( )
165
+ . await ;
166
+
167
+ // No-op! (The PUT endpoint hasn't been mocked, so this would result in a 404 if
168
+ // it were trying to hit it.)
169
+ room. subscribe_thread_if_needed ( & root_id, automatic) . await . unwrap ( ) ;
170
+ }
171
+ }
172
+
82
173
#[ async_test]
83
174
async fn test_thread_push_rule_is_triggered_for_subscribed_threads ( ) {
84
175
// This test checks that the evaluation of push rules for threads will correctly
0 commit comments