Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 5fbb25c

Browse files
authored
Make Jitsi widgets in video rooms immutable (#8244)
* Make Jitsi widgets in video rooms immutable * Test video room creation
1 parent e54eb81 commit 5fbb25c

File tree

4 files changed

+119
-33
lines changed

4 files changed

+119
-33
lines changed

src/createRoom.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,14 @@ export default async function createRoom(opts: IOpts): Promise<string | null> {
126126
[RoomCreateTypeField]: opts.roomType,
127127
};
128128

129-
// In video rooms, allow all users to send video member updates
129+
// Video rooms require custom power levels
130130
if (opts.roomType === RoomType.ElementVideo) {
131131
createOpts.power_level_content_override = {
132132
events: {
133+
// Allow all users to send video member updates
133134
[VIDEO_CHANNEL_MEMBER]: 0,
135+
// Make widgets immutable, even to admins
136+
"im.vector.modular.widgets": 200,
134137
// Annoyingly, we have to reiterate all the defaults here
135138
[EventType.RoomName]: 50,
136139
[EventType.RoomAvatar]: 50,
@@ -141,6 +144,10 @@ export default async function createRoom(opts: IOpts): Promise<string | null> {
141144
[EventType.RoomServerAcl]: 100,
142145
[EventType.RoomEncryption]: 100,
143146
},
147+
users: {
148+
// Temporarily give ourselves the power to set up a widget
149+
[client.getUserId()]: 200,
150+
},
144151
};
145152
}
146153
}
@@ -259,10 +266,15 @@ export default async function createRoom(opts: IOpts): Promise<string | null> {
259266
if (opts.parentSpace) {
260267
return SpaceStore.instance.addRoomToSpace(opts.parentSpace, roomId, [client.getDomain()], opts.suggested);
261268
}
262-
}).then(() => {
263-
// Set up video rooms with a Jitsi widget
269+
}).then(async () => {
264270
if (opts.roomType === RoomType.ElementVideo) {
265-
return addVideoChannel(roomId, createOpts.name);
271+
// Set up video rooms with a Jitsi widget
272+
await addVideoChannel(roomId, createOpts.name);
273+
274+
// Reset our power level back to admin so that the widget becomes immutable
275+
const room = client.getRoom(roomId);
276+
const plEvent = room?.currentState.getStateEvents(EventType.RoomPowerLevels, "");
277+
await client.setPowerLevel(roomId, client.getUserId(), 100, plEvent);
266278
}
267279
}).then(function() {
268280
// NB createRoom doesn't block on the client seeing the echo that the

test/createRoom-test.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/createRoom-test.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { mocked } from "jest-mock";
18+
import { MatrixClient } from "matrix-js-sdk/src/matrix";
19+
import { IDevice } from "matrix-js-sdk/src/crypto/deviceinfo";
20+
import { RoomType } from "matrix-js-sdk/src/@types/event";
21+
22+
import { stubClient, setupAsyncStoreWithClient } from "./test-utils";
23+
import { MatrixClientPeg } from "../src/MatrixClientPeg";
24+
import WidgetStore from "../src/stores/WidgetStore";
25+
import WidgetUtils from "../src/utils/WidgetUtils";
26+
import { VIDEO_CHANNEL, VIDEO_CHANNEL_MEMBER } from "../src/utils/VideoChannelUtils";
27+
import createRoom, { canEncryptToAllUsers } from '../src/createRoom';
28+
29+
describe("createRoom", () => {
30+
let client: MatrixClient;
31+
beforeEach(() => {
32+
stubClient();
33+
client = MatrixClientPeg.get();
34+
});
35+
36+
it("sets up video rooms correctly", async () => {
37+
setupAsyncStoreWithClient(WidgetStore.instance, client);
38+
jest.spyOn(WidgetUtils, "waitForRoomWidget").mockResolvedValue();
39+
40+
const userId = client.getUserId();
41+
const roomId = await createRoom({ roomType: RoomType.ElementVideo });
42+
43+
const [[{
44+
power_level_content_override: {
45+
users: {
46+
[userId]: userPower,
47+
},
48+
events: {
49+
"im.vector.modular.widgets": widgetPower,
50+
[VIDEO_CHANNEL_MEMBER]: videoMemberPower,
51+
},
52+
},
53+
}]] = mocked(client.createRoom).mock.calls as any;
54+
const [[widgetRoomId, widgetStateKey, , widgetId]] = mocked(client.sendStateEvent).mock.calls;
55+
56+
// We should have had enough power to be able to set up the Jitsi widget
57+
expect(userPower).toBeGreaterThanOrEqual(widgetPower);
58+
// and should have actually set it up
59+
expect(widgetRoomId).toEqual(roomId);
60+
expect(widgetStateKey).toEqual("im.vector.modular.widgets");
61+
expect(widgetId).toEqual(VIDEO_CHANNEL);
62+
63+
// All members should be able to update their connected devices
64+
expect(videoMemberPower).toEqual(0);
65+
// Jitsi widget should be immutable for admins
66+
expect(widgetPower).toBeGreaterThan(100);
67+
// and we should have been reset back to admin
68+
expect(client.setPowerLevel).toHaveBeenCalledWith(roomId, userId, 100, undefined);
69+
});
70+
});
71+
72+
describe("canEncryptToAllUsers", () => {
73+
const trueUser = {
74+
"@goodUser:localhost": {
75+
"DEV1": {} as unknown as IDevice,
76+
"DEV2": {} as unknown as IDevice,
77+
},
78+
};
79+
const falseUser = {
80+
"@badUser:localhost": {},
81+
};
82+
83+
let client: MatrixClient;
84+
beforeEach(() => {
85+
stubClient();
86+
client = MatrixClientPeg.get();
87+
});
88+
89+
it("returns true if all devices have crypto", async () => {
90+
mocked(client.downloadKeys).mockResolvedValue(trueUser);
91+
const response = await canEncryptToAllUsers(client, ["@goodUser:localhost"]);
92+
expect(response).toBe(true);
93+
});
94+
95+
it("returns false if not all users have crypto", async () => {
96+
mocked(client.downloadKeys).mockResolvedValue({ ...trueUser, ...falseUser });
97+
const response = await canEncryptToAllUsers(client, ["@goodUser:localhost", "@badUser:localhost"]);
98+
expect(response).toBe(false);
99+
});
100+
});

test/test-utils/test-utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ export function createTestClient(): MatrixClient {
108108
getRoomHierarchy: jest.fn().mockReturnValue({
109109
rooms: [],
110110
}),
111+
createRoom: jest.fn().mockResolvedValue({ room_id: "!1:example.org" }),
112+
setPowerLevel: jest.fn().mockResolvedValue(undefined),
111113

112114
// Used by various internal bits we aren't concerned with (yet)
113115
sessionStore: {
@@ -135,6 +137,7 @@ export function createTestClient(): MatrixClient {
135137
setPushRuleActions: jest.fn().mockResolvedValue(undefined),
136138
relations: jest.fn().mockRejectedValue(undefined),
137139
isCryptoEnabled: jest.fn().mockReturnValue(false),
140+
downloadKeys: jest.fn(),
138141
fetchRoomEvent: jest.fn(),
139142
} as unknown as MatrixClient;
140143
}

0 commit comments

Comments
 (0)