Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track users when joining encrypted rooms #251

Merged
merged 3 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/e2ee/CryptoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export class CryptoClient {
*/
public async onRoomJoin(roomId: string) {
await this.roomTracker.onRoomJoin(roomId);
if (await this.isRoomEncrypted(roomId)) {
const members = await this.client.getRoomMembers(roomId, null, ['join', 'invite']);
await this.engine.addTrackedUsers(members.map(e => e.membershipFor));
}
}

/**
Expand Down
40 changes: 40 additions & 0 deletions test/encryption/CryptoClientTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,5 +555,45 @@ describe('CryptoClient', () => {
// end up not running fast enough for our callCount checks.
await Promise.all([prom1, prom2]);
});

it('should update the tracked users when joining a new room', async () => {
// Stub the room tracker
(client.crypto as any).roomTracker.onRoomJoin = () => {};

const targetUserIds = ["@bob:example.org", "@charlie:example.org"];
const prom1 = new Promise<void>(extResolve => {
(client.crypto as any).engine.addTrackedUsers = simple.mock().callFn((uids) => {
expect(uids).toEqual(targetUserIds);
extResolve();
return Promise.resolve();
});
});

const roomId = "!room:example.org";
const prom2 = new Promise<void>(extResolve => {
client.getRoomMembers = simple.mock().callFn((rid, token, memberships) => {
expect(rid).toEqual(roomId);
expect(token).toBeFalsy();
expect(memberships).toEqual(["join", "invite"]);
extResolve();
return Promise.resolve(targetUserIds.map(u => new MembershipEvent({
type: "m.room.member",
state_key: u,
content: { membership: "join" },
sender: u,
})));
});
});

client.crypto.isRoomEncrypted = async (rid) => {
expect(rid).toEqual(roomId);
return true;
};
client.emit("room.join", roomId);

// We do weird promise things because `emit()` is sync and we're using async code, so it can
// end up not running fast enough for our callCount checks.
await Promise.all([prom1, prom2]);
});
});
});