@@ -117,11 +117,11 @@ func knockingBetweenTwoUsersTest(t *testing.T, roomID string, inRoomUser, knocki
117
117
})
118
118
119
119
t .Run ("Knocking on a room with join rule 'knock' should succeed" , func (t * testing.T ) {
120
- knockOnRoomSynced (t , knockingUser , roomID , testKnockReason , []string {"hs1" })
120
+ mustKnockOnRoomSynced (t , knockingUser , roomID , testKnockReason , []string {"hs1" })
121
121
})
122
122
123
123
t .Run ("A user that has already knocked is allowed to knock again on the same room" , func (t * testing.T ) {
124
- knockOnRoomSynced (t , knockingUser , roomID , "I really like knock knock jokes" , []string {"hs1" })
124
+ mustKnockOnRoomSynced (t , knockingUser , roomID , "I really like knock knock jokes" , []string {"hs1" })
125
125
})
126
126
127
127
t .Run ("Users in the room see a user's membership update when they knock" , func (t * testing.T ) {
@@ -183,7 +183,7 @@ func knockingBetweenTwoUsersTest(t *testing.T, roomID string, inRoomUser, knocki
183
183
})
184
184
185
185
// Knock again to return us to the knocked state
186
- knockOnRoomSynced (t , knockingUser , roomID , "Let me in... again?" , []string {"hs1" })
186
+ mustKnockOnRoomSynced (t , knockingUser , roomID , "Let me in... again?" , []string {"hs1" })
187
187
})
188
188
}
189
189
@@ -211,7 +211,7 @@ func knockingBetweenTwoUsersTest(t *testing.T, roomID string, inRoomUser, knocki
211
211
}))
212
212
213
213
// Knock again
214
- knockOnRoomSynced (t , knockingUser , roomID , "Pleeease let me in?" , []string {"hs1" })
214
+ mustKnockOnRoomSynced (t , knockingUser , roomID , "Pleeease let me in?" , []string {"hs1" })
215
215
})
216
216
217
217
t .Run ("A user can knock on a room without a reason" , func (t * testing.T ) {
@@ -227,7 +227,7 @@ func knockingBetweenTwoUsersTest(t *testing.T, roomID string, inRoomUser, knocki
227
227
)
228
228
229
229
// Knock again, this time without a reason
230
- knockOnRoomSynced (t , knockingUser , roomID , "" , []string {"hs1" })
230
+ mustKnockOnRoomSynced (t , knockingUser , roomID , "" , []string {"hs1" })
231
231
})
232
232
233
233
t .Run ("A user in the room can accept a knock" , func (t * testing.T ) {
@@ -279,22 +279,36 @@ func knockingBetweenTwoUsersTest(t *testing.T, roomID string, inRoomUser, knocki
279
279
})
280
280
}
281
281
282
- // knockOnRoomSynced will knock on a given room on the behalf of a user, and block until the knock has persisted.
282
+ func syncKnockedOn (userID , roomID string ) client.SyncCheckOpt {
283
+ return func (clientUserID string , topLevelSyncJSON gjson.Result ) error {
284
+ // two forms which depend on what the client user is:
285
+ // - passively viewing a membership for a room you're joined in
286
+ // - actively leaving the room
287
+ if clientUserID == userID {
288
+ events := topLevelSyncJSON .Get ("rooms.knock." + client .GjsonEscape (roomID ) + ".knock_state.events" )
289
+ if events .Exists () && events .IsArray () {
290
+ // We don't currently define any required state event types to be sent.
291
+ // If we've reached this point, then an entry for this room was found
292
+ return nil
293
+ }
294
+ return fmt .Errorf ("no knock section for room %s" , roomID )
295
+ }
296
+
297
+ // passive
298
+ return client .SyncTimelineHas (roomID , func (ev gjson.Result ) bool {
299
+ return ev .Get ("type" ).Str == "m.room.member" && ev .Get ("state_key" ).Str == userID && ev .Get ("content.membership" ).Str == "knock"
300
+ })(clientUserID , topLevelSyncJSON )
301
+ }
302
+ }
303
+
304
+ // mustKnockOnRoomSynced will knock on a given room on the behalf of a user, and block until the knock has persisted.
283
305
// serverNames should be populated if knocking on a room that the user's homeserver isn't currently a part of.
284
306
// Fails the test if the knock response does not return a 200 status code.
285
- func knockOnRoomSynced (t * testing.T , c * client.CSAPI , roomID , reason string , serverNames []string ) {
307
+ func mustKnockOnRoomSynced (t * testing.T , c * client.CSAPI , roomID , reason string , serverNames []string ) {
286
308
knockOnRoomWithStatus (t , c , roomID , reason , serverNames , 200 )
287
309
288
310
// The knock should have succeeded. Block until we see the knock appear down sync
289
- c .MustSyncUntil (t , client.SyncReq {}, func (clientUserID string , topLevelSyncJSON gjson.Result ) error {
290
- events := topLevelSyncJSON .Get ("rooms.knock." + client .GjsonEscape (roomID ) + ".knock_state.events" )
291
- if events .Exists () && events .IsArray () {
292
- // We don't currently define any required state event types to be sent.
293
- // If we've reached this point, then an entry for this room was found
294
- return nil
295
- }
296
- return fmt .Errorf ("no knock section for room %s" , roomID )
297
- })
311
+ c .MustSyncUntil (t , client.SyncReq {}, syncKnockedOn (c .UserID , roomID ))
298
312
}
299
313
300
314
// knockOnRoomWithStatus will knock on a given room on the behalf of a user.
0 commit comments