forked from matrix-org/sliding-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters_test.go
625 lines (602 loc) · 15.2 KB
/
filters_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
package syncv3
import (
"encoding/json"
"testing"
"time"
"github.com/matrix-org/sliding-sync/sync2"
"github.com/matrix-org/sliding-sync/sync3"
"github.com/matrix-org/sliding-sync/testutils"
"github.com/matrix-org/sliding-sync/testutils/m"
)
// Test that filters work initially and whilst streamed.
func TestFiltersEncryption(t *testing.T) {
boolTrue := true
boolFalse := false
rig := NewTestRig(t)
defer rig.Finish()
encryptedRoomID := "!TestFilters_encrypted:localhost"
unencryptedRoomID := "!TestFilters_unencrypted:localhost"
rig.SetupV2RoomsForUser(t, alice, NoFlush, map[string]RoomDescriptor{
encryptedRoomID: {
IsEncrypted: true,
},
unencryptedRoomID: {
IsEncrypted: false,
},
})
aliceToken := rig.Token(alice)
// connect and make sure either the encrypted room or not depending on what the filter says
res := rig.V3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"enc": {
Ranges: sync3.SliceRanges{
[2]int64{0, 1}, // all rooms
},
Filters: &sync3.RequestFilters{
IsEncrypted: &boolTrue,
},
},
"noenc": {
Ranges: sync3.SliceRanges{
[2]int64{0, 1}, // all rooms
},
Filters: &sync3.RequestFilters{
IsEncrypted: &boolFalse,
},
},
},
})
m.MatchResponse(t, res, m.MatchLists(
map[string][]m.ListMatcher{
"enc": {
m.MatchV3Count(1),
m.MatchV3Ops(
m.MatchV3SyncOp(0, 0, []string{encryptedRoomID}),
),
},
"noenc": {
m.MatchV3Count(1),
m.MatchV3Ops(
// Note: expect position 0 here---this is a separate list
m.MatchV3SyncOp(0, 0, []string{unencryptedRoomID}),
),
},
},
))
// change the unencrypted room into an encrypted room
rig.EncryptRoom(t, alice, unencryptedRoomID)
// now requesting the encrypted list should include it (added)
res = rig.V3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{
"enc": {
Ranges: sync3.SliceRanges{
[2]int64{0, 1}, // all rooms
},
// sticky; should remember filters
},
"noenc": {
Ranges: sync3.SliceRanges{
[2]int64{0, 1}, // all rooms
},
// sticky; should remember filters
},
},
})
m.MatchResponse(t, res, m.MatchLists(
map[string][]m.ListMatcher{
"enc": {
m.MatchV3Count(2),
m.MatchV3Ops(
m.MatchV3DeleteOp(1), m.MatchV3InsertOp(0, unencryptedRoomID),
),
},
"noenc": {
m.MatchV3Count(0),
m.MatchV3Ops(
m.MatchV3DeleteOp(0),
),
},
},
))
// requesting the encrypted list from scratch returns 2 rooms now
res = rig.V3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"enc": {
Ranges: sync3.SliceRanges{
[2]int64{0, 1}, // all rooms
},
Filters: &sync3.RequestFilters{
IsEncrypted: &boolTrue,
},
}},
})
m.MatchResponse(t, res, m.MatchLists(
map[string][]m.ListMatcher{
"enc": {
m.MatchV3Count(2),
m.MatchV3Ops(
m.MatchV3SyncOp(0, 1, []string{unencryptedRoomID, encryptedRoomID}),
),
},
},
))
// requesting the unencrypted stream from scratch returns 0 rooms
res = rig.V3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"noenc": {
Ranges: sync3.SliceRanges{
[2]int64{0, 1}, // all rooms
},
Filters: &sync3.RequestFilters{
IsEncrypted: &boolFalse,
},
}},
})
m.MatchResponse(t, res, m.MatchLists(
map[string][]m.ListMatcher{
"noenc": {
m.MatchV3Count(0),
},
},
))
}
func TestFiltersInvite(t *testing.T) {
boolTrue := true
boolFalse := false
rig := NewTestRig(t)
defer rig.Finish()
roomID := "!a:localhost"
t.Log("Alice is invited to a room.")
rig.SetupV2RoomsForUser(t, alice, NoFlush, map[string]RoomDescriptor{
roomID: {
MembershipOfSyncer: "invite",
},
})
aliceToken := rig.Token(alice)
t.Log("Alice sliding syncs, requesting two separate lists: invites and joined rooms.")
res := rig.V3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"inv": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20}, // all rooms
},
Filters: &sync3.RequestFilters{
IsInvite: &boolTrue,
},
},
"noinv": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20}, // all rooms
},
Filters: &sync3.RequestFilters{
IsInvite: &boolFalse,
},
},
},
})
t.Log("Alice should see the room appear in the invites list, and nothing in the joined rooms list.")
m.MatchResponse(t, res, m.MatchLists(
map[string][]m.ListMatcher{
"inv": {
m.MatchV3Count(1),
m.MatchV3Ops(
m.MatchV3SyncOp(0, 0, []string{roomID}),
),
},
"noinv": {
m.MatchV3Count(0),
},
},
))
t.Log("Alice accepts the invite.")
rig.V2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: map[string]sync2.SyncV2JoinResponse{
roomID: {
State: sync2.EventsResponse{
Events: createRoomState(t, "@creator:other", time.Now()),
},
Timeline: sync2.TimelineResponse{
Events: []json.RawMessage{testutils.NewJoinEvent(t, alice)},
},
},
},
},
})
// now the room should move from one room to another
res = rig.V3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{
"inv": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20}, // all rooms
},
// sticky; should remember filters
},
"noinv": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20}, // all rooms
},
// sticky; should remember filters
},
},
})
// the room swaps from the invite list to the join list
m.MatchResponse(t, res, m.MatchLists(
map[string][]m.ListMatcher{
"inv": {
m.MatchV3Count(0),
m.MatchV3Ops(
m.MatchV3DeleteOp(0),
),
},
"noinv": {
m.MatchV3Count(1),
m.MatchV3Ops(
m.MatchV3DeleteOp(0),
m.MatchV3InsertOp(0, roomID),
),
},
},
))
}
func TestFiltersRoomName(t *testing.T) {
rig := NewTestRig(t)
defer rig.Finish()
ridApple := "!a:localhost"
ridPear := "!b:localhost"
ridLemon := "!c:localhost"
ridOrange := "!d:localhost"
ridPineapple := "!e:localhost"
ridBanana := "!f:localhost"
ridKiwi := "!g:localhost"
rig.SetupV2RoomsForUser(t, alice, NoFlush, map[string]RoomDescriptor{
ridApple: {
Name: "apple",
},
ridPear: {
Name: "PEAR",
},
ridLemon: {
Name: "Lemon Lemon",
},
ridOrange: {
Name: "ooooorange",
},
ridPineapple: {
Name: "PineApple",
},
ridBanana: {
Name: "BaNaNaNaNaNaNaN",
},
ridKiwi: {
Name: "kiwiwiwiwiwiwi",
},
})
aliceToken := rig.Token(alice)
// make sure the room name filter works
res := rig.V3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20}, // all rooms
},
Filters: &sync3.RequestFilters{
RoomNameFilter: "a",
},
},
},
})
m.MatchResponse(t, res, m.MatchList("a",
m.MatchV3Count(5),
m.MatchV3Ops(
m.MatchV3SyncOp(0, 4, []string{
ridApple, ridPear, ridOrange, ridPineapple, ridBanana,
}, true),
),
))
// refine the filter
res = rig.V3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{
"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20}, // all rooms
},
Filters: &sync3.RequestFilters{
RoomNameFilter: "app",
},
},
},
})
m.MatchResponse(t, res, m.MatchList("a",
m.MatchV3Count(2),
m.MatchV3Ops(
m.MatchV3InvalidateOp(0, 4),
m.MatchV3SyncOp(0, 1, []string{
ridApple, ridPineapple,
}, true),
),
))
}
func TestFiltersRoomTypes(t *testing.T) {
rig := NewTestRig(t)
defer rig.Finish()
spaceRoomID := "!spaceRoomID:localhost"
otherRoomID := "!other:localhost"
roomID := "!roomID:localhost"
roomType := "m.space"
otherRoomType := "something_else"
invalid := "lalalalaala"
rig.SetupV2RoomsForUser(t, alice, NoFlush, map[string]RoomDescriptor{
spaceRoomID: {
RoomType: roomType,
},
roomID: {},
otherRoomID: {
RoomType: otherRoomType,
},
})
aliceToken := rig.Token(alice)
// make sure the room_types and not_room_types filters works
res := rig.V3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
// returns spaceRoomID only due to direct match
"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20}, // all rooms
},
Filters: &sync3.RequestFilters{
RoomTypes: []*string{&roomType},
},
},
// returns roomID only due to direct match (null = things without a room type)
"b": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20}, // all rooms
},
Filters: &sync3.RequestFilters{
RoomTypes: []*string{nil},
},
},
// returns roomID and otherRoomID due to exclusion
"c": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20}, // all rooms
},
Filters: &sync3.RequestFilters{
NotRoomTypes: []*string{&roomType},
},
},
// returns otherRoomID due to otherRoomType inclusive, roomType is excluded (override)
"d": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20}, // all rooms
},
Filters: &sync3.RequestFilters{
RoomTypes: []*string{&roomType, &otherRoomType},
NotRoomTypes: []*string{&roomType},
},
},
// returns no rooms as filtered room type isn't set on any rooms
"e": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20}, // all rooms
},
Filters: &sync3.RequestFilters{
RoomTypes: []*string{&invalid},
},
},
// returns all rooms as filtered not room type isn't set on any rooms
"f": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20}, // all rooms
},
Filters: &sync3.RequestFilters{
NotRoomTypes: []*string{&invalid},
},
},
},
})
m.MatchResponse(t, res, m.MatchLists(map[string][]m.ListMatcher{
"a": {
m.MatchV3Count(1), m.MatchV3Ops(m.MatchV3SyncOp(0, 0, []string{spaceRoomID})),
},
"b": {
m.MatchV3Count(1), m.MatchV3Ops(m.MatchV3SyncOp(0, 0, []string{roomID})),
},
"c": {
m.MatchV3Count(2), m.MatchV3Ops(m.MatchV3SyncOp(0, 1, []string{roomID, otherRoomID}, true)),
},
"d": {
m.MatchV3Count(1), m.MatchV3Ops(m.MatchV3SyncOp(0, 0, []string{otherRoomID})),
},
"e": {
m.MatchV3Count(0),
},
"f": {
m.MatchV3Count(3), m.MatchV3Ops(m.MatchV3SyncOp(0, 2, []string{roomID, otherRoomID, spaceRoomID}, true)),
},
}))
}
func TestFiltersTags(t *testing.T) {
tagFav := "m.favourite"
tagLow := "m.lowpriority"
rig := NewTestRig(t)
defer rig.Finish()
fav1RoomID := "!fav1:localhost"
fav2RoomID := "!fav2:localhost"
favAndLowRoomID := "!favlow:localhost"
low1RoomID := "!low1:localhost"
low2RoomID := "!low2:localhost"
rig.SetupV2RoomsForUser(t, alice, NoFlush, map[string]RoomDescriptor{
fav1RoomID: {
Tags: map[string]float64{
tagFav: 0.5,
},
},
fav2RoomID: {
Tags: map[string]float64{
tagFav: 0.3,
},
},
favAndLowRoomID: {
Tags: map[string]float64{
tagFav: 0.5,
tagLow: 0.9,
},
},
low1RoomID: {
Tags: map[string]float64{
tagLow: 0.2,
},
},
low2RoomID: {
Tags: map[string]float64{
tagLow: 0.92,
},
},
})
aliceToken := rig.Token(alice)
res := rig.V3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"fav": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20},
},
Filters: &sync3.RequestFilters{
Tags: []string{tagFav},
},
},
"lp": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20},
},
Filters: &sync3.RequestFilters{
Tags: []string{tagLow},
},
},
"favlp": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20},
},
Filters: &sync3.RequestFilters{
Tags: []string{tagFav, tagLow},
},
},
},
})
m.MatchResponse(t, res, m.MatchList("fav", m.MatchV3Count(3), m.MatchV3Ops(
m.MatchV3SyncOp(0, 2, []string{fav1RoomID, fav2RoomID, favAndLowRoomID}, true),
)), m.MatchList("lp", m.MatchV3Count(3), m.MatchV3Ops(
m.MatchV3SyncOp(0, 2, []string{low1RoomID, low2RoomID, favAndLowRoomID}, true),
)), m.MatchList("favlp", m.MatchV3Count(5), m.MatchV3Ops(
m.MatchV3SyncOp(0, 4, []string{fav1RoomID, fav2RoomID, favAndLowRoomID, low1RoomID, low2RoomID}, true),
)))
// first bump the fav1 room
rig.FlushEvent(t, alice, fav1RoomID, testutils.NewMessageEvent(t, alice, "Hi"))
res = rig.V3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{
"fav": {Ranges: sync3.SliceRanges{{0, 20}}},
"lp": {Ranges: sync3.SliceRanges{{0, 20}}},
"favlp": {Ranges: sync3.SliceRanges{{0, 20}}},
},
})
// now nuke it by removing the tag
rig.V2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: map[string]sync2.SyncV2JoinResponse{
fav1RoomID: {
AccountData: sync2.EventsResponse{
Events: []json.RawMessage{
testutils.NewAccountData(t, "m.tag", map[string]interface{}{}), // empty content
},
},
},
},
},
})
rig.V2.waitUntilEmpty(t, alice)
// we should see DELETEs
res = rig.V3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{
"fav": {Ranges: sync3.SliceRanges{{0, 20}}},
"lp": {Ranges: sync3.SliceRanges{{0, 20}}},
"favlp": {Ranges: sync3.SliceRanges{{0, 20}}},
},
})
m.MatchResponse(t, res, m.MatchList("fav", m.MatchV3Count(2), m.MatchV3Ops(
m.MatchV3DeleteOp(0),
)), m.MatchList("lp", m.MatchV3Ops()), m.MatchList("favlp", m.MatchV3Count(4), m.MatchV3Ops(
m.MatchV3DeleteOp(0),
)))
// check not_tags works
res = rig.V3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"fav": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20},
},
Filters: &sync3.RequestFilters{
Tags: []string{tagFav},
},
},
"nofav": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20},
},
Filters: &sync3.RequestFilters{
NotTags: []string{tagFav},
},
},
},
})
m.MatchResponse(t, res, m.MatchList("fav", m.MatchV3Count(2), m.MatchV3Ops(
m.MatchV3SyncOp(0, 1, []string{fav2RoomID, favAndLowRoomID}, true),
)), m.MatchList("nofav", m.MatchV3Count(3), m.MatchV3Ops(
m.MatchV3SyncOp(0, 2, []string{low1RoomID, low2RoomID, fav1RoomID}, true),
)))
// remove a fav, it should move to the other list
rig.V2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: map[string]sync2.SyncV2JoinResponse{
fav2RoomID: {
AccountData: sync2.EventsResponse{
Events: []json.RawMessage{
testutils.NewAccountData(t, "m.tag", map[string]interface{}{
"tags": map[string]interface{}{},
}),
},
},
},
},
},
})
rig.V2.waitUntilEmpty(t, alice)
res = rig.V3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{
"fav": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20},
},
},
"nofav": {
Ranges: sync3.SliceRanges{
[2]int64{0, 20},
},
},
},
})
// Room ordering is: (recent first)
// FAV1, LOW2, LOW1, FAVLOW, FAV2
// so lists before were:
// FAVLOW, FAV2
// FAV1, LOW2, LOW1
// now we have removed fav tag on FAV2 so new lists are:
// FAVLOW
// FAV1, LOW2, LOW1, FAV2
m.MatchResponse(t, res, m.MatchList("fav", m.MatchV3Count(1), m.MatchV3Ops(
m.MatchV3DeleteOp(1),
)), m.MatchList("nofav", m.MatchV3Count(4), m.MatchV3Ops(
m.MatchV3DeleteOp(3),
m.MatchV3InsertOp(3, fav2RoomID),
)))
}