Skip to content

Commit 83e98ed

Browse files
committed
regression(federation): fix duplicated DMs priority
1 parent e7d880e commit 83e98ed

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-35
lines changed

ee/packages/federation-matrix/tests/end-to-end/dms.spec.ts

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,22 +1204,7 @@ const waitForRoomEvent = async (
12041204
});
12051205

12061206
beforeAll(async () => {
1207-
// Create non-federated DM between rcUser1 and rcUser2
1208-
const nonFedDmResponse = await rcUser1.config.request
1209-
.post(api('dm.create'))
1210-
.set(rcUser1.config.credentials)
1211-
.send({
1212-
username: rcUser2.username,
1213-
})
1214-
.expect(200);
1215-
1216-
expect(nonFedDmResponse.body).toHaveProperty('success', true);
1217-
1218-
const roomInfoNonFed = await getRoomInfo(nonFedDmResponse.body.room._id, rcUser1.config);
1219-
1220-
rcRoomNonFed = roomInfoNonFed.room as IRoomNativeFederated;
1221-
1222-
// Create federated DM between rcUser1 and rcUser2 and the Synapse user
1207+
// First create a federated DM between rcUser1 and rcUser2 and the Synapse user, so this is the oldest room
12231208
const fedDmResponse = await rcUser1.config.request
12241209
.post(api('dm.create'))
12251210
.set(rcUser1.config.credentials)
@@ -1260,6 +1245,21 @@ const waitForRoomEvent = async (
12601245
// After acceptance, should display the Synapse user's ID
12611246
expect(sub).toHaveProperty('fname', `${federationConfig.hs1.adminMatrixUserId}, ${rcUser1.fullName}`);
12621247
});
1248+
1249+
// Then create non-federated DM between rcUser1 and rcUser2 which should be returned on duplication
1250+
const nonFedDmResponse = await rcUser1.config.request
1251+
.post(api('dm.create'))
1252+
.set(rcUser1.config.credentials)
1253+
.send({
1254+
username: rcUser2.username,
1255+
})
1256+
.expect(200);
1257+
1258+
expect(nonFedDmResponse.body).toHaveProperty('success', true);
1259+
1260+
const roomInfoNonFed = await getRoomInfo(nonFedDmResponse.body.room._id, rcUser1.config);
1261+
1262+
rcRoomNonFed = roomInfoNonFed.room as IRoomNativeFederated;
12631263
});
12641264

12651265
it('should have two DMs with same users', async () => {
@@ -1322,9 +1322,6 @@ const waitForRoomEvent = async (
13221322

13231323
expect(response.body).toHaveProperty('success', true);
13241324

1325-
// expect(response.body).toHaveProperty('room._id', rcRoom._id);
1326-
1327-
// TODO this is currently wrong, the correct value should be rcRoom._id
13281325
expect(response.body).toHaveProperty('room._id', rcRoomNonFed._id);
13291326
});
13301327
});
@@ -1488,10 +1485,19 @@ const waitForRoomEvent = async (
14881485

14891486
const response = await acceptRoomInvite(rcRoom._id, rcUser2.config);
14901487
expect(response.success).toBe(true);
1488+
1489+
await retry(
1490+
'wait for the join to be processed',
1491+
async () => {
1492+
const sub = await getSubscriptionByRoomId(rcRoom._id, rcUser2.config.credentials, rcUser2.config.request);
1493+
1494+
expect(sub).not.toHaveProperty('status');
1495+
},
1496+
{ delayMs: 100 },
1497+
);
14911498
});
14921499

1493-
// TODO this is failing because we cannot accept the invite properly on the line above
1494-
it.failing('should have two DMs with same users', async () => {
1500+
it('should have a single DM with same users before one leaves', async () => {
14951501
const roomsResponse = await rcUser1.config.request.get(api('rooms.get')).set(rcUser1.config.credentials).expect(200);
14961502

14971503
const dmRooms = roomsResponse.body.update.filter(
@@ -1505,7 +1511,9 @@ const waitForRoomEvent = async (
15051511

15061512
// at this time there should be only one DM with only two users (the non-federated one)
15071513
expect(dmRooms.length).toBe(1);
1514+
});
15081515

1516+
it('should leave the federated DM with three users', async () => {
15091517
// now the rc user leaves the federated DM
15101518
const response = await rcUser2.config.request
15111519
.post(api('rooms.leave'))
@@ -1517,19 +1525,21 @@ const waitForRoomEvent = async (
15171525

15181526
expect(response.body).toHaveProperty('success', true);
15191527

1520-
await retry(
1521-
'wait for the leave to be processed',
1522-
async () => {
1523-
expect(hs1Room1.getMyMembership()).toBe('leave');
1528+
// Verify room is no longer accessible to RC users
1529+
await retry('waiting for room cleanup', async () => {
1530+
const roomsResponse = await rcUser2.config.request.get(api('rooms.get')).set(rcUser2.config.credentials).expect(200);
15241531

1525-
const sub = await getSubscriptionByRoomId(rcRoom._id, rcUser1.config.credentials, rcUser1.config.request);
1532+
expect(roomsResponse.body).toHaveProperty('update');
1533+
expect(roomsResponse.body.update).toBeInstanceOf(Array);
15261534

1527-
// After leave, should display only the RC user's full name
1528-
expect(sub).toHaveProperty('fname', rcUser2.fullName);
1529-
},
1530-
{ delayMs: 100 },
1531-
);
1535+
const room = roomsResponse.body.update?.find((r: IRoom) => r._id === rcRoom._id);
1536+
1537+
// Room should not be in active rooms list
1538+
expect(room).toBeUndefined();
1539+
});
1540+
});
15321541

1542+
it('should have two DMs with same users', async () => {
15331543
// now there should be two DMs with the same users
15341544
const roomsResponseAfterLeave = await rcUser1.config.request
15351545
.get(api('rooms.get'))
@@ -1559,9 +1569,6 @@ const waitForRoomEvent = async (
15591569

15601570
expect(response.body).toHaveProperty('success', true);
15611571

1562-
// expect(response.body).toHaveProperty('room._id', rcRoom._id);
1563-
1564-
// TODO this is currently wrong, the correct value should be rcRoom._id
15651572
expect(response.body).toHaveProperty('room._id', rcRoom1on1._id);
15661573
});
15671574
});

packages/models/src/models/Rooms.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,13 @@ export class RoomsRaw extends BaseRaw<IRoom> implements IRoomsModel {
897897
uids: { $size: uid.length, $all: uid },
898898
};
899899

900-
return this.findOne<IRoom>(query, options);
900+
return this.findOne<IRoom>(query, {
901+
...options,
902+
sort: {
903+
federated: 1,
904+
ts: 1,
905+
},
906+
});
901907
}
902908

903909
findFederatedRooms(options: FindOptions<IRoom> = {}): FindCursor<IRoomFederated> {

0 commit comments

Comments
 (0)